RISC-V: Make FRM as global register [PR118103]
[gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / fold_left / 1.cc
blobd5cbc225412702d419d44ba7beee95fad67f5d3d
1 // { dg-do run { target c++23 } }
2 // { dg-add-options no_pch }
4 #include <algorithm>
6 #if __cpp_lib_ranges_fold != 202207L
7 # error "Feature-test macro __cpp_lib_ranges_fold has wrong value in <algorithm>"
8 #endif
10 #include <ranges>
11 #include <testsuite_iterators.h>
13 namespace ranges = std::ranges;
14 namespace views = std::views;
16 constexpr bool
17 test01()
19 int x[] = {1, 2, 3, 4, 5};
20 auto f = [](int&& acc, int& x) {
21 return 2 * acc + 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() );
30 return true;
33 void
34 test02()
36 int x[] = {1, 2, 3, 4, 5};
37 auto f = [](int&& acc, int& x) {
38 return 2 * acc + 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 );
46 rx.bounds.first = x;
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 );
51 rx.bounds.first = x;
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() );
58 constexpr bool
59 test03()
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 );
65 return true;
68 int
69 main()
71 static_assert(test01());
72 test02();
73 static_assert(test03());