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