15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier
931cbe0f2SLouis Dionne // UNSUPPORTED: c++03, c++11, c++14
105f8d84ecSMarshall Clow
115a83710eSEric Fiselier // <iterator>
125a83710eSEric Fiselier // template <class C> constexpr auto data(C& c) -> decltype(c.data()); // C++17
135a83710eSEric Fiselier // template <class C> constexpr auto data(const C& c) -> decltype(c.data()); // C++17
145a83710eSEric Fiselier // template <class T, size_t N> constexpr T* data(T (&array)[N]) noexcept; // C++17
155a83710eSEric Fiselier // template <class E> constexpr const E* data(initializer_list<E> il) noexcept; // C++17
165a83710eSEric Fiselier
175a83710eSEric Fiselier #include <iterator>
185a83710eSEric Fiselier #include <cassert>
195a83710eSEric Fiselier #include <vector>
205a83710eSEric Fiselier #include <array>
215a83710eSEric Fiselier #include <initializer_list>
225a83710eSEric Fiselier
235f8d84ecSMarshall Clow #include "test_macros.h"
245f8d84ecSMarshall Clow
25aafb3151SMarshall Clow #if TEST_STD_VER > 14
26aafb3151SMarshall Clow #include <string_view>
27aafb3151SMarshall Clow #endif
28aafb3151SMarshall Clow
295a83710eSEric Fiselier template<typename C>
test_const_container(const C & c)305a83710eSEric Fiselier void test_const_container( const C& c )
315a83710eSEric Fiselier {
325f8d84ecSMarshall Clow // Can't say noexcept here because the container might not be
335a83710eSEric Fiselier assert ( std::data(c) == c.data());
345a83710eSEric Fiselier }
355a83710eSEric Fiselier
365a83710eSEric Fiselier template<typename T>
test_const_container(const std::initializer_list<T> & c)375a83710eSEric Fiselier void test_const_container( const std::initializer_list<T>& c )
385a83710eSEric Fiselier {
395f8d84ecSMarshall Clow ASSERT_NOEXCEPT(std::data(c));
405a83710eSEric Fiselier assert ( std::data(c) == c.begin());
415a83710eSEric Fiselier }
425a83710eSEric Fiselier
435a83710eSEric Fiselier template<typename C>
test_container(C & c)445a83710eSEric Fiselier void test_container( C& c )
455a83710eSEric Fiselier {
465f8d84ecSMarshall Clow // Can't say noexcept here because the container might not be
475a83710eSEric Fiselier assert ( std::data(c) == c.data());
485a83710eSEric Fiselier }
495a83710eSEric Fiselier
505a83710eSEric Fiselier template<typename T>
test_container(std::initializer_list<T> & c)515a83710eSEric Fiselier void test_container( std::initializer_list<T>& c)
525a83710eSEric Fiselier {
535f8d84ecSMarshall Clow ASSERT_NOEXCEPT(std::data(c));
545a83710eSEric Fiselier assert ( std::data(c) == c.begin());
555a83710eSEric Fiselier }
565a83710eSEric Fiselier
57*fb855eb9SMark de Wever template<typename T, std::size_t Sz>
test_const_array(const T (& array)[Sz])585a83710eSEric Fiselier void test_const_array( const T (&array)[Sz] )
595a83710eSEric Fiselier {
605f8d84ecSMarshall Clow ASSERT_NOEXCEPT(std::data(array));
615a83710eSEric Fiselier assert ( std::data(array) == &array[0]);
625a83710eSEric Fiselier }
635a83710eSEric Fiselier
main(int,char **)642df59c50SJF Bastien int main(int, char**)
655a83710eSEric Fiselier {
665a83710eSEric Fiselier std::vector<int> v; v.push_back(1);
675a83710eSEric Fiselier std::array<int, 1> a; a[0] = 3;
685a83710eSEric Fiselier std::initializer_list<int> il = { 4 };
695a83710eSEric Fiselier
705a83710eSEric Fiselier test_container ( v );
715a83710eSEric Fiselier test_container ( a );
725a83710eSEric Fiselier test_container ( il );
735a83710eSEric Fiselier
745a83710eSEric Fiselier test_const_container ( v );
755a83710eSEric Fiselier test_const_container ( a );
765a83710eSEric Fiselier test_const_container ( il );
775a83710eSEric Fiselier
78aafb3151SMarshall Clow #if TEST_STD_VER > 14
79aafb3151SMarshall Clow std::string_view sv{"ABC"};
80aafb3151SMarshall Clow test_container ( sv );
81aafb3151SMarshall Clow test_const_container ( sv );
82aafb3151SMarshall Clow #endif
83aafb3151SMarshall Clow
845a83710eSEric Fiselier static constexpr int arrA [] { 1, 2, 3 };
855a83710eSEric Fiselier test_const_array ( arrA );
862df59c50SJF Bastien
872df59c50SJF Bastien return 0;
885a83710eSEric Fiselier }
89