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, size_type n, 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,unsigned n)24 test(const charT* s, unsigned n)
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 S s2(s, n);
30 assert(s2.__invariants());
31 assert(s2.size() == n);
32 assert(T::compare(s2.data(), s, n) == 0);
33 assert(s2.get_allocator() == A());
34 assert(s2.capacity() >= s2.size());
35 }
36
37 template <class charT, class A>
38 void
test(const charT * s,unsigned n,const A & a)39 test(const charT* s, unsigned n, const A& a)
40 {
41 typedef std::basic_string<charT, std::char_traits<charT>, A> S;
42 typedef typename S::traits_type T;
43 S s2(s, n, a);
44 assert(s2.__invariants());
45 assert(s2.size() == n);
46 assert(T::compare(s2.data(), s, n) == 0);
47 assert(s2.get_allocator() == a);
48 assert(s2.capacity() >= s2.size());
49 }
50
main()51 int main()
52 {
53 {
54 typedef test_allocator<char> A;
55 typedef std::basic_string<char, std::char_traits<char>, A> S;
56
57 test("", 0);
58 test("", 0, A(2));
59
60 test("1", 1);
61 test("1", 1, A(2));
62
63 test("1234567980", 10);
64 test("1234567980", 10, A(2));
65
66 test("123456798012345679801234567980123456798012345679801234567980", 60);
67 test("123456798012345679801234567980123456798012345679801234567980", 60, A(2));
68 }
69 #if __cplusplus >= 201103L
70 {
71 typedef min_allocator<char> A;
72 typedef std::basic_string<char, std::char_traits<char>, A> S;
73
74 test("", 0);
75 test("", 0, A());
76
77 test("1", 1);
78 test("1", 1, A());
79
80 test("1234567980", 10);
81 test("1234567980", 10, A());
82
83 test("123456798012345679801234567980123456798012345679801234567980", 60);
84 test("123456798012345679801234567980123456798012345679801234567980", 60, A());
85 }
86 #endif
87 }
88