From 639b270eed81f7c2627d810057d188e2e8ee67f9 Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Sun, 22 Jun 2025 18:18:44 +0100 Subject: nihil.ucl: improve construction and comparison --- nihil.ucl/tests/array.cc | 28 +++++++------- nihil.ucl/tests/boolean.cc | 27 ++++++++++--- nihil.ucl/tests/integer.cc | 28 +++++++++++--- nihil.ucl/tests/real.cc | 28 +++++++++++--- nihil.ucl/tests/string.cc | 96 ++++++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 173 insertions(+), 34 deletions(-) (limited to 'nihil.ucl/tests') diff --git a/nihil.ucl/tests/array.cc b/nihil.ucl/tests/array.cc index a4f26b6..ce86058 100644 --- a/nihil.ucl/tests/array.cc +++ b/nihil.ucl/tests/array.cc @@ -31,9 +31,9 @@ TEST_CASE("ucl: array: push_back", "[ucl]") arr.push_back(integer(666)); REQUIRE(arr.size() == 3); - REQUIRE(arr[0].value() == 1); - REQUIRE(arr[1].value() == 42); - REQUIRE(arr[2].value() == 666); + REQUIRE(arr[0] == 1); + REQUIRE(arr[1] == 42); + REQUIRE(arr[2] == 666); REQUIRE_THROWS_AS(arr[3], std::out_of_range); @@ -46,22 +46,22 @@ TEST_CASE("ucl: array: compare", "[ucl]") using namespace nihil::ucl; auto arr = array(); - arr.push_back(1); - arr.push_back(42); - arr.push_back(666); + arr.push_back(integer(1)); + arr.push_back(integer(42)); + arr.push_back(integer(666)); auto arr2 = array(); REQUIRE(arr != arr2); - arr2.push_back(1); - arr2.push_back(42); - arr2.push_back(666); + arr2.push_back(integer(1)); + arr2.push_back(integer(42)); + arr2.push_back(integer(666)); REQUIRE(arr == arr2); auto arr3 = array(); - arr3.push_back(1); - arr3.push_back(1); - arr3.push_back(1); + arr3.push_back(integer(1)); + arr3.push_back(integer(1)); + arr3.push_back(integer(1)); REQUIRE(arr != arr3); } @@ -69,7 +69,7 @@ TEST_CASE("ucl: array: iterator", "[ucl]") { using namespace nihil::ucl; - auto arr = array{1, 42, 666}; + auto arr = array{integer(1), integer(42), integer(666)}; auto it = arr.begin(); REQUIRE(*it == 1); @@ -129,7 +129,7 @@ TEST_CASE("ucl: array is a sized_range", "[ucl]") { using namespace nihil::ucl; - auto arr = array{1, 42, 666}; + auto arr = array{integer(1), integer(42), integer(666)}; static_assert(std::ranges::sized_range); auto size = std::ranges::size(arr); diff --git a/nihil.ucl/tests/boolean.cc b/nihil.ucl/tests/boolean.cc index c023498..b0b3b58 100644 --- a/nihil.ucl/tests/boolean.cc +++ b/nihil.ucl/tests/boolean.cc @@ -10,19 +10,36 @@ import nihil.ucl; TEST_CASE("ucl: boolean: construct", "[ucl]") { - auto obj = nihil::ucl::boolean(true); - REQUIRE(object_cast(obj).value() == true); + auto b = nihil::ucl::boolean(true); + REQUIRE(b == true); } -TEST_CASE("ucl: boolean: compare", "[ucl]") +TEST_CASE("ucl: boolean: default construct", "[ucl]") +{ + auto b = nihil::ucl::boolean(); + REQUIRE(b == false); +} + +TEST_CASE("ucl: boolean: operator==", "[ucl]") { auto b = nihil::ucl::boolean(true); - REQUIRE(b == nihil::ucl::boolean(true)); REQUIRE(b == true); + REQUIRE(b == nihil::ucl::boolean(true)); - REQUIRE(b != nihil::ucl::boolean(false)); REQUIRE(b != false); + REQUIRE(b != nihil::ucl::boolean(false)); +} + +TEST_CASE("ucl: boolean: operator<=>", "[ucl]") +{ + auto b = nihil::ucl::boolean(false); + + REQUIRE(b < true); + REQUIRE(b < nihil::ucl::boolean(true)); + + REQUIRE(b >= false); + REQUIRE(b >= nihil::ucl::boolean(false)); } TEST_CASE("ucl: boolean: parse", "[ucl]") diff --git a/nihil.ucl/tests/integer.cc b/nihil.ucl/tests/integer.cc index da119e6..c7db851 100644 --- a/nihil.ucl/tests/integer.cc +++ b/nihil.ucl/tests/integer.cc @@ -10,20 +10,36 @@ import nihil.ucl; TEST_CASE("ucl: integer: construct", "[ucl]") { - auto obj = nihil::ucl::integer(42); - REQUIRE(object_cast(obj).value() == 42); + auto i = nihil::ucl::integer(42); + REQUIRE(i == 42); } -TEST_CASE("ucl: integer: compare", "[ucl]") +TEST_CASE("ucl: integer: default construct", "[ucl]") +{ + auto i = nihil::ucl::integer(); + REQUIRE(i == 0); +} + +TEST_CASE("ucl: integer: operator==", "[ucl]") { auto i = nihil::ucl::integer(42); + REQUIRE(i == 42); REQUIRE(i == nihil::ucl::integer(42)); + + REQUIRE(i != 1); REQUIRE(i != nihil::ucl::integer(1)); - REQUIRE((i == 42) == true); +} + +TEST_CASE("ucl: integer: operator<=>", "[ucl]") +{ + auto i = nihil::ucl::integer(42); + + REQUIRE(i < 43); + REQUIRE(i < nihil::ucl::integer(43)); - REQUIRE(i > nihil::ucl::integer(40)); - REQUIRE(i > 40); + REQUIRE(i > 1); + REQUIRE(i > nihil::ucl::integer(1)); } TEST_CASE("ucl: parse: integer", "[ucl]") diff --git a/nihil.ucl/tests/real.cc b/nihil.ucl/tests/real.cc index 275684a..d97e767 100644 --- a/nihil.ucl/tests/real.cc +++ b/nihil.ucl/tests/real.cc @@ -16,16 +16,32 @@ TEST_CASE("ucl: real: construct", "[ucl]") Catch::Matchers::WithinRel(42.1)); } -TEST_CASE("ucl: real: compare", "[ucl]") +TEST_CASE("ucl: real: default construct", "[ucl]") { - auto i = nihil::ucl::real(42); + auto i = nihil::ucl::real(); + REQUIRE(i == 0); +} + +TEST_CASE("ucl: real: operator==", "[ucl]") +{ + auto i = nihil::ucl::real(42.5); + + REQUIRE(i == 42.5); + REQUIRE(i == nihil::ucl::real(42.5)); - REQUIRE(i == nihil::ucl::real(42)); + REQUIRE(i != 1); REQUIRE(i != nihil::ucl::real(1)); - REQUIRE((i == 42) == true); +} + +TEST_CASE("ucl: real: operator<=>", "[ucl]") +{ + auto i = nihil::ucl::real(42.5); + + REQUIRE(i < 43); + REQUIRE(i < nihil::ucl::real(43)); - REQUIRE(i > nihil::ucl::real(40)); - REQUIRE(i > 40); + REQUIRE(i > 1); + REQUIRE(i > nihil::ucl::real(1)); } TEST_CASE("ucl: real: parse", "[ucl]") diff --git a/nihil.ucl/tests/string.cc b/nihil.ucl/tests/string.cc index 4385dbb..b702b51 100644 --- a/nihil.ucl/tests/string.cc +++ b/nihil.ucl/tests/string.cc @@ -2,16 +2,106 @@ * This source code is released into the public domain. */ +#include #include +#include #include +#include import nihil.ucl; -TEST_CASE("ucl: string: construct", "[ucl]") +TEST_CASE("ucl: string: invariants", "[ucl]") { - auto obj = nihil::ucl::string("testing"); - REQUIRE(object_cast(obj).value() == "testing"); + using namespace nihil::ucl; + + static_assert(std::same_as); + REQUIRE(string::ucl_type == object_type::string); + REQUIRE(static_cast<::ucl_type>(string::ucl_type) == UCL_STRING); +} + +TEST_CASE("ucl: string: default construct", "[ucl]") +{ + auto str = nihil::ucl::string(); + REQUIRE(str == ""); +} + +TEST_CASE("ucl: string: construct from string literal", "[ucl]") +{ + auto str = nihil::ucl::string("testing"); + REQUIRE(str == "testing"); +} + +TEST_CASE("ucl: string: construct from std::string", "[ucl]") +{ + auto str = nihil::ucl::string(std::string("testing")); + REQUIRE(str == "testing"); +} + +TEST_CASE("ucl: string: construct from std::string_view", "[ucl]") +{ + auto str = nihil::ucl::string(std::string_view("testing")); + REQUIRE(str == "testing"); +} + +TEST_CASE("ucl: string: construct from contiguous range", "[ucl]") +{ + auto s = std::vector{'t', 'e', 's', 't', 'i', 'n', 'g'}; + auto str = nihil::ucl::string(std::from_range, s); + REQUIRE(str == "testing"); +} + +TEST_CASE("ucl: string: construct from non-contiguous range", "[ucl]") +{ + auto s = std::list{'t', 'e', 's', 't', 'i', 'n', 'g'}; + auto str = nihil::ucl::string(std::from_range, s); + REQUIRE(str == "testing"); +} + +TEST_CASE("ucl: string: construct from contiguous iterator", "[ucl]") +{ + auto s = std::vector{'t', 'e', 's', 't', 'i', 'n', 'g'}; + auto str = nihil::ucl::string(std::ranges::begin(s), + std::ranges::end(s)); + REQUIRE(str == "testing"); +} + +TEST_CASE("ucl: string: construct from non-contiguous iterator", "[ucl]") +{ + auto s = std::list{'t', 'e', 's', 't', 'i', 'n', 'g'}; + auto str = nihil::ucl::string(std::ranges::begin(s), + std::ranges::end(s)); + REQUIRE(str == "testing"); +} + +TEST_CASE("ucl: string: operator==", "[ucl]") +{ + auto str = nihil::ucl::string("testing"); + + REQUIRE(str == nihil::ucl::string("testing")); + REQUIRE(str == std::string_view("testing")); + REQUIRE(str == std::string("testing")); + REQUIRE(str == "testing"); + + REQUIRE(str != nihil::ucl::string("test")); + REQUIRE(str != std::string_view("test")); + REQUIRE(str != std::string("test")); + REQUIRE(str != "test"); +} + +TEST_CASE("ucl: string: operator<=>", "[ucl]") +{ + auto str = nihil::ucl::string("testing"); + + REQUIRE(str < nihil::ucl::string("zzz")); + REQUIRE(str < std::string_view("zzz")); + REQUIRE(str < std::string("zzz")); + REQUIRE(str < "zzz"); + + REQUIRE(str > nihil::ucl::string("aaa")); + REQUIRE(str > std::string_view("aaa")); + REQUIRE(str > std::string("aaa")); + REQUIRE(str > "aaa"); } TEST_CASE("ucl: string: parse", "[ucl]") -- cgit v1.2.3