Home | History | Annotate | Download | only in iterator.container
      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 #if TEST_STD_VER > 14
     27 #include <string_view>
     28 #endif
     29 
     30 template<typename C>
     31 void test_const_container( const C& c )
     32 {
     33 //  Can't say noexcept here because the container might not be
     34     assert ( std::empty(c)   == c.empty());
     35 }
     36 
     37 template<typename T>
     38 void test_const_container( const std::initializer_list<T>& c )
     39 {
     40     assert ( std::empty(c)   == (c.size() == 0));
     41 }
     42 
     43 template<typename C>
     44 void test_container( C& c )
     45 {
     46 //  Can't say noexcept here because the container might not be
     47     assert ( std::empty(c)   == c.empty());
     48 }
     49 
     50 template<typename T>
     51 void test_container( std::initializer_list<T>& c )
     52 {
     53     ASSERT_NOEXCEPT(std::empty(c));
     54     assert ( std::empty(c)   == (c.size() == 0));
     55 }
     56 
     57 template<typename T, size_t Sz>
     58 void test_const_array( const T (&array)[Sz] )
     59 {
     60     ASSERT_NOEXCEPT(std::empty(array));
     61     assert (!std::empty(array));
     62 }
     63 
     64 int main()
     65 {
     66     std::vector<int> v; v.push_back(1);
     67     std::list<int>   l; l.push_back(2);
     68     std::array<int, 1> a; a[0] = 3;
     69     std::initializer_list<int> il = { 4 };
     70 
     71     test_container ( v );
     72     test_container ( l );
     73     test_container ( a );
     74     test_container ( il );
     75 
     76     test_const_container ( v );
     77     test_const_container ( l );
     78     test_const_container ( a );
     79     test_const_container ( il );
     80 
     81 #if TEST_STD_VER > 14
     82     std::string_view sv{"ABC"};
     83     test_container ( sv );
     84     test_const_container ( sv );
     85 #endif
     86 
     87     static constexpr int arrA [] { 1, 2, 3 };
     88     test_const_array ( arrA );
     89 }
     90