xref: /llvm-project/libcxx/test/std/iterators/iterator.container/empty.pass.cpp (revision 5a83710e371fe68a06e6e3876c6a2c8b820a8976)
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <iterator>
11 // template <class C> constexpr auto empty(const C& c) -> decltype(c.empty());       // C++17
12 // template <class T, size_t N> constexpr bool empty(const T (&array)[N]) noexcept;  // C++17
13 // template <class E> constexpr bool empty(initializer_list<E> il) noexcept;         // C++17
14 
15 #if __cplusplus <= 201402L
16 int main () {}
17 #else
18 
19 
20 #include <__config>
21 
22 #include <iterator>
23 #include <cassert>
24 #include <vector>
25 #include <array>
26 #include <list>
27 #include <initializer_list>
28 
29 template<typename C>
30 void test_const_container( const C& c )
31 {
32     assert ( std::empty(c)   == c.empty());
33 }
34 
35 template<typename T>
36 void test_const_container( const std::initializer_list<T>& c )
37 {
38     assert ( std::empty(c)   == (c.size() == 0));
39 }
40 
41 template<typename C>
42 void test_container( C& c )
43 {
44     assert ( std::empty(c)   == c.empty());
45 }
46 
47 template<typename T>
48 void test_container( std::initializer_list<T>& c )
49 {
50     assert ( std::empty(c)   == (c.size() == 0));
51 }
52 
53 template<typename T, size_t Sz>
54 void test_const_array( const T (&array)[Sz] )
55 {
56     assert (!std::empty(array));
57 }
58 
59 int main()
60 {
61     std::vector<int> v; v.push_back(1);
62     std::list<int>   l; l.push_back(2);
63     std::array<int, 1> a; a[0] = 3;
64     std::initializer_list<int> il = { 4 };
65 
66     test_container ( v );
67     test_container ( l );
68     test_container ( a );
69     test_container ( il );
70 
71     test_const_container ( v );
72     test_const_container ( l );
73     test_const_container ( a );
74     test_const_container ( il );
75 
76     static constexpr int arrA [] { 1, 2, 3 };
77     test_const_array ( arrA );
78 }
79 
80 #endif
81