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

Code before:
#include "arrow/array.h"
#include "arrow/memory_pool.h"
#include "arrow/table.h"
#include "arrow/testing/gtest_util.h"
#include "arrow/testing/matchers.h"
#include "arrow/testing/random.h"
#include "arrow/testing/util.h"
#include "arrow/type.h"
#include "arrow/type_traits.h"
#include "arrow/util/logging.h"
#include "arrow/util/checked_cast.h"
#include "arrow/util/key_value_metadata.h"

namespace arrow {

using internal::checked_cast;
using internal::checked_pointer_cast;

TEST(TestTypeId, AllTypeIds) {
  const auto all_ids = AllTypeIds();
  ASSERT_EQ(static_cast<int>(all_ids.size()), Type::MAX_ID);
}

TEST(TestTypeSingleton, ParameterFreeTypes) {
  // Test successful cases - parameter-free types
  std::vector<std::pair<Type::type, std::shared_ptr<DataType>>> cases = {
      {Type::NA, null()},
      {Type::BOOL, boolean()},
      {Type::INT8, int8()},
      {Type::INT16, int16()},
      {Type::INT32, int32()},
      {Type::INT64, int64()},
      {Type::UINT8, uint8()},
      {Type::UINT16, uint16()},
      {Type::UINT32, uint32()},
      {Type::UINT64, uint64()},
      {Type::HALF_FLOAT, float16()},
      {Type::FLOAT, float32()},
      {Type::DOUBLE, float64()},
      {Type::STRING, utf8()},
      {Type::BINARY, binary()},
      {Type::LARGE_STRING, large_utf8()},
      {Type::LARGE_BINARY, large_binary()},
      {Type::DATE32, date32()},
  };

  for (const auto& test_case : cases) {
    SCOPED_TRACE("Testing type: " + std::to_string(static_cast<int>(test_case.first)));
    auto result = type_singleton(test_case.first);
    ASSERT_OK_AND_ASSIGN(auto type, result);
    ASSERT_TRUE(type->Equals(*test_case.second))

Review comment (suggestion):
If we use `TypeTraits` in the implementation then we don't need to test these exhaustively, we can just check a couple examples.

Code after:

#include "arrow/array.h"
#include "arrow/memory_pool.h"
#include "arrow/table.h"
#include "arrow/testing/gtest_util.h"
#include "arrow/testing/matchers.h"
#include "arrow/testing/random.h"
#include "arrow/testing/util.h"
#include "arrow/type.h"
#include "arrow/type_traits.h"
#include "arrow/util/checked_cast.h"
#include "arrow/util/key_value_metadata.h"

namespace arrow {

using internal::checked_cast;
using internal::checked_pointer_cast;

TEST(TestTypeId, AllTypeIds) {
  const auto all_ids = AllTypeIds();
  ASSERT_EQ(static_cast<int>(all_ids.size()), Type::MAX_ID);
}

TEST(TestTypeSingleton, ParameterFreeTypes) {
  // Test successful cases - parameter-free types (sample a few)
  std::vector<std::pair<Type::type, std::shared_ptr<DataType>>> cases = {
      {Type::NA, null()},     {Type::BOOL, boolean()},  {Type::INT32, int32()},
      {Type::STRING, utf8()}, {Type::DATE32, date32()},
  };

  for (const auto& test_case : cases) {
    ARROW_SCOPED_TRACE("Testing type: ", internal::ToString(test_case.first));
    auto result = type_singleton(test_case.first);
    ASSERT_OK_AND_ASSIGN(auto type, result);
    AssertTypeEqual(*type, *test_case.second);
  }
}

TEST(TestTypeSingleton, ParameterizedTypes) {
  // Test error cases - parameterized types (test one representative)
  auto result = type_singleton(Type::TIMESTAMP);
  ASSERT_RAISES(TypeError, result);
  EXPECT_THAT(result.status().message(),
              testing::HasSubstr("is not a parameter-free type"));
}

template <typename ReprFunc>
void CheckTypeIdReprs(ReprFunc&& repr_func, bool expect_uppercase) {
  std::unordered_set<std::string> unique_reprs;
  const auto all_ids = AllTypeIds();
  for (const auto id : all_ids) {