to_string.pass.cpp revision f5256e16dfc425c1d466f6308d4026d529ce9e0b
1//===----------------------------------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. 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
29template <std::size_t N>
30std::bitset<N>
31make_bitset()
32{
33    std::bitset<N> v;
34    for (std::size_t i = 0; i < N; ++i)
35        v[i] = static_cast<bool>(std::rand() & 1);
36    return v;
37}
38
39template <std::size_t N>
40void test_to_string()
41{
42{
43    std::bitset<N> v = make_bitset<N>();
44    {
45    std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >();
46    for (std::size_t i = 0; i < N; ++i)
47        if (v[i])
48            assert(s[N - 1 - i] == '1');
49        else
50            assert(s[N - 1 - i] == '0');
51    }
52    {
53    std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >();
54    for (std::size_t i = 0; i < N; ++i)
55        if (v[i])
56            assert(s[N - 1 - i] == '1');
57        else
58            assert(s[N - 1 - i] == '0');
59    }
60    {
61    std::string s = v.template to_string<char>();
62    for (std::size_t i = 0; i < N; ++i)
63        if (v[i])
64            assert(s[N - 1 - i] == '1');
65        else
66            assert(s[N - 1 - i] == '0');
67    }
68    {
69    std::string s = v.to_string();
70    for (std::size_t i = 0; i < N; ++i)
71        if (v[i])
72            assert(s[N - 1 - i] == '1');
73        else
74            assert(s[N - 1 - i] == '0');
75    }
76}
77{
78    std::bitset<N> v = make_bitset<N>();
79    {
80    std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >('0');
81    for (std::size_t i = 0; i < N; ++i)
82        if (v[i])
83            assert(s[N - 1 - i] == '1');
84        else
85            assert(s[N - 1 - i] == '0');
86    }
87    {
88    std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >('0');
89    for (std::size_t i = 0; i < N; ++i)
90        if (v[i])
91            assert(s[N - 1 - i] == '1');
92        else
93            assert(s[N - 1 - i] == '0');
94    }
95    {
96    std::string s = v.template to_string<char>('0');
97    for (std::size_t i = 0; i < N; ++i)
98        if (v[i])
99            assert(s[N - 1 - i] == '1');
100        else
101            assert(s[N - 1 - i] == '0');
102    }
103    {
104    std::string s = v.to_string('0');
105    for (std::size_t i = 0; i < N; ++i)
106        if (v[i])
107            assert(s[N - 1 - i] == '1');
108        else
109            assert(s[N - 1 - i] == '0');
110    }
111}
112{
113    std::bitset<N> v = make_bitset<N>();
114    {
115    std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >('0', '1');
116    for (std::size_t i = 0; i < N; ++i)
117        if (v[i])
118            assert(s[N - 1 - i] == '1');
119        else
120            assert(s[N - 1 - i] == '0');
121    }
122    {
123    std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >('0', '1');
124    for (std::size_t i = 0; i < N; ++i)
125        if (v[i])
126            assert(s[N - 1 - i] == '1');
127        else
128            assert(s[N - 1 - i] == '0');
129    }
130    {
131    std::string s = v.template to_string<char>('0', '1');
132    for (std::size_t i = 0; i < N; ++i)
133        if (v[i])
134            assert(s[N - 1 - i] == '1');
135        else
136            assert(s[N - 1 - i] == '0');
137    }
138    {
139    std::string s = v.to_string('0', '1');
140    for (std::size_t i = 0; i < N; ++i)
141        if (v[i])
142            assert(s[N - 1 - i] == '1');
143        else
144            assert(s[N - 1 - i] == '0');
145    }
146}
147}
148
149int main()
150{
151    test_to_string<0>();
152    test_to_string<1>();
153    test_to_string<31>();
154    test_to_string<32>();
155    test_to_string<33>();
156    test_to_string<63>();
157    test_to_string<64>();
158    test_to_string<65>();
159    test_to_string<1000>();
160}
161