1//===----------------------------------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// <locale>
11
12// class num_put<charT, OutputIterator>
13
14// iter_type put(iter_type s, ios_base& iob, char_type fill, unsigned long long v) const;
15
16#include <locale>
17#include <ios>
18#include <cassert>
19#include <streambuf>
20#include "test_iterators.h"
21
22typedef std::num_put<char, output_iterator<char*> > F;
23
24class my_facet
25    : public F
26{
27public:
28    explicit my_facet(std::size_t refs = 0)
29        : F(refs) {}
30};
31
32class my_numpunct
33    : public std::numpunct<char>
34{
35public:
36    my_numpunct() : std::numpunct<char>() {}
37
38protected:
39    virtual char_type do_thousands_sep() const {return '_';}
40    virtual std::string do_grouping() const {return std::string("\1\2\3");}
41};
42
43int main()
44{
45    const my_facet f(1);
46    {
47        std::ios ios(0);
48        unsigned long long v = 0;
49        char str[50];
50        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
51        std::string ex(str, iter.base());
52        assert(ex == "0");
53    }
54    {
55        std::ios ios(0);
56        unsigned long long v = 1;
57        char str[50];
58        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
59        std::string ex(str, iter.base());
60        assert(ex == "1");
61    }
62    {
63        std::ios ios(0);
64        unsigned long long v = -1;
65        char str[50];
66        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
67        std::string ex(str, iter.base());
68        assert(ex == (sizeof(unsigned long long) == 4 ? "4294967295" : "18446744073709551615"));
69    }
70    {
71        std::ios ios(0);
72        unsigned long long v = -1000;
73        char str[50];
74        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
75        std::string ex(str, iter.base());
76        assert(ex == "18446744073709550616");
77    }
78    {
79        std::ios ios(0);
80        unsigned long long v = 1000;
81        char str[50];
82        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
83        std::string ex(str, iter.base());
84        assert(ex == "1000");
85    }
86    {
87        std::ios ios(0);
88        showpos(ios);
89        unsigned long long v = 1000;
90        char str[50];
91        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
92        std::string ex(str, iter.base());
93        assert(ex == "1000");
94    }
95    {
96        std::ios ios(0);
97        oct(ios);
98        unsigned long long v = 1000;
99        char str[50];
100        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
101        std::string ex(str, iter.base());
102        assert(ex == "1750");
103    }
104    {
105        std::ios ios(0);
106        oct(ios);
107        showbase(ios);
108        unsigned long long v = 1000;
109        char str[50];
110        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
111        std::string ex(str, iter.base());
112        assert(ex == "01750");
113    }
114    {
115        std::ios ios(0);
116        hex(ios);
117        unsigned long long v = 1000;
118        char str[50];
119        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
120        std::string ex(str, iter.base());
121        assert(ex == "3e8");
122    }
123    {
124        std::ios ios(0);
125        hex(ios);
126        showbase(ios);
127        unsigned long long v = 1000;
128        char str[50];
129        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
130        std::string ex(str, iter.base());
131        assert(ex == "0x3e8");
132    }
133    {
134        std::ios ios(0);
135        hex(ios);
136        showbase(ios);
137        uppercase(ios);
138        unsigned long long v = 1000;
139        char str[50];
140        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
141        std::string ex(str, iter.base());
142        assert(ex == "0X3E8");
143    }
144    {
145        std::ios ios(0);
146        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
147        hex(ios);
148        showbase(ios);
149        uppercase(ios);
150        unsigned long long v = 1000;
151        char str[50];
152        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
153        std::string ex(str, iter.base());
154        assert(ex == "0X3E_8");
155    }
156    {
157        std::ios ios(0);
158        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
159        hex(ios);
160        showbase(ios);
161        unsigned long long v = 2147483647;
162        char str[50];
163        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
164        std::string ex(str, iter.base());
165        assert(ex == "0x7f_fff_ff_f");
166    }
167    {
168        std::ios ios(0);
169        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
170        oct(ios);
171        unsigned long long v = 0123467;
172        char str[50];
173        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
174        std::string ex(str, iter.base());
175        assert(ex == "123_46_7");
176    }
177    {
178        std::ios ios(0);
179        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
180        oct(ios);
181        showbase(ios);
182        unsigned long long v = 0123467;
183        char str[50];
184        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
185        std::string ex(str, iter.base());
186        assert(ex == "0_123_46_7");
187    }
188    {
189        std::ios ios(0);
190        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
191        oct(ios);
192        showbase(ios);
193        right(ios);
194        ios.width(15);
195        unsigned long long v = 0123467;
196        char str[50];
197        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
198        std::string ex(str, iter.base());
199        assert(ex == "*****0_123_46_7");
200    }
201    {
202        std::ios ios(0);
203        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
204        oct(ios);
205        showbase(ios);
206        left(ios);
207        ios.width(15);
208        unsigned long long v = 0123467;
209        char str[50];
210        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
211        std::string ex(str, iter.base());
212        assert(ex == "0_123_46_7*****");
213    }
214    {
215        std::ios ios(0);
216        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
217        oct(ios);
218        showbase(ios);
219        internal(ios);
220        ios.width(15);
221        unsigned long long v = 0123467;
222        char str[50];
223        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
224        std::string ex(str, iter.base());
225        assert(ex == "*****0_123_46_7");
226        assert(ios.width() == 0);
227    }
228    {
229        std::ios ios(0);
230        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
231        hex(ios);
232        showbase(ios);
233        right(ios);
234        ios.width(15);
235        unsigned long long v = 2147483647;
236        char str[50];
237        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
238        std::string ex(str, iter.base());
239        assert(ex == "**0x7f_fff_ff_f");
240    }
241    {
242        std::ios ios(0);
243        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
244        hex(ios);
245        showbase(ios);
246        left(ios);
247        ios.width(15);
248        unsigned long long v = 2147483647;
249        char str[50];
250        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
251        std::string ex(str, iter.base());
252        assert(ex == "0x7f_fff_ff_f**");
253    }
254    {
255        std::ios ios(0);
256        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
257        hex(ios);
258        showbase(ios);
259        internal(ios);
260        ios.width(15);
261        unsigned long long v = 2147483647;
262        char str[50];
263        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
264        std::string ex(str, iter.base());
265        assert(ex == "0x**7f_fff_ff_f");
266        assert(ios.width() == 0);
267    }
268    {
269        std::ios ios(0);
270        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
271        showpos(ios);
272        unsigned long long v = 1000;
273        right(ios);
274        ios.width(10);
275        char str[50];
276        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
277        std::string ex(str, iter.base());
278        assert(ex == "****1_00_0");
279        assert(ios.width() == 0);
280    }
281    {
282        std::ios ios(0);
283        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
284        showpos(ios);
285        unsigned long long v = 1000;
286        left(ios);
287        ios.width(10);
288        char str[50];
289        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
290        std::string ex(str, iter.base());
291        assert(ex == "1_00_0****");
292        assert(ios.width() == 0);
293    }
294    {
295        std::ios ios(0);
296        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
297        showpos(ios);
298        unsigned long long v = 1000;
299        internal(ios);
300        ios.width(10);
301        char str[50];
302        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
303        std::string ex(str, iter.base());
304        assert(ex == "****1_00_0");
305        assert(ios.width() == 0);
306    }
307    {
308        std::ios ios(0);
309        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
310        unsigned long long v = -1000;
311        right(ios);
312        showpos(ios);
313        ios.width(10);
314        char str[50];
315        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
316        std::string ex(str, iter.base());
317        assert(ex == "18_446_744_073_709_550_61_6");
318        assert(ios.width() == 0);
319    }
320    {
321        std::ios ios(0);
322        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
323        unsigned long long v = -1000;
324        left(ios);
325        ios.width(10);
326        char str[50];
327        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
328        std::string ex(str, iter.base());
329        assert(ex == "18_446_744_073_709_550_61_6");
330        assert(ios.width() == 0);
331    }
332    {
333        std::ios ios(0);
334        ios.imbue(std::locale(std::locale::classic(), new my_numpunct));
335        unsigned long long v = -1000;
336        internal(ios);
337        ios.width(10);
338        char str[50];
339        output_iterator<char*> iter = f.put(output_iterator<char*>(str), ios, '*', v);
340        std::string ex(str, iter.base());
341        assert(ex == "18_446_744_073_709_550_61_6");
342        assert(ios.width() == 0);
343    }
344}
345