From d760ff51420264eb50f716154f0d3f5343e12314 Mon Sep 17 00:00:00 2001 From: Niels Date: Tue, 10 Feb 2015 17:10:39 +0100 Subject: [PATCH] tests for comparison operators --- test/unit.cpp | 126 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 125 insertions(+), 1 deletion(-) diff --git a/test/unit.cpp b/test/unit.cpp index 79f8bb41..9e8db7fc 100644 --- a/test/unit.cpp +++ b/test/unit.cpp @@ -3689,4 +3689,128 @@ TEST_CASE("modifiers") } } } -} \ No newline at end of file +} + +TEST_CASE("lexicographical comparison operators") +{ + json j_values = + { + nullptr, nullptr, + 17, 42, + 3.14159, 23.42, + "foo", "bar", + true, false, + {1, 2, 3}, {"one", "two", "three"}, + {{"first", 1}, {"second", 2}}, {{"a", "A"}, {"b", {"B"}}} + }; + + SECTION("comparison: equal") + { + std::vector> expected = + { + {true, true, false, false, false, false, false, false, false, false, false, false, false, false}, + {true, true, false, false, false, false, false, false, false, false, false, false, false, false}, + {false, false, true, false, false, false, false, false, false, false, false, false, false, false}, + {false, false, false, true, false, false, false, false, false, false, false, false, false, false}, + {false, false, false, false, true, false, false, false, false, false, false, false, false, false}, + {false, false, false, false, false, true, false, false, false, false, false, false, false, false}, + {false, false, false, false, false, false, true, false, false, false, false, false, false, false}, + {false, false, false, false, false, false, false, true, false, false, false, false, false, false}, + {false, false, false, false, false, false, false, false, true, false, false, false, false, false}, + {false, false, false, false, false, false, false, false, false, true, false, false, false, false}, + {false, false, false, false, false, false, false, false, false, false, true, false, false, false}, + {false, false, false, false, false, false, false, false, false, false, false, true, false, false}, + {false, false, false, false, false, false, false, false, false, false, false, false, true, false}, + {false, false, false, false, false, false, false, false, false, false, false, false, false, true} + }; + + for (size_t i = 0; i < j_values.size(); ++i) + { + for (size_t j = 0; j < j_values.size(); ++j) + { + // check precomputed values + CHECK( (j_values[i] == j_values[j]) == expected[i][j] ); + } + } + } + + SECTION("comparison: not equal") + { + for (size_t i = 0; i < j_values.size(); ++i) + { + for (size_t j = 0; j < j_values.size(); ++j) + { + // check definition + CHECK( (j_values[i] != j_values[j]) == not(j_values[i] == j_values[j]) ); + } + } + } + + SECTION("comparison: less") + { + std::vector> expected = + { + {false, false, false, false, false, false, false, false, false, false, false, false, false, false}, + {false, false, false, false, false, false, false, false, false, false, false, false, false, false}, + {false, false, false, true, false, true, false, false, false, false, false, false, false, false}, + {false, false, false, false, false, false, false, false, false, false, false, false, false, false}, + {false, false, true, true, false, true, false, false, false, false, false, false, false, false}, + {false, false, false, true, false, false, false, false, false, false, false, false, false, false}, + {false, false, false, false, false, false, false, false, false, false, false, false, false, false}, + {false, false, false, false, false, false, true, false, false, false, false, false, false, false}, + {false, false, false, false, false, false, false, false, false, false, false, false, false, false}, + {false, false, false, false, false, false, false, false, true, false, false, false, false, false}, + {false, false, false, false, false, false, false, false, false, false, false, false, false, false}, + {false, false, false, false, false, false, false, false, false, false, false, false, false, false}, + {false, false, false, false, false, false, false, false, false, false, false, false, false, false}, + {false, false, false, false, false, false, false, false, false, false, false, false, true, false} + }; + + for (size_t i = 0; i < j_values.size(); ++i) + { + for (size_t j = 0; j < j_values.size(); ++j) + { + CAPTURE(i); + CAPTURE(j); + // check precomputed values + CHECK( (j_values[i] < j_values[j]) == expected[i][j] ); + } + } + } + + SECTION("comparison: less than or equal equal") + { + for (size_t i = 0; i < j_values.size(); ++i) + { + for (size_t j = 0; j < j_values.size(); ++j) + { + // check definition + CHECK( (j_values[i] <= j_values[j]) == not(j_values[j] < j_values[i]) ); + } + } + } + + SECTION("comparison: greater than") + { + for (size_t i = 0; i < j_values.size(); ++i) + { + for (size_t j = 0; j < j_values.size(); ++j) + { + // check definition + CHECK( (j_values[i] > j_values[j]) == (j_values[j] < j_values[i]) ); + } + } + } + + SECTION("comparison: greater than or equal") + { + for (size_t i = 0; i < j_values.size(); ++i) + { + for (size_t j = 0; j < j_values.size(); ++j) + { + // check definition + CHECK( (j_values[i] >= j_values[j]) == not(j_values[i] < j_values[j]) ); + } + } + } +}