1 // { dg-do run { target c++23 } }
2 // { dg-add-options no_pch }
6 #if __cpp_lib_ranges_fold != 202207L
7 # error "Feature-test macro __cpp_lib_ranges_fold has wrong value in <algorithm>"
11 #include <testsuite_iterators.h>
13 namespace ranges
= std::ranges
;
14 namespace views
= std::views
;
19 int x
[] = {1, 2, 3, 4, 5};
20 auto f
= [](int&& acc
, int& x
) {
23 VERIFY( ranges::fold_left(x
, 0, f
) == 57 );
24 VERIFY( ranges::fold_left(x
, 1, f
) == 89 );
25 VERIFY( ranges::fold_left(x
+0, x
+0, 1, f
) == 1 );
27 VERIFY( ranges::fold_left_first(x
, f
).value() == 57 );
28 VERIFY( !ranges::fold_left_first(x
+0, x
+0, f
).has_value() );
36 int x
[] = {1, 2, 3, 4, 5};
37 auto f
= [](int&& acc
, int& x
) {
41 __gnu_test::test_input_range
<int> rx(x
);
42 ranges::in_value_result ivr
= ranges::fold_left_with_iter(rx
, 0, f
);
43 VERIFY( ivr
.in
== rx
.end() );
44 VERIFY( ivr
.value
== 57 );
47 ranges::in_value_result ivr2
= ranges::fold_left_first_with_iter(rx
, f
);
48 VERIFY( ivr2
.in
== rx
.end() );
49 VERIFY( ivr2
.value
.value() == 57 );
52 auto v
= rx
| views::take(0);
53 ranges::in_value_result ivr3
= ranges::fold_left_first_with_iter(v
, f
);
54 VERIFY( ivr3
.in
== v
.end() );
55 VERIFY( !ivr3
.value
.has_value() );
61 double x
[] = {0.5, 0.25, 0.125, 0.125};
62 VERIFY( ranges::fold_left(x
, 0, std::plus
{}) == 1.0 );
63 VERIFY( ranges::fold_left_with_iter(x
, 0, std::plus
{}).value
== 1.0 );
71 static_assert(test01());
73 static_assert(test03());