From e6918187568dbd01842d8d1d2c808ce16a894239 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 21 Apr 2024 13:54:28 +0200 Subject: Adding upstream version 18.2.2. Signed-off-by: Daniel Baumann --- src/test/common/test_option.cc | 73 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/test/common/test_option.cc (limited to 'src/test/common/test_option.cc') diff --git a/src/test/common/test_option.cc b/src/test/common/test_option.cc new file mode 100644 index 000000000..a75f74dc9 --- /dev/null +++ b/src/test/common/test_option.cc @@ -0,0 +1,73 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:nil -*- +// vim: ts=8 sw=2 smarttab expandtab + +#include +#include +#include + +#include + +#include "common/options.h" + +using namespace std; + +TEST(Option, validate_min_max) +{ + auto opt = Option{"foo", Option::TYPE_MILLISECS, Option::LEVEL_ADVANCED} + .set_default(42) + .set_min_max(10, 128); + struct test_t { + unsigned new_val; + int expected_retval; + }; + test_t tests[] = + {{9, -EINVAL}, + {10, 0}, + {11, 0}, + {128, 0}, + {1024, -EINVAL} + }; + for (auto& test : tests) { + Option::value_t new_value = std::chrono::milliseconds{test.new_val}; + std::string err; + GTEST_ASSERT_EQ(test.expected_retval, opt.validate(new_value, &err)); + } +} + +TEST(Option, parse) +{ + auto opt = Option{"foo", Option::TYPE_MILLISECS, Option::LEVEL_ADVANCED} + .set_default(42) + .set_min_max(10, 128); + struct test_t { + string new_val; + int expected_retval; + unsigned expected_parsed_val; + }; + test_t tests[] = + {{"9", -EINVAL, 0}, + {"10", 0, 10}, + {"11", 0, 11}, + {"128", 0, 128}, + {"1024", -EINVAL, 0} + }; + for (auto& test : tests) { + Option::value_t parsed_val; + std::string err; + GTEST_ASSERT_EQ(test.expected_retval, + opt.parse_value(test.new_val, &parsed_val, &err)); + if (test.expected_retval == 0) { + Option::value_t expected_parsed_val = + std::chrono::milliseconds{test.expected_parsed_val}; + GTEST_ASSERT_EQ(parsed_val, expected_parsed_val); + } + } +} + +/* + * Local Variables: + * compile-command: "cd ../../../build ; + * ninja unittest_option && bin/unittest_option + * " + * End: + */ -- cgit v1.2.3