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
11 // These tests require locale for non-char paths
12 // UNSUPPORTED: no-localization
13
14 // <filesystem>
15
16 // class path
17
18 // template <class ECharT, class Traits = char_traits<ECharT>,
19 // class Allocator = allocator<ECharT>>
20 // basic_string<ECharT, Traits, Allocator>
21 // generic_string(const Allocator& a = Allocator()) const;
22
23 #include "filesystem_include.h"
24 #include <type_traits>
25 #include <cassert>
26
27 #include "test_macros.h"
28 #include "test_iterators.h"
29 #include "count_new.h"
30 #include "min_allocator.h"
31 #include "filesystem_test_helper.h"
32
33 MultiStringType longString = MKSTR("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/123456789/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
34
35
36 // generic_string<C, T, A> forwards to string<C, T, A>. Tests for
37 // string<C, T, A>() are in "path.native.obs/string_alloc.pass.cpp".
38 // generic_string is minimally tested here.
39 template <class CharT>
doAllocTest()40 void doAllocTest()
41 {
42 using namespace fs;
43 using Traits = std::char_traits<CharT>;
44 using Alloc = malloc_allocator<CharT>;
45 using Str = std::basic_string<CharT, Traits, Alloc>;
46 const CharT* expect = longString;
47 const path p((const char*)longString);
48 {
49 // On Windows, charset conversions cause allocations outside of the
50 // provided allocator.
51 TEST_NOT_WIN32(DisableAllocationGuard g);
52 Alloc a;
53 Alloc::disable_default_constructor = true;
54 Str s = p.generic_string<CharT, Traits, Alloc>(a);
55 assert(s == expect);
56 assert(Alloc::alloc_count > 0);
57 assert(Alloc::outstanding_alloc() == 1);
58 Alloc::disable_default_constructor = false;
59 }
60 }
61
main(int,char **)62 int main(int, char**)
63 {
64 doAllocTest<char>();
65 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
66 doAllocTest<wchar_t>();
67 #endif
68 doAllocTest<char16_t>();
69 doAllocTest<char32_t>();
70 #if TEST_STD_VER > 17 && defined(__cpp_lib_char8_t)
71 doAllocTest<char8_t>();
72 #endif
73 return 0;
74 }
75