to_string.pass.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. // test:
  10. // template <class charT, class traits, class Allocator>
  11. // basic_string<charT, traits, Allocator>
  12. // to_string(charT zero = charT('0'), charT one = charT('1')) const;
  13. //
  14. // template <class charT, class traits>
  15. // basic_string<charT, traits, allocator<charT> > to_string() const;
  16. //
  17. // template <class charT>
  18. // basic_string<charT, char_traits<charT>, allocator<charT> > to_string() const;
  19. //
  20. // basic_string<char, char_traits<char>, allocator<char> > to_string() const;
  21. #include <bitset>
  22. #include <string>
  23. #include <cstdlib>
  24. #include <cassert>
  25. #pragma clang diagnostic ignored "-Wtautological-compare"
  26. template <std::size_t N>
  27. std::bitset<N>
  28. make_bitset()
  29. {
  30. std::bitset<N> v;
  31. for (std::size_t i = 0; i < N; ++i)
  32. v[i] = static_cast<bool>(std::rand() & 1);
  33. return v;
  34. }
  35. template <std::size_t N>
  36. void test_to_string()
  37. {
  38. {
  39. std::bitset<N> v = make_bitset<N>();
  40. {
  41. std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >();
  42. for (std::size_t i = 0; i < N; ++i)
  43. if (v[i])
  44. assert(s[N - 1 - i] == '1');
  45. else
  46. assert(s[N - 1 - i] == '0');
  47. }
  48. {
  49. std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >();
  50. for (std::size_t i = 0; i < N; ++i)
  51. if (v[i])
  52. assert(s[N - 1 - i] == '1');
  53. else
  54. assert(s[N - 1 - i] == '0');
  55. }
  56. {
  57. std::string s = v.template to_string<char>();
  58. for (std::size_t i = 0; i < N; ++i)
  59. if (v[i])
  60. assert(s[N - 1 - i] == '1');
  61. else
  62. assert(s[N - 1 - i] == '0');
  63. }
  64. {
  65. std::string s = v.to_string();
  66. for (std::size_t i = 0; i < N; ++i)
  67. if (v[i])
  68. assert(s[N - 1 - i] == '1');
  69. else
  70. assert(s[N - 1 - i] == '0');
  71. }
  72. }
  73. {
  74. std::bitset<N> v = make_bitset<N>();
  75. {
  76. std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >('0');
  77. for (std::size_t i = 0; i < N; ++i)
  78. if (v[i])
  79. assert(s[N - 1 - i] == '1');
  80. else
  81. assert(s[N - 1 - i] == '0');
  82. }
  83. {
  84. std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >('0');
  85. for (std::size_t i = 0; i < N; ++i)
  86. if (v[i])
  87. assert(s[N - 1 - i] == '1');
  88. else
  89. assert(s[N - 1 - i] == '0');
  90. }
  91. {
  92. std::string s = v.template to_string<char>('0');
  93. for (std::size_t i = 0; i < N; ++i)
  94. if (v[i])
  95. assert(s[N - 1 - i] == '1');
  96. else
  97. assert(s[N - 1 - i] == '0');
  98. }
  99. {
  100. std::string s = v.to_string('0');
  101. for (std::size_t i = 0; i < N; ++i)
  102. if (v[i])
  103. assert(s[N - 1 - i] == '1');
  104. else
  105. assert(s[N - 1 - i] == '0');
  106. }
  107. }
  108. {
  109. std::bitset<N> v = make_bitset<N>();
  110. {
  111. std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >('0', '1');
  112. for (std::size_t i = 0; i < N; ++i)
  113. if (v[i])
  114. assert(s[N - 1 - i] == '1');
  115. else
  116. assert(s[N - 1 - i] == '0');
  117. }
  118. {
  119. std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >('0', '1');
  120. for (std::size_t i = 0; i < N; ++i)
  121. if (v[i])
  122. assert(s[N - 1 - i] == '1');
  123. else
  124. assert(s[N - 1 - i] == '0');
  125. }
  126. {
  127. std::string s = v.template to_string<char>('0', '1');
  128. for (std::size_t i = 0; i < N; ++i)
  129. if (v[i])
  130. assert(s[N - 1 - i] == '1');
  131. else
  132. assert(s[N - 1 - i] == '0');
  133. }
  134. {
  135. std::string s = v.to_string('0', '1');
  136. for (std::size_t i = 0; i < N; ++i)
  137. if (v[i])
  138. assert(s[N - 1 - i] == '1');
  139. else
  140. assert(s[N - 1 - i] == '0');
  141. }
  142. }
  143. }
  144. int main()
  145. {
  146. test_to_string<0>();
  147. test_to_string<1>();
  148. test_to_string<31>();
  149. test_to_string<32>();
  150. test_to_string<33>();
  151. test_to_string<63>();
  152. test_to_string<64>();
  153. test_to_string<65>();
  154. test_to_string<1000>();
  155. }