string_alloc.pass.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. MultiStringType shortString = MKSTR("abc");
  26. MultiStringType longString = MKSTR("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/123456789/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
  27. template <class CharT>
  28. void doShortStringTest(MultiStringType const& MS) {
  29. using namespace fs;
  30. using Ptr = CharT const*;
  31. using Str = std::basic_string<CharT>;
  32. using Alloc = std::allocator<CharT>;
  33. Ptr value = MS;
  34. const path p((const char*)MS);
  35. {
  36. DisableAllocationGuard g;
  37. if (!std::is_same<CharT, char>::value)
  38. g.release();
  39. Str s = p.string<CharT>();
  40. assert(s == value);
  41. Str s2 = p.string<CharT>(Alloc{});
  42. assert(s2 == value);
  43. }
  44. using MAlloc = malloc_allocator<CharT>;
  45. MAlloc::reset();
  46. {
  47. using Traits = std::char_traits<CharT>;
  48. using AStr = std::basic_string<CharT, Traits, MAlloc>;
  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. MAlloc a;
  59. // don't allow another allocator to be default constructed.
  60. MAlloc::disable_default_constructor = true;
  61. AStr s = p.string<CharT, Traits, MAlloc>(a);
  62. assert(s == value);
  63. assert(MAlloc::alloc_count == 0);
  64. assert(MAlloc::outstanding_alloc() == 0);
  65. }
  66. MAlloc::reset();
  67. }
  68. template <class CharT>
  69. void doLongStringTest(MultiStringType const& MS) {
  70. using namespace fs;
  71. using Ptr = CharT const*;
  72. using Str = std::basic_string<CharT>;
  73. Ptr value = MS;
  74. const path p((const char*)MS);
  75. { // Default allocator
  76. using Alloc = std::allocator<CharT>;
  77. Str s = p.string<CharT>();
  78. assert(s == value);
  79. Str s2 = p.string<CharT>(Alloc{});
  80. assert(s2 == value);
  81. }
  82. using MAlloc = malloc_allocator<CharT>;
  83. MAlloc::reset();
  84. { // Other allocator - default construct
  85. using Traits = std::char_traits<CharT>;
  86. using AStr = std::basic_string<CharT, Traits, MAlloc>;
  87. AStr s = p.string<CharT, Traits, MAlloc>();
  88. assert(s == value);
  89. assert(MAlloc::alloc_count > 0);
  90. assert(MAlloc::outstanding_alloc() == 1);
  91. }
  92. MAlloc::reset();
  93. { // Other allocator - provided copy
  94. using Traits = std::char_traits<CharT>;
  95. using AStr = std::basic_string<CharT, Traits, MAlloc>;
  96. MAlloc a;
  97. // don't allow another allocator to be default constructed.
  98. MAlloc::disable_default_constructor = true;
  99. AStr s = p.string<CharT, Traits, MAlloc>(a);
  100. assert(s == value);
  101. assert(MAlloc::alloc_count > 0);
  102. assert(MAlloc::outstanding_alloc() == 1);
  103. }
  104. MAlloc::reset();
  105. /////////////////////////////////////////////////////////////////////////////
  106. }
  107. int main()
  108. {
  109. using namespace fs;
  110. {
  111. auto const& S = shortString;
  112. doShortStringTest<char>(S);
  113. doShortStringTest<wchar_t>(S);
  114. doShortStringTest<char16_t>(S);
  115. doShortStringTest<char32_t>(S);
  116. }
  117. {
  118. auto const& S = longString;
  119. doLongStringTest<char>(S);
  120. doLongStringTest<wchar_t>(S);
  121. doLongStringTest<char16_t>(S);
  122. doLongStringTest<char32_t>(S);
  123. }
  124. }