aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.ucl/tests/array.cc
blob: 866fa45193a187b0b800d24fdb2247ca8f90f5e8 (plain) (blame)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
 * This source code is released into the public domain.
 */

#include <algorithm>
#include <concepts>
#include <expected>
#include <ranges>
#include <string>

#include <catch2/catch_test_macros.hpp>
#include <ucl.h>

import nihil.ucl;

TEST_CASE("ucl: array: invariants", "[ucl]")
{
	using namespace nihil::ucl;

	REQUIRE(array<>::ucl_type == object_type::array);
	REQUIRE(static_cast<::ucl_type>(array<>::ucl_type) == UCL_ARRAY);

	static_assert(std::destructible<array<>>);
	static_assert(std::default_initializable<array<>>);
	static_assert(std::move_constructible<array<>>);
	static_assert(std::copy_constructible<array<>>);
	static_assert(std::equality_comparable<array<>>);
	static_assert(std::totally_ordered<array<>>);
	static_assert(std::swappable<array<>>);
	
	static_assert(std::ranges::sized_range<array<integer>>);
	static_assert(std::same_as<std::ranges::range_value_t<array<integer>>,
				   integer>);
}

TEST_CASE("ucl: array: constructor", "[ucl]")
{
	using namespace nihil::ucl;

	SECTION("default") {
		auto arr = array<integer>();
		REQUIRE(arr.size() == 0);
		REQUIRE(str(arr.type()) == "array");
	}

	SECTION("from range") {
		auto vec = std::vector{integer(1), integer(42)};
		auto arr = array<integer>(std::from_range, vec);

		REQUIRE(arr.size() == 2);
		REQUIRE(arr[0] == 1);
		REQUIRE(arr[1] == 42);
	}

	SECTION("from iterator pair") {
		auto vec = std::vector{integer(1), integer(42)};
		auto arr = array<integer>(std::ranges::begin(vec),
					  std::ranges::end(vec));

		REQUIRE(arr.size() == 2);
		REQUIRE(arr[0] == 1);
		REQUIRE(arr[1] == 42);
	}

	SECTION("from initializer_list") {
		auto arr = array<integer>{integer(1), integer(42)};

		REQUIRE(arr.size() == 2);
		REQUIRE(arr[0] == 1);
		REQUIRE(arr[1] == 42);
	}
}

TEST_CASE("ucl: array: construct from UCL object", "[ucl]")
{
	using namespace nihil::ucl;

	SECTION("ref, correct type") {
		auto uarr = ::ucl_object_typed_new(UCL_ARRAY);
		auto uint = ::ucl_object_fromint(42);
		::ucl_array_append(uarr, uint);

		auto arr = array<integer>(ref, uarr);
		REQUIRE(arr[0] == 42);

		::ucl_object_unref(uarr);
	}

	SECTION("noref, correct type") {
		auto uarr = ::ucl_object_typed_new(UCL_ARRAY);
		auto uint = ::ucl_object_fromint(42);
		::ucl_array_append(uarr, uint);

		auto arr = array<integer>(noref, uarr);
		REQUIRE(arr[0] == 42);
	}

	SECTION("ref, wrong element type") {
		auto uarr = ::ucl_object_typed_new(UCL_ARRAY);
		auto uint = ::ucl_object_frombool(true);
		::ucl_array_append(uarr, uint);

		auto arr = array<integer>(noref, uarr);
		REQUIRE_THROWS_AS(arr[0], type_mismatch);
	}

	SECTION("ref, wrong type") {
		auto uobj = ::ucl_object_frombool(true);

		REQUIRE_THROWS_AS(array(ref, uobj), type_mismatch);

		::ucl_object_unref(uobj);
	}

	SECTION("noref, wrong type") {
		auto uobj = ::ucl_object_frombool(true);

		REQUIRE_THROWS_AS(array(noref, uobj), type_mismatch);

		::ucl_object_unref(uobj);
	}
}

TEST_CASE("ucl: array: swap", "[ucl]")
{
	// do not add using namespace nihil::ucl

	auto arr1 = nihil::ucl::array<nihil::ucl::integer>{
		nihil::ucl::integer(1),
		nihil::ucl::integer(2)
	};

	auto arr2 = nihil::ucl::array<nihil::ucl::integer>{
		nihil::ucl::integer(3),
	};

	swap(arr1, arr2);

	REQUIRE(arr1.size() == 1);
	REQUIRE(arr1[0] == 3);

	REQUIRE(arr2.size() == 2);
	REQUIRE(arr2[0] == 1);
}

