summaryrefslogtreecommitdiffstats
path: root/src/test/common/test_option.cc
blob: a75f74dc96daf1a402033f16573adba6d1104804 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:nil -*-
// vim: ts=8 sw=2 smarttab expandtab

#include <string.h>
#include <errno.h>
#include <stdlib.h>

#include <gtest/gtest.h>

#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:
 */