pointer_size_alloc.pass.cpp (2348B)
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_macros.h" 20 #include "test_allocator.h" 21 #include "min_allocator.h" 22 23 template <class charT> 24 void 25 test(const charT* s, unsigned n) 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 S s2(s, n); 31 LIBCPP_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 40 test(const charT* s, unsigned n, const A& a) 41 { 42 typedef std::basic_string<charT, std::char_traits<charT>, A> S; 43 typedef typename S::traits_type T; 44 S s2(s, n, a); 45 LIBCPP_ASSERT(s2.__invariants()); 46 assert(s2.size() == n); 47 assert(T::compare(s2.data(), s, n) == 0); 48 assert(s2.get_allocator() == a); 49 assert(s2.capacity() >= s2.size()); 50 } 51 52 int main() 53 { 54 { 55 typedef test_allocator<char> A; 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 TEST_STD_VER >= 11 70 { 71 typedef min_allocator<char> A; 72 73 test("", 0); 74 test("", 0, A()); 75 76 test("1", 1); 77 test("1", 1, A()); 78 79 test("1234567980", 10); 80 test("1234567980", 10, A()); 81 82 test("123456798012345679801234567980123456798012345679801234567980", 60); 83 test("123456798012345679801234567980123456798012345679801234567980", 60, A()); 84 } 85 #endif 86 87 #if TEST_STD_VER > 3 88 { // LWG 2946 89 std::string s({"abc", 1}); 90 assert(s.size() == 1); 91 assert(s == "a"); 92 } 93 #endif 94 }