Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // test: |
| 11 | |
| 12 | // template <class charT, class traits, class Allocator> |
| 13 | // basic_string<charT, traits, Allocator> |
| 14 | // to_string(charT zero = charT('0'), charT one = charT('1')) const; |
| 15 | // |
| 16 | // template <class charT, class traits> |
| 17 | // basic_string<charT, traits, allocator<charT> > to_string() const; |
| 18 | // |
| 19 | // template <class charT> |
| 20 | // basic_string<charT, char_traits<charT>, allocator<charT> > to_string() const; |
| 21 | // |
| 22 | // basic_string<char, char_traits<char>, allocator<char> > to_string() const; |
| 23 | |
| 24 | #include <bitset> |
| 25 | #include <string> |
| 26 | #include <cstdlib> |
| 27 | #include <cassert> |
| 28 | |
Howard Hinnant | 97ecd64 | 2011-05-17 19:12:55 +0000 | [diff] [blame] | 29 | #pragma clang diagnostic ignored "-Wtautological-compare" |
| 30 | |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 31 | template <std::size_t N> |
| 32 | std::bitset<N> |
| 33 | make_bitset() |
| 34 | { |
| 35 | std::bitset<N> v; |
| 36 | for (std::size_t i = 0; i < N; ++i) |
| 37 | v[i] = static_cast<bool>(std::rand() & 1); |
| 38 | return v; |
| 39 | } |
| 40 | |
| 41 | template <std::size_t N> |
| 42 | void test_to_string() |
| 43 | { |
| 44 | { |
| 45 | std::bitset<N> v = make_bitset<N>(); |
| 46 | { |
| 47 | std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >(); |
| 48 | for (std::size_t i = 0; i < N; ++i) |
| 49 | if (v[i]) |
| 50 | assert(s[N - 1 - i] == '1'); |
| 51 | else |
| 52 | assert(s[N - 1 - i] == '0'); |
| 53 | } |
| 54 | { |
| 55 | std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >(); |
| 56 | for (std::size_t i = 0; i < N; ++i) |
| 57 | if (v[i]) |
| 58 | assert(s[N - 1 - i] == '1'); |
| 59 | else |
| 60 | assert(s[N - 1 - i] == '0'); |
| 61 | } |
| 62 | { |
| 63 | std::string s = v.template to_string<char>(); |
| 64 | for (std::size_t i = 0; i < N; ++i) |
| 65 | if (v[i]) |
| 66 | assert(s[N - 1 - i] == '1'); |
| 67 | else |
| 68 | assert(s[N - 1 - i] == '0'); |
| 69 | } |
| 70 | { |
| 71 | std::string s = v.to_string(); |
| 72 | for (std::size_t i = 0; i < N; ++i) |
| 73 | if (v[i]) |
| 74 | assert(s[N - 1 - i] == '1'); |
| 75 | else |
| 76 | assert(s[N - 1 - i] == '0'); |
| 77 | } |
| 78 | } |
| 79 | { |
| 80 | std::bitset<N> v = make_bitset<N>(); |
| 81 | { |
| 82 | std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >('0'); |
| 83 | for (std::size_t i = 0; i < N; ++i) |
| 84 | if (v[i]) |
| 85 | assert(s[N - 1 - i] == '1'); |
| 86 | else |
| 87 | assert(s[N - 1 - i] == '0'); |
| 88 | } |
| 89 | { |
| 90 | std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >('0'); |
| 91 | for (std::size_t i = 0; i < N; ++i) |
| 92 | if (v[i]) |
| 93 | assert(s[N - 1 - i] == '1'); |
| 94 | else |
| 95 | assert(s[N - 1 - i] == '0'); |
| 96 | } |
| 97 | { |
| 98 | std::string s = v.template to_string<char>('0'); |
| 99 | for (std::size_t i = 0; i < N; ++i) |
| 100 | if (v[i]) |
| 101 | assert(s[N - 1 - i] == '1'); |
| 102 | else |
| 103 | assert(s[N - 1 - i] == '0'); |
| 104 | } |
| 105 | { |
| 106 | std::string s = v.to_string('0'); |
| 107 | for (std::size_t i = 0; i < N; ++i) |
| 108 | if (v[i]) |
| 109 | assert(s[N - 1 - i] == '1'); |
| 110 | else |
| 111 | assert(s[N - 1 - i] == '0'); |
| 112 | } |
| 113 | } |
| 114 | { |
| 115 | std::bitset<N> v = make_bitset<N>(); |
| 116 | { |
| 117 | std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >('0', '1'); |
| 118 | for (std::size_t i = 0; i < N; ++i) |
| 119 | if (v[i]) |
| 120 | assert(s[N - 1 - i] == '1'); |
| 121 | else |
| 122 | assert(s[N - 1 - i] == '0'); |
| 123 | } |
| 124 | { |
| 125 | std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >('0', '1'); |
| 126 | for (std::size_t i = 0; i < N; ++i) |
| 127 | if (v[i]) |
| 128 | assert(s[N - 1 - i] == '1'); |
| 129 | else |
| 130 | assert(s[N - 1 - i] == '0'); |
| 131 | } |
| 132 | { |
| 133 | std::string s = v.template to_string<char>('0', '1'); |
| 134 | for (std::size_t i = 0; i < N; ++i) |
| 135 | if (v[i]) |
| 136 | assert(s[N - 1 - i] == '1'); |
| 137 | else |
| 138 | assert(s[N - 1 - i] == '0'); |
| 139 | } |
| 140 | { |
| 141 | std::string s = v.to_string('0', '1'); |
| 142 | for (std::size_t i = 0; i < N; ++i) |
| 143 | if (v[i]) |
| 144 | assert(s[N - 1 - i] == '1'); |
| 145 | else |
| 146 | assert(s[N - 1 - i] == '0'); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | int main() |
| 152 | { |
| 153 | test_to_string<0>(); |
| 154 | test_to_string<1>(); |
| 155 | test_to_string<31>(); |
| 156 | test_to_string<32>(); |
| 157 | test_to_string<33>(); |
| 158 | test_to_string<63>(); |
| 159 | test_to_string<64>(); |
| 160 | test_to_string<65>(); |
| 161 | test_to_string<1000>(); |
| 162 | } |