]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/json/test/value_to.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / json / test / value_to.cpp
1 //
2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/boostorg/json
8 //
9
10 // Test that header file is self-contained.
11 #include <boost/json/value_to.hpp>
12
13 #include "test_suite.hpp"
14
15 #include <array>
16 #include <map>
17 #include <unordered_map>
18 #include <vector>
19
20 BOOST_JSON_NS_BEGIN
21
22
23 template <class T, class = void>
24 struct can_apply_value_to
25 : std::false_type
26 {
27 };
28
29 template <class T>
30 struct can_apply_value_to<T, detail::void_t<decltype(
31 value_to<int>(std::declval<T>()))
32 >>
33 : std::true_type
34 {
35 };
36
37 BOOST_STATIC_ASSERT(!can_apply_value_to<int>::value);
38
39
40 class value_to_test
41 {
42 public:
43
44 template<class T>
45 void
46 check(T t)
47 {
48 BOOST_TEST(value_to<T>(value_from(t)) == t);
49 }
50
51 void
52 testNumberCast()
53 {
54 check((short)-1);
55 check((int)-2);
56 check((long)-3);
57 check((long long)-4);
58 check((unsigned short)1);
59 check((unsigned int)2);
60 check((unsigned long)3);
61 check((unsigned long long)4);
62 check((float)1.5);
63 check((double)2.5);
64 check(true);
65 }
66
67 void
68 testJsonTypes()
69 {
70 value_to<object>(value(object_kind));
71 value_to<array>(value(array_kind));
72 value_to<string>(value(string_kind));
73 }
74
75 void
76 testGenerics()
77 {
78 check(std::string("test"));
79 check(std::map<std::string, int>
80 {
81 {"a", 1}, {"b", 2}, {"c", 3}
82 });
83 check(std::unordered_map<std::string, int>
84 {
85 { "a", 1 }, {"b", 2}, {"c", 3}
86 });
87 check(std::vector<int>{1, 2, 3, 4});
88 check(std::make_pair(std::string("test"), 5));
89 check(std::make_tuple(std::string("outer"),
90 std::make_pair(std::string("test"), 5)));
91 check(std::map<int, int>
92 {
93 {2, 4}, {3, 9}, {5, 25}
94 });
95
96 {
97 std::array<int, 1000> arr;
98 arr.fill(0);
99 check(arr);
100 }
101
102 BOOST_TEST_THROWS(
103 (value_to<std::tuple<int, int>>(value{1, 2, 3})),
104 std::invalid_argument);
105 BOOST_TEST_THROWS(
106 (value_to<std::tuple<int, int, int, int>>(value{1, 2, 3})),
107 std::invalid_argument);
108
109 BOOST_TEST_THROWS(
110 (value_to<std::array<int, 4>>(value{1, 2, 3})),
111 std::invalid_argument);
112 BOOST_TEST_THROWS(
113 (value_to<std::array<int, 4>>(value{1, 2, 3, 4, 5})),
114 std::invalid_argument);
115 }
116
117 void
118 run()
119 {
120 testNumberCast();
121 testJsonTypes();
122 testGenerics();
123 }
124 };
125
126 TEST_SUITE(value_to_test, "boost.json.value_to");
127
128 BOOST_JSON_NS_END