proxygen
DynamicConverterTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2012-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // @author Nicholas Ormrod <njormrod@fb.com>
18 
19 #include <folly/DynamicConverter.h>
20 
22 
23 #include <algorithm>
24 #include <map>
25 #include <vector>
26 
27 using namespace folly;
28 using namespace folly::dynamicconverter_detail;
29 
30 TEST(DynamicConverter, template_metaprogramming) {
31  struct A {};
32 
33  bool c1f = is_container<int>::value;
35  bool c3f = is_container<A>::value;
37 
42 
43  EXPECT_EQ(c1f, false);
44  EXPECT_EQ(c2f, false);
45  EXPECT_EQ(c3f, false);
46  EXPECT_EQ(c4f, false);
47  EXPECT_EQ(c1t, true);
48  EXPECT_EQ(c2t, true);
49  EXPECT_EQ(c3t, true);
50  EXPECT_EQ(c4t, true);
51 
52  bool m1f = is_map<int>::value;
53  bool m2f = is_map<std::set<int>>::value;
54 
56 
57  EXPECT_EQ(m1f, false);
58  EXPECT_EQ(m2f, false);
59  EXPECT_EQ(m1t, true);
60 
61  bool r1f = is_range<int>::value;
62 
65 
66  EXPECT_EQ(r1f, false);
67  EXPECT_EQ(r1t, true);
68  EXPECT_EQ(r2t, true);
69 }
70 
71 TEST(DynamicConverter, arithmetic_types) {
72  dynamic d1 = 12;
73  auto i1 = convertTo<int>(d1);
74  EXPECT_EQ(i1, 12);
75 
76  dynamic d2 = 123456789012345;
77  auto i2 = convertTo<int64_t>(d2);
78  EXPECT_EQ(i2, 123456789012345);
79 
80  dynamic d4 = 3.141;
81  auto i4 = convertTo<float>(d4);
82  EXPECT_EQ((int)(i4 * 100), 314);
83 
84  dynamic d5 = true;
85  auto i5 = convertTo<bool>(d5);
86  EXPECT_EQ(i5, true);
87 
88  dynamic d6 = 15;
89  const auto i6 = convertTo<const int>(d6);
90  EXPECT_EQ(i6, 15);
91 
92  dynamic d7 = "87";
93  auto i7 = convertTo<int>(d7);
94  EXPECT_EQ(i7, 87);
95 
96  dynamic d8 = "false";
97  auto i8 = convertTo<bool>(d8);
98  EXPECT_EQ(i8, false);
99 }
100 
102  enum enum1 { foo = 1, bar = 2 };
103 
104  dynamic d1 = 1;
105  auto i1 = convertTo<enum1>(d1);
106  EXPECT_EQ(i1, foo);
107 
108  dynamic d2 = 2;
109  auto i2 = convertTo<enum1>(d2);
110  EXPECT_EQ(i2, bar);
111 
112  enum class enum2 { FOO = 1, BAR = 2 };
113 
114  dynamic d3 = 1;
115  auto i3 = convertTo<enum2>(d3);
116  EXPECT_EQ(i3, enum2::FOO);
117 
118  dynamic d4 = 2;
119  auto i4 = convertTo<enum2>(d4);
120  EXPECT_EQ(i4, enum2::BAR);
121 }
122 
123 TEST(DynamicConverter, simple_builtins) {
124  dynamic d1 = "Haskell";
125  auto i1 = convertTo<folly::fbstring>(d1);
126  EXPECT_EQ(i1, "Haskell");
127 
128  dynamic d2 = 13;
129  auto i2 = convertTo<std::string>(d2);
130  EXPECT_EQ(i2, "13");
131 
132  dynamic d3 = dynamic::array(12, "Scala");
133  auto i3 = convertTo<std::pair<int, std::string>>(d3);
134  EXPECT_EQ(i3.first, 12);
135  EXPECT_EQ(i3.second, "Scala");
136 
137  dynamic d4 = dynamic::object("C", "C++");
138  auto i4 = convertTo<std::pair<std::string, folly::fbstring>>(d4);
139  EXPECT_EQ(i4.first, "C");
140  EXPECT_EQ(i4.second, "C++");
141 }
142 
143 TEST(DynamicConverter, simple_fbvector) {
144  dynamic d1 = dynamic::array(1, 2, 3);
145  auto i1 = convertTo<folly::fbvector<int>>(d1);
146  decltype(i1) i1b = {1, 2, 3};
147  EXPECT_EQ(i1, i1b);
148 }
149 
150 TEST(DynamicConverter, simple_container) {
151  dynamic d1 = dynamic::array(1, 2, 3);
152  auto i1 = convertTo<std::vector<int>>(d1);
153  decltype(i1) i1b = {1, 2, 3};
154  EXPECT_EQ(i1, i1b);
155 
156  dynamic d2 = dynamic::array(1, 3, 5, 2, 4);
157  auto i2 = convertTo<std::set<int>>(d2);
158  decltype(i2) i2b = {1, 2, 3, 5, 4};
159  EXPECT_EQ(i2, i2b);
160 }
161 
162 TEST(DynamicConverter, simple_map) {
163  dynamic d1 = dynamic::object(1, "one")(2, "two");
164  auto i1 = convertTo<std::map<int, std::string>>(d1);
165  decltype(i1) i1b = {{1, "one"}, {2, "two"}};
166  EXPECT_EQ(i1, i1b);
167 
168  dynamic d2 =
169  dynamic::array(dynamic::array(3, "three"), dynamic::array(4, "four"));
170  auto i2 = convertTo<std::unordered_map<int, std::string>>(d2);
171  decltype(i2) i2b = {{3, "three"}, {4, "four"}};
172  EXPECT_EQ(i2, i2b);
173 }
174 
175 TEST(DynamicConverter, map_keyed_by_string) {
176  dynamic d1 = dynamic::object("1", "one")("2", "two");
177  auto i1 = convertTo<std::map<std::string, std::string>>(d1);
178  decltype(i1) i1b = {{"1", "one"}, {"2", "two"}};
179  EXPECT_EQ(i1, i1b);
180 
181  dynamic d2 =
182  dynamic::array(dynamic::array("3", "three"), dynamic::array("4", "four"));
183  auto i2 = convertTo<std::unordered_map<std::string, std::string>>(d2);
184  decltype(i2) i2b = {{"3", "three"}, {"4", "four"}};
185  EXPECT_EQ(i2, i2b);
186 }
187 
188 TEST(DynamicConverter, map_to_vector_of_pairs) {
189  dynamic d1 = dynamic::object("1", "one")("2", "two");
190  auto i1 = convertTo<std::vector<std::pair<std::string, std::string>>>(d1);
191  std::sort(i1.begin(), i1.end());
192  decltype(i1) i1b = {{"1", "one"}, {"2", "two"}};
193  EXPECT_EQ(i1, i1b);
194 }
195 
196 TEST(DynamicConverter, nested_containers) {
197  dynamic d1 =
199  auto i1 = convertTo<folly::fbvector<std::vector<uint8_t>>>(d1);
200  decltype(i1) i1b = {{1}, {}, {2, 3}};
201  EXPECT_EQ(i1, i1b);
202 
203  dynamic h2a = dynamic::array("3", ".", "1", "4");
204  dynamic h2b = dynamic::array("2", ".", "7", "2");
205  dynamic d2 = dynamic::object(3.14, h2a)(2.72, h2b);
206  auto i2 = convertTo<std::map<double, std::vector<folly::fbstring>>>(d2);
207  decltype(i2) i2b = {
208  {3.14, {"3", ".", "1", "4"}},
209  {2.72, {"2", ".", "7", "2"}},
210  };
211  EXPECT_EQ(i2, i2b);
212 }
213 
214 struct A {
215  int i;
216  bool operator==(const A& o) const {
217  return i == o.i;
218  }
219 };
220 namespace folly {
221 template <>
223  static A convert(const dynamic& d) {
224  return {convertTo<int>(d["i"])};
225  }
226 };
227 } // namespace folly
228 TEST(DynamicConverter, custom_class) {
229  dynamic d1 = dynamic::object("i", 17);
230  auto i1 = convertTo<A>(d1);
231  EXPECT_EQ(i1.i, 17);
232 
233  dynamic d2 =
234  dynamic::array(dynamic::object("i", 18), dynamic::object("i", 19));
235  auto i2 = convertTo<std::vector<A>>(d2);
236  decltype(i2) i2b = {{18}, {19}};
237  EXPECT_EQ(i2, i2b);
238 }
239 
241  // we are going to create a vector<unordered_map<bool, T>>
242  // we will construct some of the maps from dynamic objects,
243  // some from a vector of KV pairs.
244  // T will be vector<set<string>>
245 
246  std::set<std::string> s1 = {"a", "e", "i", "o", "u"};
247  std::set<std::string> s2 = {"2", "3", "5", "7"};
248  std::set<std::string> s3 = {"Hello", "World"};
249 
250  std::vector<std::set<std::string>> v1 = {};
251  std::vector<std::set<std::string>> v2 = {s1, s2};
252  std::vector<std::set<std::string>> v3 = {s3};
253 
254  std::unordered_map<bool, std::vector<std::set<std::string>>> m1 = {
255  {true, v1}, {false, v2}};
256  std::unordered_map<bool, std::vector<std::set<std::string>>> m2 = {
257  {true, v3}};
258 
259  std::vector<std::unordered_map<bool, std::vector<std::set<std::string>>>> f1 =
260  {m1, m2};
261 
262  dynamic ds1 = dynamic::array("a", "e", "i", "o", "u");
263  dynamic ds2 = dynamic::array("2", "3", "5", "7");
264  dynamic ds3 = dynamic::array("Hello", "World");
265 
266  dynamic dv1 = dynamic::array;
267  dynamic dv2 = dynamic::array(ds1, ds2);
268  dynamic dv3(dynamic::array(ds3));
269 
270  dynamic dm1 = dynamic::object(true, dv1)(false, dv2);
271  dynamic dm2 = dynamic::array(dynamic::array(true, dv3));
272 
273  dynamic df1 = dynamic::array(dm1, dm2);
274 
275  auto i = convertTo<std::vector<
276  std::unordered_map<bool, std::vector<std::set<std::string>>>>>(
277  df1); // yes, that is 5 close-chevrons
278 
279  EXPECT_EQ(f1, i);
280 }
281 
283  dynamic d1 = 7.5;
284  auto i1 = convertTo<const double>(d1);
285  EXPECT_EQ(7.5, i1);
286 
287  dynamic d2 = "Hello";
288  auto i2 = convertTo<const std::string>(d2);
289  decltype(i2) i2b = "Hello";
290  EXPECT_EQ(i2b, i2);
291 
292  dynamic d3 = true;
293  auto i3 = convertTo<const bool>(d3);
294  EXPECT_TRUE(i3);
295 
296  dynamic d4 = "true";
297  auto i4 = convertTo<const bool>(d4);
298  EXPECT_TRUE(i4);
299 
300  dynamic d5 = dynamic::array(1, 2);
301  auto i5 = convertTo<const std::pair<const int, const int>>(d5);
302  decltype(i5) i5b = {1, 2};
303  EXPECT_EQ(i5b, i5);
304 }
305 
306 struct Token {
307  int kind_;
309 
310  explicit Token(int kind, const fbstring& lexeme)
311  : kind_(kind), lexeme_(lexeme) {}
312 };
313 
314 namespace folly {
315 template <>
317  static Token convert(const dynamic& d) {
318  int k = convertTo<int>(d["KIND"]);
319  fbstring lex = convertTo<fbstring>(d["LEXEME"]);
320  return Token(k, lex);
321  }
322 };
323 } // namespace folly
324 
326  dynamic d1 = dynamic::object("KIND", 2)("LEXEME", "a token");
327  auto i1 = convertTo<Token>(d1);
328  EXPECT_EQ(i1.kind_, 2);
329  EXPECT_EQ(i1.lexeme_, "a token");
330 }
331 
332 TEST(DynamicConverter, construct) {
333  using std::map;
334  using std::pair;
335  using std::string;
336  using std::vector;
337  {
338  vector<int> c{1, 2, 3};
339  dynamic d = dynamic::array(1, 2, 3);
340  EXPECT_EQ(d, toDynamic(c));
341  }
342 
343  {
344  vector<float> c{1.0f, 2.0f, 4.0f};
345  dynamic d = dynamic::array(1.0, 2.0, 4.0);
346  EXPECT_EQ(d, toDynamic(c));
347  }
348 
349  {
350  map<int, int> c{{2, 4}, {3, 9}};
351  dynamic d = dynamic::object(2, 4)(3, 9);
352  EXPECT_EQ(d, toDynamic(c));
353  }
354 
355  {
356  map<string, string> c{{"a", "b"}};
357  dynamic d = dynamic::object("a", "b");
358  EXPECT_EQ(d, toDynamic(c));
359  }
360 
361  {
362  map<string, pair<string, int>> c{{"a", {"b", 3}}};
363  dynamic d = dynamic::object("a", dynamic::array("b", 3));
364  EXPECT_EQ(d, toDynamic(c));
365  }
366 
367  {
368  map<string, pair<string, int>> c{{"a", {"b", 3}}};
369  dynamic d = dynamic::object("a", dynamic::array("b", 3));
370  EXPECT_EQ(d, toDynamic(c));
371  }
372 
373  {
374  vector<int> vi{2, 3, 4, 5};
375  auto c = std::make_pair(
376  range(vi.begin(), vi.begin() + 3),
377  range(vi.begin() + 1, vi.begin() + 4));
378  dynamic d =
379  dynamic::array(dynamic::array(2, 3, 4), dynamic::array(3, 4, 5));
380  EXPECT_EQ(d, toDynamic(c));
381  }
382 
383  {
384  vector<bool> vb{true, false};
385  dynamic d = dynamic::array(true, false);
386  EXPECT_EQ(d, toDynamic(vb));
387  }
388 }
389 
391  const auto int32Over =
392  static_cast<int64_t>(std::numeric_limits<int32_t>().max()) + 1;
393  const auto floatOver =
394  static_cast<double>(std::numeric_limits<float>().max()) * 2;
395 
396  dynamic d1 = int32Over;
397  EXPECT_THROW(convertTo<int32_t>(d1), std::range_error);
398 
399  dynamic d2 = floatOver;
400  EXPECT_THROW(convertTo<float>(d2), std::range_error);
401 }
402 
403 TEST(DynamicConverter, partial_dynamics) {
404  std::vector<dynamic> c{
405  dynamic::array(2, 3, 4),
406  dynamic::array(3, 4, 5),
407  };
408  dynamic d = dynamic::array(dynamic::array(2, 3, 4), dynamic::array(3, 4, 5));
409  EXPECT_EQ(d, toDynamic(c));
410 
411  std::unordered_map<std::string, dynamic> m{{"one", 1}, {"two", 2}};
412  dynamic md = dynamic::object("one", 1)("two", 2);
413  EXPECT_EQ(md, toDynamic(m));
414 }
415 
416 TEST(DynamicConverter, asan_exception_case_umap) {
417  EXPECT_THROW(
418  (convertTo<std::unordered_map<int, int>>(dynamic::array(1))), TypeError);
419 }
420 
421 TEST(DynamicConverter, asan_exception_case_uset) {
422  EXPECT_THROW(
423  (convertTo<std::unordered_set<int>>(
424  dynamic::array(1, dynamic::array(), 3))),
425  TypeError);
426 }
427 
428 static int constructB = 0;
429 static int destroyB = 0;
430 static int ticker = 0;
431 struct B {
432  struct BException : std::exception {};
433 
434  /* implicit */ B(int x) : x_(x) {
435  if (ticker-- == 0) {
436  throw BException();
437  }
438  constructB++;
439  }
440  B(const B& o) : x_(o.x_) {
441  constructB++;
442  }
443  ~B() {
444  destroyB++;
445  }
446  int x_;
447 };
448 namespace folly {
449 template <>
451  static B convert(const dynamic& d) {
452  return B(convertTo<int>(d));
453  }
454 };
455 } // namespace folly
456 
457 TEST(DynamicConverter, double_destroy) {
458  dynamic d = dynamic::array(1, 3, 5, 7, 9, 11, 13, 15, 17);
459  ticker = 3;
460 
461  EXPECT_THROW(convertTo<std::vector<B>>(d), B::BException);
463 }
464 
465 TEST(DynamicConverter, simple_vector_bool) {
466  std::vector<bool> bools{true, false};
467  auto d = toDynamic(bools);
468  auto actual = convertTo<decltype(bools)>(d);
469  EXPECT_EQ(bools, actual);
470 }
Definition: InvokeTest.cpp:58
static ObjectMaker object()
Definition: dynamic-inl.h:240
static int destroyB
std::unique_ptr< int > A
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
LogLevel max
Definition: LogLevel.cpp:31
T convertTo(const dynamic &)
static Token convert(const dynamic &d)
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
RfcParam d5(false, exampleHex3)
fbstring lexeme_
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
Definition: lib.cpp:18
RfcParam d6(true, exampleHex4)
Token(int kind, const fbstring &lexeme)
static B convert(const dynamic &d)
RfcParam d4(true, exampleHex2)
static A convert(const dynamic &d)
static Map map(mapCap)
constexpr Range< Iter > range(Iter first, Iter last)
Definition: Range.h:1114
static map< string, int > m
Definition: Traits.h:594
Definition: Traits.h:588
static int ticker
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
bool operator==(const A &o) const
const char * string
Definition: Conv.cpp:212
static void array(EmptyArrayTag)
Definition: dynamic-inl.h:233
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
int x_
RfcParam d3(false, exampleHex1)
char c
KeyT k
static int constructB
TEST(SequencedExecutor, CPUThreadPoolExecutor)
dynamic toDynamic(const T &)
#define B(name, bit)
Definition: CpuId.h:178
B(const B &o)