Skip to content

Commit 91cd927

Browse files
committed
add more unit tests
1 parent f51e8d3 commit 91cd927

File tree

1 file changed

+92
-1
lines changed

1 file changed

+92
-1
lines changed

test/core/type_test.cc

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121

2222
#include <format>
2323
#include <memory>
24+
#include <stdexcept>
2425
#include <string>
2526

27+
#include <gmock/gmock.h>
2628
#include <gtest/gtest.h>
2729

28-
#include "gtest/gtest.h"
2930
#include "iceberg/util/formatter.h"
3031

3132
struct TypeTestCase {
@@ -254,3 +255,93 @@ INSTANTIATE_TEST_SUITE_P(Primitive, TypeTest, ::testing::ValuesIn(kPrimitiveType
254255

255256
INSTANTIATE_TEST_SUITE_P(Nested, TypeTest, ::testing::ValuesIn(kNestedTypes),
256257
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

Comments
 (0)