1 // Copyright (C) 2013-2016 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7 #include <config.h>
8
9 #include <util/labeled_value.h>
10
11 #include <gtest/gtest.h>
12
13 using namespace std;
14 using namespace isc;
15 using namespace isc::util;
16
17 namespace {
18
19 /// @brief Verifies basic construction and accessors for LabeledValue.
TEST(LabeledValue,construction)20 TEST(LabeledValue, construction) {
21 /// Verify that an empty label is not allowed.
22 ASSERT_THROW(LabeledValue(1, ""), LabeledValueError);
23
24 /// Verify that a valid constructor works.
25 LabeledValuePtr lvp;
26 ASSERT_NO_THROW(lvp.reset(new LabeledValue(1, "NotBlank")));
27 ASSERT_TRUE(lvp);
28
29 // Verify that the value can be accessed.
30 EXPECT_EQ(1, lvp->getValue());
31
32 // Verify that the label can be accessed.
33 EXPECT_EQ("NotBlank", lvp->getLabel());
34 }
35
36 /// @brief Verifies the logical operators defined for LabeledValue.
TEST(LabeledValue,operators)37 TEST(LabeledValue, operators) {
38 LabeledValuePtr lvp1;
39 LabeledValuePtr lvp1Also;
40 LabeledValuePtr lvp2;
41
42 // Create three instances, two of which have the same numeric value.
43 ASSERT_NO_THROW(lvp1.reset(new LabeledValue(1, "One")));
44 ASSERT_NO_THROW(lvp1Also.reset(new LabeledValue(1, "OneAlso")));
45 ASSERT_NO_THROW(lvp2.reset(new LabeledValue(2, "Two")));
46
47 // Verify each of the operators.
48 EXPECT_TRUE(*lvp1 == *lvp1Also);
49 EXPECT_TRUE(*lvp1 != *lvp2);
50 EXPECT_TRUE(*lvp1 < *lvp2);
51 EXPECT_FALSE(*lvp2 < *lvp1);
52 }
53
54 /// @brief Verifies the default constructor for LabeledValueSet.
TEST(LabeledValueSet,construction)55 TEST(LabeledValueSet, construction) {
56 ASSERT_NO_THROW (LabeledValueSet());
57 }
58
59 /// @brief Verifies the basic operations of a LabeledValueSet.
60 /// Essentially we verify that we can define a set of valid entries and
61 /// look them up without issue.
TEST(LabeledValueSet,basicOperation)62 TEST(LabeledValueSet, basicOperation) {
63 const char* labels[] = {"Zero", "One", "Two", "Three" };
64 LabeledValueSet lvset;
65 LabeledValuePtr lvp;
66
67 // Verify the we cannot add an empty pointer to the set.
68 EXPECT_THROW(lvset.add(lvp), LabeledValueError);
69
70 // Verify that we can add an entry to the set via pointer.
71 ASSERT_NO_THROW(lvp.reset(new LabeledValue(0, labels[0])));
72 EXPECT_NO_THROW(lvset.add(lvp));
73
74 // Verify that we cannot add a duplicate entry.
75 EXPECT_THROW(lvset.add(lvp), LabeledValueError);
76
77 // Add the remaining entries using add(int,char*) variant.
78 for (int i = 1; i < 3; i++) {
79 EXPECT_NO_THROW(lvset.add(i, labels[i]));
80 }
81
82 // Verify that we can't add a duplicate entry this way either.
83 EXPECT_THROW ((lvset.add(0, labels[0])), LabeledValueError);
84
85 // Verify that we can look up all of the defined entries properly.
86 for (int i = 1; i < 3; i++) {
87 EXPECT_TRUE(lvset.isDefined(i));
88 EXPECT_NO_THROW(lvp = lvset.get(i));
89 EXPECT_EQ(lvp->getValue(), i);
90 EXPECT_EQ(lvp->getLabel(), labels[i]);
91 EXPECT_EQ(lvset.getLabel(i), labels[i]);
92 }
93
94 // Verify behavior for a value that is not defined.
95 EXPECT_FALSE(lvset.isDefined(4));
96 EXPECT_NO_THROW(lvp = lvset.get(4));
97 EXPECT_FALSE(lvp);
98 EXPECT_EQ(lvset.getLabel(4), LabeledValueSet::UNDEFINED_LABEL);
99 }
100
101 }
102