File: using_iterators.cpp

package info (click to toggle)
tomlplusplus 3.3.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,184 kB
  • sloc: cpp: 35,145; ansic: 2,220; python: 983; makefile: 25; sh: 17
file content (52 lines) | stat: -rw-r--r-- 1,599 bytes parent folder | download
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
#include "tests.h"

TOML_DISABLE_WARNINGS;
#include <algorithm>
TOML_ENABLE_WARNINGS;

TEST_CASE("using iterators")
{
	constexpr auto data = R"(array=[1,"Foo",true]
string="Bar"
number=5)"sv;
	parsing_should_succeed(
		FILE_LINE_ARGS,
		data,
		[](auto&& tbl)
		{
			const auto tbl_begin = tbl.begin();
			const auto tbl_end	 = tbl.end();

			auto count_table_lambda = [tbl_begin, tbl_end](node_type type) noexcept {
				return std::count_if(tbl_begin,
									 tbl_end,
									 [type](const auto& pair) noexcept { return pair.second.type() == type; });
			};

			CHECK(std::distance(tbl_begin, tbl_end) == 3);
			CHECK(count_table_lambda(node_type::table) == 0);
			CHECK(count_table_lambda(node_type::integer) == 1);
			CHECK(count_table_lambda(node_type::string) == 1);
			CHECK(std::next(tbl_begin, 3) == tbl_end);

			const auto arr_iter =
				std::find_if(tbl_begin, tbl_end, [](const auto& pair) noexcept { return pair.second.is_array(); });

			REQUIRE(arr_iter != tbl_end);
			const auto& arr		 = arr_iter->second.as_array();
			const auto arr_begin = arr->begin();
			const auto arr_end	 = arr->end();

			auto count_array_lambda = [arr_begin, arr_end](node_type type) noexcept {
				return std::count_if(arr_begin,
									 arr_end,
									 [type](const auto& node) noexcept { return node.type() == type; });
			};

			CHECK(std::distance(arr_begin, arr_end) == 3);
			CHECK(count_array_lambda(node_type::table) == 0);
			CHECK(count_array_lambda(node_type::integer) == 1);
			CHECK(count_array_lambda(node_type::string) == 1);
			CHECK(std::next(arr_begin, 2) != arr_end);
		});
}