TEST_CASE("ucl: array: push_back", "[ucl]")
{
	using namespace nihil::ucl;

	auto arr = array<integer>();
	REQUIRE(arr.size() == 0);

	arr.push_back(integer(1));
	arr.push_back(integer(42));
	arr.push_back(integer(666));

	REQUIRE(arr.size() == 3);
	REQUIRE(arr[0] == 1);
	REQUIRE(arr[1] == 42);
	REQUIRE(arr[2] == 666);

	REQUIRE_THROWS_AS(arr[3], std::out_of_range);

	REQUIRE(arr.front() == 1);
	REQUIRE(arr.back() == 666);
}

TEST_CASE("ucl: array: compare", "[ucl]")
{
	using namespace nihil::ucl;

	auto arr = array<integer>{
		integer(1), integer(42), integer(666)
	};

	auto arr2 = array<integer>();
	REQUIRE(arr != arr2);

	arr2.push_back(integer(1));
	arr2.push_back(integer(42));
	arr2.push_back(integer(666));
	REQUIRE(arr == arr2);

	auto arr3 = array<integer>{
		integer(1), integer(1), integer(1)
	};

	REQUIRE(arr != arr3);
}

TEST_CASE("ucl: array: iterator", "[ucl]")
{
	using namespace nihil::ucl;

	auto arr = array<integer>{integer(1), integer(42), integer(666)};

	auto it = arr.begin();
	REQUIRE(*it == 1);
	auto end = arr.end();
	REQUIRE(it != end);
	REQUIRE(it < end);

	++it;
	REQUIRE(*it == 42);

	++it;
	REQUIRE(*it == 666);

	--it;
	REQUIRE(*it == 42);

	++it;
	REQUIRE(it != end);
	++it;
	REQUIRE(it == end);
}

TEST_CASE("ucl: array: parse", "[ucl]")
{
	using namespace std::literals;
	using namespace nihil::ucl;

	auto obj = parse("value = [1, 42, 666]"sv).value();

	auto arr = object_cast<array<integer>>(obj["value"]).value();

	REQUIRE(arr.size() == 3);
	REQUIRE(arr[0] == 1);
	REQUIRE(arr[1] == 42);
	REQUIRE(arr[2] == 666);
}

TEST_CASE("ucl: array: emit", "[ucl]")
{
	using namespace nihil::ucl;

	auto ucl = parse("array = [1, 42, 666];").value();

	auto output = std::format("{:c}", ucl);
	REQUIRE(output == 
"array [\n"
"    1,\n"
"    42,\n"
"    666,\n"
"]\n");
}

TEST_CASE("ucl: array: format", "[ucl]")
{
	using namespace nihil::ucl;

	SECTION("empty array") {
		auto arr = array<integer>();
		REQUIRE(std::format("{}", arr) == "[]");
	}

	SECTION("bare array") {
		auto arr = array<integer>{
			integer(1), integer(42), integer(666)
		};

		auto output = std::format("{}", arr);
		REQUIRE(output == "[1, 42, 666]");
	}

	SECTION("parsed array") {
		auto ucl = parse("array = [1, 42, 666];").value();
		auto arr = object_cast<array<integer>>(ucl["array"]).value();

		auto output = std::format("{}", arr);
		REQUIRE(output == "[1, 42, 666]");
	}
}

TEST_CASE("ucl: array: print to ostream", "[ucl]")
{
	using namespace nihil::ucl;

	SECTION("empty array") {
		auto arr = array<integer>();
		auto strm = std::ostringstream();
		strm << arr;

		REQUIRE(strm.str() == "[]");
	}

	SECTION("bare array") {
		auto arr = array<integer>{
			integer(1), integer(42), integer(666)
		};
		auto strm = std::ostringstream();
		strm << arr;

		REQUIRE(strm.str() == "[1, 42, 666]");
	}

	SECTION("parsed array") {
		auto ucl = parse("array = [1, 42, 666];").value();
		auto arr = object_cast<array<integer>>(ucl["array"]).value();
		auto strm = std::ostringstream();
		strm << arr;

		REQUIRE(strm.str() == "[1, 42, 666]");
	}
}

TEST_CASE("ucl: array is a sized_range", "[ucl]")
{
	using namespace nihil::ucl;

	auto arr = array<integer>{integer(1), integer(42), integer(666)};

	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<integer>();
	std::ranges::copy(arr, std::back_inserter(vec));
	REQUIRE(std::ranges::equal(arr, vec));

	auto arr_as_ints =
		arr | std::views::transform(&integer::value);
	auto int_vec = std::vector<integer::contained_type>();
	std::ranges::copy(arr_as_ints, std::back_inserter(int_vec));
	REQUIRE(int_vec == std::vector<std::int64_t>{1, 42, 666});

}

