Skip to content

Commit

Permalink
test: add unit cases
Browse files Browse the repository at this point in the history
  • Loading branch information
QuenKar committed Dec 12, 2023
1 parent 0759bc9 commit 43ea71e
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 24 deletions.
28 changes: 6 additions & 22 deletions src/datatypes/src/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,30 +428,14 @@ pub mod tests {
}

#[test]
fn test_mutable_vector_finish_cloned() {
// create a primitive type mutable vector
let mut builder = Int32VectorBuilder::with_capacity(1024);
builder.push(Some(1));
builder.push(Some(2));
builder.push(Some(3));
// use finish_cloned won't reset builder
let vector = builder.finish_cloned();
assert_eq!(vector.len(), 3);
assert_eq!(builder.len(), 3);

builder.push(Some(4));
assert_eq!(builder.len(), 4);

// use finish will reset builder
let vector = builder.finish();
assert_eq!(vector.len(), 4);
assert_eq!(builder.len(), 0);
fn test_mutable_vector_to_vector_cloned() {
// create a string vector builder
let mut builder = ConcreteDataType::string_datatype().create_mutable_vector(1024);
builder.push_value_ref(ValueRef::String("hello"));
builder.push_value_ref(ValueRef::String("world"));
builder.push_value_ref(ValueRef::String("!"));

// use MutableVector trait to_vector_cloned won't reset builder
let mut builder = StringVectorBuilder::with_capacity(1024);
builder.push(Some("1"));
builder.push(Some("2"));
builder.push(Some("3"));
let vector = builder.to_vector_cloned();
assert_eq!(vector.len(), 3);
assert_eq!(builder.len(), 3);
Expand Down
17 changes: 17 additions & 0 deletions src/datatypes/src/vectors/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,21 @@ mod tests {
let expect: VectorRef = Arc::new(BinaryVector::from_slice(&[b"hello", b"one", b"two"]));
assert_eq!(expect, vector);
}

#[test]
fn test_binary_vector_builder_finish_cloned() {
let mut builder = BinaryVectorBuilder::with_capacity(1024);
builder.push(Some(b"one"));
builder.push(Some(b"two"));
builder.push(Some(b"three"));
let vector = builder.finish_cloned();
assert_eq!(b"one", vector.get_data(0).unwrap());
assert_eq!(vector.len(), 3);
assert_eq!(builder.len(), 3);

builder.push(Some(b"four"));
let vector = builder.finish_cloned();
assert_eq!(b"four", vector.get_data(3).unwrap());
assert_eq!(builder.len(), 4);
}
}
17 changes: 17 additions & 0 deletions src/datatypes/src/vectors/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,4 +368,21 @@ mod tests {
let expect: VectorRef = Arc::new(BooleanVector::from_slice(&[true, false, true]));
assert_eq!(expect, vector);
}

#[test]
fn test_boolean_vector_builder_finish_cloned() {
let mut builder = BooleanVectorBuilder::with_capacity(1024);
builder.push(Some(true));
builder.push(Some(false));
builder.push(Some(true));
let vector = builder.finish_cloned();
assert!(vector.get_data(0).unwrap());
assert_eq!(vector.len(), 3);
assert_eq!(builder.len(), 3);

builder.push(Some(false));
let vector = builder.finish_cloned();
assert!(!vector.get_data(3).unwrap());
assert_eq!(builder.len(), 4);
}
}
12 changes: 12 additions & 0 deletions src/datatypes/src/vectors/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,4 +563,16 @@ pub mod tests {
.collect::<Vec<_>>();
assert_eq!(values, vec![2, 4, 6, 8]);
}

#[test]
fn test_decimal128_vector_builder_finish_cloned() {
let mut builder = Decimal128VectorBuilder::with_capacity(1024);
builder.push(Some(Decimal128::new(1, 3, 1)));
builder.push(Some(Decimal128::new(1, 3, 1)));
builder.push(Some(Decimal128::new(1, 3, 1)));
builder.push(Some(Decimal128::new(1, 3, 1)));
let vector = builder.finish_cloned();
assert_eq!(vector.len(), 4);
assert_eq!(builder.len(), 4);
}
}
20 changes: 20 additions & 0 deletions src/datatypes/src/vectors/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -763,4 +763,24 @@ pub mod tests {
iter.nth(1).unwrap().unwrap()
);
}

