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 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
51 {
52 std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >();
53 check_equal(s, v, L'0', L'1');
54 }
55 {
56 std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >();
57 check_equal(s, v, L'0', L'1');
58 }
59 #endif
60 {
61 std::string s = v.template to_string<char>();
62 check_equal(s, v, '0', '1');
63 }
64 {
65 std::string s = v.to_string();
66 check_equal(s, v, '0', '1');
67 }
68 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
69 {
70 std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >('0');
71 check_equal(s, v, L'0', L'1');
72 }
73 {
74 std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >('0');
75 check_equal(s, v, L'0', L'1');
76 }
77 #endif
78 {
79 std::string s = v.template to_string<char>('0');
80 check_equal(s, v, '0', '1');
81 }
82 {
83 std::string s = v.to_string('0');
84 check_equal(s, v, '0', '1');
85 }
86 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
87 {
88 std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >('0', '1');
89 check_equal(s, v, L'0', L'1');
90 }
91 {
92 std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >('0', '1');
93 check_equal(s, v, L'0', L'1');
94 }
95 #endif
96 {
97 std::string s = v.template to_string<char>('0', '1');
98 check_equal(s, v, '0', '1');
99 }
100 {
101 std::string s = v.to_string('0', '1');
102 check_equal(s, v, '0', '1');
103 }
104 {
105 std::string s = v.to_string('x', 'y');
106 check_equal(s, v, 'x', 'y');
107 }
108 }
109 }
110
main(int,char **)111 int main(int, char**) {
112 test_to_string<0>();
113 test_to_string<1>();
114 test_to_string<31>();
115 test_to_string<32>();
116 test_to_string<33>();
117 test_to_string<63>();
118 test_to_string<64>();
119 test_to_string<65>();
120 test_to_string<1000>();
121
122 return 0;
123 }
124