1c5f01b2fSopenharmony_ci//     __ _____ _____ _____
2c5f01b2fSopenharmony_ci//  __|  |   __|     |   | |  JSON for Modern C++ (supporting code)
3c5f01b2fSopenharmony_ci// |  |  |__   |  |  | | | |  version 3.11.2
4c5f01b2fSopenharmony_ci// |_____|_____|_____|_|___|  https://github.com/nlohmann/json
5c5f01b2fSopenharmony_ci//
6c5f01b2fSopenharmony_ci// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
7c5f01b2fSopenharmony_ci// SPDX-License-Identifier: MIT
8c5f01b2fSopenharmony_ci
9c5f01b2fSopenharmony_ci#include "doctest_compatibility.h"
10c5f01b2fSopenharmony_ci
11c5f01b2fSopenharmony_ci#include <nlohmann/json.hpp>
12c5f01b2fSopenharmony_ciusing nlohmann::ordered_map;
13c5f01b2fSopenharmony_ci
14c5f01b2fSopenharmony_ci
15c5f01b2fSopenharmony_ciTEST_CASE("ordered_map")
16c5f01b2fSopenharmony_ci{
17c5f01b2fSopenharmony_ci    SECTION("constructor")
18c5f01b2fSopenharmony_ci    {
19c5f01b2fSopenharmony_ci        SECTION("constructor from iterator range")
20c5f01b2fSopenharmony_ci        {
21c5f01b2fSopenharmony_ci            std::map<std::string, std::string> m {{"eins", "one"}, {"zwei", "two"}, {"drei", "three"}};
22c5f01b2fSopenharmony_ci            ordered_map<std::string, std::string> om(m.begin(), m.end());
23c5f01b2fSopenharmony_ci            CHECK(om.size() == 3);
24c5f01b2fSopenharmony_ci        }
25c5f01b2fSopenharmony_ci
26c5f01b2fSopenharmony_ci        SECTION("copy assignment")
27c5f01b2fSopenharmony_ci        {
28c5f01b2fSopenharmony_ci            std::map<std::string, std::string> m {{"eins", "one"}, {"zwei", "two"}, {"drei", "three"}};
29c5f01b2fSopenharmony_ci            ordered_map<std::string, std::string> om(m.begin(), m.end());
30c5f01b2fSopenharmony_ci            const auto com = om;
31c5f01b2fSopenharmony_ci            om.clear(); // silence a warning by forbidding having "const auto& com = om;"
32c5f01b2fSopenharmony_ci            CHECK(com.size() == 3);
33c5f01b2fSopenharmony_ci        }
34c5f01b2fSopenharmony_ci    }
35c5f01b2fSopenharmony_ci
36c5f01b2fSopenharmony_ci    SECTION("at")
37c5f01b2fSopenharmony_ci    {
38c5f01b2fSopenharmony_ci        std::map<std::string, std::string> m {{"eins", "one"}, {"zwei", "two"}, {"drei", "three"}};
39c5f01b2fSopenharmony_ci        ordered_map<std::string, std::string> om(m.begin(), m.end());
40c5f01b2fSopenharmony_ci        const auto com = om;
41c5f01b2fSopenharmony_ci
42c5f01b2fSopenharmony_ci        SECTION("with Key&&")
43c5f01b2fSopenharmony_ci        {
44c5f01b2fSopenharmony_ci            CHECK(om.at(std::string("eins")) == std::string("one"));
45c5f01b2fSopenharmony_ci            CHECK(com.at(std::string("eins")) == std::string("one"));
46c5f01b2fSopenharmony_ci            CHECK_THROWS_AS(om.at(std::string("vier")), std::out_of_range);
47c5f01b2fSopenharmony_ci            CHECK_THROWS_AS(com.at(std::string("vier")), std::out_of_range);
48c5f01b2fSopenharmony_ci        }
49c5f01b2fSopenharmony_ci
50c5f01b2fSopenharmony_ci        SECTION("with const Key&&")
51c5f01b2fSopenharmony_ci        {
52c5f01b2fSopenharmony_ci            const std::string eins = "eins";
53c5f01b2fSopenharmony_ci            const std::string vier = "vier";
54c5f01b2fSopenharmony_ci            CHECK(om.at(eins) == std::string("one"));
55c5f01b2fSopenharmony_ci            CHECK(com.at(eins) == std::string("one"));
56c5f01b2fSopenharmony_ci            CHECK_THROWS_AS(om.at(vier), std::out_of_range);
57c5f01b2fSopenharmony_ci            CHECK_THROWS_AS(com.at(vier), std::out_of_range);
58c5f01b2fSopenharmony_ci        }
59c5f01b2fSopenharmony_ci
60c5f01b2fSopenharmony_ci        SECTION("with string literal")
61c5f01b2fSopenharmony_ci        {
62c5f01b2fSopenharmony_ci            CHECK(om.at("eins") == std::string("one"));
63c5f01b2fSopenharmony_ci            CHECK(com.at("eins") == std::string("one"));
64c5f01b2fSopenharmony_ci            CHECK_THROWS_AS(om.at("vier"), std::out_of_range);
65c5f01b2fSopenharmony_ci            CHECK_THROWS_AS(com.at("vier"), std::out_of_range);
66c5f01b2fSopenharmony_ci        }
67c5f01b2fSopenharmony_ci    }
68c5f01b2fSopenharmony_ci
69c5f01b2fSopenharmony_ci    SECTION("operator[]")
70c5f01b2fSopenharmony_ci    {
71c5f01b2fSopenharmony_ci        std::map<std::string, std::string> m {{"eins", "one"}, {"zwei", "two"}, {"drei", "three"}};
72c5f01b2fSopenharmony_ci        ordered_map<std::string, std::string> om(m.begin(), m.end());
73c5f01b2fSopenharmony_ci        const auto com = om;
74c5f01b2fSopenharmony_ci
75c5f01b2fSopenharmony_ci        SECTION("with Key&&")
76c5f01b2fSopenharmony_ci        {
77c5f01b2fSopenharmony_ci            CHECK(om[std::string("eins")] == std::string("one"));
78c5f01b2fSopenharmony_ci            CHECK(com[std::string("eins")] == std::string("one"));
79c5f01b2fSopenharmony_ci
80c5f01b2fSopenharmony_ci            CHECK(om[std::string("vier")] == std::string(""));
81c5f01b2fSopenharmony_ci            CHECK(om.size() == 4);
82c5f01b2fSopenharmony_ci        }
83c5f01b2fSopenharmony_ci
84c5f01b2fSopenharmony_ci        SECTION("with const Key&&")
85c5f01b2fSopenharmony_ci        {
86c5f01b2fSopenharmony_ci            const std::string eins = "eins";
87c5f01b2fSopenharmony_ci            const std::string vier = "vier";
88c5f01b2fSopenharmony_ci
89c5f01b2fSopenharmony_ci            CHECK(om[eins] == std::string("one"));
90c5f01b2fSopenharmony_ci            CHECK(com[eins] == std::string("one"));
91c5f01b2fSopenharmony_ci
92c5f01b2fSopenharmony_ci            CHECK(om[vier] == std::string(""));
93c5f01b2fSopenharmony_ci            CHECK(om.size() == 4);
94c5f01b2fSopenharmony_ci        }
95c5f01b2fSopenharmony_ci
96c5f01b2fSopenharmony_ci        SECTION("with string literal")
97c5f01b2fSopenharmony_ci        {
98c5f01b2fSopenharmony_ci            CHECK(om["eins"] == std::string("one"));
99c5f01b2fSopenharmony_ci            CHECK(com["eins"] == std::string("one"));
100c5f01b2fSopenharmony_ci
101c5f01b2fSopenharmony_ci            CHECK(om["vier"] == std::string(""));
102c5f01b2fSopenharmony_ci            CHECK(om.size() == 4);
103c5f01b2fSopenharmony_ci        }
104c5f01b2fSopenharmony_ci    }
105c5f01b2fSopenharmony_ci
106c5f01b2fSopenharmony_ci    SECTION("erase")
107c5f01b2fSopenharmony_ci    {
108c5f01b2fSopenharmony_ci        ordered_map<std::string, std::string> om;
109c5f01b2fSopenharmony_ci        om["eins"] = "one";
110c5f01b2fSopenharmony_ci        om["zwei"] = "two";
111c5f01b2fSopenharmony_ci        om["drei"] = "three";
112c5f01b2fSopenharmony_ci
113c5f01b2fSopenharmony_ci        {
114c5f01b2fSopenharmony_ci            auto it = om.begin();
115c5f01b2fSopenharmony_ci            CHECK(it->first == "eins");
116c5f01b2fSopenharmony_ci            ++it;
117c5f01b2fSopenharmony_ci            CHECK(it->first == "zwei");
118c5f01b2fSopenharmony_ci            ++it;
119c5f01b2fSopenharmony_ci            CHECK(it->first == "drei");
120c5f01b2fSopenharmony_ci            ++it;
121c5f01b2fSopenharmony_ci            CHECK(it == om.end());
122c5f01b2fSopenharmony_ci        }
123c5f01b2fSopenharmony_ci
124c5f01b2fSopenharmony_ci        SECTION("with Key&&")
125c5f01b2fSopenharmony_ci        {
126c5f01b2fSopenharmony_ci            CHECK(om.size() == 3);
127c5f01b2fSopenharmony_ci            CHECK(om.erase(std::string("eins")) == 1);
128c5f01b2fSopenharmony_ci            CHECK(om.size() == 2);
129c5f01b2fSopenharmony_ci            CHECK(om.erase(std::string("vier")) == 0);
130c5f01b2fSopenharmony_ci            CHECK(om.size() == 2);
131c5f01b2fSopenharmony_ci
132c5f01b2fSopenharmony_ci            auto it = om.begin();
133c5f01b2fSopenharmony_ci            CHECK(it->first == "zwei");
134c5f01b2fSopenharmony_ci            ++it;
135c5f01b2fSopenharmony_ci            CHECK(it->first == "drei");
136c5f01b2fSopenharmony_ci            ++it;
137c5f01b2fSopenharmony_ci            CHECK(it == om.end());
138c5f01b2fSopenharmony_ci        }
139c5f01b2fSopenharmony_ci
140c5f01b2fSopenharmony_ci        SECTION("with const Key&&")
141c5f01b2fSopenharmony_ci        {
142c5f01b2fSopenharmony_ci            const std::string eins = "eins";
143c5f01b2fSopenharmony_ci            const std::string vier = "vier";
144c5f01b2fSopenharmony_ci            CHECK(om.size() == 3);
145c5f01b2fSopenharmony_ci            CHECK(om.erase(eins) == 1);
146c5f01b2fSopenharmony_ci            CHECK(om.size() == 2);
147c5f01b2fSopenharmony_ci            CHECK(om.erase(vier) == 0);
148c5f01b2fSopenharmony_ci            CHECK(om.size() == 2);
149c5f01b2fSopenharmony_ci
150c5f01b2fSopenharmony_ci            auto it = om.begin();
151c5f01b2fSopenharmony_ci            CHECK(it->first == "zwei");
152c5f01b2fSopenharmony_ci            ++it;
153c5f01b2fSopenharmony_ci            CHECK(it->first == "drei");
154c5f01b2fSopenharmony_ci            ++it;
155c5f01b2fSopenharmony_ci            CHECK(it == om.end());
156c5f01b2fSopenharmony_ci        }
157c5f01b2fSopenharmony_ci
158c5f01b2fSopenharmony_ci        SECTION("with string literal")
159c5f01b2fSopenharmony_ci        {
160c5f01b2fSopenharmony_ci            CHECK(om.size() == 3);
161c5f01b2fSopenharmony_ci            CHECK(om.erase("eins") == 1);
162c5f01b2fSopenharmony_ci            CHECK(om.size() == 2);
163c5f01b2fSopenharmony_ci            CHECK(om.erase("vier") == 0);
164c5f01b2fSopenharmony_ci            CHECK(om.size() == 2);
165c5f01b2fSopenharmony_ci
166c5f01b2fSopenharmony_ci            auto it = om.begin();
167c5f01b2fSopenharmony_ci            CHECK(it->first == "zwei");
168c5f01b2fSopenharmony_ci            ++it;
169c5f01b2fSopenharmony_ci            CHECK(it->first == "drei");
170c5f01b2fSopenharmony_ci            ++it;
171c5f01b2fSopenharmony_ci            CHECK(it == om.end());
172c5f01b2fSopenharmony_ci        }
173c5f01b2fSopenharmony_ci
174c5f01b2fSopenharmony_ci        SECTION("with iterator")
175c5f01b2fSopenharmony_ci        {
176c5f01b2fSopenharmony_ci            CHECK(om.size() == 3);
177c5f01b2fSopenharmony_ci            CHECK(om.begin()->first == "eins");
178c5f01b2fSopenharmony_ci            CHECK(std::next(om.begin(), 1)->first == "zwei");
179c5f01b2fSopenharmony_ci            CHECK(std::next(om.begin(), 2)->first == "drei");
180c5f01b2fSopenharmony_ci
181c5f01b2fSopenharmony_ci            auto it = om.erase(om.begin());
182c5f01b2fSopenharmony_ci            CHECK(it->first == "zwei");
183c5f01b2fSopenharmony_ci            CHECK(om.size() == 2);
184c5f01b2fSopenharmony_ci
185c5f01b2fSopenharmony_ci            auto it2 = om.begin();
186c5f01b2fSopenharmony_ci            CHECK(it2->first == "zwei");
187c5f01b2fSopenharmony_ci            ++it2;
188c5f01b2fSopenharmony_ci            CHECK(it2->first == "drei");
189c5f01b2fSopenharmony_ci            ++it2;
190c5f01b2fSopenharmony_ci            CHECK(it2 == om.end());
191c5f01b2fSopenharmony_ci        }
192c5f01b2fSopenharmony_ci
193c5f01b2fSopenharmony_ci        SECTION("with iterator pair")
194c5f01b2fSopenharmony_ci        {
195c5f01b2fSopenharmony_ci            SECTION("range in the middle")
196c5f01b2fSopenharmony_ci            {
197c5f01b2fSopenharmony_ci                // need more elements
198c5f01b2fSopenharmony_ci                om["vier"] = "four";
199c5f01b2fSopenharmony_ci                om["fünf"] = "five";
200c5f01b2fSopenharmony_ci
201c5f01b2fSopenharmony_ci                // delete "zwei" and "drei"
202c5f01b2fSopenharmony_ci                auto it = om.erase(om.begin() + 1, om.begin() + 3);
203c5f01b2fSopenharmony_ci                CHECK(it->first == "vier");
204c5f01b2fSopenharmony_ci                CHECK(om.size() == 3);
205c5f01b2fSopenharmony_ci            }
206c5f01b2fSopenharmony_ci
207c5f01b2fSopenharmony_ci            SECTION("range at the beginning")
208c5f01b2fSopenharmony_ci            {
209c5f01b2fSopenharmony_ci                // need more elements
210c5f01b2fSopenharmony_ci                om["vier"] = "four";
211c5f01b2fSopenharmony_ci                om["fünf"] = "five";
212c5f01b2fSopenharmony_ci
213c5f01b2fSopenharmony_ci                // delete "eins" and "zwei"
214c5f01b2fSopenharmony_ci                auto it = om.erase(om.begin(), om.begin() + 2);
215c5f01b2fSopenharmony_ci                CHECK(it->first == "drei");
216c5f01b2fSopenharmony_ci                CHECK(om.size() == 3);
217c5f01b2fSopenharmony_ci            }
218c5f01b2fSopenharmony_ci
219c5f01b2fSopenharmony_ci            SECTION("range at the end")
220c5f01b2fSopenharmony_ci            {
221c5f01b2fSopenharmony_ci                // need more elements
222c5f01b2fSopenharmony_ci                om["vier"] = "four";
223c5f01b2fSopenharmony_ci                om["fünf"] = "five";
224c5f01b2fSopenharmony_ci
225c5f01b2fSopenharmony_ci                // delete "vier" and "fünf"
226c5f01b2fSopenharmony_ci                auto it = om.erase(om.begin() + 3, om.end());
227c5f01b2fSopenharmony_ci                CHECK(it == om.end());
228c5f01b2fSopenharmony_ci                CHECK(om.size() == 3);
229c5f01b2fSopenharmony_ci            }
230c5f01b2fSopenharmony_ci        }
231c5f01b2fSopenharmony_ci    }
232c5f01b2fSopenharmony_ci
233c5f01b2fSopenharmony_ci    SECTION("count")
234c5f01b2fSopenharmony_ci    {
235c5f01b2fSopenharmony_ci        ordered_map<std::string, std::string> om;
236c5f01b2fSopenharmony_ci        om["eins"] = "one";
237c5f01b2fSopenharmony_ci        om["zwei"] = "two";
238c5f01b2fSopenharmony_ci        om["drei"] = "three";
239c5f01b2fSopenharmony_ci
240c5f01b2fSopenharmony_ci        const std::string eins("eins");
241c5f01b2fSopenharmony_ci        const std::string vier("vier");
242c5f01b2fSopenharmony_ci        CHECK(om.count("eins") == 1);
243c5f01b2fSopenharmony_ci        CHECK(om.count(std::string("eins")) == 1);
244c5f01b2fSopenharmony_ci        CHECK(om.count(eins) == 1);
245c5f01b2fSopenharmony_ci        CHECK(om.count("vier") == 0);
246c5f01b2fSopenharmony_ci        CHECK(om.count(std::string("vier")) == 0);
247c5f01b2fSopenharmony_ci        CHECK(om.count(vier) == 0);
248c5f01b2fSopenharmony_ci    }
249c5f01b2fSopenharmony_ci
250c5f01b2fSopenharmony_ci    SECTION("find")
251c5f01b2fSopenharmony_ci    {
252c5f01b2fSopenharmony_ci        ordered_map<std::string, std::string> om;
253c5f01b2fSopenharmony_ci        om["eins"] = "one";
254c5f01b2fSopenharmony_ci        om["zwei"] = "two";
255c5f01b2fSopenharmony_ci        om["drei"] = "three";
256c5f01b2fSopenharmony_ci        const auto com = om;
257c5f01b2fSopenharmony_ci
258c5f01b2fSopenharmony_ci        const std::string eins("eins");
259c5f01b2fSopenharmony_ci        const std::string vier("vier");
260c5f01b2fSopenharmony_ci        CHECK(om.find("eins") == om.begin());
261c5f01b2fSopenharmony_ci        CHECK(om.find(std::string("eins")) == om.begin());
262c5f01b2fSopenharmony_ci        CHECK(om.find(eins) == om.begin());
263c5f01b2fSopenharmony_ci        CHECK(om.find("vier") == om.end());
264c5f01b2fSopenharmony_ci        CHECK(om.find(std::string("vier")) == om.end());
265c5f01b2fSopenharmony_ci        CHECK(om.find(vier) == om.end());
266c5f01b2fSopenharmony_ci
267c5f01b2fSopenharmony_ci        CHECK(com.find("eins") == com.begin());
268c5f01b2fSopenharmony_ci        CHECK(com.find(std::string("eins")) == com.begin());
269c5f01b2fSopenharmony_ci        CHECK(com.find(eins) == com.begin());
270c5f01b2fSopenharmony_ci        CHECK(com.find("vier") == com.end());
271c5f01b2fSopenharmony_ci        CHECK(com.find(std::string("vier")) == com.end());
272c5f01b2fSopenharmony_ci        CHECK(com.find(vier) == com.end());
273c5f01b2fSopenharmony_ci    }
274c5f01b2fSopenharmony_ci
275c5f01b2fSopenharmony_ci    SECTION("insert")
276c5f01b2fSopenharmony_ci    {
277c5f01b2fSopenharmony_ci        ordered_map<std::string, std::string> om;
278c5f01b2fSopenharmony_ci        om["eins"] = "one";
279c5f01b2fSopenharmony_ci        om["zwei"] = "two";
280c5f01b2fSopenharmony_ci        om["drei"] = "three";
281c5f01b2fSopenharmony_ci
282c5f01b2fSopenharmony_ci        SECTION("const value_type&")
283c5f01b2fSopenharmony_ci        {
284c5f01b2fSopenharmony_ci            ordered_map<std::string, std::string>::value_type vt1 {"eins", "1"};
285c5f01b2fSopenharmony_ci            ordered_map<std::string, std::string>::value_type vt4 {"vier", "four"};
286c5f01b2fSopenharmony_ci
287c5f01b2fSopenharmony_ci            auto res1 = om.insert(vt1);
288c5f01b2fSopenharmony_ci            CHECK(res1.first == om.begin());
289c5f01b2fSopenharmony_ci            CHECK(res1.second == false);
290c5f01b2fSopenharmony_ci            CHECK(om.size() == 3);
291c5f01b2fSopenharmony_ci
292c5f01b2fSopenharmony_ci            auto res4 = om.insert(vt4);
293c5f01b2fSopenharmony_ci            CHECK(res4.first == om.begin() + 3);
294c5f01b2fSopenharmony_ci            CHECK(res4.second == true);
295c5f01b2fSopenharmony_ci            CHECK(om.size() == 4);
296c5f01b2fSopenharmony_ci        }
297c5f01b2fSopenharmony_ci
298c5f01b2fSopenharmony_ci        SECTION("value_type&&")
299c5f01b2fSopenharmony_ci        {
300c5f01b2fSopenharmony_ci            auto res1 = om.insert({"eins", "1"});
301c5f01b2fSopenharmony_ci            CHECK(res1.first == om.begin());
302c5f01b2fSopenharmony_ci            CHECK(res1.second == false);
303c5f01b2fSopenharmony_ci            CHECK(om.size() == 3);
304c5f01b2fSopenharmony_ci
305c5f01b2fSopenharmony_ci            auto res4 = om.insert({"vier", "four"});
306c5f01b2fSopenharmony_ci            CHECK(res4.first == om.begin() + 3);
307c5f01b2fSopenharmony_ci            CHECK(res4.second == true);
308c5f01b2fSopenharmony_ci            CHECK(om.size() == 4);
309c5f01b2fSopenharmony_ci        }
310c5f01b2fSopenharmony_ci    }
311c5f01b2fSopenharmony_ci}
312