Debian Codegraph @

Utilities/cmcppdap/src/any_test.cpp

raw
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "dap/any.h"
16 #include "dap/typeof.h"
17 #include "dap/types.h"
18
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
21
22 namespace dap {
23
24 struct AnyTestObject {
25 dap::integer i;
26 dap::number n;
27 };
28
29 DAP_STRUCT_TYPEINFO(AnyTestObject,
30 "AnyTestObject",
31 DAP_FIELD(i, "i"),
32 DAP_FIELD(n, "n"));
33
34 inline bool operator==(const AnyTestObject& a, const AnyTestObject& b) {
35 return a.i == b.i && a.n == b.n;
36 }
37
38 } // namespace dap
39
40 namespace {
41
42 template <typename T>
43 struct TestValue {};
44
45 template <>
46 struct TestValue<dap::null> {
47 static const dap::null value;
48 };
49 template <>
50 struct TestValue<dap::integer> {
51 static const dap::integer value;
52 };
53 template <>
54 struct TestValue<dap::boolean> {
55 static const dap::boolean value;
56 };
57 template <>
58 struct TestValue<dap::number> {
59 static const dap::number value;
60 };
61 template <>
62 struct TestValue<dap::string> {
63 static const dap::string value;
64 };
65 template <>
66 struct TestValue<dap::array<dap::string>> {
67 static const dap::array<dap::string> value;
68 };
69 template <>
70 struct TestValue<dap::AnyTestObject> {
71 static const dap::AnyTestObject value;
72 };
73
74 const dap::null TestValue<dap::null>::value = nullptr;
75 const dap::integer TestValue<dap::integer>::value = 20;
76 const dap::boolean TestValue<dap::boolean>::value = true;
77 const dap::number TestValue<dap::number>::value = 123.45;
78 const dap::string TestValue<dap::string>::value = "hello world";
79 const dap::array<dap::string> TestValue<dap::array<dap::string>>::value = {
80 "one", "two", "three"};
81 const dap::AnyTestObject TestValue<dap::AnyTestObject>::value = {10, 20.30};
82
83 } // namespace
84
85 TEST(Any, EmptyConstruct) {
86 dap::any any;
87 ASSERT_TRUE(any.is<dap::null>());
88 ASSERT_FALSE(any.is<dap::boolean>());
89 ASSERT_FALSE(any.is<dap::integer>());
90 ASSERT_FALSE(any.is<dap::number>());
91 ASSERT_FALSE(any.is<dap::object>());
92 ASSERT_FALSE(any.is<dap::string>());
93 ASSERT_FALSE(any.is<dap::array<dap::integer>>());
94 ASSERT_FALSE(any.is<dap::AnyTestObject>());
95 }
96
97 TEST(Any, Boolean) {
98 dap::any any(dap::boolean(true));
99 ASSERT_TRUE(any.is<dap::boolean>());
100 ASSERT_EQ(any.get<dap::boolean>(), dap::boolean(true));
101 }
102
103 TEST(Any, Integer) {
104 dap::any any(dap::integer(10));
105 ASSERT_TRUE(any.is<dap::integer>());
106 ASSERT_EQ(any.get<dap::integer>(), dap::integer(10));
107 }
108
109 TEST(Any, Number) {
110 dap::any any(dap::number(123.0f));
111 ASSERT_TRUE(any.is<dap::number>());
112 ASSERT_EQ(any.get<dap::number>(), dap::number(123.0f));
113 }
114
115 TEST(Any, String) {
116 dap::any any(dap::string("hello world"));
117 ASSERT_TRUE(any.is<dap::string>());
118 ASSERT_EQ(any.get<dap::string>(), dap::string("hello world"));
119 }
120
121 TEST(Any, Array) {
122 using array = dap::array<dap::integer>;
123 dap::any any(array({10, 20, 30}));
124 ASSERT_TRUE(any.is<array>());
125 ASSERT_EQ(any.get<array>(), array({10, 20, 30}));
126 }
127
128 TEST(Any, Object) {
129 dap::object o;
130 o["one"] = dap::integer(1);
131 o["two"] = dap::integer(2);
132 o["three"] = dap::integer(3);
133 dap::any any(o);
134 ASSERT_TRUE(any.is<dap::object>());
135 if (any.is<dap::object>()) {
136 auto got = any.get<dap::object>();
137 ASSERT_EQ(got.size(), 3U);
138 ASSERT_EQ(got.count("one"), 1U);
139 ASSERT_EQ(got.count("two"), 1U);
140 ASSERT_EQ(got.count("three"), 1U);
141 ASSERT_TRUE(got["one"].is<dap::integer>());
142 ASSERT_TRUE(got["two"].is<dap::integer>());
143 ASSERT_TRUE(got["three"].is<dap::integer>());
144 ASSERT_EQ(got["one"].get<dap::integer>(), dap::integer(1));
145 ASSERT_EQ(got["two"].get<dap::integer>(), dap::integer(2));
146 ASSERT_EQ(got["three"].get<dap::integer>(), dap::integer(3));
147 }
148 }
149
150 TEST(Any, TestObject) {
151 dap::any any(dap::AnyTestObject{5, 3.0});
152 ASSERT_TRUE(any.is<dap::AnyTestObject>());
153 ASSERT_EQ(any.get<dap::AnyTestObject>().i, 5);
154 ASSERT_EQ(any.get<dap::AnyTestObject>().n, 3.0);
155 }
156
157 template <typename T>
158 class AnyT : public ::testing::Test {
159 protected:
160 template <typename T0,
161 typename = std::enable_if<std::is_same<T, T0>::value &&
162 !std::is_same<T0, dap::null>::value>>
163 void check_val(const dap::any& any, const T0& expect) {
164 ASSERT_EQ(any.is<T>(), any.is<T0>());
165 ASSERT_EQ(any.get<T>(), expect);
166 }
167
168 // Special case for Null assignment, as we can assign nullptr_t to any but
169 // can't `get()` it
170 template <typename = dap::null>
171 void check_val(const dap::any& any, const dap::null& expect) {
172 ASSERT_EQ(nullptr, expect);
173 ASSERT_TRUE(any.is<dap::null>());
174 }
175
176 void check_type(const dap::any& any) {
177 ASSERT_EQ(any.is<dap::null>(), (std::is_same<T, dap::null>::value));
178 ASSERT_EQ(any.is<dap::integer>(), (std::is_same<T, dap::integer>::value));
179 ASSERT_EQ(any.is<dap::boolean>(), (std::is_same<T, dap::boolean>::value));
180 ASSERT_EQ(any.is<dap::number>(), (std::is_same<T, dap::number>::value));
181 ASSERT_EQ(any.is<dap::string>(), (std::is_same<T, dap::string>::value));
182 ASSERT_EQ(any.is<dap::array<dap::string>>(),
183 (std::is_same<T, dap::array<dap::string>>::value));
184 ASSERT_EQ(any.is<dap::AnyTestObject>(),
185 (std::is_same<T, dap::AnyTestObject>::value));
186 }
187 };
188 TYPED_TEST_SUITE_P(AnyT);
189
190 TYPED_TEST_P(AnyT, CopyConstruct) {
191 auto val = TestValue<TypeParam>::value;
192 dap::any any(val);
193 this->check_type(any);
194 this->check_val(any, val);
195 }
196
197 TYPED_TEST_P(AnyT, MoveConstruct) {
198 auto val = TestValue<TypeParam>::value;
199 dap::any any(std::move(val));
200 this->check_type(any);
201 this->check_val(any, val);
202 }
203
204 TYPED_TEST_P(AnyT, Assign) {
205 auto val = TestValue<TypeParam>::value;
206 dap::any any;
207 any = val;
208 this->check_type(any);
209 this->check_val(any, val);
210 }
211
212 TYPED_TEST_P(AnyT, MoveAssign) {
213 auto val = TestValue<TypeParam>::value;
214 dap::any any;
215 any = std::move(val);
216 this->check_type(any);
217 this->check_val(any, val);
218 }
219
220 TYPED_TEST_P(AnyT, RepeatedAssign) {
221 dap::string str = "hello world";
222 auto val = TestValue<TypeParam>::value;
223 dap::any any;
224 any = str;
225 any = val;
226 this->check_type(any);
227 this->check_val(any, val);
228 }
229
230 TYPED_TEST_P(AnyT, RepeatedMoveAssign) {
231 dap::string str = "hello world";
232 auto val = TestValue<TypeParam>::value;
233 dap::any any;
234 any = std::move(str);
235 any = std::move(val);
236 this->check_type(any);
237 this->check_val(any, val);
238 }
239
240 REGISTER_TYPED_TEST_SUITE_P(AnyT,
241 CopyConstruct,
242 MoveConstruct,
243 Assign,
244 MoveAssign,
245 RepeatedAssign,
246 RepeatedMoveAssign);
247
248 using AnyTypes = ::testing::Types<dap::null,
249 dap::integer,
250 dap::boolean,
251 dap::number,
252 dap::string,
253 dap::array<dap::string>,
254 dap::AnyTestObject>;
255 INSTANTIATE_TYPED_TEST_SUITE_P(T, AnyT, AnyTypes);
256
257 TEST(Any, Reset) {
258 dap::any any(dap::integer(10));
259 ASSERT_TRUE(any.is<dap::integer>());
260 any.reset();
261 ASSERT_FALSE(any.is<dap::integer>());
262 }