|
| 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 <string> |
| 21 | + |
| 22 | +#include <gtest/gtest.h> |
| 23 | +#include <iceberg/util/config.h> |
| 24 | + |
| 25 | +namespace iceberg { |
| 26 | + |
| 27 | +enum class TestEnum { VALUE1, VALUE2, VALUE3 }; |
| 28 | + |
| 29 | +std::string EnumToString(const TestEnum& val) { |
| 30 | + switch (val) { |
| 31 | + case TestEnum::VALUE1: |
| 32 | + return "VALUE1"; |
| 33 | + case TestEnum::VALUE2: |
| 34 | + return "VALUE2"; |
| 35 | + case TestEnum::VALUE3: |
| 36 | + return "VALUE3"; |
| 37 | + default: |
| 38 | + throw std::runtime_error("Invalid enum value"); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +TestEnum StringToEnum(const std::string& val) { |
| 43 | + if (val == "VALUE1") { |
| 44 | + return TestEnum::VALUE1; |
| 45 | + } else if (val == "VALUE2") { |
| 46 | + return TestEnum::VALUE2; |
| 47 | + } else if (val == "VALUE3") { |
| 48 | + return TestEnum::VALUE3; |
| 49 | + } else { |
| 50 | + throw std::runtime_error("Invalid enum string"); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// Define a concrete config class for testing |
| 55 | +class TestConfig : public ConfigBase<TestConfig> { |
| 56 | + public: |
| 57 | + template <typename T> |
| 58 | + using Entry = const ConfigBase<TestConfig>::Entry<T>; |
| 59 | + |
| 60 | + inline static const Entry<std::string> kStringConfig{"string_config", "default_value"}; |
| 61 | + inline static const Entry<int> kIntConfig{"int_config", 25}; |
| 62 | + inline static const Entry<bool> kBoolConfig{"bool_config", false}; |
| 63 | + inline static const Entry<TestEnum> kEnumConfig{"enum_config", TestEnum::VALUE1, |
| 64 | + EnumToString, StringToEnum}; |
| 65 | + inline static const Entry<double> kDoubleConfig{"double_config", 3.14}; |
| 66 | +}; |
| 67 | + |
| 68 | +TEST(ConfigTest, BasicOperations) { |
| 69 | + TestConfig config; |
| 70 | + |
| 71 | + // Test default values |
| 72 | + ASSERT_EQ(config.Get(TestConfig::kStringConfig), std::string("default_value")); |
| 73 | + ASSERT_EQ(config.Get(TestConfig::kIntConfig), 25); |
| 74 | + ASSERT_EQ(config.Get(TestConfig::kBoolConfig), false); |
| 75 | + ASSERT_EQ(config.Get(TestConfig::kEnumConfig), TestEnum::VALUE1); |
| 76 | + ASSERT_EQ(config.Get(TestConfig::kDoubleConfig), 3.14); |
| 77 | + |
| 78 | + // Test setting values |
| 79 | + config.Set(TestConfig::kStringConfig, std::string("new_value")); |
| 80 | + config.Set(TestConfig::kIntConfig, 100); |
| 81 | + config.Set(TestConfig::kBoolConfig, true); |
| 82 | + config.Set(TestConfig::kEnumConfig, TestEnum::VALUE2); |
| 83 | + config.Set(TestConfig::kDoubleConfig, 2.99); |
| 84 | + |
| 85 | + ASSERT_EQ(config.Get(TestConfig::kStringConfig), "new_value"); |
| 86 | + ASSERT_EQ(config.Get(TestConfig::kIntConfig), 100); |
| 87 | + ASSERT_EQ(config.Get(TestConfig::kBoolConfig), true); |
| 88 | + ASSERT_EQ(config.Get(TestConfig::kEnumConfig), TestEnum::VALUE2); |
| 89 | + ASSERT_EQ(config.Get(TestConfig::kDoubleConfig), 2.99); |
| 90 | + |
| 91 | + // Test unsetting a value |
| 92 | + config.Unset(TestConfig::kIntConfig); |
| 93 | + config.Unset(TestConfig::kEnumConfig); |
| 94 | + config.Unset(TestConfig::kDoubleConfig); |
| 95 | + ASSERT_EQ(config.Get(TestConfig::kIntConfig), 25); |
| 96 | + ASSERT_EQ(config.Get(TestConfig::kStringConfig), "new_value"); |
| 97 | + ASSERT_EQ(config.Get(TestConfig::kBoolConfig), true); |
| 98 | + ASSERT_EQ(config.Get(TestConfig::kEnumConfig), TestEnum::VALUE1); |
| 99 | + ASSERT_EQ(config.Get(TestConfig::kDoubleConfig), 3.14); |
| 100 | + |
| 101 | + // Test resetting all values |
| 102 | + config.Reset(); |
| 103 | + ASSERT_EQ(config.Get(TestConfig::kStringConfig), "default_value"); |
| 104 | + ASSERT_EQ(config.Get(TestConfig::kIntConfig), 25); |
| 105 | + ASSERT_EQ(config.Get(TestConfig::kBoolConfig), false); |
| 106 | + ASSERT_EQ(config.Get(TestConfig::kEnumConfig), TestEnum::VALUE1); |
| 107 | + ASSERT_EQ(config.Get(TestConfig::kDoubleConfig), 3.14); |
| 108 | +} |
| 109 | + |
| 110 | +} // namespace iceberg |
0 commit comments