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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <array>
10 
11 // A string is a contiguous container
12 
13 #include <string>
14 #include <cassert>
15 
16 #include "test_macros.h"
17 #include "test_allocator.h"
18 #include "min_allocator.h"
19 
20 template <class C>
test_contiguous(const C & c)21 TEST_CONSTEXPR_CXX20 void test_contiguous(const C& c) {
22   for (std::size_t i = 0; i < c.size(); ++i)
23     assert(*(c.begin() + static_cast<typename C::difference_type>(i)) == *(std::addressof(*c.begin()) + i));
24 }
25 
26 template <class Alloc>
test_string(const Alloc & a)27 TEST_CONSTEXPR_CXX20 void test_string(const Alloc& a) {
28   typedef std::basic_string<char, std::char_traits<char>, Alloc> S;
29 
30   {
31     test_contiguous(S());
32     test_contiguous(S("1"));
33     test_contiguous(S("1234567890123456789012345678901234567890123456789012345678901234567890"));
34   }
35   {
36     test_contiguous(S(Alloc()));
37     test_contiguous(S("1", Alloc()));
38     test_contiguous(S("1234567890123456789012345678901234567890123456789012345678901234567890", Alloc()));
39   }
40   {
41     test_contiguous(S(Alloc(a)));
42     test_contiguous(S("1", Alloc(a)));
43     test_contiguous(S("1234567890123456789012345678901234567890123456789012345678901234567890", Alloc(a)));
44   }
45 }
46 
test()47 TEST_CONSTEXPR_CXX20 bool test() {
48   test_string(std::allocator<char>());
49   test_string(test_allocator<char>());
50   test_string(test_allocator<char>(3));
51 #if TEST_STD_VER >= 11
52   test_string(min_allocator<char>());
53 #endif
54 
55   return true;
56 }
57 
main(int,char **)58 int main(int, char**) {
59   test();
60 #if TEST_STD_VER > 17
61   static_assert(test());
62 #endif
63 
64   return 0;
65 }
66