1
2
3
4
5
6
7 package printf
8
9 import (
10 "fmt"
11 logpkg "log"
12 "math"
13 "os"
14 "testing"
15 "unsafe"
16
17
18 )
19
20 func UnsafePointerPrintfTest() {
21 var up unsafe.Pointer
22 fmt.Printf("%p, %x %X", up, up, up)
23 }
24
25
26 type errorTest1 int
27
28 func (errorTest1) Error(...interface{}) string {
29 return "hi"
30 }
31
32 type errorTest2 int
33 func (errorTest2) Error(...interface{}) {
34 }
35
36 type errorTest3 int
37
38 func (errorTest3) Error() {
39 }
40
41 type errorTest4 int
42
43 func (errorTest4) Error() int {
44 return 3
45 }
46
47 type errorTest5 int
48
49 func (errorTest5) error() {
50 }
51
52
53
54 func PrintfTests() {
55 var b bool
56 var i int
57 var r rune
58 var s string
59 var x float64
60 var p *int
61 var imap map[int]int
62 var fslice []float64
63 var c complex64
64
65 fmt.Printf("")
66 fmt.Printf("%b %b %b", 3, i, x)
67 fmt.Printf("%c %c %c %c", 3, i, 'x', r)
68 fmt.Printf("%d %d %d", 3, i, imap)
69 fmt.Printf("%e %e %e %e", 3e9, x, fslice, c)
70 fmt.Printf("%E %E %E %E", 3e9, x, fslice, c)
71 fmt.Printf("%f %f %f %f", 3e9, x, fslice, c)
72 fmt.Printf("%F %F %F %F", 3e9, x, fslice, c)
73 fmt.Printf("%g %g %g %g", 3e9, x, fslice, c)
74 fmt.Printf("%G %G %G %G", 3e9, x, fslice, c)
75 fmt.Printf("%b %b %b %b", 3e9, x, fslice, c)
76 fmt.Printf("%o %o", 3, i)
77 fmt.Printf("%p", p)
78 fmt.Printf("%q %q %q", rune(3), 'x', r)
79 fmt.Printf("%s %s %s", "hi", s, []byte{65})
80 fmt.Printf("%t %t", true, b)
81 fmt.Printf("%T %T", 3, i)
82 fmt.Printf("%U %U", 3, i)
83 fmt.Printf("%v %v", 3, i)
84 fmt.Printf("%x %x %x %x %x %x %x", 3, i, "hi", s, x, c, fslice)
85 fmt.Printf("%X %X %X %X %X %X %X", 3, i, "hi", s, x, c, fslice)
86 fmt.Printf("%.*s %d %g", 3, "hi", 23, 2.3)
87 fmt.Printf("%s", &stringerv)
88 fmt.Printf("%v", &stringerv)
89 fmt.Printf("%T", &stringerv)
90 fmt.Printf("%s", &embeddedStringerv)
91 fmt.Printf("%v", &embeddedStringerv)
92 fmt.Printf("%T", &embeddedStringerv)
93 fmt.Printf("%v", notstringerv)
94 fmt.Printf("%T", notstringerv)
95 fmt.Printf("%q", stringerarrayv)
96 fmt.Printf("%v", stringerarrayv)
97 fmt.Printf("%s", stringerarrayv)
98 fmt.Printf("%v", notstringerarrayv)
99 fmt.Printf("%T", notstringerarrayv)
100 fmt.Printf("%d", new(fmt.Formatter))
101
102 fmt.Printf("%*%", 2)
103 fmt.Printf("%s", interface{}(nil))
104
105 fmt.Printf("%g", 1+2i)
106 fmt.Printf("%#e %#E %#f %#F %#g %#G", 1.2, 1.2, 1.2, 1.2, 1.2, 1.2)
107
108 fmt.Printf("%b", "hi")
109 fmt.Printf("%t", c)
110 fmt.Printf("%t", 1+2i)
111 fmt.Printf("%c", 2.3)
112 fmt.Printf("%d", 2.3)
113 fmt.Printf("%e", "hi")
114 fmt.Printf("%E", true)
115 fmt.Printf("%f", "hi")
116 fmt.Printf("%F", 'x')
117 fmt.Printf("%g", "hi")
118 fmt.Printf("%g", imap)
119 fmt.Printf("%G", i)
120 fmt.Printf("%o", x)
121 fmt.Printf("%p", nil)
122 fmt.Printf("%p", 23)
123 fmt.Printf("%q", x)
124 fmt.Printf("%s", b)
125 fmt.Printf("%s", byte(65))
126 fmt.Printf("%t", 23)
127 fmt.Printf("%U", x)
128 fmt.Printf("%x", nil)
129 fmt.Printf("%s", stringerv)
130 fmt.Printf("%t", stringerv)
131 fmt.Printf("%s", embeddedStringerv)
132 fmt.Printf("%t", embeddedStringerv)
133 fmt.Printf("%q", notstringerv)
134 fmt.Printf("%t", notstringerv)
135 fmt.Printf("%t", stringerarrayv)
136 fmt.Printf("%t", notstringerarrayv)
137 fmt.Printf("%q", notstringerarrayv)
138 fmt.Printf("%d", BoolFormatter(true))
139 fmt.Printf("%z", FormatterVal(true))
140 fmt.Printf("%d", FormatterVal(true))
141 fmt.Printf("%s", nonemptyinterface)
142 fmt.Printf("%.*s %d %6g", 3, "hi", 23, 'x')
143 fmt.Println()
144 fmt.Println("%s", "hi")
145 fmt.Println("%v", "hi")
146 fmt.Println("%T", "hi")
147 fmt.Println("0.0%")
148 fmt.Printf("%s", "hi", 3)
149 _ = fmt.Sprintf("%"+("s"), "hi", 3)
150 fmt.Printf("%s%%%d", "hi", 3)
151 fmt.Printf("%08s", "woo")
152 fmt.Printf("% 8s", "woo")
153 fmt.Printf("%.*d", 3, 3)
154 fmt.Printf("%.*d x", 3, 3, 3, 3)
155 fmt.Printf("%.*d x", "hi", 3)
156 fmt.Printf("%.*d x", i, 3)
157 fmt.Printf("%.*d x", s, 3)
158 fmt.Printf("%*% x", 0.22)
159 fmt.Printf("%q %q", multi()...)
160 fmt.Printf("%#q", `blah`)
161
162 Printf("now is the time", "buddy")
163 Printf("hi")
164 const format = "%s %s\n"
165 Printf(format, "hi", "there")
166 Printf(format, "hi")
167 Printf("%s %d %.3v %q", "str", 4)
168 f := new(ptrStringer)
169 f.Warn(0, "%s", "hello", 3)
170 f.Warnf(0, "%s", "hello", 3)
171 f.Warnf(0, "%r", "hello")
172 f.Warnf(0, "%#s", "hello")
173 f.Warn2(0, "%s", "hello", 3)
174 f.Warnf2(0, "%s", "hello", 3)
175 f.Warnf2(0, "%r", "hello")
176 f.Warnf2(0, "%#s", "hello")
177 f.Wrap(0, "%s", "hello", 3)
178 f.Wrapf(0, "%s", "hello", 3)
179 f.Wrapf(0, "%r", "hello")
180 f.Wrapf(0, "%#s", "hello")
181 f.Wrap2(0, "%s", "hello", 3)
182 f.Wrapf2(0, "%s", "hello", 3)
183 f.Wrapf2(0, "%r", "hello")
184 f.Wrapf2(0, "%#s", "hello")
185 fmt.Printf("%#s", FormatterVal(true))
186 Printf("d%", 2)
187 Printf("%d", percentDV)
188 Printf("%d", &percentDV)
189 Printf("%d", notPercentDV)
190 Printf("%d", ¬PercentDV)
191 Printf("%p", ¬PercentDV)
192 Printf("%q", &percentDV)
193 Printf("%s", percentSV)
194 Printf("%s", &percentSV)
195
196 Printf("%[1]d", 3)
197 Printf("%[1]*d", 3, 1)
198 Printf("%[2]*[1]d", 1, 3)
199 Printf("%[2]*.[1]*[3]d", 2, 3, 4)
200 fmt.Fprintf(os.Stderr, "%[2]*.[1]*[3]d", 2, 3, 4)
201
202 Printf("%[xd", 3)
203 Printf("%[x]d x", 3)
204 Printf("%[3]*s x", "hi", 2)
205 _ = fmt.Sprintf("%[3]d x", 2)
206 Printf("%[2]*.[1]*[3]d x", 2, "hi", 4)
207 Printf("%[0]s x", "arg1")
208 Printf("%[0]d x", 1)
209
210 var e error
211 fmt.Println(e.Error())
212
213
214 var et1 *testing.T
215 et1.Error()
216 et1.Error("hi")
217 et1.Error("%d", 3)
218 var et3 errorTest3
219 et3.Error()
220 var et4 errorTest4
221 et4.Error()
222 var et5 errorTest5
223 et5.error()
224
225 var iface interface {
226 ToTheMadness() bool
227 }
228 fmt.Printf("%f", iface)
229
230 Printf("%d", someFunction)
231 Printf("%v", someFunction)
232 Println(someFunction)
233 Printf("%p", someFunction)
234 Printf("%T", someFunction)
235
236 Printf("%p %x", recursiveStructV, recursiveStructV.next)
237 Printf("%p %x", recursiveStruct1V, recursiveStruct1V.next)
238 Printf("%p %x", recursiveSliceV, recursiveSliceV)
239 Printf("%p %x", recursiveMapV, recursiveMapV)
240
241 math.Log(3)
242 var t *testing.T
243 t.Log("%d", 3)
244 t.Logf("%d", 3)
245 t.Logf("%d", "hi")
246
247 Errorf(1, "%d", 3)
248 Errorf(1, "%d", "hi")
249
250
251 errorf("WARNING", "foobar")
252 errorf("INFO", "s=%s, n=%d", "foo", 1)
253 errorf("ERROR", "%d")
254
255
256
257
258
259
260
261
262
263
264
265
266
267 ss := &someStruct{}
268 ss.Log(someFunction, "foo")
269 ss.Error(someFunction, someFunction)
270 ss.Println()
271 ss.Println(1.234, "foo")
272 ss.Println(1, someFunction)
273 ss.log(someFunction)
274 ss.log(someFunction, "bar", 1.33)
275 ss.log(someFunction, someFunction)
276
277
278 Printf("%d %[3]d %d %[2]d x", 1, 2, 3, 4)
279 Printf("%d %[0]d %d %[2]d x", 1, 2, 3, 4)
280 Printf("%d %[3]d %d %[-2]d x", 1, 2, 3, 4)
281 Printf("%d %[3]d %d %[2234234234234]d x", 1, 2, 3, 4)
282 Printf("%d %[3]d %-10d %[2]d x", 1, 2, 3)
283 Printf("%[1][3]d x", 1, 2)
284 Printf("%[1]d x", 1, 2)
285 Printf("%d %[3]d %d %[2]d x", 1, 2, 3, 4, 5)
286
287
288 Printf("%p\n", os.Stdout)
289 Println(os.Stdout, "hello")
290
291 Printf(someString(), "hello")
292
293
294 logpkg.Fatal("%d", 1)
295 logpkg.Fatalf("%d", "x")
296 logpkg.Fatalln("%d", 1)
297 logpkg.Panic("%d", 1)
298 logpkg.Panicf("%d", "x")
299 logpkg.Panicln("%d", 1)
300 logpkg.Print("%d", 1)
301 logpkg.Printf("%d", "x")
302 logpkg.Println("%d", 1)
303
304
305 var l *logpkg.Logger
306 l.Fatal("%d", 1)
307 l.Fatalf("%d", "x")
308 l.Fatalln("%d", 1)
309 l.Panic("%d", 1)
310 l.Panicf("%d", "x")
311 l.Panicln("%d", 1)
312 l.Print("%d", 1)
313 l.Printf("%d", "x")
314 l.Println("%d", 1)
315
316
317 dbg("", 1)
318 }
319
320 func someString() string { return "X" }
321
322 type someStruct struct{}
323
324
325
326
327 func (ss *someStruct) Log(f func(), s string) {}
328
329
330
331
332 func (ss *someStruct) Error(args ...func()) {}
333
334
335
336 func (ss *someStruct) Println(args ...interface{}) {}
337
338
339
340 func (ss *someStruct) log(f func(), args ...interface{}) {}
341
342
343 func someFunction() {}
344
345
346 func Printf(format string, args ...interface{}) {
347 fmt.Printf(format, args...)
348 }
349
350
351 func Println(args ...interface{}) {
352 fmt.Println(args...)
353 }
354
355
356 func printf(format string, args ...interface{}) {
357 fmt.Printf(format, args...)
358 }
359
360
361
362 func Errorf(i int, format string, args ...interface{}) {
363 _ = fmt.Errorf(format, args...)
364 }
365
366
367
368 func errorf(level, format string, args ...interface{}) {
369 _ = fmt.Errorf(format, args...)
370 }
371
372
373 func multi() []interface{} {
374 panic("don't call - testing only")
375 }
376
377 type stringer int
378
379 func (stringer) String() string { return "string" }
380
381 type ptrStringer float64
382
383 var stringerv ptrStringer
384
385 func (*ptrStringer) String() string {
386 return "string"
387 }
388
389 func (p *ptrStringer) Warn2(x int, args ...interface{}) string {
390 return p.Warn(x, args...)
391 }
392
393 func (p *ptrStringer) Warnf2(x int, format string, args ...interface{}) string {
394 return p.Warnf(x, format, args...)
395 }
396
397 func (*ptrStringer) Warn(x int, args ...interface{}) string {
398 return "warn"
399 }
400
401 func (*ptrStringer) Warnf(x int, format string, args ...interface{}) string {
402 return "warnf"
403 }
404
405 func (p *ptrStringer) Wrap2(x int, args ...interface{}) string {
406 return p.Wrap(x, args...)
407 }
408
409 func (p *ptrStringer) Wrapf2(x int, format string, args ...interface{}) string {
410 return p.Wrapf(x, format, args...)
411 }
412
413 func (*ptrStringer) Wrap(x int, args ...interface{}) string {
414 return fmt.Sprint(args...)
415 }
416
417 func (*ptrStringer) Wrapf(x int, format string, args ...interface{}) string {
418 return fmt.Sprintf(format, args...)
419 }
420
421 func (*ptrStringer) BadWrap(x int, args ...interface{}) string {
422 return fmt.Sprint(args)
423 }
424
425 func (*ptrStringer) BadWrapf(x int, format string, args ...interface{}) string {
426 return fmt.Sprintf(format, args)
427 }
428
429 func (*ptrStringer) WrapfFalsePositive(x int, arg1 string, arg2 ...interface{}) string {
430 return fmt.Sprintf("%s %v", arg1, arg2)
431 }
432
433 type embeddedStringer struct {
434 foo string
435 ptrStringer
436 bar int
437 }
438
439 var embeddedStringerv embeddedStringer
440
441 type notstringer struct {
442 f float64
443 }
444
445 var notstringerv notstringer
446
447 type stringerarray [4]float64
448
449 func (stringerarray) String() string {
450 return "string"
451 }
452
453 var stringerarrayv stringerarray
454
455 type notstringerarray [4]float64
456
457 var notstringerarrayv notstringerarray
458
459 var nonemptyinterface = interface {
460 f()
461 }(nil)
462
463
464 type percentDStruct struct {
465 a int
466 b []byte
467 c *float64
468 }
469
470 var percentDV percentDStruct
471
472
473 type notPercentDStruct struct {
474 a int
475 b []byte
476 c bool
477 }
478
479 var notPercentDV notPercentDStruct
480
481
482 type percentSStruct struct {
483 a string
484 b []byte
485 C stringerarray
486 }
487
488 var percentSV percentSStruct
489
490 type recursiveStringer int
491
492 func (s recursiveStringer) String() string {
493 _ = fmt.Sprintf("%d", s)
494 _ = fmt.Sprintf("%#v", s)
495 _ = fmt.Sprintf("%v", s)
496 _ = fmt.Sprintf("%v", &s)
497 _ = fmt.Sprintf("%T", s)
498 return fmt.Sprintln(s)
499 }
500
501 type recursivePtrStringer int
502
503 func (p *recursivePtrStringer) String() string {
504 _ = fmt.Sprintf("%v", *p)
505 _ = fmt.Sprint(&p)
506 return fmt.Sprintln(p)
507 }
508
509 type BoolFormatter bool
510
511 func (*BoolFormatter) Format(fmt.State, rune) {
512 }
513
514
515 type FormatterVal bool
516
517 func (FormatterVal) Format(fmt.State, rune) {
518 }
519
520 type RecursiveSlice []RecursiveSlice
521
522 var recursiveSliceV = &RecursiveSlice{}
523
524 type RecursiveMap map[int]RecursiveMap
525
526 var recursiveMapV = make(RecursiveMap)
527
528 type RecursiveStruct struct {
529 next *RecursiveStruct
530 }
531
532 var recursiveStructV = &RecursiveStruct{}
533
534 type RecursiveStruct1 struct {
535 next *RecursiveStruct2
536 }
537
538 type RecursiveStruct2 struct {
539 next *RecursiveStruct1
540 }
541
542 var recursiveStruct1V = &RecursiveStruct1{}
543
544 type unexportedInterface struct {
545 f interface{}
546 }
547
548
549 type unexportedStringer struct {
550 t ptrStringer
551 }
552 type unexportedStringerOtherFields struct {
553 s string
554 t ptrStringer
555 S string
556 }
557
558
559 type unexportedError struct {
560 e error
561 }
562 type unexportedErrorOtherFields struct {
563 s string
564 e error
565 S string
566 }
567
568 type errorer struct{}
569
570 func (e errorer) Error() string { return "errorer" }
571
572 type unexportedCustomError struct {
573 e errorer
574 }
575
576 type errorInterface interface {
577 error
578 ExtraMethod()
579 }
580
581 type unexportedErrorInterface struct {
582 e errorInterface
583 }
584
585 func UnexportedStringerOrError() {
586 fmt.Printf("%s", unexportedInterface{"foo"})
587 fmt.Printf("%s", unexportedInterface{3})
588
589 us := unexportedStringer{}
590 fmt.Printf("%s", us)
591 fmt.Printf("%s", &us)
592
593 usf := unexportedStringerOtherFields{
594 s: "foo",
595 S: "bar",
596 }
597 fmt.Printf("%s", usf)
598 fmt.Printf("%s", &usf)
599
600 ue := unexportedError{
601 e: &errorer{},
602 }
603 fmt.Printf("%s", ue)
604 fmt.Printf("%s", &ue)
605
606 uef := unexportedErrorOtherFields{
607 s: "foo",
608 e: &errorer{},
609 S: "bar",
610 }
611 fmt.Printf("%s", uef)
612 fmt.Printf("%s", &uef)
613
614 uce := unexportedCustomError{
615 e: errorer{},
616 }
617 fmt.Printf("%s", uce)
618
619 uei := unexportedErrorInterface{}
620 fmt.Printf("%s", uei)
621 fmt.Println("foo\n", "bar")
622
623 fmt.Println("foo\n")
624 fmt.Println("foo\\n")
625 fmt.Println(`foo\n`)
626
627 intSlice := []int{3, 4}
628 fmt.Printf("%s", intSlice)
629 nonStringerArray := [1]unexportedStringer{{}}
630 fmt.Printf("%s", nonStringerArray)
631 fmt.Printf("%s", []stringer{3, 4})
632 fmt.Printf("%s", [2]stringer{3, 4})
633 }
634
635
636
637 func DisableErrorForFlag0() {
638 fmt.Printf("%0t", true)
639 }
640
641
642 func dbg(format string, args ...interface{}) {
643 if format == "" {
644 format = "%v"
645 }
646 fmt.Printf(format, args...)
647 }
648
649 func PointersToCompoundTypes() {
650 stringSlice := []string{"a", "b"}
651 fmt.Printf("%s", &stringSlice)
652
653 intSlice := []int{3, 4}
654 fmt.Printf("%s", &intSlice)
655
656 stringArray := [2]string{"a", "b"}
657 fmt.Printf("%s", &stringArray)
658
659 intArray := [2]int{3, 4}
660 fmt.Printf("%s", &intArray)
661
662 stringStruct := struct{ F string }{"foo"}
663 fmt.Printf("%s", &stringStruct)
664
665 intStruct := struct{ F int }{3}
666 fmt.Printf("%s", &intStruct)
667
668 stringMap := map[string]string{"foo": "bar"}
669 fmt.Printf("%s", &stringMap)
670
671 intMap := map[int]int{3: 4}
672 fmt.Printf("%s", &intMap)
673
674 type T2 struct {
675 X string
676 }
677 type T1 struct {
678 X *T2
679 }
680 fmt.Printf("%s\n", T1{&T2{"x"}})
681 }
682
683
684
685 func printfUsingAnyNotEmptyInterface(format string, args ...any) {
686 _ = fmt.Sprintf(format, args...)
687 }
688 func _() {
689 printfUsingAnyNotEmptyInterface("%s", 123)
690 }
691
View as plain text