• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2     __ _____ _____ _____
3  __|  |   __|     |   | |  JSON for Modern C++ (test suite)
4 |  |  |__   |  |  | | | |  version 3.10.0
5 |_____|_____|_____|_|___|  https://github.com/nlohmann/json
6 
7 Licensed under the MIT License <http://opensource.org/licenses/MIT>.
8 SPDX-License-Identifier: MIT
9 Copyright (c) 2013-2019 Niels Lohmann <http://nlohmann.me>.
10 
11 Permission is hereby  granted, free of charge, to any  person obtaining a copy
12 of this software and associated  documentation files (the "Software"), to deal
13 in the Software  without restriction, including without  limitation the rights
14 to  use, copy,  modify, merge,  publish, distribute,  sublicense, and/or  sell
15 copies  of  the Software,  and  to  permit persons  to  whom  the Software  is
16 furnished to do so, subject to the following conditions:
17 
18 The above copyright notice and this permission notice shall be included in all
19 copies or substantial portions of the Software.
20 
21 THE SOFTWARE  IS PROVIDED "AS  IS", WITHOUT WARRANTY  OF ANY KIND,  EXPRESS OR
22 IMPLIED,  INCLUDING BUT  NOT  LIMITED TO  THE  WARRANTIES OF  MERCHANTABILITY,
23 FITNESS FOR  A PARTICULAR PURPOSE AND  NONINFRINGEMENT. IN NO EVENT  SHALL THE
24 AUTHORS  OR COPYRIGHT  HOLDERS  BE  LIABLE FOR  ANY  CLAIM,  DAMAGES OR  OTHER
25 LIABILITY, WHETHER IN AN ACTION OF  CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE  OR THE USE OR OTHER DEALINGS IN THE
27 SOFTWARE.
28 */
29 
30 #include "doctest_compatibility.h"
31 
32 #include <nlohmann/json.hpp>
33 using nlohmann::json;
34 
35 namespace
36 {
37 // helper function to check std::less<json::value_t>
38 // see https://en.cppreference.com/w/cpp/utility/functional/less
39 template <typename A, typename B, typename U = std::less<json::value_t>>
f(A a,B b,U u=U ())40 bool f(A a, B b, U u = U())
41 {
42     return u(a, b);
43 }
44 } // namespace
45 
46 TEST_CASE("lexicographical comparison operators")
47 {
48     SECTION("types")
49     {
50         std::vector<json::value_t> j_types =
51         {
52             json::value_t::null,
53             json::value_t::boolean,
54             json::value_t::number_integer,
55             json::value_t::number_unsigned,
56             json::value_t::number_float,
57             json::value_t::object,
58             json::value_t::array,
59             json::value_t::string,
60             json::value_t::binary
61         };
62 
63         SECTION("comparison: less")
64         {
65             std::vector<std::vector<bool>> expected =
66             {
67                 {false, true, true, true, true, true, true, true, true},
68                 {false, false, true, true, true, true, true, true, true},
69                 {false, false, false, false, false, true, true, true, true},
70                 {false, false, false, false, false, true, true, true, true},
71                 {false, false, false, false, false, true, true, true, true},
72                 {false, false, false, false, false, false, true, true, true},
73                 {false, false, false, false, false, false, false, true, true},
74                 {false, false, false, false, false, false, false, false, true},
75                 {false, false, false, false, false, false, false, false, false}
76             };
77 
78             for (size_t i = 0; i < j_types.size(); ++i)
79             {
80                 for (size_t j = 0; j < j_types.size(); ++j)
81                 {
82                     CAPTURE(i)
83                     CAPTURE(j)
84                     // check precomputed values
85                     CHECK(operator<(j_types[i], j_types[j]) == expected[i][j]);
86                     CHECK(f(j_types[i], j_types[j]) == expected[i][j]);
87                 }
88             }
89         }
90     }
91 
92     SECTION("values")
93     {
94         json j_values =
95         {
96             nullptr, nullptr,
97             -17, 42,
98             8u, 13u,
99             3.14159, 23.42,
100             "foo", "bar",
101             true, false,
102             {1, 2, 3}, {"one", "two", "three"},
103             {{"first", 1}, {"second", 2}}, {{"a", "A"}, {"b", {"B"}}},
104             json::binary({1, 2, 3}), json::binary({1, 2, 4})
105         };
106 
107         SECTION("comparison: equal")
108         {
109             std::vector<std::vector<bool>> expected =
110             {
111                 {true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
112                 {true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
113                 {false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
114                 {false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
115                 {false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false},
116                 {false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false},
117                 {false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false},
118                 {false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false},
119                 {false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false},
120                 {false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false},
121                 {false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false},
122                 {false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false},
123                 {false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false},
124                 {false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false},
125                 {false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false},
126                 {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false},
127                 {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false},
128                 {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true}
129             };
130 
131             for (size_t i = 0; i < j_values.size(); ++i)
132             {
133                 for (size_t j = 0; j < j_values.size(); ++j)
134                 {
135                     CAPTURE(i)
136                     CAPTURE(j)
137                     CAPTURE(j_values[i])
138                     CAPTURE(j_values[j])
139                     // check precomputed values
140                     CHECK( (j_values[i] == j_values[j]) == expected[i][j] );
141                 }
142             }
143 
144             // comparison with discarded elements
145             json j_discarded(json::value_t::discarded);
146             for (const auto& v : j_values)
147             {
148                 CHECK( (v == j_discarded) == false);
149                 CHECK( (j_discarded == v) == false);
150                 CHECK( (j_discarded == j_discarded) == false);
151             }
152 
153             // compare with null pointer
154             json j_null;
155             CHECK(j_null == nullptr);
156             CHECK(nullptr == j_null);
157         }
158 
159         SECTION("comparison: not equal")
160         {
161             for (size_t i = 0; i < j_values.size(); ++i)
162             {
163                 for (size_t j = 0; j < j_values.size(); ++j)
164                 {
165                     CAPTURE(i)
166                     CAPTURE(j)
167                     // check definition
168                     CHECK( (j_values[i] != j_values[j]) == !(j_values[i] == j_values[j]) );
169                 }
170             }
171 
172             // compare with null pointer
173             json j_null;
174             CHECK( (j_null != nullptr) == false);
175             CHECK( (nullptr != j_null) == false);
176             CHECK( (j_null != nullptr) == !(j_null == nullptr));
177             CHECK( (nullptr != j_null) == !(nullptr == j_null));
178         }
179 
180         SECTION("comparison: less")
181         {
182             std::vector<std::vector<bool>> expected =
183             {
184                 {false, false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true},
185                 {false, false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true},
186                 {false, false, false, true, true, true, true, true, true, true, false, false, true, true, true, true, true, true},
187                 {false, false, false, false, false, false, false, false, true, true, false, false, true, true, true, true, true, true},
188                 {false, false, false, true, false, true, false, true, true, true, false, false, true, true, true, true, true, true},
189                 {false, false, false, true, false, false, false, true, true, true, false, false, true, true, true, true, true, true},
190                 {false, false, false, true, true, true, false, true, true, true, false, false, true, true, true, true, true, true},
191                 {false, false, false, true, false, false, false, false, true, true, false, false, true, true, true, true, true, true},
192                 {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, true},
193                 {false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, true, true},
194                 {false, false, true, true, true, true, true, true, true, true, false, false, true, true, true, true, true, true},
195                 {false, false, true, true, true, true, true, true, true, true, true, false, true, true, true, true, true, true},
196                 {false, false, false, false, false, false, false, false, true, true, false, false, false, true, false, false, true, true},
197                 {false, false, false, false, false, false, false, false, true, true, false, false, false, false, false, false, true, true},
198                 {false, false, false, false, false, false, false, false, true, true, false, false, true, true, false, false, true, true},
199                 {false, false, false, false, false, false, false, false, true, true, false, false, true, true, true, false, true, true},
200                 {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true},
201                 {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false}
202             };
203 
204             for (size_t i = 0; i < j_values.size(); ++i)
205             {
206                 for (size_t j = 0; j < j_values.size(); ++j)
207                 {
208                     CAPTURE(i)
209                     CAPTURE(j)
210                     CAPTURE(j_values[i])
211                     CAPTURE(j_values[j])
212                     // check precomputed values
213                     CHECK( (j_values[i] < j_values[j]) == expected[i][j] );
214                 }
215             }
216 
217             // comparison with discarded elements
218             json j_discarded(json::value_t::discarded);
219             for (size_t i = 0; i < j_values.size(); ++i)
220             {
221                 CAPTURE(i)
222                 CHECK( (j_values[i] < j_discarded) == false);
223                 CHECK( (j_discarded < j_values[i]) == false);
224                 CHECK( (j_discarded < j_discarded) == false);
225             }
226         }
227 
228         SECTION("comparison: less than or equal equal")
229         {
230             for (size_t i = 0; i < j_values.size(); ++i)
231             {
232                 for (size_t j = 0; j < j_values.size(); ++j)
233                 {
234                     CAPTURE(i)
235                     CAPTURE(j)
236                     // check definition
237                     CHECK( (j_values[i] <= j_values[j]) == !(j_values[j] < j_values[i]) );
238                 }
239             }
240         }
241 
242         SECTION("comparison: greater than")
243         {
244             for (size_t i = 0; i < j_values.size(); ++i)
245             {
246                 for (size_t j = 0; j < j_values.size(); ++j)
247                 {
248                     CAPTURE(i)
249                     CAPTURE(j)
250                     // check definition
251                     CHECK( (j_values[i] > j_values[j]) == (j_values[j] < j_values[i]) );
252                 }
253             }
254         }
255 
256         SECTION("comparison: greater than or equal")
257         {
258             for (size_t i = 0; i < j_values.size(); ++i)
259             {
260                 for (size_t j = 0; j < j_values.size(); ++j)
261                 {
262                     CAPTURE(i)
263                     CAPTURE(j)
264                     // check definition
265                     CHECK( (j_values[i] >= j_values[j]) == !(j_values[i] < j_values[j]) );
266                 }
267             }
268         }
269     }
270 }
271