string_alloc.pass.cpp 4.2 KB

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