]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/std/ranges/access/size.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / std / ranges / access / size.cc
CommitLineData
8d9254fc 1// Copyright (C) 2019-2020 Free Software Foundation, Inc.
6d0dff49
JW
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
328b52d6 21#include <ranges>
6d0dff49
JW
22#include <testsuite_hooks.h>
23#include <testsuite_iterators.h>
24
25void
26test01()
27{
28 constexpr int a[10] = { };
29 static_assert( std::ranges::size(a) == 10 );
30 static_assert( noexcept(std::ranges::size(a)) );
31
32 int a2[2];
33 VERIFY( std::ranges::size(a2) == 2);
34 static_assert( noexcept(std::ranges::size(a2)) );
beaecb2d
JW
35
36 struct Incomplete;
37 using A = Incomplete[2]; // bounded array of incomplete type
38 extern A& f();
39 static_assert( std::same_as<decltype(std::ranges::size(f())), std::size_t> );
6d0dff49
JW
40}
41
42void
43test02()
44{
45 struct R
46 {
47 int size() { return 1; }
48 long size() const noexcept { return 2; }
49 };
50 R r;
51 const R& c = r;
52 VERIFY( std::ranges::size(r) == 1 );
53 static_assert( !noexcept(std::ranges::size(r)) );
54 VERIFY( std::ranges::size(c) == 2L );
55 static_assert( noexcept(std::ranges::size(c)) );
56
57 int a[3] = { };
58 __gnu_test::test_sized_range<int, __gnu_test::input_iterator_wrapper> ri(a);
59 VERIFY( std::ranges::size(ri) == 3 );
60 static_assert( noexcept(std::ranges::size(ri)) );
61}
62
63struct R3
64{
65 int* size() { return nullptr; }
66 friend int size(R3&) noexcept { return 1; }
67 friend long size(const R3&) { return 2L; }
68 friend unsigned int size(R3&&) { return 3U; }
69 friend unsigned long size(const R3&&) noexcept { return 4UL; }
70};
71
72void
73test03()
74{
75 R3 r;
76 const R3& c = r;
77 VERIFY( std::ranges::size(r) == 1 );
78 static_assert( noexcept(std::ranges::size(r)) );
79 VERIFY( std::ranges::size(std::move(r)) == 3U );
80 static_assert( !noexcept(std::ranges::size(std::move(r))) );
81 VERIFY( std::ranges::size(c) == 2L );
82 static_assert( !noexcept(std::ranges::size(c)) );
83 VERIFY( std::ranges::size(std::move(c)) == 4UL );
84 static_assert( noexcept(std::ranges::size(std::move(c))) );
85}
86
87void
88test04()
89{
90 int a[] = { 0, 1 };
91 __gnu_test::test_range<int, __gnu_test::random_access_iterator_wrapper> r(a);
beaecb2d 92 VERIFY( std::ranges::size(r) == unsigned(std::ranges::end(r) - std::ranges::begin(r)) );
6d0dff49
JW
93}
94
95struct R5
96{
97 int size() const noexcept { return 0; }
98 R5* begin() { return this; }
99 R5* end() { return this + 1; }
100};
101
102template<>
103constexpr bool std::ranges::disable_sized_range<R5> = true;
104
105void
106test05()
107{
108 R5 r;
109 VERIFY( std::ranges::size(r) == 1 );
110}
111
112int
113main()
114{
115 test01();
116 test02();
117 test03();
118 test04();
119 test05();
120}