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