1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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; // constexpr since C++23
15 // template <class charT, class traits>
16 // basic_string<charT, traits, allocator<charT> > to_string() const; // constexpr since C++23
18 // template <class charT>
19 // basic_string<charT, char_traits<charT>, allocator<charT> > to_string() const; // constexpr since C++23
21 // basic_string<char, char_traits<char>, allocator<char> > to_string() const; // constexpr since C++23
26 #include <memory> // for std::allocator
30 #include "../bitset_test_cases.h"
31 #include "test_macros.h"
33 template <class CharT
, std::size_t N
>
34 TEST_CONSTEXPR_CXX23
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
) {
38 assert(s
[b
.size() - 1 - i
] == one
);
40 assert(s
[b
.size() - 1 - i
] == zero
);
45 template <std::size_t N
>
46 TEST_CONSTEXPR_CXX23
bool 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
];
51 std::string s
= v
.template to_string
<char>();
52 check_equal(s
, v
, '0', '1');
55 std::string s
= v
.to_string();
56 check_equal(s
, v
, '0', '1');
59 std::string s
= v
.template to_string
<char>('0');
60 check_equal(s
, v
, '0', '1');
63 std::string s
= v
.to_string('0');
64 check_equal(s
, v
, '0', '1');
67 std::string s
= v
.template to_string
<char>('0', '1');
68 check_equal(s
, v
, '0', '1');
71 std::string s
= v
.to_string('0', '1');
72 check_equal(s
, v
, '0', '1');
75 std::string s
= v
.to_string('x', 'y');
76 check_equal(s
, v
, 'x', 'y');
82 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
83 template <std::size_t N
>
84 TEST_CONSTEXPR_CXX23
bool test_to_string_wchar() {
85 std::vector
<std::bitset
<N
> > const cases
= get_test_cases
<N
>();
86 for (std::size_t c
= 0; c
!= cases
.size(); ++c
) {
87 std::bitset
<N
> const v
= cases
[c
];
89 std::wstring s
= v
.template to_string
<wchar_t, std::char_traits
<wchar_t>, std::allocator
<wchar_t> >();
90 check_equal(s
, v
, L
'0', L
'1');
93 std::wstring s
= v
.template to_string
<wchar_t, std::char_traits
<wchar_t> >();
94 check_equal(s
, v
, L
'0', L
'1');
97 std::wstring s
= v
.template to_string
<wchar_t, std::char_traits
<wchar_t>, std::allocator
<wchar_t> >('0');
98 check_equal(s
, v
, L
'0', L
'1');
101 std::wstring s
= v
.template to_string
<wchar_t, std::char_traits
<wchar_t> >('0');
102 check_equal(s
, v
, L
'0', L
'1');
105 std::wstring s
= v
.template to_string
<wchar_t, std::char_traits
<wchar_t>, std::allocator
<wchar_t> >('0', '1');
106 check_equal(s
, v
, L
'0', L
'1');
109 std::wstring s
= v
.template to_string
<wchar_t, std::char_traits
<wchar_t> >('0', '1');
110 check_equal(s
, v
, L
'0', L
'1');
117 int main(int, char**) {
120 test_to_string
<31>();
121 test_to_string
<32>();
122 test_to_string
<33>();
123 test_to_string
<63>();
124 test_to_string
<64>();
125 test_to_string
<65>();
126 test_to_string
<1000>(); // not in constexpr because of constexpr evaluation step limits
127 #if TEST_STD_VER > 20
128 static_assert(test_to_string
<0>());
129 static_assert(test_to_string
<1>());
130 static_assert(test_to_string
<31>());
131 static_assert(test_to_string
<32>());
132 static_assert(test_to_string
<33>());
133 static_assert(test_to_string
<63>());
134 static_assert(test_to_string
<64>());
135 static_assert(test_to_string
<65>());
138 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
139 test_to_string_wchar
<0>();
140 test_to_string_wchar
<1>();
141 test_to_string_wchar
<31>();
142 test_to_string_wchar
<32>();
143 test_to_string_wchar
<33>();
144 test_to_string_wchar
<63>();
145 test_to_string_wchar
<64>();
146 test_to_string_wchar
<65>();
147 test_to_string_wchar
<1000>(); // not in constexpr because of constexpr evaluation step limits
148 #if TEST_STD_VER > 20
149 static_assert(test_to_string_wchar
<0>());
150 static_assert(test_to_string_wchar
<1>());
151 static_assert(test_to_string_wchar
<31>());
152 static_assert(test_to_string_wchar
<32>());
153 static_assert(test_to_string_wchar
<33>());
154 static_assert(test_to_string_wchar
<63>());