string_alloc.pass.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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; // should not allocate
  37. Str s = p.string<CharT>();
  38. assert(s == value);
  39. Str s2 = p.string<CharT>(Alloc{});
  40. assert(s2 == value);
  41. }
  42. }
  43. template <class CharT>
  44. void doLongStringTest(MultiStringType const& MS) {
  45. using namespace fs;
  46. using Ptr = CharT const*;
  47. using Str = std::basic_string<CharT>;
  48. Ptr value = MS;
  49. const path p((const char*)MS);
  50. { // Default allocator
  51. using Alloc = std::allocator<CharT>;
  52. RequireAllocationGuard g; // should not allocate because
  53. Str s = p.string<CharT>();
  54. assert(s == value);
  55. Str s2 = p.string<CharT>(Alloc{});
  56. assert(s2 == value);
  57. }
  58. using MAlloc = malloc_allocator<CharT>;
  59. MAlloc::reset();
  60. { // Other allocator - default construct
  61. using Traits = std::char_traits<CharT>;
  62. using AStr = std::basic_string<CharT, Traits, MAlloc>;
  63. DisableAllocationGuard g;
  64. AStr s = p.string<CharT, Traits, MAlloc>();
  65. assert(s == value);
  66. assert(MAlloc::alloc_count > 0);
  67. assert(MAlloc::outstanding_alloc() == 1);
  68. }
  69. MAlloc::reset();
  70. { // Other allocator - provided copy
  71. using Traits = std::char_traits<CharT>;
  72. using AStr = std::basic_string<CharT, Traits, MAlloc>;
  73. DisableAllocationGuard g;
  74. MAlloc a;
  75. // don't allow another allocator to be default constructed.
  76. MAlloc::disable_default_constructor = true;
  77. AStr s = p.string<CharT, Traits, MAlloc>(a);
  78. assert(s == value);
  79. assert(MAlloc::alloc_count > 0);
  80. assert(MAlloc::outstanding_alloc() == 1);
  81. }
  82. MAlloc::reset();
  83. /////////////////////////////////////////////////////////////////////////////
  84. }
  85. int main()
  86. {
  87. using namespace fs;
  88. {
  89. auto const& S = shortString;
  90. doShortStringTest<char>(S);
  91. doShortStringTest<wchar_t>(S);
  92. doShortStringTest<char16_t>(S);
  93. doShortStringTest<char32_t>(S);
  94. }
  95. {
  96. auto const& S = longString;
  97. doLongStringTest<char>(S);
  98. doLongStringTest<wchar_t>(S);
  99. doLongStringTest<char16_t>(S);
  100. doLongStringTest<char32_t>(S);
  101. }
  102. }