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 
19 #include "test_allocator.h"
20 #include "min_allocator.h"
21 
22 template <class charT>
23 void
test(const charT * s)24 test(const charT* s)
25 {
26     typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
27     typedef typename S::traits_type T;
28     typedef typename S::allocator_type A;
29     unsigned n = T::length(s);
30     S s2(s);
31     assert(s2.__invariants());
32     assert(s2.size() == n);
33     assert(T::compare(s2.data(), s, n) == 0);
34     assert(s2.get_allocator() == A());
35     assert(s2.capacity() >= s2.size());
36 }
37 
38 template <class charT, class A>
39 void
test(const charT * s,const A & a)40 test(const charT* s, const A& a)
41 {
42     typedef std::basic_string<charT, std::char_traits<charT>, A> S;
43     typedef typename S::traits_type T;
44     unsigned n = T::length(s);
45     S s2(s, a);
46     assert(s2.__invariants());
47     assert(s2.size() == n);
48     assert(T::compare(s2.data(), s, n) == 0);
49     assert(s2.get_allocator() == a);
50     assert(s2.capacity() >= s2.size());
51 }
52 
main()53 int main()
54 {
55     {
56     typedef test_allocator<char> A;
57     typedef std::basic_string<char, std::char_traits<char>, A> S;
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 __cplusplus >= 201103L
72     {
73     typedef min_allocator<char> A;
74     typedef std::basic_string<char, std::char_traits<char>, A> S;
75 
76     test("");
77     test("", A());
78 
79     test("1");
80     test("1", A());
81 
82     test("1234567980");
83     test("1234567980", A());
84 
85     test("123456798012345679801234567980123456798012345679801234567980");
86     test("123456798012345679801234567980123456798012345679801234567980", A());
87     }
88 #endif
89 }
90