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