@@ -34,32 +34,14 @@ namespace arrow {
3434
3535class Buffer ;
3636
37- TEST (TypesTest, TestCharType) {
38- CharType t1 (5 );
39-
40- ASSERT_EQ (t1.type , Type::CHAR);
41- ASSERT_EQ (t1.size , 5 );
42-
43- ASSERT_EQ (t1.ToString (), std::string (" char(5)" ));
44-
45- // Test copy constructor
46- CharType t2 = t1;
47- ASSERT_EQ (t2.type , Type::CHAR);
48- ASSERT_EQ (t2.size , 5 );
49- }
50-
51- TEST (TypesTest, TestVarcharType) {
52- VarcharType t1 (5 );
53-
54- ASSERT_EQ (t1.type , Type::VARCHAR);
55- ASSERT_EQ (t1.size , 5 );
56-
57- ASSERT_EQ (t1.ToString (), std::string (" varchar(5)" ));
58-
59- // Test copy constructor
60- VarcharType t2 = t1;
61- ASSERT_EQ (t2.type , Type::VARCHAR);
62- ASSERT_EQ (t2.size , 5 );
37+ TEST (TypesTest, BinaryType) {
38+ BinaryType t1;
39+ BinaryType e1 ;
40+ StringType t2;
41+ EXPECT_TRUE (t1.Equals (&e1 ));
42+ EXPECT_FALSE (t1.Equals (&t2));
43+ ASSERT_EQ (t1.type , Type::BINARY);
44+ ASSERT_EQ (t1.ToString (), std::string (" binary" ));
6345}
6446
6547TEST (TypesTest, TestStringType) {
@@ -119,6 +101,7 @@ class TestStringContainer : public ::testing::Test {
119101TEST_F (TestStringContainer, TestArrayBasics) {
120102 ASSERT_EQ (length_, strings_->length ());
121103 ASSERT_EQ (1 , strings_->null_count ());
104+ ASSERT_OK (strings_->Validate ());
122105}
123106
124107TEST_F (TestStringContainer, TestType) {
@@ -163,7 +146,10 @@ class TestStringBuilder : public TestBuilder {
163146 builder_.reset (new StringBuilder (pool_, type_));
164147 }
165148
166- void Done () { result_ = std::dynamic_pointer_cast<StringArray>(builder_->Finish ()); }
149+ void Done () {
150+ result_ = std::dynamic_pointer_cast<StringArray>(builder_->Finish ());
151+ result_->Validate ();
152+ }
167153
168154 protected:
169155 TypePtr type_;
@@ -216,4 +202,152 @@ TEST_F(TestStringBuilder, TestZeroLength) {
216202 Done ();
217203}
218204
205+ // Binary container type
206+ // TODO(emkornfield) there should be some way to refactor these to avoid code duplicating
207+ // with String
208+ class TestBinaryContainer : public ::testing::Test {
209+ public:
210+ void SetUp () {
211+ chars_ = {' a' , ' b' , ' b' , ' c' , ' c' , ' c' };
212+ offsets_ = {0 , 1 , 1 , 1 , 3 , 6 };
213+ valid_bytes_ = {1 , 1 , 0 , 1 , 1 };
214+ expected_ = {" a" , " " , " " , " bb" , " ccc" };
215+
216+ MakeArray ();
217+ }
218+
219+ void MakeArray () {
220+ length_ = offsets_.size () - 1 ;
221+ int nchars = chars_.size ();
222+
223+ value_buf_ = test::to_buffer (chars_);
224+ values_ = ArrayPtr (new UInt8Array (nchars, value_buf_));
225+
226+ offsets_buf_ = test::to_buffer (offsets_);
227+
228+ null_bitmap_ = test::bytes_to_null_buffer (valid_bytes_);
229+ null_count_ = test::null_count (valid_bytes_);
230+
231+ strings_ = std::make_shared<BinaryArray>(
232+ length_, offsets_buf_, values_, null_count_, null_bitmap_);
233+ }
234+
235+ protected:
236+ std::vector<int32_t > offsets_;
237+ std::vector<char > chars_;
238+ std::vector<uint8_t > valid_bytes_;
239+
240+ std::vector<std::string> expected_;
241+
242+ std::shared_ptr<Buffer> value_buf_;
243+ std::shared_ptr<Buffer> offsets_buf_;
244+ std::shared_ptr<Buffer> null_bitmap_;
245+
246+ int null_count_;
247+ int length_;
248+
249+ ArrayPtr values_;
250+ std::shared_ptr<BinaryArray> strings_;
251+ };
252+
253+ TEST_F (TestBinaryContainer, TestArrayBasics) {
254+ ASSERT_EQ (length_, strings_->length ());
255+ ASSERT_EQ (1 , strings_->null_count ());
256+ ASSERT_OK (strings_->Validate ());
257+ }
258+
259+ TEST_F (TestBinaryContainer, TestType) {
260+ TypePtr type = strings_->type ();
261+
262+ ASSERT_EQ (Type::BINARY, type->type );
263+ ASSERT_EQ (Type::BINARY, strings_->type_enum ());
264+ }
265+
266+ TEST_F (TestBinaryContainer, TestListFunctions) {
267+ int pos = 0 ;
268+ for (size_t i = 0 ; i < expected_.size (); ++i) {
269+ ASSERT_EQ (pos, strings_->value_offset (i));
270+ ASSERT_EQ (expected_[i].size (), strings_->value_length (i));
271+ pos += expected_[i].size ();
272+ }
273+ }
274+
275+ TEST_F (TestBinaryContainer, TestDestructor) {
276+ auto arr = std::make_shared<BinaryArray>(
277+ length_, offsets_buf_, values_, null_count_, null_bitmap_);
278+ }
279+
280+ TEST_F (TestBinaryContainer, TestGetValue) {
281+ for (size_t i = 0 ; i < expected_.size (); ++i) {
282+ if (valid_bytes_[i] == 0 ) {
283+ ASSERT_TRUE (strings_->IsNull (i));
284+ } else {
285+ int32_t len = -1 ;
286+ const uint8_t * bytes = strings_->GetValue (i, &len);
287+ ASSERT_EQ (0 , std::memcmp (expected_[i].data (), bytes, len));
288+ }
289+ }
290+ }
291+
292+ class TestBinaryBuilder : public TestBuilder {
293+ public:
294+ void SetUp () {
295+ TestBuilder::SetUp ();
296+ type_ = TypePtr (new BinaryType ());
297+ builder_.reset (new BinaryBuilder (pool_, type_));
298+ }
299+
300+ void Done () {
301+ result_ = std::dynamic_pointer_cast<BinaryArray>(builder_->Finish ());
302+ result_->Validate ();
303+ }
304+
305+ protected:
306+ TypePtr type_;
307+
308+ std::unique_ptr<BinaryBuilder> builder_;
309+ std::shared_ptr<BinaryArray> result_;
310+ };
311+
312+ TEST_F (TestBinaryBuilder, TestScalarAppend) {
313+ std::vector<std::string> strings = {" " , " bb" , " a" , " " , " ccc" };
314+ std::vector<uint8_t > is_null = {0 , 0 , 0 , 1 , 0 };
315+
316+ int N = strings.size ();
317+ int reps = 1000 ;
318+
319+ for (int j = 0 ; j < reps; ++j) {
320+ for (int i = 0 ; i < N; ++i) {
321+ if (is_null[i]) {
322+ builder_->AppendNull ();
323+ } else {
324+ builder_->Append (
325+ reinterpret_cast <const uint8_t *>(strings[i].data ()), strings[i].size ());
326+ }
327+ }
328+ }
329+ Done ();
330+ ASSERT_OK (result_->Validate ());
331+ ASSERT_EQ (reps * N, result_->length ());
332+ ASSERT_EQ (reps, result_->null_count ());
333+ ASSERT_EQ (reps * 6 , result_->values ()->length ());
334+
335+ int32_t length;
336+ for (int i = 0 ; i < N * reps; ++i) {
337+ if (is_null[i % N]) {
338+ ASSERT_TRUE (result_->IsNull (i));
339+ } else {
340+ ASSERT_FALSE (result_->IsNull (i));
341+ const uint8_t * vals = result_->GetValue (i, &length);
342+ ASSERT_EQ (strings[i % N].size (), length);
343+ ASSERT_EQ (0 , std::memcmp (vals, strings[i % N].data (), length));
344+ }
345+ }
346+ }
347+
348+ TEST_F (TestBinaryBuilder, TestZeroLength) {
349+ // All buffers are null
350+ Done ();
351+ }
352+
219353} // namespace arrow
0 commit comments