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 // UNSUPPORTED: c++98, c++03
11
12 // <filesystem>
13
14 // class path
15
16 // template <class ECharT, class Traits = char_traits<ECharT>,
17 // class Allocator = allocator<ECharT>>
18 // basic_string<ECharT, Traits, Allocator>
19 // string(const Allocator& a = Allocator()) const;
20
21 #include "filesystem_include.hpp"
22 #include <type_traits>
23 #include <cassert>
24
25 #include "test_macros.h"
26 #include "test_iterators.h"
27 #include "count_new.hpp"
28 #include "min_allocator.h"
29 #include "filesystem_test_helper.hpp"
30
31
32 // the SSO is always triggered for strings of size 2.
33 MultiStringType shortString = MKSTR("a");
34 MultiStringType longString = MKSTR("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/123456789/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
35
36 template <class CharT>
doShortStringTest(MultiStringType const & MS)37 void doShortStringTest(MultiStringType const& MS) {
38 using namespace fs;
39 using Ptr = CharT const*;
40 using Str = std::basic_string<CharT>;
41 using Alloc = std::allocator<CharT>;
42 Ptr value = MS;
43 const path p((const char*)MS);
44 {
45 DisableAllocationGuard g;
46 Str s = p.string<CharT>();
47 assert(s == value);
48 Str s2 = p.string<CharT>(Alloc{});
49 assert(s2 == value);
50 }
51 using MAlloc = malloc_allocator<CharT>;
52 MAlloc::reset();
53 {
54 using Traits = std::char_traits<CharT>;
55 using AStr = std::basic_string<CharT, Traits, MAlloc>;
56 DisableAllocationGuard g;
57 AStr s = p.string<CharT, Traits, MAlloc>();
58 assert(s == value);
59 assert(MAlloc::alloc_count == 0);
60 assert(MAlloc::outstanding_alloc() == 0);
61 }
62 MAlloc::reset();
63 { // Other allocator - provided copy
64 using Traits = std::char_traits<CharT>;
65 using AStr = std::basic_string<CharT, Traits, MAlloc>;
66 DisableAllocationGuard g;
67 MAlloc a;
68 // don't allow another allocator to be default constructed.
69 MAlloc::disable_default_constructor = true;
70 AStr s = p.string<CharT, Traits, MAlloc>(a);
71 assert(s == value);
72 assert(MAlloc::alloc_count == 0);
73 assert(MAlloc::outstanding_alloc() == 0);
74 }
75 MAlloc::reset();
76 }
77
78 template <class CharT>
doLongStringTest(MultiStringType const & MS)79 void doLongStringTest(MultiStringType const& MS) {
80 using namespace fs;
81 using Ptr = CharT const*;
82 using Str = std::basic_string<CharT>;
83 Ptr value = MS;
84 const path p((const char*)MS);
85 { // Default allocator
86 using Alloc = std::allocator<CharT>;
87 Str s = p.string<CharT>();
88 assert(s == value);
89 Str s2 = p.string<CharT>(Alloc{});
90 assert(s2 == value);
91 }
92 using MAlloc = malloc_allocator<CharT>;
93 MAlloc::reset();
94 { // Other allocator - default construct
95 using Traits = std::char_traits<CharT>;
96 using AStr = std::basic_string<CharT, Traits, MAlloc>;
97 DisableAllocationGuard g;
98 AStr s = p.string<CharT, Traits, MAlloc>();
99 assert(s == value);
100 assert(MAlloc::alloc_count > 0);
101 assert(MAlloc::outstanding_alloc() == 1);
102 }
103 MAlloc::reset();
104 { // Other allocator - provided copy
105 using Traits = std::char_traits<CharT>;
106 using AStr = std::basic_string<CharT, Traits, MAlloc>;
107 DisableAllocationGuard g;
108 MAlloc a;
109 // don't allow another allocator to be default constructed.
110 MAlloc::disable_default_constructor = true;
111 AStr s = p.string<CharT, Traits, MAlloc>(a);
112 assert(s == value);
113 assert(MAlloc::alloc_count > 0);
114 assert(MAlloc::outstanding_alloc() == 1);
115 }
116 MAlloc::reset();
117 /////////////////////////////////////////////////////////////////////////////
118 }
119
main()120 int main()
121 {
122 using namespace fs;
123 {
124 auto const& S = shortString;
125 doShortStringTest<char>(S);
126 doShortStringTest<wchar_t>(S);
127 doShortStringTest<char16_t>(S);
128 doShortStringTest<char32_t>(S);
129 }
130 {
131 auto const& S = longString;
132 doLongStringTest<char>(S);
133 doLongStringTest<wchar_t>(S);
134 doLongStringTest<char16_t>(S);
135 doLongStringTest<char32_t>(S);
136 }
137 }
138