|
21 | 21 |
|
22 | 22 | #include <format> |
23 | 23 | #include <memory> |
| 24 | +#include <stdexcept> |
24 | 25 | #include <string> |
25 | 26 |
|
| 27 | +#include <gmock/gmock.h> |
26 | 28 | #include <gtest/gtest.h> |
27 | 29 |
|
28 | | -#include "gtest/gtest.h" |
29 | 30 | #include "iceberg/util/formatter.h" |
30 | 31 |
|
31 | 32 | struct TypeTestCase { |
@@ -254,3 +255,93 @@ INSTANTIATE_TEST_SUITE_P(Primitive, TypeTest, ::testing::ValuesIn(kPrimitiveType |
254 | 255 |
|
255 | 256 | INSTANTIATE_TEST_SUITE_P(Nested, TypeTest, ::testing::ValuesIn(kNestedTypes), |
256 | 257 | TypeTestCaseToString); |
| 258 | + |
| 259 | +TEST(TypeTest, Equality) { |
| 260 | + std::vector<std::shared_ptr<iceberg::Type>> alltypes; |
| 261 | + for (const auto& test_case : kPrimitiveTypes) { |
| 262 | + alltypes.push_back(test_case.type); |
| 263 | + } |
| 264 | + for (const auto& test_case : kNestedTypes) { |
| 265 | + alltypes.push_back(test_case.type); |
| 266 | + } |
| 267 | + |
| 268 | + for (size_t i = 0; i < alltypes.size(); i++) { |
| 269 | + for (size_t j = 0; j < alltypes.size(); j++) { |
| 270 | + SCOPED_TRACE(std::format("{} == {}", *alltypes[i], *alltypes[j])); |
| 271 | + |
| 272 | + if (i == j) { |
| 273 | + ASSERT_EQ(*alltypes[i], *alltypes[j]); |
| 274 | + } else { |
| 275 | + ASSERT_NE(*alltypes[i], *alltypes[j]); |
| 276 | + } |
| 277 | + } |
| 278 | + } |
| 279 | +} |
| 280 | + |
| 281 | +TEST(TypeTest, Decimal) { |
| 282 | + { |
| 283 | + iceberg::DecimalType decimal(38, 2); |
| 284 | + ASSERT_EQ(38, decimal.precision()); |
| 285 | + ASSERT_EQ(2, decimal.scale()); |
| 286 | + } |
| 287 | + { |
| 288 | + iceberg::DecimalType decimal(10, -10); |
| 289 | + ASSERT_EQ(10, decimal.precision()); |
| 290 | + ASSERT_EQ(-10, decimal.scale()); |
| 291 | + } |
| 292 | + ASSERT_THAT([]() { iceberg::DecimalType decimal(-1, 10); }, |
| 293 | + ::testing::ThrowsMessage<std::runtime_error>( |
| 294 | + ::testing::HasSubstr("precision must be in [0, 38], was -1"))); |
| 295 | + |
| 296 | + ASSERT_THAT([]() { iceberg::DecimalType decimal(39, 10); }, |
| 297 | + ::testing::ThrowsMessage<std::runtime_error>( |
| 298 | + ::testing::HasSubstr("precision must be in [0, 38], was 39"))); |
| 299 | +} |
| 300 | + |
| 301 | +TEST(TypeTest, Fixed) { |
| 302 | + { |
| 303 | + iceberg::FixedType fixed(0); |
| 304 | + ASSERT_EQ(0, fixed.length()); |
| 305 | + } |
| 306 | + { |
| 307 | + iceberg::FixedType fixed(1); |
| 308 | + ASSERT_EQ(1, fixed.length()); |
| 309 | + } |
| 310 | + { |
| 311 | + iceberg::FixedType fixed(127); |
| 312 | + ASSERT_EQ(127, fixed.length()); |
| 313 | + } |
| 314 | + ASSERT_THAT([]() { iceberg::FixedType decimal(-1); }, |
| 315 | + ::testing::ThrowsMessage<std::runtime_error>( |
| 316 | + ::testing::HasSubstr("length must be >= 0, was -1"))); |
| 317 | +} |
| 318 | + |
| 319 | +TEST(TypeTest, List) { |
| 320 | + { |
| 321 | + iceberg::SchemaField field(5, "element", std::make_shared<iceberg::Int32Type>(), |
| 322 | + true); |
| 323 | + iceberg::ListType list(field); |
| 324 | + std::span<const iceberg::SchemaField> fields = list.fields(); |
| 325 | + ASSERT_EQ(1, fields.size()); |
| 326 | + ASSERT_EQ(field, fields[0]); |
| 327 | + ASSERT_THAT(list.GetFieldById(5), ::testing::Optional(field)); |
| 328 | + ASSERT_THAT(list.GetFieldByIndex(0), ::testing::Optional(field)); |
| 329 | + ASSERT_THAT(list.GetFieldByName("element"), ::testing::Optional(field)); |
| 330 | + |
| 331 | + ASSERT_EQ(std::nullopt, list.GetFieldById(0)); |
| 332 | + ASSERT_EQ(std::nullopt, list.GetFieldByIndex(1)); |
| 333 | + ASSERT_EQ(std::nullopt, list.GetFieldByIndex(-1)); |
| 334 | + ASSERT_EQ(std::nullopt, list.GetFieldByName("foo")); |
| 335 | + } |
| 336 | + ASSERT_THAT( |
| 337 | + []() { |
| 338 | + iceberg::ListType list(iceberg::SchemaField( |
| 339 | + 1, "wrongname", std::make_shared<iceberg::BooleanType>(), true)); |
| 340 | + }, |
| 341 | + ::testing::ThrowsMessage<std::runtime_error>( |
| 342 | + ::testing::HasSubstr("child field name should be 'element', was 'wrongname'"))); |
| 343 | +} |
| 344 | + |
| 345 | +TEST(TypeTest, Map) {} |
| 346 | + |
| 347 | +TEST(TypeTest, Struct) {} |
0 commit comments