• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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 
29 #if defined(__clang__)
30 #pragma clang diagnostic ignored "-Wtautological-compare"
31 #endif
32 
33 template <std::size_t N>
34 std::bitset<N>
make_bitset()35 make_bitset()
36 {
37     std::bitset<N> v;
38     for (std::size_t i = 0; i < N; ++i)
39         v[i] = static_cast<bool>(std::rand() & 1);
40     return v;
41 }
42 
43 template <std::size_t N>
test_to_string()44 void test_to_string()
45 {
46 {
47     std::bitset<N> v = make_bitset<N>();
48     {
49     std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<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::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >();
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.template to_string<char>();
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     std::string s = v.to_string();
74     for (std::size_t i = 0; i < N; ++i)
75         if (v[i])
76             assert(s[N - 1 - i] == '1');
77         else
78             assert(s[N - 1 - i] == '0');
79     }
80 }
81 {
82     std::bitset<N> v = make_bitset<N>();
83     {
84     std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<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::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >('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.template to_string<char>('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     std::string s = v.to_string('0');
109     for (std::size_t i = 0; i < N; ++i)
110         if (v[i])
111             assert(s[N - 1 - i] == '1');
112         else
113             assert(s[N - 1 - i] == '0');
114     }
115 }
116 {
117     std::bitset<N> v = make_bitset<N>();
118     {
119     std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<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::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >('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.template to_string<char>('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     std::string s = v.to_string('0', '1');
144     for (std::size_t i = 0; i < N; ++i)
145         if (v[i])
146             assert(s[N - 1 - i] == '1');
147         else
148             assert(s[N - 1 - i] == '0');
149     }
150 }
151 }
152 
main()153 int main()
154 {
155     test_to_string<0>();
156     test_to_string<1>();
157     test_to_string<31>();
158     test_to_string<32>();
159     test_to_string<33>();
160     test_to_string<63>();
161     test_to_string<64>();
162     test_to_string<65>();
163     test_to_string<1000>();
164 }
165