blob: 81537ba52bb650bc17ae546422bcb4d6bf9ece77 [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// explicit basic_string(const Allocator& a = Allocator());
13
14#include <string>
15#include <cassert>
16
Marshall Clowcbf166a2015-06-03 19:56:43 +000017#include "test_macros.h"
Marshall Clowc3deeb52013-12-03 00:18:10 +000018#include "test_allocator.h"
Marshall Clowe34f6f62013-11-26 20:58:02 +000019#include "min_allocator.h"
Howard Hinnant3e519522010-05-11 19:42:16 +000020
21template <class S>
22void
23test()
24{
25 {
Marshall Clowcbf166a2015-06-03 19:56:43 +000026#if TEST_STD_VER > 14
27 static_assert((noexcept(S{})), "" );
28#elif TEST_STD_VER >= 11
29 static_assert((noexcept(S()) == noexcept(typename S::allocator_type())), "" );
30#endif
Howard Hinnant3e519522010-05-11 19:42:16 +000031 S s;
Eric Fiselier1f4231f2016-04-28 22:28:23 +000032 LIBCPP_ASSERT(s.__invariants());
Howard Hinnant3e519522010-05-11 19:42:16 +000033 assert(s.data());
34 assert(s.size() == 0);
35 assert(s.capacity() >= s.size());
36 assert(s.get_allocator() == typename S::allocator_type());
37 }
38 {
Marshall Clowcbf166a2015-06-03 19:56:43 +000039#if TEST_STD_VER > 14
40 static_assert((noexcept(S{typename S::allocator_type{}})), "" );
41#elif TEST_STD_VER >= 11
42 static_assert((noexcept(S(typename S::allocator_type())) == std::is_nothrow_copy_constructible<typename S::allocator_type>::value), "" );
43#endif
Howard Hinnant3e519522010-05-11 19:42:16 +000044 S s(typename S::allocator_type(5));
Eric Fiselier1f4231f2016-04-28 22:28:23 +000045 LIBCPP_ASSERT(s.__invariants());
Howard Hinnant3e519522010-05-11 19:42:16 +000046 assert(s.data());
47 assert(s.size() == 0);
48 assert(s.capacity() >= s.size());
49 assert(s.get_allocator() == typename S::allocator_type(5));
50 }
51}
52
Marshall Clowcbf166a2015-06-03 19:56:43 +000053#if TEST_STD_VER >= 11
Howard Hinnanteec72182013-06-28 16:59:19 +000054
55template <class S>
56void
57test2()
58{
59 {
Marshall Clowcbf166a2015-06-03 19:56:43 +000060#if TEST_STD_VER > 14
61 static_assert((noexcept(S{})), "" );
62#elif TEST_STD_VER >= 11
63 static_assert((noexcept(S()) == noexcept(typename S::allocator_type())), "" );
64#endif
Howard Hinnanteec72182013-06-28 16:59:19 +000065 S s;
Eric Fiselier1f4231f2016-04-28 22:28:23 +000066 LIBCPP_ASSERT(s.__invariants());
Howard Hinnanteec72182013-06-28 16:59:19 +000067 assert(s.data());
68 assert(s.size() == 0);
69 assert(s.capacity() >= s.size());
70 assert(s.get_allocator() == typename S::allocator_type());
71 }
72 {
Marshall Clowcbf166a2015-06-03 19:56:43 +000073#if TEST_STD_VER > 14
74 static_assert((noexcept(S{typename S::allocator_type{}})), "" );
75#elif TEST_STD_VER >= 11
76 static_assert((noexcept(S(typename S::allocator_type())) == std::is_nothrow_copy_constructible<typename S::allocator_type>::value), "" );
77#endif
Howard Hinnanteec72182013-06-28 16:59:19 +000078 S s(typename S::allocator_type{});
Eric Fiselier1f4231f2016-04-28 22:28:23 +000079 LIBCPP_ASSERT(s.__invariants());
Howard Hinnanteec72182013-06-28 16:59:19 +000080 assert(s.data());
81 assert(s.size() == 0);
82 assert(s.capacity() >= s.size());
83 assert(s.get_allocator() == typename S::allocator_type());
84 }
85}
86
87#endif
88
Howard Hinnant3e519522010-05-11 19:42:16 +000089int main()
90{
91 test<std::basic_string<char, std::char_traits<char>, test_allocator<char> > >();
Marshall Clowcbf166a2015-06-03 19:56:43 +000092#if TEST_STD_VER >= 11
Howard Hinnanteec72182013-06-28 16:59:19 +000093 test2<std::basic_string<char, std::char_traits<char>, min_allocator<char> > >();
Marshall Clow2a10c962016-08-17 05:58:40 +000094 test2<std::basic_string<char, std::char_traits<char>, explicit_allocator<char> > >();
Howard Hinnanteec72182013-06-28 16:59:19 +000095#endif
Howard Hinnant3e519522010-05-11 19:42:16 +000096}