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