string_alloc.pass.cpp 4.2 KB

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