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