aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.ucl/tests/array.cc
diff options
context:
space:
mode:
authorLexi Winter <lexi@le-fay.org>2025-06-22 15:34:48 +0100
committerLexi Winter <lexi@le-fay.org>2025-06-22 15:34:48 +0100
commit429be0c13e16b51b8fc7695c5f3ff65ac057fca7 (patch)
treeffe19573dbafa2e49680ea0395a63690eb721efd /nihil.ucl/tests/array.cc
parenta14cb70cb715beb714e7b05d8fe631c0df4a7053 (diff)
downloadnihil-429be0c13e16b51b8fc7695c5f3ff65ac057fca7.tar.gz
nihil-429be0c13e16b51b8fc7695c5f3ff65ac057fca7.tar.bz2
nihil.ucl: add a range test for array
Diffstat (limited to 'nihil.ucl/tests/array.cc')
-rw-r--r--nihil.ucl/tests/array.cc35
1 files changed, 30 insertions, 5 deletions
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 <algorithm>
+#include <ranges>
#include <string>
#include <catch2/catch_test_macros.hpp>
@@ -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<nihil::ucl::integer>{1, 42, 666};
+ static_assert(std::ranges::sized_range<decltype(arr)>);
+
+ auto size = std::ranges::size(arr);
+ REQUIRE(size == 3);
+
+ auto begin = std::ranges::begin(arr);
+ static_assert(std::random_access_iterator<decltype(begin)>);
+ auto end = std::ranges::end(arr);
+ static_assert(std::sentinel_for<decltype(end), decltype(begin)>);
+
+ REQUIRE(std::distance(begin, end) == 3);
+
+ auto vec = std::vector<nihil::ucl::integer>();
+ 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<nihil::ucl::integer::value_type>();
+ std::ranges::copy(arr_as_ints, std::back_inserter(int_vec));
+ REQUIRE(int_vec == std::vector<std::int64_t>{1, 42, 666});
+
+}