1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
/*
* This source code is released into the public domain.
*/
#include <algorithm>
#include <ranges>
#include <string>
#include <catch2/catch_test_macros.hpp>
import nihil.ucl;
TEST_CASE("ucl: array: construct", "[ucl]")
{
auto arr = nihil::ucl::array<nihil::ucl::integer>();
REQUIRE(arr.size() == 0);
}
TEST_CASE("ucl: array: push_back", "[ucl]")
{
auto arr = nihil::ucl::array<nihil::ucl::integer>();
REQUIRE(arr.size() == 0);
arr.push_back(nihil::ucl::integer(1));
arr.push_back(nihil::ucl::integer(42));
arr.push_back(nihil::ucl::integer(666));
REQUIRE(arr.size() == 3);
REQUIRE(arr[0].value() == 1);
REQUIRE(arr[1].value() == 42);
REQUIRE(arr[2].value() == 666);
REQUIRE_THROWS_AS(arr[3], std::out_of_range);
REQUIRE(arr.front() == 1);
REQUIRE(arr.back() == 666);
}
TEST_CASE("ucl: array: compare", "[ucl]")
{
auto arr = nihil::ucl::array<nihil::ucl::integer>();
arr.push_back(1);
arr.push_back(42);
arr.push_back(666);
auto arr2 = nihil::ucl::array<nihil::ucl::integer>();
REQUIRE(arr != arr2);
arr2.push_back(1);
arr2.push_back(42);
arr2.push_back(666);
REQUIRE(arr == arr2);
auto arr3 = nihil::ucl::array<nihil::ucl::integer>();
arr3.push_back(1);
arr3.push_back(1);
arr3.push_back(1);
REQUIRE(arr != arr3);
}
TEST_CASE("ucl: array: iterator", "[ucl]")
{
auto arr = nihil::ucl::array<nihil::ucl::integer>{1, 42, 666};
auto it = arr.begin();
REQUIRE(*it == 1);
++it;
REQUIRE(*it == 42);
++it;
REQUIRE(*it == 666);
--it;
REQUIRE(*it == 42);
}
TEST_CASE("ucl: array: parse", "[ucl]")
{
using namespace std::literals;
auto input = "value = [1, 42, 666]"sv;
auto obj = nihil::ucl::parse(input);
auto v = obj.lookup("value");
REQUIRE(v);
REQUIRE(v->key() == "value");
auto arr = object_cast<nihil::ucl::array<nihil::ucl::integer>>(*v);
REQUIRE(arr.size() == 3);
REQUIRE(arr[0] == 1);
REQUIRE(arr[1] == 42);
REQUIRE(arr[2] == 666);
}
TEST_CASE("ucl: array: emit", "[ucl]")
{
auto ucl = nihil::ucl::parse("array = [1, 42, 666];");
auto output = std::format("{:c}", ucl);
REQUIRE(output ==
"array [\n"
" 1,\n"
" 42,\n"
" 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});
}
|