xref: /llvm-project/libcxx/test/std/strings/basic.string/string.cons/pointer_alloc.pass.cpp (revision d4b83e6dfd075856468086364f792de00cdb2f9f)
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 // <string>
11 
12 // basic_string(const charT* s, const Allocator& a = Allocator());
13 
14 #include <string>
15 #include <stdexcept>
16 #include <algorithm>
17 #include <cassert>
18 #include <cstddef>
19 
20 #include "test_macros.h"
21 #include "test_allocator.h"
22 #include "min_allocator.h"
23 
24 template <class charT>
25 void
26 test(const charT* s)
27 {
28     typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
29     typedef typename S::traits_type T;
30     typedef typename S::allocator_type A;
31     std::size_t n = T::length(s);
32     S s2(s);
33     LIBCPP_ASSERT(s2.__invariants());
34     assert(s2.size() == n);
35     assert(T::compare(s2.data(), s, n) == 0);
36     assert(s2.get_allocator() == A());
37     assert(s2.capacity() >= s2.size());
38 }
39 
40 template <class charT, class A>
41 void
42 test(const charT* s, const A& a)
43 {
44     typedef std::basic_string<charT, std::char_traits<charT>, A> S;
45     typedef typename S::traits_type T;
46     std::size_t n = T::length(s);
47     S s2(s, a);
48     LIBCPP_ASSERT(s2.__invariants());
49     assert(s2.size() == n);
50     assert(T::compare(s2.data(), s, n) == 0);
51     assert(s2.get_allocator() == a);
52     assert(s2.capacity() >= s2.size());
53 }
54 
55 int main()
56 {
57     {
58     typedef test_allocator<char> A;
59     typedef std::basic_string<char, std::char_traits<char>, A> S;
60 
61     test("");
62     test("", A(2));
63 
64     test("1");
65     test("1", A(2));
66 
67     test("1234567980");
68     test("1234567980", A(2));
69 
70     test("123456798012345679801234567980123456798012345679801234567980");
71     test("123456798012345679801234567980123456798012345679801234567980", A(2));
72     }
73 #if TEST_STD_VER >= 11
74     {
75     typedef min_allocator<char> A;
76     typedef std::basic_string<char, std::char_traits<char>, A> S;
77 
78     test("");
79     test("", A());
80 
81     test("1");
82     test("1", A());
83 
84     test("1234567980");
85     test("1234567980", A());
86 
87     test("123456798012345679801234567980123456798012345679801234567980");
88     test("123456798012345679801234567980123456798012345679801234567980", A());
89     }
90 #endif
91 }
92