91 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++1410 11// <iterator>12// template <class C> constexpr auto empty(const C& c) -> decltype(c.empty()); // C++1713// template <class T, size_t N> constexpr bool empty(const T (&array)[N]) noexcept; // C++1714// template <class E> constexpr bool empty(initializer_list<E> il) noexcept; // C++1715 16#include <iterator>17#include <cassert>18#include <vector>19#include <array>20#include <list>21#include <initializer_list>22 23#include "test_macros.h"24 25#if TEST_STD_VER > 1426#include <string_view>27#endif28 29template<typename C>30void test_const_container( const C& c )31{32// Can't say noexcept here because the container might not be33 assert ( std::empty(c) == c.empty());34}35 36template<typename T>37void test_const_container( const std::initializer_list<T>& c )38{39 assert ( std::empty(c) == (c.size() == 0));40}41 42template<typename C>43void test_container( C& c )44{45// Can't say noexcept here because the container might not be46 assert ( std::empty(c) == c.empty());47}48 49template<typename T>50void test_container( std::initializer_list<T>& c )51{52 ASSERT_NOEXCEPT(std::empty(c));53 assert ( std::empty(c) == (c.size() == 0));54}55 56template<typename T, std::size_t Sz>57void test_const_array( const T (&array)[Sz] )58{59 ASSERT_NOEXCEPT(std::empty(array));60 assert (!std::empty(array));61}62 63int main(int, char**)64{65 std::vector<int> v; v.push_back(1);66 std::list<int> l; l.push_back(2);67 std::array<int, 1> a; a[0] = 3;68 std::initializer_list<int> il = { 4 };69 70 test_container ( v );71 test_container ( l );72 test_container ( a );73 test_container ( il );74 75 test_const_container ( v );76 test_const_container ( l );77 test_const_container ( a );78 test_const_container ( il );79 80#if TEST_STD_VER > 1481 std::string_view sv{"ABC"};82 test_container ( sv );83 test_const_container ( sv );84#endif85 86 static constexpr int arrA [] { 1, 2, 3 };87 test_const_array ( arrA );88 89 return 0;90}91