blob: 8aa88c9fe726ddb0058c0bc4a85de6ca1573091c [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Howard Hinnant5b08a8a2010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnant3e519522010-05-11 19:42:16 +00004//
Howard Hinnant412dbeb2010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnant3e519522010-05-11 19:42:16 +00007//
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"
Marshall Clowe34f6f62013-11-26 20:58:02 +000020#include "min_allocator.h"
Howard Hinnant3e519522010-05-11 19:42:16 +000021
22template <class charT>
23void
24test(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
Howard Hinnanteec72182013-06-28 16:59:19 +000038template <class charT, class A>
Howard Hinnant3e519522010-05-11 19:42:16 +000039void
Howard Hinnanteec72182013-06-28 16:59:19 +000040test(const charT* s, const A& a)
Howard Hinnant3e519522010-05-11 19:42:16 +000041{
Howard Hinnanteec72182013-06-28 16:59:19 +000042 typedef std::basic_string<charT, std::char_traits<charT>, A> S;
Howard Hinnant3e519522010-05-11 19:42:16 +000043 typedef typename S::traits_type T;
Howard Hinnant3e519522010-05-11 19:42:16 +000044 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
53int main()
54{
Howard Hinnanteec72182013-06-28 16:59:19 +000055 {
Howard Hinnant3e519522010-05-11 19:42:16 +000056 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));
Howard Hinnanteec72182013-06-28 16:59:19 +000070 }
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
Howard Hinnant3e519522010-05-11 19:42:16 +000089}