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 9 // test: 10 11 // template <class charT, class traits, class Allocator> 12 // basic_string<charT, traits, Allocator> 13 // to_string(charT zero = charT('0'), charT one = charT('1')) const; 14 // 15 // template <class charT, class traits> 16 // basic_string<charT, traits, allocator<charT> > to_string() const; 17 // 18 // template <class charT> 19 // basic_string<charT, char_traits<charT>, allocator<charT> > to_string() const; 20 // 21 // basic_string<char, char_traits<char>, allocator<char> > to_string() const; 22 23 #include <bitset> 24 #include <string> 25 #include <cstdlib> 26 #include <cassert> 27 28 #include "test_macros.h" 29 30 #if defined(TEST_COMPILER_CLANG) 31 #pragma clang diagnostic ignored "-Wtautological-compare" 32 #elif defined(TEST_COMPILER_C1XX) 33 #pragma warning(disable: 6294) // Ill-defined for-loop: initial condition does not satisfy test. Loop body not executed. 34 #endif 35 36 template <std::size_t N> 37 std::bitset<N> 38 make_bitset() 39 { 40 std::bitset<N> v; 41 for (std::size_t i = 0; i < N; ++i) 42 v[i] = static_cast<bool>(std::rand() & 1); 43 return v; 44 } 45 46 template <std::size_t N> 47 void test_to_string() 48 { 49 { 50 std::bitset<N> v = make_bitset<N>(); 51 { 52 std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >(); 53 for (std::size_t i = 0; i < N; ++i) 54 if (v[i]) 55 assert(s[N - 1 - i] == '1'); 56 else 57 assert(s[N - 1 - i] == '0'); 58 } 59 { 60 std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >(); 61 for (std::size_t i = 0; i < N; ++i) 62 if (v[i]) 63 assert(s[N - 1 - i] == '1'); 64 else 65 assert(s[N - 1 - i] == '0'); 66 } 67 { 68 std::string s = v.template to_string<char>(); 69 for (std::size_t i = 0; i < N; ++i) 70 if (v[i]) 71 assert(s[N - 1 - i] == '1'); 72 else 73 assert(s[N - 1 - i] == '0'); 74 } 75 { 76 std::string s = v.to_string(); 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 { 85 std::bitset<N> v = make_bitset<N>(); 86 { 87 std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >('0'); 88 for (std::size_t i = 0; i < N; ++i) 89 if (v[i]) 90 assert(s[N - 1 - i] == '1'); 91 else 92 assert(s[N - 1 - i] == '0'); 93 } 94 { 95 std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >('0'); 96 for (std::size_t i = 0; i < N; ++i) 97 if (v[i]) 98 assert(s[N - 1 - i] == '1'); 99 else 100 assert(s[N - 1 - i] == '0'); 101 } 102 { 103 std::string s = v.template to_string<char>('0'); 104 for (std::size_t i = 0; i < N; ++i) 105 if (v[i]) 106 assert(s[N - 1 - i] == '1'); 107 else 108 assert(s[N - 1 - i] == '0'); 109 } 110 { 111 std::string s = v.to_string('0'); 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 { 120 std::bitset<N> v = make_bitset<N>(); 121 { 122 std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >('0', '1'); 123 for (std::size_t i = 0; i < N; ++i) 124 if (v[i]) 125 assert(s[N - 1 - i] == '1'); 126 else 127 assert(s[N - 1 - i] == '0'); 128 } 129 { 130 std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >('0', '1'); 131 for (std::size_t i = 0; i < N; ++i) 132 if (v[i]) 133 assert(s[N - 1 - i] == '1'); 134 else 135 assert(s[N - 1 - i] == '0'); 136 } 137 { 138 std::string s = v.template to_string<char>('0', '1'); 139 for (std::size_t i = 0; i < N; ++i) 140 if (v[i]) 141 assert(s[N - 1 - i] == '1'); 142 else 143 assert(s[N - 1 - i] == '0'); 144 } 145 { 146 std::string s = v.to_string('0', '1'); 147 for (std::size_t i = 0; i < N; ++i) 148 if (v[i]) 149 assert(s[N - 1 - i] == '1'); 150 else 151 assert(s[N - 1 - i] == '0'); 152 } 153 } 154 } 155 156 int main(int, char**) 157 { 158 test_to_string<0>(); 159 test_to_string<1>(); 160 test_to_string<31>(); 161 test_to_string<32>(); 162 test_to_string<33>(); 163 test_to_string<63>(); 164 test_to_string<64>(); 165 test_to_string<65>(); 166 test_to_string<1000>(); 167 168 return 0; 169 } 170