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 <cassert>
25 #include <cstddef>
26 #include <memory> // for std::allocator
27 #include <string>
28 #include <vector>
29 
30 #include "../bitset_test_cases.h"
31 #include "test_macros.h"
32 
33 template <class CharT, std::size_t N>
check_equal(std::basic_string<CharT> const & s,std::bitset<N> const & b,CharT zero,CharT one)34 void check_equal(std::basic_string<CharT> const& s, std::bitset<N> const& b, CharT zero, CharT one) {
35     assert(s.size() == b.size());
36     for (std::size_t i = 0; i < b.size(); ++i) {
37         if (b[i]) {
38             assert(s[b.size() - 1 - i] == one);
39         } else {
40             assert(s[b.size() - 1 - i] == zero);
41         }
42     }
43 }
44 
45 template <std::size_t N>
test_to_string()46 void test_to_string() {
47     std::vector<std::bitset<N> > const cases = get_test_cases<N>();
48     for (std::size_t c = 0; c != cases.size(); ++c) {
49         std::bitset<N> const v = cases[c];
50         {
51             std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >();
52             check_equal(s, v, L'0', L'1');
53         }
54         {
55             std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >();
56             check_equal(s, v, L'0', L'1');
57         }
58         {
59             std::string s = v.template to_string<char>();
60             check_equal(s, v, '0', '1');
61         }
62         {
63             std::string s = v.to_string();
64             check_equal(s, v, '0', '1');
65         }
66         {
67             std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >('0');
68             check_equal(s, v, L'0', L'1');
69         }
70         {
71             std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >('0');
72             check_equal(s, v, L'0', L'1');
73         }
74         {
75             std::string s = v.template to_string<char>('0');
76             check_equal(s, v, '0', '1');
77         }
78         {
79             std::string s = v.to_string('0');
80             check_equal(s, v, '0', '1');
81         }
82         {
83             std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >('0', '1');
84             check_equal(s, v, L'0', L'1');
85         }
86         {
87             std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >('0', '1');
88             check_equal(s, v, L'0', L'1');
89         }
90         {
91             std::string s = v.template to_string<char>('0', '1');
92             check_equal(s, v, '0', '1');
93         }
94         {
95             std::string s = v.to_string('0', '1');
96             check_equal(s, v, '0', '1');
97         }
98         {
99             std::string s = v.to_string('x', 'y');
100             check_equal(s, v, 'x', 'y');
101         }
102     }
103 }
104 
main(int,char **)105 int main(int, char**) {
106     test_to_string<0>();
107     test_to_string<1>();
108     test_to_string<31>();
109     test_to_string<32>();
110     test_to_string<33>();
111     test_to_string<63>();
112     test_to_string<64>();
113     test_to_string<65>();
114     test_to_string<1000>();
115 
116     return 0;
117 }
118