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

Code before:

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) {
    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))
        << "Failed on type " << test_case.first << ". Expected "
        << test_case.second->ToString() << " but got " << type->ToString();
  }
}

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

TEST(TestTypeSingleton, InvalidType) {
  // Test with an invalid type ID
  auto result = type_singleton(static_cast<Type::type>(9999));
  ASSERT_FALSE(result.ok());
  EXPECT_TRUE(result.status().IsTypeError());
}

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) {
    std::string repr = repr_func(id);
    ASSERT_TRUE(std::all_of(repr.begin(), repr.end(),

Review comment (suggestion):
Suggested improvement: you can use `AssertTypeEqual` to automate the generation of a nice error message if the types don't match.

Code after:
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) {
    std::string repr = repr_func(id);
    ASSERT_TRUE(std::all_of(repr.begin(), repr.end(),
                            [=](const char c) {
                              return c == '_' || std::isdigit(c) ||
                                     (expect_uppercase ? std::isupper(c)
                                                       : std::islower(c));
                            }))
        << "Invalid type id repr: '" << repr << "'";
    unique_reprs.insert(std::move(repr));
  }
  // No duplicates
  ASSERT_EQ(unique_reprs.size(), all_ids.size());
}

TEST(TestTypeId, ToString) {