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; // constexpr since C++23
14//
15// template <class charT, class traits>
16// basic_string<charT, traits, allocator<charT> > to_string() const; // constexpr since C++23
17//
18// template <class charT>
19// basic_string<charT, char_traits<charT>, allocator<charT> > to_string() const; // constexpr since C++23
20//
21// basic_string<char, char_traits<char>, allocator<char> > to_string() const; // constexpr since C++23
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
33template <class CharT, std::size_t N>
34TEST_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) {
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
45template <std::size_t N>
46TEST_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];
50 {
51 std::string s = v.template to_string<char>();
52 check_equal(s, v, '0', '1');
53 }
54 {
55 std::string s = v.to_string();
56 check_equal(s, v, '0', '1');
57 }
58 {
59 std::string s = v.template to_string<char>('0');
60 check_equal(s, v, '0', '1');
61 }
62 {
63 std::string s = v.to_string('0');
64 check_equal(s, v, '0', '1');
65 }
66 {
67 std::string s = v.template to_string<char>('0', '1');
68 check_equal(s, v, '0', '1');
69 }
70 {
71 std::string s = v.to_string('0', '1');
72 check_equal(s, v, '0', '1');
73 }
74 {
75 std::string s = v.to_string('x', 'y');
76 check_equal(s, v, 'x', 'y');
77 }
78 }
79 return true;
80}
81
82#ifndef TEST_HAS_NO_WIDE_CHARACTERS
83template <std::size_t N>
84TEST_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];
88 {
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');
91 }
92 {
93 std::wstring s = v.template to_string<wchar_t, std::char_traits<wchar_t> >();
94 check_equal(s, v, L'0', L'1');
95 }
96 {
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');
99 }
100 {
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');
103 }
104 {
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');
107 }
108 {
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');
111 }
112 }
113 return true;
114}
115#endif
116
117int main(int, char**) {
118 test_to_string<0>();
119 test_to_string<1>();
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>());
136#endif
137
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>());
155#endif
156#endif
157 return 0;
158}
159

source code of libcxx/test/std/utilities/template.bitset/bitset.members/to_string.pass.cpp