]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/common/test_option.cc
import quincy beta 17.1.0
[ceph.git] / ceph / src / test / common / test_option.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:nil -*-
2 // vim: ts=8 sw=2 smarttab expandtab
3
4 #include <string.h>
5 #include <errno.h>
6 #include <stdlib.h>
7
8 #include <gtest/gtest.h>
9
10 #include "common/options.h"
11
12 using namespace std;
13
14 TEST(Option, validate_min_max)
15 {
16 auto opt = Option{"foo", Option::TYPE_MILLISECS, Option::LEVEL_ADVANCED}
17 .set_default(42)
18 .set_min_max(10, 128);
19 struct test_t {
20 unsigned new_val;
21 int expected_retval;
22 };
23 test_t tests[] =
24 {{9, -EINVAL},
25 {10, 0},
26 {11, 0},
27 {128, 0},
28 {1024, -EINVAL}
29 };
30 for (auto& test : tests) {
31 Option::value_t new_value = std::chrono::milliseconds{test.new_val};
32 std::string err;
33 GTEST_ASSERT_EQ(test.expected_retval, opt.validate(new_value, &err));
34 }
35 }
36
37 TEST(Option, parse)
38 {
39 auto opt = Option{"foo", Option::TYPE_MILLISECS, Option::LEVEL_ADVANCED}
40 .set_default(42)
41 .set_min_max(10, 128);
42 struct test_t {
43 string new_val;
44 int expected_retval;
45 unsigned expected_parsed_val;
46 };
47 test_t tests[] =
48 {{"9", -EINVAL, 0},
49 {"10", 0, 10},
50 {"11", 0, 11},
51 {"128", 0, 128},
52 {"1024", -EINVAL, 0}
53 };
54 for (auto& test : tests) {
55 Option::value_t parsed_val;
56 std::string err;
57 GTEST_ASSERT_EQ(test.expected_retval,
58 opt.parse_value(test.new_val, &parsed_val, &err));
59 if (test.expected_retval == 0) {
60 Option::value_t expected_parsed_val =
61 std::chrono::milliseconds{test.expected_parsed_val};
62 GTEST_ASSERT_EQ(parsed_val, expected_parsed_val);
63 }
64 }
65 }
66
67 /*
68 * Local Variables:
69 * compile-command: "cd ../../../build ;
70 * ninja unittest_option && bin/unittest_option
71 * "
72 * End:
73 */