xref: /llvm-project/libcxx/test/std/strings/basic.string/string.require/contiguous.pass.cpp (revision a40bada91aeda276a772acfbcae6e8de26755a11)
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 // An 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>
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 TEST_CONSTEXPR_CXX20 bool test() {
27   {
28     typedef std::string S;
29     test_contiguous(S());
30     test_contiguous(S("1"));
31     test_contiguous(S("1234567890123456789012345678901234567890123456789012345678901234567890"));
32   }
33 
34   {
35     typedef test_allocator<char> A;
36     typedef std::basic_string<char, std::char_traits<char>, A> S;
37     test_contiguous(S(A(3)));
38     test_contiguous(S("1", A(5)));
39     test_contiguous(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)));
40   }
41 #if TEST_STD_VER >= 11
42   {
43     typedef min_allocator<char> A;
44     typedef std::basic_string<char, std::char_traits<char>, A> S;
45     test_contiguous(S(A{}));
46     test_contiguous(S("1", A()));
47     test_contiguous(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()));
48   }
49 #endif
50 
51   return true;
52 }
53 
54 int main(int, char**) {
55   test();
56 #if TEST_STD_VER > 17
57   static_assert(test());
58 #endif
59 
60   return 0;
61 }
62