1 // __ _____ _____ _____ 2 // __| | __| | | | JSON for Modern C++ (supporting code) 3 // | | |__ | | | | | | version 3.11.2 4 // |_____|_____|_____|_|___| https://github.com/nlohmann/json 5 // 6 // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me> 7 // SPDX-License-Identifier: MIT 8 9 #include "doctest_compatibility.h" 10 11 #include <nlohmann/json.hpp> 12 using json = nlohmann::json; 13 using ordered_json = nlohmann::ordered_json; 14 15 #include <set> 16 17 TEST_CASE("hash<nlohmann::json>") 18 { 19 // Collect hashes for different JSON values and make sure that they are distinct 20 // We cannot compare against fixed values, because the implementation of 21 // std::hash may differ between compilers. 22 23 std::set<std::size_t> hashes; 24 25 // null 26 hashes.insert(std::hash<json> {}(json(nullptr))); 27 28 // boolean 29 hashes.insert(std::hash<json> {}(json(true))); 30 hashes.insert(std::hash<json> {}(json(false))); 31 32 // string 33 hashes.insert(std::hash<json> {}(json(""))); 34 hashes.insert(std::hash<json> {}(json("foo"))); 35 36 // number 37 hashes.insert(std::hash<json> {}(json(0))); 38 hashes.insert(std::hash<json> {}(json(static_cast<unsigned>(0)))); 39 40 hashes.insert(std::hash<json> {}(json(-1))); 41 hashes.insert(std::hash<json> {}(json(0.0))); 42 hashes.insert(std::hash<json> {}(json(42.23))); 43 44 // array 45 hashes.insert(std::hash<json> {}(json::array())); 46 hashes.insert(std::hash<json> {}(json::array({1, 2, 3}))); 47 48 // object 49 hashes.insert(std::hash<json> {}(json::object())); 50 hashes.insert(std::hash<json> {}(json::object({{"foo", "bar"}}))); 51 52 // binary 53 hashes.insert(std::hash<json> {}(json::binary({}))); 54 hashes.insert(std::hash<json> {}(json::binary({}, 0))); 55 hashes.insert(std::hash<json> {}(json::binary({}, 42))); 56 hashes.insert(std::hash<json> {}(json::binary({1, 2, 3}))); 57 hashes.insert(std::hash<json> {}(json::binary({1, 2, 3}, 0))); 58 hashes.insert(std::hash<json> {}(json::binary({1, 2, 3}, 42))); 59 60 // discarded 61 hashes.insert(std::hash<json> {}(json(json::value_t::discarded))); 62 63 CHECK(hashes.size() == 21); 64 } 65 66 TEST_CASE("hash<nlohmann::ordered_json>") 67 { 68 // Collect hashes for different JSON values and make sure that they are distinct 69 // We cannot compare against fixed values, because the implementation of 70 // std::hash may differ between compilers. 71 72 std::set<std::size_t> hashes; 73 74 // null 75 hashes.insert(std::hash<ordered_json> {}(ordered_json(nullptr))); 76 77 // boolean 78 hashes.insert(std::hash<ordered_json> {}(ordered_json(true))); 79 hashes.insert(std::hash<ordered_json> {}(ordered_json(false))); 80 81 // string 82 hashes.insert(std::hash<ordered_json> {}(ordered_json(""))); 83 hashes.insert(std::hash<ordered_json> {}(ordered_json("foo"))); 84 85 // number 86 hashes.insert(std::hash<ordered_json> {}(ordered_json(0))); 87 hashes.insert(std::hash<ordered_json> {}(ordered_json(static_cast<unsigned>(0)))); 88 89 hashes.insert(std::hash<ordered_json> {}(ordered_json(-1))); 90 hashes.insert(std::hash<ordered_json> {}(ordered_json(0.0))); 91 hashes.insert(std::hash<ordered_json> {}(ordered_json(42.23))); 92 93 // array 94 hashes.insert(std::hash<ordered_json> {}(ordered_json::array())); 95 hashes.insert(std::hash<ordered_json> {}(ordered_json::array({1, 2, 3}))); 96 97 // object 98 hashes.insert(std::hash<ordered_json> {}(ordered_json::object())); 99 hashes.insert(std::hash<ordered_json> {}(ordered_json::object({{"foo", "bar"}}))); 100 101 // binary 102 hashes.insert(std::hash<ordered_json> {}(ordered_json::binary({}))); 103 hashes.insert(std::hash<ordered_json> {}(ordered_json::binary({}, 0))); 104 hashes.insert(std::hash<ordered_json> {}(ordered_json::binary({}, 42))); 105 hashes.insert(std::hash<ordered_json> {}(ordered_json::binary({1, 2, 3}))); 106 hashes.insert(std::hash<ordered_json> {}(ordered_json::binary({1, 2, 3}, 0))); 107 hashes.insert(std::hash<ordered_json> {}(ordered_json::binary({1, 2, 3}, 42))); 108 109 // discarded 110 hashes.insert(std::hash<ordered_json> {}(ordered_json(ordered_json::value_t::discarded))); 111 112 CHECK(hashes.size() == 21); 113 } 114