Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_format_write.d

raw
1 @safe pure unittest
2 {
3 import std.format.write;
4
5 import std.array : appender;
6 import std.format.spec : singleSpec;
7
8 auto w1 = appender!string();
9 auto spec1 = singleSpec("%s");
10 formatValue(w1, true, spec1);
11
12 assert(w1.data == "true");
13
14 auto w2 = appender!string();
15 auto spec2 = singleSpec("%#x");
16 formatValue(w2, true, spec2);
17
18 assert(w2.data == "0x1");
19 }
20
21 @safe pure unittest
22 {
23 import std.format.write;
24
25 import std.array : appender;
26 import std.format.spec : singleSpec;
27
28 auto w = appender!string();
29 auto spec = singleSpec("%s");
30 formatValue(w, null, spec);
31
32 assert(w.data == "null");
33 }
34
35 @safe pure unittest
36 {
37 import std.format.write;
38
39 import std.array : appender;
40 import std.format.spec : singleSpec;
41
42 auto w1 = appender!string();
43 auto spec1 = singleSpec("%d");
44 formatValue(w1, -1337, spec1);
45
46 assert(w1.data == "-1337");
47
48 auto w2 = appender!string();
49 auto spec2 = singleSpec("%x");
50 formatValue(w2, -1337, spec2);
51
52 assert(w2.data == "fffffac7");
53 }
54
55 @safe unittest
56 {
57 import std.format.write;
58
59 import std.array : appender;
60 import std.format.spec : singleSpec;
61
62 auto w1 = appender!string();
63 auto spec1 = singleSpec("%.3f");
64 formatValue(w1, 1337.7779, spec1);
65
66 assert(w1.data == "1337.778");
67
68 auto w2 = appender!string();
69 auto spec2 = singleSpec("%.3e");
70 formatValue(w2, 1337.7779, spec2);
71
72 assert(w2.data == "1.338e+03");
73
74 auto w3 = appender!string();
75 auto spec3 = singleSpec("%.3g");
76 formatValue(w3, 1337.7779, spec3);
77
78 assert(w3.data == "1.34e+03");
79
80 auto w4 = appender!string();
81 auto spec4 = singleSpec("%.3a");
82 formatValue(w4, 1337.7779, spec4);
83
84 assert(w4.data == "0x1.4e7p+10");
85 }
86
87 @safe pure unittest
88 {
89 import std.format.write;
90
91 import std.array : appender;
92 import std.format.spec : singleSpec;
93
94 auto w1 = appender!string();
95 auto spec1 = singleSpec("%c");
96 formatValue(w1, 'ì', spec1);
97
98 assert(w1.data == "ì");
99
100 auto w2 = appender!string();
101 auto spec2 = singleSpec("%#x");
102 formatValue(w2, 'ì', spec2);
103
104 assert(w2.data == "0xec");
105 }
106
107 @safe pure unittest
108 {
109 import std.format.write;
110
111 import std.array : appender;
112 import std.format.spec : singleSpec;
113
114 auto w1 = appender!string();
115 auto spec1 = singleSpec("%s");
116 formatValue(w1, "hello", spec1);
117
118 assert(w1.data == "hello");
119
120 auto w2 = appender!string();
121 auto spec2 = singleSpec("%(%#x%|/%)");
122 formatValue(w2, "hello", spec2);
123
124 assert(w2.data == "0x68/0x65/0x6c/0x6c/0x6f");
125 }
126
127 @safe pure unittest
128 {
129 import std.format.write;
130
131 import std.array : appender;
132 import std.format.spec : singleSpec;
133
134 auto w = appender!string();
135 auto spec = singleSpec("%s");
136 int[2] two = [1, 2];
137 formatValue(w, two, spec);
138
139 assert(w.data == "[1, 2]");
140 }
141
142 @safe pure unittest
143 {
144 import std.format.write;
145
146 import std.array : appender;
147 import std.format.spec : singleSpec;
148
149 auto w1 = appender!string();
150 auto spec1 = singleSpec("%s");
151 auto two = [1, 2];
152 formatValue(w1, two, spec1);
153
154 assert(w1.data == "[1, 2]");
155
156 auto w2 = appender!string();
157 auto spec2 = singleSpec("%(%g%|, %)");
158 auto consts = [3.1415926, 299792458, 6.67430e-11];
159 formatValue(w2, consts, spec2);
160
161 assert(w2.data == "3.14159, 2.99792e+08, 6.6743e-11");
162
163 // void[] is treated like ubyte[]
164 auto w3 = appender!string();
165 auto spec3 = singleSpec("%s");
166 void[] val = cast(void[]) cast(ubyte[])[1, 2, 3];
167 formatValue(w3, val, spec3);
168
169 assert(w3.data == "[1, 2, 3]");
170 }
171
172 @safe pure unittest
173 {
174 import std.format.write;
175
176 import std.array : appender;
177 import std.format.spec : singleSpec;
178
179 auto aa = [10:17.5, 20:9.99];
180
181 auto w1 = appender!string();
182 auto spec1 = singleSpec("%s");
183 formatValue(w1, aa, spec1);
184
185 assert(w1.data == "[10:17.5, 20:9.99]" || w1.data == "[20:9.99, 10:17.5]");
186
187 auto w2 = appender!string();
188 auto spec2 = singleSpec("%(%x = %.0e%| # %)");
189 formatValue(w2, aa, spec2);
190
191 assert(w2.data == "a = 2e+01 # 14 = 1e+01" || w2.data == "14 = 1e+01 # a = 2e+01");
192 }
193
194 @safe pure unittest
195 {
196 import std.format.write;
197
198 import std.array : appender;
199 import std.format.spec : singleSpec;
200
201 enum A { first, second, third }
202
203 auto w1 = appender!string();
204 auto spec1 = singleSpec("%s");
205 formatValue(w1, A.second, spec1);
206
207 assert(w1.data == "second");
208
209 auto w2 = appender!string();
210 auto spec2 = singleSpec("%d");
211 formatValue(w2, A.second, spec2);
212
213 assert(w2.data == "1");
214
215 // values of an enum that have no name are formatted with %s using a cast
216 A a = A.third;
217 a++;
218
219 auto w3 = appender!string();
220 auto spec3 = singleSpec("%s");
221 formatValue(w3, a, spec3);
222
223 assert(w3.data == "cast(A)3");
224 }
225
226 @safe unittest
227 {
228 import std.format.write;
229
230 import std.array : appender;
231 import std.format.spec : FormatSpec, singleSpec;
232
233 // Using a `toString` with a writer
234 static struct Point1
235 {
236 import std.range.primitives : isOutputRange, put;
237
238 int x, y;
239
240 void toString(W)(ref W writer, scope const ref FormatSpec!char f)
241 if (isOutputRange!(W, char))
242 {
243 put(writer, "(");
244 formatValue(writer, x, f);
245 put(writer, ",");
246 formatValue(writer, y, f);
247 put(writer, ")");
248 }
249 }
250
251 auto w1 = appender!string();
252 auto spec1 = singleSpec("%s");
253 auto p1 = Point1(16, 11);
254
255 formatValue(w1, p1, spec1);
256 assert(w1.data == "(16,11)");
257
258 // Using a `toString` with a sink
259 static struct Point2
260 {
261 int x, y;
262
263 void toString(scope void delegate(scope const(char)[]) @safe sink,
264 scope const FormatSpec!char fmt) const
265 {
266 sink("(");
267 sink.formatValue(x, fmt);
268 sink(",");
269 sink.formatValue(y, fmt);
270 sink(")");
271 }
272 }
273
274 auto w2 = appender!string();
275 auto spec2 = singleSpec("%03d");
276 auto p2 = Point2(16,11);
277
278 formatValue(w2, p2, spec2);
279 assert(w2.data == "(016,011)");
280
281 // Using `string toString()`
282 static struct Point3
283 {
284 int x, y;
285
286 string toString()
287 {
288 import std.conv : to;
289
290 return "(" ~ to!string(x) ~ "," ~ to!string(y) ~ ")";
291 }
292 }
293
294 auto w3 = appender!string();
295 auto spec3 = singleSpec("%s"); // has to be %s
296 auto p3 = Point3(16,11);
297
298 formatValue(w3, p3, spec3);
299 assert(w3.data == "(16,11)");
300
301 // without `toString`
302 static struct Point4
303 {
304 int x, y;
305 }
306
307 auto w4 = appender!string();
308 auto spec4 = singleSpec("%s"); // has to be %s
309 auto p4 = Point4(16,11);
310
311 formatValue(w4, p4, spec3);
312 assert(w4.data == "Point4(16, 11)");
313 }
314
315 @safe pure unittest
316 {
317 import std.format.write;
318
319 import std.array : appender;
320 import std.format.spec : singleSpec;
321
322 auto w1 = appender!string();
323 auto spec1 = singleSpec("%s");
324 auto p1 = () @trusted { return cast(void*) 0xFFEECCAA; } ();
325 formatValue(w1, p1, spec1);
326
327 assert(w1.data == "FFEECCAA");
328
329 // null pointers are printed as `"null"` when used with `%s` and as hexadecimal integer else
330 auto w2 = appender!string();
331 auto spec2 = singleSpec("%s");
332 auto p2 = () @trusted { return cast(void*) 0x00000000; } ();
333 formatValue(w2, p2, spec2);
334
335 assert(w2.data == "null");
336
337 auto w3 = appender!string();
338 auto spec3 = singleSpec("%x");
339 formatValue(w3, p2, spec3);
340
341 assert(w3.data == "0");
342 }
343
344 @safe unittest
345 {
346 import std.format.write;
347
348 import core.simd; // cannot be selective, because float4 might not be defined
349 import std.array : appender;
350 import std.format.spec : singleSpec;
351
352 auto w = appender!string();
353 auto spec = singleSpec("%s");
354
355 static if (is(float4))
356 {
357 version (X86) {}
358 else
359 {
360 float4 f4;
361 f4.array[0] = 1;
362 f4.array[1] = 2;
363 f4.array[2] = 3;
364 f4.array[3] = 4;
365
366 formatValue(w, f4, spec);
367 assert(w.data == "[1, 2, 3, 4]");
368 }
369 }
370 }
371
372 @safe pure unittest
373 {
374 import std.format.write;
375
376 import std.array : appender;
377
378 auto writer1 = appender!string();
379 formattedWrite(writer1, "%s is the ultimate %s.", 42, "answer");
380 assert(writer1[] == "42 is the ultimate answer.");
381
382 auto writer2 = appender!string();
383 formattedWrite(writer2, "Increase: %7.2f %%", 17.4285);
384 assert(writer2[] == "Increase: 17.43 %");
385 }
386
387 @safe pure unittest
388 {
389 import std.format.write;
390
391 import std.array : appender;
392
393 auto writer = appender!string();
394 writer.formattedWrite!"%d is the ultimate %s."(42, "answer");
395 assert(writer[] == "42 is the ultimate answer.");
396
397 // This line doesn't compile, because 3.14 cannot be formatted with %d:
398 // writer.formattedWrite!"%d is the ultimate %s."(3.14, "answer");
399 }
400
401 @safe pure unittest
402 {
403 import std.format.write;
404
405 import std.array : appender;
406 import std.format.spec : singleSpec;
407
408 auto writer = appender!string();
409 auto spec = singleSpec("%08b");
410 writer.formatValue(42, spec);
411 assert(writer.data == "00101010");
412
413 spec = singleSpec("%2s");
414 writer.formatValue('=', spec);
415 assert(writer.data == "00101010 =");
416
417 spec = singleSpec("%+14.6e");
418 writer.formatValue(42.0, spec);
419 assert(writer.data == "00101010 = +4.200000e+01");
420 }
421