xref: /llvm-project/libcxx/test/std/iterators/iterator.container/empty.pass.cpp (revision 5f8d84ec924900b337f151f7fc5a77620f25a932)
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 // UNSUPPORTED: c++98, c++03, c++11, c++14
11 
12 // <iterator>
13 // template <class C> constexpr auto empty(const C& c) -> decltype(c.empty());       // C++17
14 // template <class T, size_t N> constexpr bool empty(const T (&array)[N]) noexcept;  // C++17
15 // template <class E> constexpr bool empty(initializer_list<E> il) noexcept;         // C++17
16 
17 #include <iterator>
18 #include <cassert>
19 #include <vector>
20 #include <array>
21 #include <list>
22 #include <initializer_list>
23 
24 #include "test_macros.h"
25 
26 template<typename C>
27 void test_const_container( const C& c )
28 {
29 //  Can't say noexcept here because the container might not be
30     assert ( std::empty(c)   == c.empty());
31 }
32 
33 template<typename T>
34 void test_const_container( const std::initializer_list<T>& c )
35 {
36     assert ( std::empty(c)   == (c.size() == 0));
37 }
38 
39 template<typename C>
40 void test_container( C& c )
41 {
42 //  Can't say noexcept here because the container might not be
43     assert ( std::empty(c)   == c.empty());
44 }
45 
46 template<typename T>
47 void test_container( std::initializer_list<T>& c )
48 {
49     ASSERT_NOEXCEPT(std::empty(c));
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_NOEXCEPT(std::empty(array));
57     assert (!std::empty(array));
58 }
59 
60 int main()
61 {
62     std::vector<int> v; v.push_back(1);
63     std::list<int>   l; l.push_back(2);
64     std::array<int, 1> a; a[0] = 3;
65     std::initializer_list<int> il = { 4 };
66 
67     test_container ( v );
68     test_container ( l );
69     test_container ( a );
70     test_container ( il );
71 
72     test_const_container ( v );
73     test_const_container ( l );
74     test_const_container ( a );
75     test_const_container ( il );
76 
77     static constexpr int arrA [] { 1, 2, 3 };
78     test_const_array ( arrA );
79 }
80