|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/util/formatter.h" |
| 21 | + |
| 22 | +#include <format> |
| 23 | +#include <map> |
| 24 | +#include <memory> |
| 25 | +#include <string> |
| 26 | +#include <unordered_map> |
| 27 | +#include <vector> |
| 28 | + |
| 29 | +#include <gtest/gtest.h> |
| 30 | + |
| 31 | +#include "iceberg/statistics_file.h" |
| 32 | + |
| 33 | +namespace iceberg { |
| 34 | + |
| 35 | +// Tests for the std::format specializations |
| 36 | +TEST(FormatterTest, VectorFormat) { |
| 37 | + std::vector<int> empty; |
| 38 | + EXPECT_EQ("[]", std::format("{}", empty)); |
| 39 | + |
| 40 | + std::vector<int> nums = {1, 2, 3, 4, 5}; |
| 41 | + EXPECT_EQ("[1, 2, 3, 4, 5]", std::format("{}", nums)); |
| 42 | + |
| 43 | + std::vector<std::string> names = {"Alice", "Bob", "Charlie"}; |
| 44 | + EXPECT_EQ("[Alice, Bob, Charlie]", std::format("{}", names)); |
| 45 | +} |
| 46 | + |
| 47 | +TEST(FormatterTest, MapFormat) { |
| 48 | + std::map<std::string, int> empty; |
| 49 | + EXPECT_EQ("{}", std::format("{}", empty)); |
| 50 | + |
| 51 | + std::map<std::string, int> ages = {{"Alice", 30}, {"Bob", 25}, {"Charlie", 35}}; |
| 52 | + EXPECT_EQ("{Alice: 30, Bob: 25, Charlie: 35}", std::format("{}", ages)); |
| 53 | +} |
| 54 | + |
| 55 | +TEST(FormatterTest, UnorderedMapFormat) { |
| 56 | + std::unordered_map<std::string, double> empty; |
| 57 | + EXPECT_EQ("{}", std::format("{}", empty)); |
| 58 | + |
| 59 | + std::unordered_map<std::string, double> scores = { |
| 60 | + {"Alice", 95.5}, {"Bob", 87.0}, {"Charlie", 92.3}}; |
| 61 | + std::string str = std::format("{}", scores); |
| 62 | + EXPECT_TRUE(str.find("Alice: 95.5") != std::string::npos); |
| 63 | + EXPECT_TRUE(str.find("Bob: 87") != std::string::npos); |
| 64 | + EXPECT_TRUE(str.find("Charlie: 92.3") != std::string::npos); |
| 65 | +} |
| 66 | + |
| 67 | +TEST(FormatterTest, NestedContainersFormat) { |
| 68 | + std::vector<std::map<std::string, int>> nested = {{{"a", 1}, {"b", 2}}, |
| 69 | + {{"c", 3}, {"d", 4}}}; |
| 70 | + |
| 71 | + EXPECT_EQ("[{a: 1, b: 2}, {c: 3, d: 4}]", std::format("{}", nested)); |
| 72 | + |
| 73 | + std::map<std::string, std::vector<int>> nested_map = { |
| 74 | + {"primes", {2, 3, 5, 7, 11}}, {"fibonacci", {1, 1, 2, 3, 5, 8, 13}}}; |
| 75 | + std::string result = std::format("{}", nested_map); |
| 76 | + EXPECT_TRUE(result.find("primes") != std::string::npos); |
| 77 | + EXPECT_TRUE(result.find("fibonacci") != std::string::npos); |
| 78 | + EXPECT_TRUE(result.find("[2, 3, 5, 7, 11]") != std::string::npos); |
| 79 | + EXPECT_TRUE(result.find("[1, 1, 2, 3, 5, 8, 13]") != std::string::npos); |
| 80 | +} |
| 81 | + |
| 82 | +TEST(FormatterTest, EdgeCasesFormat) { |
| 83 | + std::vector<int> single_vec = {42}; |
| 84 | + EXPECT_EQ("[42]", std::format("{}", single_vec)); |
| 85 | + |
| 86 | + std::map<std::string, int> single_map = {{"key", 42}}; |
| 87 | + EXPECT_EQ("{key: 42}", std::format("{}", single_map)); |
| 88 | + |
| 89 | + std::vector<std::vector<int>> nested_empty = {{}, {1, 2}, {}}; |
| 90 | + EXPECT_EQ("[[], [1, 2], []]", std::format("{}", nested_empty)); |
| 91 | +} |
| 92 | + |
| 93 | +TEST(FormatterTest, SmartPointerFormat) { |
| 94 | + std::vector<std::shared_ptr<int>> int_ptrs; |
| 95 | + int_ptrs.push_back(std::make_shared<int>(42)); |
| 96 | + int_ptrs.push_back(std::make_shared<int>(123)); |
| 97 | + int_ptrs.push_back(nullptr); |
| 98 | + EXPECT_EQ("[42, 123, null]", std::format("{}", int_ptrs)); |
| 99 | + |
| 100 | + std::vector<std::shared_ptr<std::string>> str_ptrs; |
| 101 | + str_ptrs.push_back(std::make_shared<std::string>("hello")); |
| 102 | + str_ptrs.push_back(std::make_shared<std::string>("world")); |
| 103 | + str_ptrs.push_back(nullptr); |
| 104 | + EXPECT_EQ("[hello, world, null]", std::format("{}", str_ptrs)); |
| 105 | + |
| 106 | + std::map<std::string, std::shared_ptr<int>> map_with_ptr_values; |
| 107 | + map_with_ptr_values["one"] = std::make_shared<int>(1); |
| 108 | + map_with_ptr_values["two"] = std::make_shared<int>(2); |
| 109 | + map_with_ptr_values["null"] = nullptr; |
| 110 | + EXPECT_EQ("{null: null, one: 1, two: 2}", std::format("{}", map_with_ptr_values)); |
| 111 | + |
| 112 | + std::unordered_map<std::string, std::shared_ptr<double>> scores; |
| 113 | + scores["Alice"] = std::make_shared<double>(95.5); |
| 114 | + scores["Bob"] = std::make_shared<double>(87.0); |
| 115 | + scores["Charlie"] = nullptr; |
| 116 | + std::string str = std::format("{}", scores); |
| 117 | + EXPECT_TRUE(str.find("Alice: 95.5") != std::string::npos); |
| 118 | + EXPECT_TRUE(str.find("Bob: 87") != std::string::npos); |
| 119 | + EXPECT_TRUE(str.find("Charlie: null") != std::string::npos); |
| 120 | + |
| 121 | + std::vector<std::map<std::string, std::shared_ptr<int>>> nested; |
| 122 | + std::map<std::string, std::shared_ptr<int>> map1; |
| 123 | + map1["a"] = std::make_shared<int>(1); |
| 124 | + map1["b"] = std::make_shared<int>(2); |
| 125 | + std::map<std::string, std::shared_ptr<int>> map2; |
| 126 | + map2["c"] = std::make_shared<int>(3); |
| 127 | + map2["d"] = nullptr; |
| 128 | + nested.push_back(std::move(map1)); |
| 129 | + nested.push_back(std::move(map2)); |
| 130 | + EXPECT_EQ("[{a: 1, b: 2}, {c: 3, d: null}]", std::format("{}", nested)); |
| 131 | +} |
| 132 | + |
| 133 | +TEST(FormatterTest, StatisticsFileFormat) { |
| 134 | + StatisticsFile statistics_file{ |
| 135 | + .snapshot_id = 123, |
| 136 | + .path = "test_path", |
| 137 | + .file_size_in_bytes = 100, |
| 138 | + .file_footer_size_in_bytes = 20, |
| 139 | + .blob_metadata = {BlobMetadata{.type = "type1", |
| 140 | + .source_snapshot_id = 1, |
| 141 | + .source_snapshot_sequence_number = 1, |
| 142 | + .fields = {1, 2, 3}, |
| 143 | + .properties = {{"key1", "value1"}}}, |
| 144 | + BlobMetadata{.type = "type2", |
| 145 | + .source_snapshot_id = 2, |
| 146 | + .source_snapshot_sequence_number = 2, |
| 147 | + .fields = {4, 5, 6}, |
| 148 | + .properties = {}}}}; |
| 149 | + |
| 150 | + const std::string expected = |
| 151 | + "StatisticsFile[" |
| 152 | + "snapshotId=123,path=test_path,fileSizeInBytes=100,fileFooterSizeInBytes=20," |
| 153 | + "blobMetadata=[" |
| 154 | + "BlobMetadata[type='type1',sourceSnapshotId=1,sourceSnapshotSequenceNumber=1," |
| 155 | + "fields=[1, 2, 3],properties={key1: value1}], " |
| 156 | + "BlobMetadata[type='type2',sourceSnapshotId=2,sourceSnapshotSequenceNumber=2," |
| 157 | + "fields=[4, 5, 6],properties={}]" |
| 158 | + "]" |
| 159 | + "]"; |
| 160 | + EXPECT_EQ(expected, std::format("{}", statistics_file)); |
| 161 | +} |
| 162 | + |
| 163 | +} // namespace iceberg |
0 commit comments