Repository: apache/arrow
File: cpp/src/arrow/array/array_test.cc (C++)

Code before:
        slices.push_back(slice);

        ArraySpan span(*slice->data());
        ASSERT_OK(builder->AppendArraySlice(span, 0, slice->length()));
      }

      std::shared_ptr<Array> actual;
      ASSERT_OK(builder->Finish(&actual));
      ASSERT_OK(actual->ValidateFull());

      ASSERT_OK_AND_ASSIGN(auto expected, Concatenate(slices, pool_));
      AssertResult(*expected, *actual);
    }
  }
};

template <typename Type>
class TestBuilderAppendArraySliceTyped : public TestBuilderAppendArraySlice {};

TYPED_TEST_SUITE(TestBuilderAppendArraySliceTyped, PrimitiveArrowTypes);
TYPED_TEST(TestBuilderAppendArraySliceTyped, Primitives) {
  this->CheckAppendArraySlice(TypeTraits<TypeParam>::type_singleton());
}

template <typename Type>
class TestBuilderAppendArraySliceBinary : public TestBuilderAppendArraySlice {};

using BinaryAppendArraySliceTypes =
    ::testing::Types<BinaryType, LargeBinaryType, StringType, LargeStringType>;
TYPED_TEST_SUITE(TestBuilderAppendArraySliceBinary, BinaryAppendArraySliceTypes);
TYPED_TEST(TestBuilderAppendArraySliceBinary, Binary) {
  this->CheckAppendArraySlice(TypeTraits<TypeParam>::type_singleton());
}

TEST_F(TestBuilderAppendArraySlice, List) { CheckAppendArraySlice(list(int32())); }

TEST_F(TestBuilderAppendArraySlice, LargeList) {
  CheckAppendArraySlice(large_list(int32()));
}

TEST_F(TestBuilderAppendArraySlice, ListView) {
  CheckAppendArraySlice(list_view(int32()));
}

TEST_F(TestBuilderAppendArraySlice, LargeListView) {
  CheckAppendArraySlice(large_list_view(int32()));
}

TEST_F(TestBuilderAppendArraySlice, FixedSizeBinary) {
  CheckAppendArraySlice(fixed_size_binary(10));
}

Review comment (suggestion):
We don't actually need templating, we can just generate the types dynamically using `PrimitiveTypes()`, `BaseBinaryTypes()` etc. This will reduce code generation and compile times slightly.

Code after:
        slices.push_back(slice);

        ArraySpan span(*slice->data());
        ASSERT_OK(builder->AppendArraySlice(span, 0, slice->length()));
      }

      std::shared_ptr<Array> actual;
      ASSERT_OK(builder->Finish(&actual));
      ASSERT_OK(actual->ValidateFull());

      ASSERT_OK_AND_ASSIGN(auto expected, Concatenate(slices, pool_));
      AssertResult(*expected, *actual);
    }
  }

  void CheckAppendArraySlice(const std::vector<std::shared_ptr<DataType>>& types) {
    for (const auto& type : types) {
      ARROW_SCOPED_TRACE("type = ", type->ToString());
      CheckAppendArraySlice(type);
    }
  }
};

TEST_F(TestBuilderAppendArraySlice, Primitives) {
  CheckAppendArraySlice(PrimitiveTypes());
}

TEST_F(TestBuilderAppendArraySlice, Temporals) { CheckAppendArraySlice(TemporalTypes()); }

TEST_F(TestBuilderAppendArraySlice, Intervals) { CheckAppendArraySlice(IntervalTypes()); }

TEST_F(TestBuilderAppendArraySlice, Durations) { CheckAppendArraySlice(DurationTypes()); }

TEST_F(TestBuilderAppendArraySlice, Decimals) {
  CheckAppendArraySlice(
      {decimal32(7, 2), decimal64(12, 2), decimal128(10, 2), decimal256(10, 2)});
}

TEST_F(TestBuilderAppendArraySlice, Nested) {
  CheckAppendArraySlice({list(int32()), large_list(int32()), list_view(int32()),
                         large_list_view(int32()), fixed_size_list(int32(), 3),
                         struct_({field("a", int32()), field("b", utf8())}),
                         sparse_union({field("a", int32()), field("b", utf8())}),
                         dense_union({field("a", int32()), field("b", utf8())})});
}

TEST_F(TestBuilderAppendArraySlice, FixedSizeBinary) {
  CheckAppendArraySlice(fixed_size_binary(10));
}

TEST_F(TestBuilderAppendArraySlice, Float16) { CheckAppendArraySlice(float16()); }