#[test]
fn test_list_vector_builder_finish_cloned() {
let mut builder =
ListVectorBuilder::with_type_capacity(ConcreteDataType::int32_datatype(), 2);
builder.push(None);
builder.push(Some(ListValueRef::Ref {
val: &ListValue::new(
Some(Box::new(vec![
Value::Int32(4),
Value::Null,
Value::Int32(6),
])),
ConcreteDataType::int32_datatype(),
),
}));
let vector = builder.finish_cloned();
assert_eq!(vector.len(), 2);
assert_eq!(builder.len(), 2);
}
}
10 changes: 10 additions & 0 deletions src/datatypes/src/vectors/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,14 @@ mod tests {
let expect: VectorRef = Arc::new(input);
assert_eq!(expect, vector);
}

#[test]
fn test_null_vector_builder_finish_cloned() {
let mut builder = NullType.create_mutable_vector(3);
builder.push_null();
builder.push_null();
let vector = builder.to_vector_cloned();
assert_eq!(vector.len(), 2);
assert_eq!(vector.null_count(), 2);
}
}
26 changes: 24 additions & 2 deletions src/datatypes/src/vectors/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,8 @@ pub(crate) fn replicate_primitive<T: LogicalPrimitiveType>(

#[cfg(test)]
mod tests {
use std::vec;

use arrow::array::{
Int32Array, Time32MillisecondArray, Time32SecondArray, Time64MicrosecondArray,
Time64NanosecondArray,
Expand All @@ -416,13 +418,14 @@ mod tests {
use super::*;
use crate::data_type::DataType;
use crate::serialize::Serializable;
use crate::timestamp::TimestampMillisecond;
use crate::types::Int64Type;
use crate::vectors::{
DurationMicrosecondVector, DurationMillisecondVector, DurationNanosecondVector,
DurationSecondVector, IntervalDayTimeVector, IntervalYearMonthVector,
TimeMicrosecondVector, TimeMillisecondVector, TimeNanosecondVector, TimeSecondVector,
TimestampMicrosecondVector, TimestampMillisecondVector, TimestampNanosecondVector,
TimestampSecondVector,
TimestampMicrosecondVector, TimestampMillisecondVector, TimestampMillisecondVectorBuilder,
TimestampNanosecondVector, TimestampSecondVector,
};

fn check_vec(v: Int32Vector) {
Expand Down Expand Up @@ -692,4 +695,23 @@ mod tests {
vector
);
}

#[test]
fn test_primitive_vector_builder_finish_cloned() {
let mut builder = Int64Type::default().create_mutable_vector(3);
builder.push_value_ref(ValueRef::Int64(123));
builder.push_value_ref(ValueRef::Int64(456));
let vector = builder.to_vector_cloned();
assert_eq!(vector.len(), 2);
assert_eq!(vector.null_count(), 0);
assert_eq!(builder.len(), 2);

let mut builder = TimestampMillisecondVectorBuilder::with_capacity(1024);
builder.push(Some(TimestampMillisecond::new(1)));
builder.push(Some(TimestampMillisecond::new(2)));
builder.push(Some(TimestampMillisecond::new(3)));
let vector = builder.finish_cloned();
assert_eq!(vector.len(), 3);
assert_eq!(builder.len(), 3);
}
}
17 changes: 17 additions & 0 deletions src/datatypes/src/vectors/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ vectors::impl_try_from_arrow_array_for_vector!(StringArray, StringVector);
#[cfg(test)]
mod tests {

use std::vec;

use arrow::datatypes::DataType;

use super::*;
Expand Down Expand Up @@ -369,4 +371,19 @@ mod tests {
let serialized = serde_json::to_string(&vector.serialize_to_json().unwrap()).unwrap();
assert_eq!(r#"["🀀🀀🀀","🀁🀁🀁","🀂🀂🀂","🀃🀃🀃","🀆🀆"]"#, serialized);
}

#[test]
fn test_string_vector_builder_finish_cloned() {
let mut builder = StringVectorBuilder::with_capacity(1024);
builder.push(Some("1"));
builder.push(Some("2"));
builder.push(Some("3"));
let vector = builder.finish_cloned();
assert_eq!(vector.len(), 3);
assert_eq!(
r#"["1","2","3"]"#,
serde_json::to_string(&vector.serialize_to_json().unwrap()).unwrap(),
);
assert_eq!(builder.len(), 3);
}
}

0 comments on commit 43ea71e

Please sign in to comment.