|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include <gtest/gtest.h> |
| 19 | +#include <cstdint> |
| 20 | +#include <memory> |
| 21 | +#include <string> |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +#include "arrow/field.h" |
| 25 | +#include "arrow/schema.h" |
| 26 | +#include "arrow/table/column.h" |
| 27 | +#include "arrow/test-util.h" |
| 28 | +#include "arrow/type.h" |
| 29 | +#include "arrow/types/integer.h" |
| 30 | +#include "arrow/util/bit-util.h" |
| 31 | +#include "arrow/util/buffer.h" |
| 32 | +#include "arrow/util/memory-pool.h" |
| 33 | +#include "arrow/util/status.h" |
| 34 | + |
| 35 | +using std::shared_ptr; |
| 36 | +using std::vector; |
| 37 | + |
| 38 | +namespace arrow { |
| 39 | + |
| 40 | +class TestColumn : public ::testing::Test { |
| 41 | + public: |
| 42 | + void SetUp() { |
| 43 | + pool_ = GetDefaultMemoryPool(); |
| 44 | + } |
| 45 | + |
| 46 | + template <typename ArrayType> |
| 47 | + std::shared_ptr<Array> MakeArray(int32_t length, int32_t null_count = 0) { |
| 48 | + auto data = std::make_shared<PoolBuffer>(pool_); |
| 49 | + auto nulls = std::make_shared<PoolBuffer>(pool_); |
| 50 | + data->Resize(length * sizeof(typename ArrayType::value_type)); |
| 51 | + nulls->Resize(util::bytes_for_bits(length)); |
| 52 | + return std::make_shared<ArrayType>(length, data, 10, nulls); |
| 53 | + } |
| 54 | + |
| 55 | + protected: |
| 56 | + MemoryPool* pool_; |
| 57 | + |
| 58 | + std::shared_ptr<ChunkedArray> data_; |
| 59 | + std::unique_ptr<Column> column_; |
| 60 | +}; |
| 61 | + |
| 62 | +TEST_F(TestColumn, BasicAPI) { |
| 63 | + ArrayVector arrays; |
| 64 | + arrays.push_back(MakeArray<Int32Array>(100)); |
| 65 | + arrays.push_back(MakeArray<Int32Array>(100, 10)); |
| 66 | + arrays.push_back(MakeArray<Int32Array>(100, 20)); |
| 67 | + |
| 68 | + auto field = std::make_shared<Field>("c0", INT32); |
| 69 | + column_.reset(new Column(field, arrays)); |
| 70 | + |
| 71 | + ASSERT_EQ("c0", column_->name()); |
| 72 | + ASSERT_TRUE(column_->type()->Equals(INT32)); |
| 73 | + ASSERT_EQ(300, column_->length()); |
| 74 | + ASSERT_EQ(30, column_->null_count()); |
| 75 | + ASSERT_EQ(3, column_->data()->num_chunks()); |
| 76 | +} |
| 77 | + |
| 78 | +TEST_F(TestColumn, ChunksInhomogeneous) { |
| 79 | + ArrayVector arrays; |
| 80 | + arrays.push_back(MakeArray<Int32Array>(100)); |
| 81 | + arrays.push_back(MakeArray<Int32Array>(100, 10)); |
| 82 | + |
| 83 | + auto field = std::make_shared<Field>("c0", INT32); |
| 84 | + column_.reset(new Column(field, arrays)); |
| 85 | + |
| 86 | + ASSERT_OK(column_->ValidateData()); |
| 87 | + |
| 88 | + arrays.push_back(MakeArray<Int16Array>(100, 10)); |
| 89 | + column_.reset(new Column(field, arrays)); |
| 90 | + ASSERT_RAISES(Invalid, column_->ValidateData()); |
| 91 | +} |
| 92 | + |
| 93 | +} // namespace arrow |
0 commit comments