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// <locale>
10
11// class num_put<charT, OutputIterator>
12
13// iter_type put(iter_type s, ios_base& iob, char_type fill, long v) const;
14
15#include <locale>
16#include <ios>
17#include <cassert>
18#include <streambuf>
19#include "test_macros.h"
20#include "test_iterators.h"
21
22typedef std::num_put<char, cpp17_output_iterator<char*> > F;
23
24class my_facet : public F {
25public:
26 explicit my_facet(std::size_t refs = 0) : F(refs) {}
27};
28
29class my_numpunct : public std::numpunct<char> {
30public:
31 my_numpunct() : std::numpunct<char>() {}
32
33protected:
34 virtual char_type do_thousands_sep() const { return '_'; }
35 virtual std::string do_grouping() const { return std::string("\1\2\3"); }
36};
37
38int main(int, char**) {
39 const my_facet f(1);
40 { // test decimal
41 {
42 std::ios ios(0);
43 long v = 0;
44 char str[50];
45 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
46 std::string ex(str, base(iter));
47 assert(ex == "0");
48 }
49 {
50 std::ios ios(0);
51 long v = 1;
52 char str[50];
53 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
54 std::string ex(str, base(iter));
55 assert(ex == "1");
56 }
57 {
58 std::ios ios(0);
59 long v = -1;
60 char str[50];
61 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
62 std::string ex(str, base(iter));
63 assert(ex == "-1");
64 }
65 {
66 std::ios ios(0);
67 long v = -1000;
68 char str[50];
69 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
70 std::string ex(str, base(iter));
71 assert(ex == "-1000");
72 }
73 {
74 std::ios ios(0);
75 long v = 1000;
76 char str[50];
77 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
78 std::string ex(str, base(iter));
79 assert(ex == "1000");
80 }
81 {
82 std::ios ios(0);
83 std::showpos(base&: ios);
84 long v = 1000;
85 char str[50];
86 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
87 std::string ex(str, base(iter));
88 assert(ex == "+1000");
89 }
90 { // Ensure that std::showpos always prints the +
91 std::ios ios(0);
92 std::showpos(base&: ios);
93 long v = 0;
94 char str[50];
95 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
96 std::string ex(str, base(iter));
97 assert(ex == "+0");
98 }
99 }
100 { // test octal
101 {
102 std::ios ios(0);
103 std::oct(base&: ios);
104 long v = 1000;
105 char str[50];
106 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
107 std::string ex(str, base(iter));
108 assert(ex == "1750");
109 }
110 {
111 std::ios ios(0);
112 std::oct(base&: ios);
113 std::showbase(base&: ios);
114 long v = 1000;
115 char str[50];
116 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
117 std::string ex(str, base(iter));
118 assert(ex == "01750");
119 }
120 { // Ensure that std::showbase doesn't print a prefix for 0
121 std::ios ios(0);
122 std::oct(base&: ios);
123 std::showbase(base&: ios);
124 long v = 0;
125 char str[50];
126 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
127 std::string ex(str, base(iter));
128 assert(ex == "0");
129 }
130 { // Ensure that std::showpos doesn't print a '+' when printing octal
131 std::ios ios(0);
132 std::oct(base&: ios);
133 std::showpos(base&: ios);
134 long v = 0;
135 char str[50];
136 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
137 std::string ex(str, base(iter));
138 assert(ex == "0");
139 }
140 }
141 { // test hexadecimal
142 {
143 std::ios ios(0);
144 std::hex(base&: ios);
145 long v = 1000;
146 char str[50];
147 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
148 std::string ex(str, base(iter));
149 assert(ex == "3e8");
150 }
151 {
152 std::ios ios(0);
153 std::hex(base&: ios);
154 std::showbase(base&: ios);
155 long v = 1000;
156 char str[50];
157 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
158 std::string ex(str, base(iter));
159 assert(ex == "0x3e8");
160 }
161 {
162 std::ios ios(0);
163 std::hex(base&: ios);
164 std::showbase(base&: ios);
165 std::uppercase(base&: ios);
166 long v = 1000;
167 char str[50];
168 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
169 std::string ex(str, base(iter));
170 assert(ex == "0X3E8");
171 }
172 {
173 std::ios ios(0);
174 ios.imbue(loc: std::locale(std::locale::classic(), new my_numpunct));
175 std::hex(base&: ios);
176 std::showbase(base&: ios);
177 std::uppercase(base&: ios);
178 long v = 1000;
179 char str[50];
180 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
181 std::string ex(str, base(iter));
182 assert(ex == "0X3E_8");
183 }
184 {
185 std::ios ios(0);
186 ios.imbue(loc: std::locale(std::locale::classic(), new my_numpunct));
187 std::hex(base&: ios);
188 std::showbase(base&: ios);
189 long v = 2147483647;
190 char str[50];
191 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
192 std::string ex(str, base(iter));
193 assert(ex == "0x7f_fff_ff_f");
194 }
195 { // Ensure that std::showbase doesn't print a prefix for 0
196 std::ios ios(0);
197 std::hex(base&: ios);
198 std::showbase(base&: ios);
199 long v = 0;
200 char str[50];
201 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
202 std::string ex(str, base(iter));
203 assert(ex == "0");
204 }
205 { // Ensure that std::showpos doesn't print a '+' when printing hexadecimal
206 std::ios ios(0);
207 std::hex(base&: ios);
208 std::showpos(base&: ios);
209 long v = 0;
210 char str[50];
211 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
212 std::string ex(str, base(iter));
213 assert(ex == "0");
214 }
215 }
216 {
217 std::ios ios(0);
218 ios.imbue(loc: std::locale(std::locale::classic(), new my_numpunct));
219 std::oct(base&: ios);
220 long v = 0123467;
221 char str[50];
222 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
223 std::string ex(str, base(iter));
224 assert(ex == "123_46_7");
225 }
226 {
227 std::ios ios(0);
228 ios.imbue(loc: std::locale(std::locale::classic(), new my_numpunct));
229 std::oct(base&: ios);
230 std::showbase(base&: ios);
231 long v = 0123467;
232 char str[50];
233 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
234 std::string ex(str, base(iter));
235 assert(ex == "0_123_46_7");
236 }
237 {
238 std::ios ios(0);
239 ios.imbue(loc: std::locale(std::locale::classic(), new my_numpunct));
240 std::oct(base&: ios);
241 std::showbase(base&: ios);
242 std::right(base&: ios);
243 ios.width(wide: 15);
244 long v = 0123467;
245 char str[50];
246 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
247 std::string ex(str, base(iter));
248 assert(ex == "*****0_123_46_7");
249 }
250 {
251 std::ios ios(0);
252 ios.imbue(loc: std::locale(std::locale::classic(), new my_numpunct));
253 std::oct(base&: ios);
254 std::showbase(base&: ios);
255 std::left(base&: ios);
256 ios.width(wide: 15);
257 long v = 0123467;
258 char str[50];
259 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
260 std::string ex(str, base(iter));
261 assert(ex == "0_123_46_7*****");
262 }
263 {
264 std::ios ios(0);
265 ios.imbue(loc: std::locale(std::locale::classic(), new my_numpunct));
266 std::oct(base&: ios);
267 std::showbase(base&: ios);
268 std::internal(base&: ios);
269 ios.width(wide: 15);
270 long v = 0123467;
271 char str[50];
272 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
273 std::string ex(str, base(iter));
274 assert(ex == "*****0_123_46_7");
275 assert(ios.width() == 0);
276 }
277 {
278 std::ios ios(0);
279 ios.imbue(loc: std::locale(std::locale::classic(), new my_numpunct));
280 std::hex(base&: ios);
281 std::showbase(base&: ios);
282 std::right(base&: ios);
283 ios.width(wide: 15);
284 long v = 2147483647;
285 char str[50];
286 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
287 std::string ex(str, base(iter));
288 assert(ex == "**0x7f_fff_ff_f");
289 }
290 {
291 std::ios ios(0);
292 ios.imbue(loc: std::locale(std::locale::classic(), new my_numpunct));
293 std::hex(base&: ios);
294 std::showbase(base&: ios);
295 std::left(base&: ios);
296 ios.width(wide: 15);
297 long v = 2147483647;
298 char str[50];
299 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
300 std::string ex(str, base(iter));
301 assert(ex == "0x7f_fff_ff_f**");
302 }
303 {
304 std::ios ios(0);
305 ios.imbue(loc: std::locale(std::locale::classic(), new my_numpunct));
306 std::hex(base&: ios);
307 std::showbase(base&: ios);
308 std::internal(base&: ios);
309 ios.width(wide: 15);
310 long v = 2147483647;
311 char str[50];
312 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
313 std::string ex(str, base(iter));
314 assert(ex == "0x**7f_fff_ff_f");
315 assert(ios.width() == 0);
316 }
317 {
318 std::ios ios(0);
319 ios.imbue(loc: std::locale(std::locale::classic(), new my_numpunct));
320 std::showpos(base&: ios);
321 long v = 1000;
322 std::right(base&: ios);
323 ios.width(wide: 10);
324 char str[50];
325 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
326 std::string ex(str, base(iter));
327 assert(ex == "***+1_00_0");
328 assert(ios.width() == 0);
329 }
330 {
331 std::ios ios(0);
332 ios.imbue(loc: std::locale(std::locale::classic(), new my_numpunct));
333 std::showpos(base&: ios);
334 long v = 1000;
335 std::left(base&: ios);
336 ios.width(wide: 10);
337 char str[50];
338 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
339 std::string ex(str, base(iter));
340 assert(ex == "+1_00_0***");
341 assert(ios.width() == 0);
342 }
343 {
344 std::ios ios(0);
345 ios.imbue(loc: std::locale(std::locale::classic(), new my_numpunct));
346 std::showpos(base&: ios);
347 long v = 1000;
348 std::internal(base&: ios);
349 ios.width(wide: 10);
350 char str[50];
351 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
352 std::string ex(str, base(iter));
353 assert(ex == "+***1_00_0");
354 assert(ios.width() == 0);
355 }
356 {
357 std::ios ios(0);
358 ios.imbue(loc: std::locale(std::locale::classic(), new my_numpunct));
359 long v = -1000;
360 std::right(base&: ios);
361 std::showpos(base&: ios);
362 ios.width(wide: 10);
363 char str[50];
364 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
365 std::string ex(str, base(iter));
366 assert(ex == "***-1_00_0");
367 assert(ios.width() == 0);
368 }
369 {
370 std::ios ios(0);
371 ios.imbue(loc: std::locale(std::locale::classic(), new my_numpunct));
372 long v = -1000;
373 std::left(base&: ios);
374 ios.width(wide: 10);
375 char str[50];
376 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
377 std::string ex(str, base(iter));
378 assert(ex == "-1_00_0***");
379 assert(ios.width() == 0);
380 }
381 {
382 std::ios ios(0);
383 ios.imbue(loc: std::locale(std::locale::classic(), new my_numpunct));
384 long v = -1000;
385 std::internal(base&: ios);
386 ios.width(wide: 10);
387 char str[50];
388 cpp17_output_iterator<char*> iter = f.put(cpp17_output_iterator<char*>(str), ios, '*', v);
389 std::string ex(str, base(iter));
390 assert(ex == "-***1_00_0");
391 assert(ios.width() == 0);
392 }
393
394 return 0;
395}
396

source code of libcxx/test/std/localization/locale.categories/category.numeric/locale.nm.put/facet.num.put.members/put_long.pass.cpp