Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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 | |
Marshall Clow | 1b92188 | 2013-12-03 00:18:10 +0000 | [diff] [blame] | 19 | #include "test_allocator.h" |
Marshall Clow | 061d0cc | 2013-11-26 20:58:02 +0000 | [diff] [blame] | 20 | #include "min_allocator.h" |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 21 | |
| 22 | template <class charT> |
| 23 | void |
| 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); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 30 | assert(s2.__invariants()); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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 | |
Howard Hinnant | 9dcdcde | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 37 | template <class charT, class A> |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 38 | void |
Howard Hinnant | 9dcdcde | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 39 | test(const charT* s, unsigned n, const A& a) |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 40 | { |
Howard Hinnant | 9dcdcde | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 41 | typedef std::basic_string<charT, std::char_traits<charT>, A> S; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 42 | typedef typename S::traits_type T; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 43 | S s2(s, n, a); |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 44 | assert(s2.__invariants()); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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 | |
| 51 | int main() |
| 52 | { |
Howard Hinnant | 9dcdcde | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 53 | { |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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)); |
Howard Hinnant | 9dcdcde | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 68 | } |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 69 | #if __cplusplus >= 201103L |
Howard Hinnant | 9dcdcde | 2013-06-28 16:59:19 +0000 | [diff] [blame] | 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 |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 87 | } |