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

Code before:
      {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(),
                            [=](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());
}


Review comment (suggestion):
I don't think this test is useful, passing an invalid type id is a programming error.

Code after:
      {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) {
  // Should be all uppercase strings (corresponding to the enum member names)
  CheckTypeIdReprs([](Type::type id) { return internal::ToString(id); },
                   /* expect_uppercase=*/true);
}

TEST(TestTypeId, ToTypeName) {
  // Should be all lowercase strings (corresponding to TypeClass::type_name())
  CheckTypeIdReprs([](Type::type id) { return internal::ToTypeName(id); },
                   /* expect_uppercase=*/false);
}

TEST(TestField, Basics) {