From 429be0c13e16b51b8fc7695c5f3ff65ac057fca7 Mon Sep 17 00:00:00 2001 From: Lexi Winter Date: Sun, 22 Jun 2025 15:34:48 +0100 Subject: nihil.ucl: add a range test for array --- nihil.ucl/tests/array.cc | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) (limited to 'nihil.ucl/tests/array.cc') diff --git a/nihil.ucl/tests/array.cc b/nihil.ucl/tests/array.cc index 687b02c..60cb61d 100644 --- a/nihil.ucl/tests/array.cc +++ b/nihil.ucl/tests/array.cc @@ -2,6 +2,8 @@ * This source code is released into the public domain. */ +#include +#include #include #include @@ -95,11 +97,7 @@ TEST_CASE("ucl: array: parse", "[ucl]") TEST_CASE("ucl: array: emit", "[ucl]") { auto ucl = nihil::ucl::parse("array = [1, 42, 666];"); - - auto output = std::string(); - emit(ucl, nihil::ucl::emitter::configuration, - std::back_inserter(output)); - + auto output = std::format("{:c}", ucl); REQUIRE(output == "array [\n" " 1,\n" @@ -107,3 +105,30 @@ TEST_CASE("ucl: array: emit", "[ucl]") " 666,\n" "]\n"); } + +TEST_CASE("ucl: array is a sized_range", "[ucl]") +{ + auto arr = nihil::ucl::array{1, 42, 666}; + static_assert(std::ranges::sized_range); + + auto size = std::ranges::size(arr); + REQUIRE(size == 3); + + auto begin = std::ranges::begin(arr); + static_assert(std::random_access_iterator); + auto end = std::ranges::end(arr); + static_assert(std::sentinel_for); + + REQUIRE(std::distance(begin, end) == 3); + + auto vec = std::vector(); + std::ranges::copy(arr, std::back_inserter(vec)); + REQUIRE(std::ranges::equal(arr, vec)); + + auto arr_as_ints = + arr | std::views::transform(&nihil::ucl::integer::value); + auto int_vec = std::vector(); + std::ranges::copy(arr_as_ints, std::back_inserter(int_vec)); + REQUIRE(int_vec == std::vector{1, 42, 666}); + +} -- cgit v1.2.3