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