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