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_macros.h" 20 #include "test_allocator.h" 21 #include "min_allocator.h" 22 23 template <class charT> 24 void 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 unsigned 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 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 unsigned 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 54 int main() 55 { 56 { 57 typedef test_allocator<char> A; 58 typedef std::basic_string<char, std::char_traits<char>, A> S; 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 typedef std::basic_string<char, std::char_traits<char>, A> S; 76 77 test(""); 78 test("", A()); 79 80 test("1"); 81 test("1", A()); 82 83 test("1234567980"); 84 test("1234567980", A()); 85 86 test("123456798012345679801234567980123456798012345679801234567980"); 87 test("123456798012345679801234567980123456798012345679801234567980", A()); 88 } 89 #endif 90 } 91