• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2     __ _____ _____ _____
3  __|  |   __|     |   | |  JSON for Modern C++ (test suite)
4 |  |  |__   |  |  | | | |  version 3.9.1
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::ordered_map;
34 
35 
36 TEST_CASE("ordered_map")
37 {
38     SECTION("constructor")
39     {
40         SECTION("constructor from iterator range")
41         {
42             std::map<std::string, std::string> m {{"eins", "one"}, {"zwei", "two"}, {"drei", "three"}};
43             ordered_map<std::string, std::string> om(m.begin(), m.end());
44             CHECK(om.size() == 3);
45         }
46 
47         SECTION("copy assignment")
48         {
49             std::map<std::string, std::string> m {{"eins", "one"}, {"zwei", "two"}, {"drei", "three"}};
50             ordered_map<std::string, std::string> om(m.begin(), m.end());
51             const auto com = om;
52             CHECK(com.size() == 3);
53         }
54     }
55 
56     SECTION("at")
57     {
58         std::map<std::string, std::string> m {{"eins", "one"}, {"zwei", "two"}, {"drei", "three"}};
59         ordered_map<std::string, std::string> om(m.begin(), m.end());
60         const auto com = om;
61 
62         SECTION("with Key&&")
63         {
64             CHECK(om.at(std::string("eins")) == std::string("one"));
65             CHECK(com.at(std::string("eins")) == std::string("one"));
66             CHECK_THROWS_AS(om.at(std::string("vier")), std::out_of_range);
67             CHECK_THROWS_AS(com.at(std::string("vier")), std::out_of_range);
68         }
69 
70         SECTION("with const Key&&")
71         {
72             const std::string eins = "eins";
73             const std::string vier = "vier";
74             CHECK(om.at(eins) == std::string("one"));
75             CHECK(com.at(eins) == std::string("one"));
76             CHECK_THROWS_AS(om.at(vier), std::out_of_range);
77             CHECK_THROWS_AS(com.at(vier), std::out_of_range);
78         }
79 
80         SECTION("with string literal")
81         {
82             CHECK(om.at("eins") == std::string("one"));
83             CHECK(com.at("eins") == std::string("one"));
84             CHECK_THROWS_AS(om.at("vier"), std::out_of_range);
85             CHECK_THROWS_AS(com.at("vier"), std::out_of_range);
86         }
87     }
88 
89     SECTION("operator[]")
90     {
91         std::map<std::string, std::string> m {{"eins", "one"}, {"zwei", "two"}, {"drei", "three"}};
92         ordered_map<std::string, std::string> om(m.begin(), m.end());
93         const auto com = om;
94 
95         SECTION("with Key&&")
96         {
97             CHECK(om[std::string("eins")] == std::string("one"));
98             CHECK(com[std::string("eins")] == std::string("one"));
99 
100             CHECK(om[std::string("vier")] == std::string(""));
101             CHECK(om.size() == 4);
102         }
103 
104         SECTION("with const Key&&")
105         {
106             const std::string eins = "eins";
107             const std::string vier = "vier";
108 
109             CHECK(om[eins] == std::string("one"));
110             CHECK(com[eins] == std::string("one"));
111 
112             CHECK(om[vier] == std::string(""));
113             CHECK(om.size() == 4);
114         }
115 
116         SECTION("with string literal")
117         {
118             CHECK(om["eins"] == std::string("one"));
119             CHECK(com["eins"] == std::string("one"));
120 
121             CHECK(om["vier"] == std::string(""));
122             CHECK(om.size() == 4);
123         }
124     }
125 
126     SECTION("erase")
127     {
128         ordered_map<std::string, std::string> om;
129         om["eins"] = "one";
130         om["zwei"] = "two";
131         om["drei"] = "three";
132 
133         {
134             auto it = om.begin();
135             CHECK(it->first == "eins");
136             ++it;
137             CHECK(it->first == "zwei");
138             ++it;
139             CHECK(it->first == "drei");
140             ++it;
141             CHECK(it == om.end());
142         }
143 
144         SECTION("with Key&&")
145         {
146             CHECK(om.size() == 3);
147             CHECK(om.erase(std::string("eins")) == 1);
148             CHECK(om.size() == 2);
149             CHECK(om.erase(std::string("vier")) == 0);
150             CHECK(om.size() == 2);
151 
152             auto it = om.begin();
153             CHECK(it->first == "zwei");
154             ++it;
155             CHECK(it->first == "drei");
156             ++it;
157             CHECK(it == om.end());
158         }
159 
160         SECTION("with const Key&&")
161         {
162             const std::string eins = "eins";
163             const std::string vier = "vier";
164             CHECK(om.size() == 3);
165             CHECK(om.erase(eins) == 1);
166             CHECK(om.size() == 2);
167             CHECK(om.erase(vier) == 0);
168             CHECK(om.size() == 2);
169 
170             auto it = om.begin();
171             CHECK(it->first == "zwei");
172             ++it;
173             CHECK(it->first == "drei");
174             ++it;
175             CHECK(it == om.end());
176         }
177 
178         SECTION("with string literal")
179         {
180             CHECK(om.size() == 3);
181             CHECK(om.erase("eins") == 1);
182             CHECK(om.size() == 2);
183             CHECK(om.erase("vier") == 0);
184             CHECK(om.size() == 2);
185 
186             auto it = om.begin();
187             CHECK(it->first == "zwei");
188             ++it;
189             CHECK(it->first == "drei");
190             ++it;
191             CHECK(it == om.end());
192         }
193 
194         SECTION("with iterator")
195         {
196             CHECK(om.size() == 3);
197             CHECK(om.begin()->first == "eins");
198             CHECK(std::next(om.begin(), 1)->first == "zwei");
199             CHECK(std::next(om.begin(), 2)->first == "drei");
200 
201             auto it = om.erase(om.begin());
202             CHECK(it->first == "zwei");
203             CHECK(om.size() == 2);
204 
205             auto it2 = om.begin();
206             CHECK(it2->first == "zwei");
207             ++it2;
208             CHECK(it2->first == "drei");
209             ++it2;
210             CHECK(it2 == om.end());
211         }
212     }
213 
214     SECTION("count")
215     {
216         ordered_map<std::string, std::string> om;
217         om["eins"] = "one";
218         om["zwei"] = "two";
219         om["drei"] = "three";
220 
221         const std::string eins("eins");
222         const std::string vier("vier");
223         CHECK(om.count("eins") == 1);
224         CHECK(om.count(std::string("eins")) == 1);
225         CHECK(om.count(eins) == 1);
226         CHECK(om.count("vier") == 0);
227         CHECK(om.count(std::string("vier")) == 0);
228         CHECK(om.count(vier) == 0);
229     }
230 
231     SECTION("find")
232     {
233         ordered_map<std::string, std::string> om;
234         om["eins"] = "one";
235         om["zwei"] = "two";
236         om["drei"] = "three";
237         const auto com = om;
238 
239         const std::string eins("eins");
240         const std::string vier("vier");
241         CHECK(om.find("eins") == om.begin());
242         CHECK(om.find(std::string("eins")) == om.begin());
243         CHECK(om.find(eins) == om.begin());
244         CHECK(om.find("vier") == om.end());
245         CHECK(om.find(std::string("vier")) == om.end());
246         CHECK(om.find(vier) == om.end());
247 
248         CHECK(com.find("eins") == com.begin());
249         CHECK(com.find(std::string("eins")) == com.begin());
250         CHECK(com.find(eins) == com.begin());
251         CHECK(com.find("vier") == com.end());
252         CHECK(com.find(std::string("vier")) == com.end());
253         CHECK(com.find(vier) == com.end());
254     }
255 
256     SECTION("insert")
257     {
258         ordered_map<std::string, std::string> om;
259         om["eins"] = "one";
260         om["zwei"] = "two";
261         om["drei"] = "three";
262 
263         SECTION("const value_type&")
264         {
265             ordered_map<std::string, std::string>::value_type vt1 {"eins", "1"};
266             ordered_map<std::string, std::string>::value_type vt4 {"vier", "four"};
267 
268             auto res1 = om.insert(vt1);
269             CHECK(res1.first == om.begin());
270             CHECK(res1.second == false);
271             CHECK(om.size() == 3);
272 
273             auto res4 = om.insert(vt4);
274             CHECK(res4.first == om.begin() + 3);
275             CHECK(res4.second == true);
276             CHECK(om.size() == 4);
277         }
278 
279         SECTION("value_type&&")
280         {
281             auto res1 = om.insert({"eins", "1"});
282             CHECK(res1.first == om.begin());
283             CHECK(res1.second == false);
284             CHECK(om.size() == 3);
285 
286             auto res4 = om.insert({"vier", "four"});
287             CHECK(res4.first == om.begin() + 3);
288             CHECK(res4.second == true);
289             CHECK(om.size() == 4);
290         }
291     }
292 }
293