TEST_CASE("ucl: array: bad object_cast", "[ucl]")
{
	using namespace nihil::ucl;

	auto arr = array<integer>();

	auto cast_ok = object_cast<integer>(arr);
	REQUIRE(!cast_ok);
}

TEST_CASE("ucl: array: heterogeneous elements", "[ucl]")
{
	using namespace std::literals;
	using namespace nihil::ucl;

	auto obj_err = parse("array [ 42, true, \"test\" ];");
	REQUIRE(obj_err);
	auto obj = *obj_err;

	auto err = object_cast<array<>>(obj["array"]);
	REQUIRE(err);

	auto arr = *err;
	REQUIRE(arr.size() == 3);

	auto int_obj = object_cast<integer>(arr[0]);
	REQUIRE(int_obj);
	REQUIRE(*int_obj == 42);

	auto bool_obj = object_cast<boolean>(arr[1]);
	REQUIRE(bool_obj);
	REQUIRE(*bool_obj == true);

	auto string_obj = object_cast<string>(arr[2]);
	REQUIRE(string_obj);
	REQUIRE(*string_obj == "test");
}

TEST_CASE("ucl: array: heterogenous cast", "[ucl]")
{
	using namespace nihil::ucl;

	auto arr = array<>();
	arr.push_back(integer(42));
	arr.push_back(boolean(true));

	// Converting to an array<integer> should fail.
	auto cast_ok = object_cast<array<integer>>(arr);
	REQUIRE(!cast_ok);

	// Converting to array<object> should succeed.
	auto err = object_cast<array<object>>(arr);
	REQUIRE(err);

	auto obj_arr = *err;
	REQUIRE(obj_arr[0] == integer(42));
}

TEST_CASE("ucl: array: homogeneous cast", "[ucl]")
{
	using namespace nihil::ucl;

	auto arr = array<>();
	arr.push_back(integer(1));
	arr.push_back(integer(42));

	auto obj = object(ref, arr.get_ucl_object());

	// Converting to array<string> should fail.
	auto cast_ok = object_cast<array<string>>(obj);
	REQUIRE(!cast_ok);

	// Converting to an array<integer> should succeed.
	auto err = object_cast<array<integer>>(obj);
	REQUIRE(err);

	auto obj_arr = *err;
	REQUIRE(obj_arr[0] == 1);
	REQUIRE(obj_arr[1] == 42);
}

TEST_CASE("array iterator: empty iterator", "[ucl]")
{
	using namespace nihil::ucl;

	auto it = array_iterator<integer>();

	REQUIRE_THROWS_AS(*it, std::logic_error);
	REQUIRE_THROWS_AS(it[0], std::logic_error);
	REQUIRE_THROWS_AS(it++, std::logic_error);
	REQUIRE_THROWS_AS(++it, std::logic_error);

	auto it2 = array_iterator<integer>();
	REQUIRE(it == it2);
	REQUIRE((it < it2) == false);
	REQUIRE((it > it2) == false);
}

TEST_CASE("array iterator: invalid operations", "[ucl]")
{
	using namespace nihil::ucl;

	auto arr = array<integer>{ integer(42) };
	auto it = arr.begin();

	SECTION("decrement before start") {
		REQUIRE_THROWS_AS(--it, std::logic_error);
		REQUIRE_THROWS_AS(it--, std::logic_error);
		REQUIRE_THROWS_AS(it - 1, std::logic_error);
	}

	SECTION("increment past end") {
		++it;
		REQUIRE(it == arr.end());

		REQUIRE_THROWS_AS(++it, std::logic_error);
		REQUIRE_THROWS_AS(it++, std::logic_error);
		REQUIRE_THROWS_AS(it + 1, std::logic_error);
	}

	SECTION("dereference iterator at end") {
		REQUIRE_THROWS_AS(it[1], std::logic_error);

		++it;
		REQUIRE(it == arr.end());

		REQUIRE_THROWS_AS(*it, std::logic_error);
	}

	SECTION("compare with different array") {
		auto arr2 = array<integer>{ integer(42) };
		REQUIRE_THROWS_AS(it == arr2.begin(), std::logic_error);
		REQUIRE_THROWS_AS(it > arr2.begin(), std::logic_error);
		REQUIRE_THROWS_AS(it - arr2.begin(), std::logic_error);
	}

	SECTION("compare with empty iterator") {
		auto it2 = array_iterator<integer>();
		REQUIRE_THROWS_AS(it == it2, std::logic_error);
		REQUIRE_THROWS_AS(it > it2, std::logic_error);
		REQUIRE_THROWS_AS(it - it2, std::logic_error);
	}
}