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
test(const charT * s)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
test(const charT * s,const A & a)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
main()55 int main()
56 {
57 {
58 typedef test_allocator<char> A;
59
60 test("");
61 test("", A(2));
62
63 test("1");
64 test("1", A(2));
65
66 test("1234567980");
67 test("1234567980", A(2));
68
69 test("123456798012345679801234567980123456798012345679801234567980");
70 test("123456798012345679801234567980123456798012345679801234567980", A(2));
71 }
72 #if TEST_STD_VER >= 11
73 {
74 typedef min_allocator<char> A;
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