string_alloc.pass.cpp 3.6 KB

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