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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246 | // Copyright (C) 2018-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
#include <cc/stamped_value.h>
#include <exceptions/exceptions.h>
#include <boost/scoped_ptr.hpp><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <gtest/gtest.h><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
using namespace isc;
using namespace isc::data;
namespace {
// Tests that the stamped value can be created with a NULL value.
TEST(StampedValueTest, createNull) {
StampedValuePtr value;
ASSERT_NO_THROW(value = StampedValue::create("bar"));
EXPECT_TRUE(value->amNull());
EXPECT_THROW(value->getType(), InvalidOperation);
EXPECT_THROW(value->getValue(), InvalidOperation);
EXPECT_THROW(value->getIntegerValue(), InvalidOperation);
EXPECT_THROW(value->getBoolValue(), InvalidOperation);
EXPECT_THROW(value->getDoubleValue(), InvalidOperation);
}
// Tests that stamped value from string can be created.
TEST(StampedValueTest, createFromString) {<--- syntax error
StampedValuePtr value;
ASSERT_NO_THROW(value = StampedValue::create("bar", Element::create("foo")));
EXPECT_FALSE(value->amNull());
EXPECT_EQ(Element::string, value->getType());
EXPECT_EQ("bar", value->getName());
EXPECT_EQ("foo", value->getValue());
EXPECT_THROW(value->getIntegerValue(), TypeError);
EXPECT_THROW(value->getBoolValue(), TypeError);
EXPECT_THROW(value->getDoubleValue(), TypeError);
}
// Tests that the stamped value can be created from string using the
// factory function variant that takes parameter type as an argument.
TEST(StampedValueTest, convertStringToString) {
StampedValuePtr value;
ASSERT_NO_THROW(value = StampedValue::create("bar", "foo", Element::string));
EXPECT_FALSE(value->amNull());
EXPECT_EQ(Element::string, value->getType());
EXPECT_EQ("bar", value->getName());
EXPECT_EQ("foo", value->getValue());
}
// Tests that stamped value from integer can be created.
TEST(StampedValueTest, createFromInteger) {
StampedValuePtr value;
ASSERT_NO_THROW(value = StampedValue::create("bar", Element::create(static_cast<int64_t>(5))));
EXPECT_FALSE(value->amNull());
EXPECT_EQ(Element::integer, value->getType());
EXPECT_EQ("bar", value->getName());
EXPECT_EQ("5", value->getValue());
int64_t signed_integer = 0;
ASSERT_NO_THROW(signed_integer = value->getIntegerValue());
EXPECT_EQ(5, signed_integer);
EXPECT_THROW(value->getBoolValue(), TypeError);
EXPECT_THROW(value->getDoubleValue(), TypeError);
}
// Tests that stamped value can be converted from string to integer.
TEST(StampedValueTest, convertStringToInteger) {
StampedValuePtr value;
ASSERT_NO_THROW(value = StampedValue::create("bar", "123", Element::integer));
EXPECT_FALSE(value->amNull());
EXPECT_EQ(Element::integer, value->getType());
EXPECT_EQ("bar", value->getName());
EXPECT_EQ(123, value->getIntegerValue());
EXPECT_THROW(StampedValue::create("bar", "hoho", Element::integer), BadValue);
}
// Tests that stamped value from bool can be created.
TEST(StampedValueTest, createFromBool) {
StampedValuePtr value;
ASSERT_NO_THROW(value = StampedValue::create("bar", Element::create(static_cast<bool>(true))));
EXPECT_FALSE(value->amNull());
EXPECT_EQ(Element::boolean, value->getType());
EXPECT_EQ("bar", value->getName());
EXPECT_EQ("1", value->getValue());
bool bool_value = false;
ASSERT_NO_THROW(bool_value = value->getBoolValue());
EXPECT_TRUE(bool_value);
EXPECT_THROW(value->getIntegerValue(), TypeError);
EXPECT_THROW(value->getDoubleValue(), TypeError);
}
// Tests that stamped value can be converted from string to boolean.
TEST(StampedValueTest, convertStringToBoolean) {
StampedValuePtr value;
ASSERT_NO_THROW(value = StampedValue::create("bar", "1", Element::boolean));
EXPECT_FALSE(value->amNull());
EXPECT_EQ(Element::boolean, value->getType());
EXPECT_EQ("bar", value->getName());
EXPECT_TRUE(value->getBoolValue());
ASSERT_NO_THROW(value = StampedValue::create("foo", "0", Element::boolean));
EXPECT_FALSE(value->amNull());
EXPECT_EQ(Element::boolean, value->getType());
EXPECT_EQ("foo", value->getName());
EXPECT_FALSE(value->getBoolValue());
EXPECT_THROW(StampedValue::create("bar", "888", Element::boolean), BadValue);
}
// Tests that stamped value from real can be created.
TEST(StampedValueTest, createFromDouble) {
StampedValuePtr value;
ASSERT_NO_THROW(value = StampedValue::create("bar", Element::create(static_cast<double>(1.45))));
EXPECT_FALSE(value->amNull());
EXPECT_EQ(Element::real, value->getType());
EXPECT_EQ("bar", value->getName());
EXPECT_EQ("1.45", value->getValue());
double double_value = 0;
ASSERT_NO_THROW(double_value = value->getDoubleValue());
EXPECT_EQ(1.45, double_value);
EXPECT_THROW(value->getIntegerValue(), TypeError);
EXPECT_THROW(value->getBoolValue(), TypeError);
}
// Tests that stamped value from real can handle a round value.
TEST(StampedValueTest, createFromDoubleRound) {
StampedValuePtr value;
ASSERT_NO_THROW(value = StampedValue::create("bar", Element::create(static_cast<double>(7.0))));
EXPECT_FALSE(value->amNull());
EXPECT_EQ(Element::real, value->getType());
EXPECT_EQ("bar", value->getName());
EXPECT_EQ("7.0", value->getValue());
double double_value = 0;
ASSERT_NO_THROW(double_value = value->getDoubleValue());
EXPECT_EQ(7.0, double_value);
EXPECT_THROW(value->getIntegerValue(), TypeError);
EXPECT_THROW(value->getBoolValue(), TypeError);
}
// Tests that stamped value can be converted from string to real.
TEST(StampedValueTest, convertStringToDouble) {
StampedValuePtr value;
ASSERT_NO_THROW(value = StampedValue::create("bar", "1.67", Element::real));
EXPECT_FALSE(value->amNull());
EXPECT_EQ(Element::real, value->getType());
EXPECT_EQ("bar", value->getName());
EXPECT_EQ(1.67, value->getDoubleValue());
EXPECT_THROW(StampedValue::create("bar", "hoho", Element::real), BadValue);
}
// Tests that stamped value from map can be created, but only with at most one element.
TEST(StampedValueTest, createFromMap) {
StampedValuePtr value;
ElementPtr map = Element::createMap();
ASSERT_NO_THROW(value = StampedValue::create("bar", map));
EXPECT_FALSE(value->amNull());
EXPECT_EQ(Element::map, value->getType());
EXPECT_EQ("bar", value->getName());
ASSERT_THROW(value->getValue(), TypeError);
EXPECT_EQ(value->getElementValue()->getType(), Element::map);
ASSERT_EQ(value->getElementValue()->mapValue().size(), 0);
EXPECT_THROW(value->getIntegerValue(), TypeError);
EXPECT_THROW(value->getBoolValue(), TypeError);
EXPECT_THROW(value->getDoubleValue(), TypeError);
map->set("foo", Element::create("0"));
ASSERT_NO_THROW(value = StampedValue::create("bar", map));
EXPECT_FALSE(value->amNull());
EXPECT_EQ(Element::map, value->getType());
EXPECT_EQ("bar", value->getName());
ASSERT_THROW(value->getValue(), TypeError);
EXPECT_EQ(value->getElementValue()->getType(), Element::map);
ASSERT_EQ(value->getElementValue()->mapValue().size(), 1);
EXPECT_EQ(value->getElementValue()->mapValue().begin()->first, "foo");
EXPECT_EQ(value->getElementValue()->mapValue().begin()->second->getType(), Element::string);
EXPECT_EQ(value->getElementValue()->mapValue().begin()->second->stringValue(), "0");
map->set("foo", Element::create(true));
ASSERT_NO_THROW(value = StampedValue::create("bar", map));
EXPECT_FALSE(value->amNull());
EXPECT_EQ(Element::map, value->getType());
EXPECT_EQ("bar", value->getName());
ASSERT_THROW(value->getValue(), TypeError);
EXPECT_EQ(value->getElementValue()->getType(), Element::map);
ASSERT_EQ(value->getElementValue()->mapValue().size(), 1);
EXPECT_EQ(value->getElementValue()->mapValue().begin()->first, "foo");
EXPECT_EQ(value->getElementValue()->mapValue().begin()->second->getType(), Element::boolean);
EXPECT_EQ(value->getElementValue()->mapValue().begin()->second->boolValue(), true);
map->set("foo", Element::create(0));
ASSERT_NO_THROW(value = StampedValue::create("bar", map));
EXPECT_FALSE(value->amNull());
EXPECT_EQ(Element::map, value->getType());
EXPECT_EQ("bar", value->getName());
ASSERT_THROW(value->getValue(), TypeError);
EXPECT_EQ(value->getElementValue()->getType(), Element::map);
ASSERT_EQ(value->getElementValue()->mapValue().size(), 1);
EXPECT_EQ(value->getElementValue()->mapValue().begin()->first, "foo");
EXPECT_EQ(value->getElementValue()->mapValue().begin()->second->getType(), Element::integer);
EXPECT_EQ(value->getElementValue()->mapValue().begin()->second->intValue(), 0);
map->set("foo", Element::create(0.0));
ASSERT_NO_THROW(value = StampedValue::create("bar", map));
EXPECT_FALSE(value->amNull());
EXPECT_EQ(Element::map, value->getType());
EXPECT_EQ("bar", value->getName());
ASSERT_THROW(value->getValue(), TypeError);
EXPECT_EQ(value->getElementValue()->getType(), Element::map);
ASSERT_EQ(value->getElementValue()->mapValue().size(), 1);
EXPECT_EQ(value->getElementValue()->mapValue().begin()->first, "foo");
EXPECT_EQ(value->getElementValue()->mapValue().begin()->second->getType(), Element::real);
EXPECT_EQ(value->getElementValue()->mapValue().begin()->second->doubleValue(), 0.0);
}
// Tests that the value must have an allowed type.
TEST(StampedValueTest, createFailures) {
EXPECT_THROW(StampedValue::create("bar", ElementPtr()), BadValue);
EXPECT_THROW(StampedValue::create("bar", Element::createList()), TypeError);
ElementPtr map = Element::createMap();
map->set("foo", Element::create("0"));
map->set("test", Element::create("true"));
EXPECT_THROW(StampedValue::create("bar", map), BadValue);
map = Element::createMap();
map->set("foo", Element::createMap());
EXPECT_THROW(StampedValue::create("bar", map), BadValue);
EXPECT_THROW(StampedValue::create("bar", "1", Element::map), TypeError);
EXPECT_THROW(StampedValue::create("bar", "1", Element::list), TypeError);
EXPECT_THROW(StampedValue::create("bar", "1", Element::null), TypeError);
}
}
|