1 | //===----------------------------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | // UNSUPPORTED: c++03, c++11, c++14 |
10 | |
11 | // <iterator> |
12 | // template <class C> constexpr auto size(const C& c) -> decltype(c.size()); // C++17 |
13 | // template <class T, size_t N> constexpr size_t size(const T (&array)[N]) noexcept; // C++17 |
14 | |
15 | #include <iterator> |
16 | #include <cassert> |
17 | #include <vector> |
18 | #include <array> |
19 | #include <list> |
20 | #include <initializer_list> |
21 | |
22 | #include "test_macros.h" |
23 | |
24 | #if TEST_STD_VER > 14 |
25 | #include <string_view> |
26 | #endif |
27 | |
28 | |
29 | template<typename C> |
30 | void test_container( C& c) |
31 | { |
32 | // Can't say noexcept here because the container might not be |
33 | assert ( std::size(c) == c.size()); |
34 | } |
35 | |
36 | template<typename C> |
37 | void test_const_container( const C& c ) |
38 | { |
39 | // Can't say noexcept here because the container might not be |
40 | assert ( std::size(c) == c.size()); |
41 | } |
42 | |
43 | template<typename T> |
44 | void test_const_container( const std::initializer_list<T>& c) |
45 | { |
46 | LIBCPP_ASSERT_NOEXCEPT(std::size(c)); // our std::size is conditionally noexcept |
47 | assert ( std::size(c) == c.size()); |
48 | } |
49 | |
50 | template<typename T> |
51 | void test_container( std::initializer_list<T>& c ) |
52 | { |
53 | LIBCPP_ASSERT_NOEXCEPT(std::size(c)); // our std::size is conditionally noexcept |
54 | assert ( std::size(c) == c.size()); |
55 | } |
56 | |
57 | template<typename T, std::size_t Sz> |
58 | void test_const_array( const T (&array)[Sz] ) |
59 | { |
60 | ASSERT_NOEXCEPT(std::size(array)); |
61 | assert ( std::size(array) == Sz ); |
62 | } |
63 | |
64 | int main(int, char**) |
65 | { |
66 | std::vector<int> v; v.push_back(x: 1); |
67 | std::list<int> l; l.push_back(x: 2); |
68 | std::array<int, 1> a; a[0] = 3; |
69 | std::initializer_list<int> il = { 4 }; |
70 | test_container ( c&: v ); |
71 | test_container ( c&: l ); |
72 | test_container ( c&: a ); |
73 | test_container ( c&: il ); |
74 | |
75 | test_const_container ( c: v ); |
76 | test_const_container ( c: l ); |
77 | test_const_container ( c: a ); |
78 | test_const_container ( c: il ); |
79 | |
80 | #if TEST_STD_VER > 14 |
81 | std::string_view sv{"ABC" }; |
82 | test_container ( sv ); |
83 | test_const_container ( sv ); |
84 | #endif |
85 | |
86 | static constexpr int arrA [] { 1, 2, 3 }; |
87 | test_const_array ( array: arrA ); |
88 | |
89 | return 0; |
90 | } |
91 | |