💚 make test deterministic

parent 9449dfcc
...@@ -32,59 +32,53 @@ SOFTWARE. ...@@ -32,59 +32,53 @@ SOFTWARE.
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::json; using json = nlohmann::json;
#include <set>
TEST_CASE("hash") TEST_CASE("hash")
{ {
SECTION("null") // Collect hashes for different JSON values and make sure that they are distinct
{ // We cannot compare against fixed values, because the implementation of
CHECK(std::hash<json> {}(json(nullptr)) == 2654435769U); // std::hash may differ between compilers.
}
std::set<std::size_t> hashes;
SECTION("boolean")
{ // null
CHECK(std::hash<json> {}(json(true)) == 2654436031U); hashes.insert(std::hash<json> {}(json(nullptr)));
CHECK(std::hash<json> {}(json(false)) == 2654436030U);
} // boolean
hashes.insert(std::hash<json> {}(json(true)));
SECTION("string") hashes.insert(std::hash<json> {}(json(false)));
{
CHECK(std::hash<json> {}(json("")) == 11160318156688833227U); // string
CHECK(std::hash<json> {}(json("foo")) == 910203211069189493U); hashes.insert(std::hash<json> {}(json("")));
} hashes.insert(std::hash<json> {}(json("foo")));
SECTION("number") // number
{ hashes.insert(std::hash<json> {}(json(int(0))));
CHECK(std::hash<json> {}(json(int(0))) == 2654436095U); hashes.insert(std::hash<json> {}(json(unsigned(0))));
CHECK(std::hash<json> {}(json(unsigned(0))) == 2654436156U);
hashes.insert(std::hash<json> {}(json(-1)));
CHECK(std::hash<json> {}(json(-1)) == 2654436092U); hashes.insert(std::hash<json> {}(json(0.0)));
CHECK(std::hash<json> {}(json(0.0)) == 2654436221U); hashes.insert(std::hash<json> {}(json(42.23)));
CHECK(std::hash<json> {}(json(42.23)) == 4631140164097181104U);
} // array
hashes.insert(std::hash<json> {}(json::array()));
SECTION("array") hashes.insert(std::hash<json> {}(json::array({1, 2, 3})));
{
CHECK(std::hash<json> {}(json::array()) == 2654435899U); // object
CHECK(std::hash<json> {}(json::array({1, 2, 3})) == 717272658337467U); hashes.insert(std::hash<json> {}(json::object()));
} hashes.insert(std::hash<json> {}(json::object({{"foo", "bar"}})));
SECTION("object") // binary
{ hashes.insert(std::hash<json> {}(json::binary({})));
CHECK(std::hash<json> {}(json::object()) == 2654435832U); hashes.insert(std::hash<json> {}(json::binary({}, 0)));
CHECK(std::hash<json> {}(json::object({{"foo", "bar"}})) == 4042265434648078139U); hashes.insert(std::hash<json> {}(json::binary({}, 42)));
} hashes.insert(std::hash<json> {}(json::binary({1, 2, 3})));
hashes.insert(std::hash<json> {}(json::binary({1, 2, 3}, 0)));
SECTION("binary") hashes.insert(std::hash<json> {}(json::binary({1, 2, 3}, 42)));
{
CHECK(std::hash<json> {}(json::binary({})) == 11093832941624U); // discarded
CHECK(std::hash<json> {}(json::binary({}, 0)) == 11093832941691U); hashes.insert(std::hash<json> {}(json(json::value_t::discarded)));
CHECK(std::hash<json> {}(json::binary({}, 42)) == 11093832941581U);
CHECK(std::hash<json> {}(json::binary({1, 2, 3})) == 3005324138949694928U); CHECK(hashes.size() == 21);
CHECK(std::hash<json> {}(json::binary({1, 2, 3}, 0)) == 3005324138988516582U);
CHECK(std::hash<json> {}(json::binary({1, 2, 3}, 42)) == 3005324138986241627U);
}
SECTION("discarded")
{
CHECK(std::hash<json> {}(json(json::value_t::discarded)) == 2654436338U);
}
} }
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment