1 // Copyright (C) 2019-2021 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library.  This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3.  If not see
16 // <http://www.gnu.org/licenses/>.
17 
18 // { dg-options "-std=gnu++2a" }
19 // { dg-do run { target c++2a } }
20 
21 #include <ranges>
22 #include <testsuite_hooks.h>
23 #include <testsuite_iterators.h>
24 
25 using std::same_as;
26 
27 void
test01()28 test01()
29 {
30   struct R
31   {
32     constexpr int empty() const & { return 0; }
33     constexpr const void* empty() const && { return this; }
34   };
35   constexpr R r;
36   static_assert( !std::ranges::empty(r) );
37   static_assert( same_as<decltype(std::ranges::empty(r)), bool> );
38   // PR libstdc++/100824
39   // ranges::empty should treat the subexpression as an lvalue
40   static_assert( !std::ranges::empty(std::move(r)) );
41   static_assert( same_as<decltype(std::ranges::empty(std::move(r))), bool> );
42 }
43 
44 void
test02()45 test02()
46 {
47   using __gnu_test::test_range;
48   using __gnu_test::test_sized_range;
49   using __gnu_test::random_access_iterator_wrapper;
50   using __gnu_test::forward_iterator_wrapper;
51   using __gnu_test::input_iterator_wrapper;
52   using __gnu_test::output_iterator_wrapper;
53 
54   int a[] = { 0, 1 };
55   VERIFY( !std::ranges::empty(a) );
56 
57   test_range<int, random_access_iterator_wrapper> r(a);
58   VERIFY( !std::ranges::empty(r) );
59 
60   test_range<int, forward_iterator_wrapper> i(a);
61   VERIFY( !std::ranges::empty(i) );
62 
63   test_sized_range<int, random_access_iterator_wrapper> sr(a);
64   VERIFY( !std::ranges::empty(sr) );
65 
66   test_sized_range<int, input_iterator_wrapper> si(a);
67   VERIFY( !std::ranges::empty(si) );
68 
69   test_sized_range<int, output_iterator_wrapper> so(a);
70   VERIFY( !std::ranges::empty(so) );
71 }
72 
73 void
test03()74 test03()
75 {
76   // PR libstdc++/100824
77   // ranges::empty should treat the subexpression as an lvalue
78 
79   struct R
80   {
81     constexpr bool empty() & { return true; }
82   };
83   static_assert( std::ranges::empty(R{}) );
84 
85   struct R2
86   {
87     constexpr unsigned size() & { return 0; }
88   };
89   static_assert( std::ranges::empty(R2{}) );
90 }
91 
92 void
test04()93 test04()
94 {
95   struct E1
96   {
97     bool empty() const noexcept { return {}; }
98   };
99 
100   static_assert( noexcept(std::ranges::empty(E1{})) );
101 
102   struct E2
103   {
104     bool empty() const noexcept(false) { return {}; }
105   };
106 
107   static_assert( ! noexcept(std::ranges::empty(E2{})) );
108 
109   struct E3
110   {
111     struct B
112     {
113       explicit operator bool() const noexcept(false) { return true; }
114     };
115 
116     B empty() const noexcept { return {}; }
117   };
118 
119   static_assert( ! noexcept(std::ranges::empty(E3{})) );
120 }
121 
122 int
main()123 main()
124 {
125   test01();
126   test02();
127   test03();
128   test04();
129 }
130