2013-05-17 14:20:39 -06:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// This file contains tests for the printf checker.
|
|
|
|
|
2013-05-22 11:20:50 -06:00
|
|
|
package testdata
|
2013-05-17 14:20:39 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-05-31 14:31:01 -06:00
|
|
|
"os"
|
2013-05-17 14:20:39 -06:00
|
|
|
"unsafe" // just for test case printing unsafe.Pointer
|
|
|
|
)
|
|
|
|
|
|
|
|
func UnsafePointerPrintfTest() {
|
|
|
|
var up unsafe.Pointer
|
|
|
|
fmt.Printf("%p, %x %X", up, up, up)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error methods that do not satisfy the Error interface and should be checked.
|
|
|
|
type errorTest1 int
|
|
|
|
|
|
|
|
func (errorTest1) Error(...interface{}) string {
|
|
|
|
return "hi"
|
|
|
|
}
|
|
|
|
|
|
|
|
type errorTest2 int // Analogous to testing's *T type.
|
|
|
|
func (errorTest2) Error(...interface{}) {
|
|
|
|
}
|
|
|
|
|
|
|
|
type errorTest3 int
|
|
|
|
|
|
|
|
func (errorTest3) Error() { // No return value.
|
|
|
|
}
|
|
|
|
|
|
|
|
type errorTest4 int
|
|
|
|
|
|
|
|
func (errorTest4) Error() int { // Different return type.
|
|
|
|
return 3
|
|
|
|
}
|
|
|
|
|
|
|
|
type errorTest5 int
|
|
|
|
|
|
|
|
func (errorTest5) error() { // niladic; don't complain if no args (was bug)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function never executes, but it serves as a simple test for the program.
|
|
|
|
// Test with make test.
|
|
|
|
func PrintfTests() {
|
|
|
|
var b bool
|
|
|
|
var i int
|
|
|
|
var r rune
|
|
|
|
var s string
|
|
|
|
var x float64
|
|
|
|
var p *int
|
2013-07-15 16:37:49 -06:00
|
|
|
var imap map[int]int
|
|
|
|
var fslice []float64
|
|
|
|
var c complex64
|
2013-05-17 14:20:39 -06:00
|
|
|
// Some good format/argtypes
|
|
|
|
fmt.Printf("")
|
2013-07-15 16:37:49 -06:00
|
|
|
fmt.Printf("%b %b %b", 3, i, x)
|
2013-05-17 14:20:39 -06:00
|
|
|
fmt.Printf("%c %c %c %c", 3, i, 'x', r)
|
2013-07-15 16:37:49 -06:00
|
|
|
fmt.Printf("%d %d %d", 3, i, imap)
|
|
|
|
fmt.Printf("%e %e %e %e", 3e9, x, fslice, c)
|
|
|
|
fmt.Printf("%E %E %E %E", 3e9, x, fslice, c)
|
|
|
|
fmt.Printf("%f %f %f %f", 3e9, x, fslice, c)
|
|
|
|
fmt.Printf("%F %F %F %F", 3e9, x, fslice, c)
|
|
|
|
fmt.Printf("%g %g %g %g", 3e9, x, fslice, c)
|
|
|
|
fmt.Printf("%G %G %G %G", 3e9, x, fslice, c)
|
2013-09-15 16:18:27 -06:00
|
|
|
fmt.Printf("%b %b %b %b", 3e9, x, fslice, c)
|
2013-05-17 14:20:39 -06:00
|
|
|
fmt.Printf("%o %o", 3, i)
|
|
|
|
fmt.Printf("%p %p", p, nil)
|
|
|
|
fmt.Printf("%q %q %q %q", 3, i, 'x', r)
|
2013-07-15 16:37:49 -06:00
|
|
|
fmt.Printf("%s %s %s", "hi", s, []byte{65})
|
2013-05-17 14:20:39 -06:00
|
|
|
fmt.Printf("%t %t", true, b)
|
|
|
|
fmt.Printf("%T %T", 3, i)
|
|
|
|
fmt.Printf("%U %U", 3, i)
|
|
|
|
fmt.Printf("%v %v", 3, i)
|
|
|
|
fmt.Printf("%x %x %x %x", 3, i, "hi", s)
|
|
|
|
fmt.Printf("%X %X %X %X", 3, i, "hi", s)
|
|
|
|
fmt.Printf("%.*s %d %g", 3, "hi", 23, 2.3)
|
2013-07-12 15:52:00 -06:00
|
|
|
fmt.Printf("%s", &stringerv)
|
2013-07-29 22:00:52 -06:00
|
|
|
fmt.Printf("%v", &stringerv)
|
2013-07-12 15:52:00 -06:00
|
|
|
fmt.Printf("%T", &stringerv)
|
2013-07-29 21:15:31 -06:00
|
|
|
fmt.Printf("%v", notstringerv)
|
|
|
|
fmt.Printf("%T", notstringerv)
|
2013-07-29 22:00:52 -06:00
|
|
|
fmt.Printf("%q", stringerarrayv)
|
|
|
|
fmt.Printf("%v", stringerarrayv)
|
|
|
|
fmt.Printf("%s", stringerarrayv)
|
|
|
|
fmt.Printf("%v", notstringerarrayv)
|
|
|
|
fmt.Printf("%T", notstringerarrayv)
|
2013-08-27 19:24:43 -06:00
|
|
|
fmt.Printf("%d", new(Formatter))
|
2013-07-29 23:51:35 -06:00
|
|
|
fmt.Printf("%*%", 2) // Ridiculous but allowed.
|
|
|
|
fmt.Printf("%s", interface{}(nil)) // Nothing useful we can say.
|
2013-07-15 16:37:49 -06:00
|
|
|
|
|
|
|
fmt.Printf("%g", 1+2i)
|
2013-05-17 14:20:39 -06:00
|
|
|
// Some bad format/argTypes
|
|
|
|
fmt.Printf("%b", "hi") // ERROR "arg .hi. for printf verb %b of wrong type"
|
2013-09-15 16:18:27 -06:00
|
|
|
fmt.Printf("%t", c) // ERROR "arg c for printf verb %t of wrong type"
|
|
|
|
fmt.Printf("%t", 1+2i) // ERROR "arg 1 \+ 2i for printf verb %t of wrong type"
|
2013-05-17 14:20:39 -06:00
|
|
|
fmt.Printf("%c", 2.3) // ERROR "arg 2.3 for printf verb %c of wrong type"
|
|
|
|
fmt.Printf("%d", 2.3) // ERROR "arg 2.3 for printf verb %d of wrong type"
|
|
|
|
fmt.Printf("%e", "hi") // ERROR "arg .hi. for printf verb %e of wrong type"
|
|
|
|
fmt.Printf("%E", true) // ERROR "arg true for printf verb %E of wrong type"
|
|
|
|
fmt.Printf("%f", "hi") // ERROR "arg .hi. for printf verb %f of wrong type"
|
|
|
|
fmt.Printf("%F", 'x') // ERROR "arg 'x' for printf verb %F of wrong type"
|
|
|
|
fmt.Printf("%g", "hi") // ERROR "arg .hi. for printf verb %g of wrong type"
|
2013-07-15 16:37:49 -06:00
|
|
|
fmt.Printf("%g", imap) // ERROR "arg imap for printf verb %g of wrong type"
|
2013-05-17 14:20:39 -06:00
|
|
|
fmt.Printf("%G", i) // ERROR "arg i for printf verb %G of wrong type"
|
|
|
|
fmt.Printf("%o", x) // ERROR "arg x for printf verb %o of wrong type"
|
|
|
|
fmt.Printf("%p", 23) // ERROR "arg 23 for printf verb %p of wrong type"
|
|
|
|
fmt.Printf("%q", x) // ERROR "arg x for printf verb %q of wrong type"
|
|
|
|
fmt.Printf("%s", b) // ERROR "arg b for printf verb %s of wrong type"
|
2013-07-15 16:37:49 -06:00
|
|
|
fmt.Printf("%s", byte(65)) // ERROR "arg byte\(65\) for printf verb %s of wrong type"
|
2013-05-17 14:20:39 -06:00
|
|
|
fmt.Printf("%t", 23) // ERROR "arg 23 for printf verb %t of wrong type"
|
|
|
|
fmt.Printf("%U", x) // ERROR "arg x for printf verb %U of wrong type"
|
|
|
|
fmt.Printf("%x", nil) // ERROR "arg nil for printf verb %x of wrong type"
|
|
|
|
fmt.Printf("%X", 2.3) // ERROR "arg 2.3 for printf verb %X of wrong type"
|
2013-07-15 16:37:49 -06:00
|
|
|
fmt.Printf("%s", stringerv) // ERROR "arg stringerv for printf verb %s of wrong type"
|
2013-06-06 17:05:14 -06:00
|
|
|
fmt.Printf("%t", stringerv) // ERROR "arg stringerv for printf verb %t of wrong type"
|
2013-07-29 21:15:31 -06:00
|
|
|
fmt.Printf("%q", notstringerv) // ERROR "arg notstringerv for printf verb %q of wrong type"
|
|
|
|
fmt.Printf("%t", notstringerv) // ERROR "arg notstringerv for printf verb %t of wrong type"
|
2013-07-29 22:00:52 -06:00
|
|
|
fmt.Printf("%t", stringerarrayv) // ERROR "arg stringerarrayv for printf verb %t of wrong type"
|
|
|
|
fmt.Printf("%t", notstringerarrayv) // ERROR "arg notstringerarrayv for printf verb %t of wrong type"
|
|
|
|
fmt.Printf("%q", notstringerarrayv) // ERROR "arg notstringerarrayv for printf verb %q of wrong type"
|
2013-08-27 19:24:43 -06:00
|
|
|
fmt.Printf("%d", Formatter(true)) // ERROR "arg Formatter\(true\) for printf verb %d of wrong type"
|
2013-08-05 15:02:52 -06:00
|
|
|
fmt.Printf("%s", nonemptyinterface) // ERROR "for printf verb %s of wrong type" (Disabled temporarily because of bug in IsAssignableTo)
|
2013-05-17 14:20:39 -06:00
|
|
|
fmt.Printf("%.*s %d %g", 3, "hi", 23, 'x') // ERROR "arg 'x' for printf verb %g of wrong type"
|
2013-05-28 13:37:34 -06:00
|
|
|
fmt.Println() // not an error
|
|
|
|
fmt.Println("%s", "hi") // ERROR "possible formatting directive in Println call"
|
|
|
|
fmt.Printf("%s", "hi", 3) // ERROR "wrong number of args for format in Printf call"
|
2013-06-18 09:21:06 -06:00
|
|
|
fmt.Sprintf("%"+("s"), "hi", 3) // ERROR "wrong number of args for format in Sprintf call"
|
2013-05-28 13:37:34 -06:00
|
|
|
fmt.Printf("%s%%%d", "hi", 3) // correct
|
|
|
|
fmt.Printf("%08s", "woo") // correct
|
|
|
|
fmt.Printf("% 8s", "woo") // correct
|
|
|
|
fmt.Printf("%.*d", 3, 3) // correct
|
|
|
|
fmt.Printf("%.*d", 3, 3, 3) // ERROR "wrong number of args for format in Printf call"
|
|
|
|
fmt.Printf("%.*d", "hi", 3) // ERROR "arg .hi. for \* in printf format not of type int"
|
|
|
|
fmt.Printf("%.*d", i, 3) // correct
|
|
|
|
fmt.Printf("%.*d", s, 3) // ERROR "arg s for \* in printf format not of type int"
|
|
|
|
fmt.Printf("%*%", 0.22) // ERROR "arg 0.22 for \* in printf format not of type int"
|
|
|
|
fmt.Printf("%q %q", multi()...) // ok
|
|
|
|
fmt.Printf("%#q", `blah`) // ok
|
|
|
|
printf("now is the time", "buddy") // ERROR "no formatting directive"
|
|
|
|
Printf("now is the time", "buddy") // ERROR "no formatting directive"
|
|
|
|
Printf("hi") // ok
|
2013-05-17 14:20:39 -06:00
|
|
|
const format = "%s %s\n"
|
|
|
|
Printf(format, "hi", "there")
|
2013-06-24 13:56:35 -06:00
|
|
|
Printf(format, "hi") // ERROR "missing argument for Printf verb %s: need 2, have 1"
|
2013-06-06 17:05:14 -06:00
|
|
|
f := new(stringer)
|
2013-05-17 14:20:39 -06:00
|
|
|
f.Warn(0, "%s", "hello", 3) // ERROR "possible formatting directive in Warn call"
|
|
|
|
f.Warnf(0, "%s", "hello", 3) // ERROR "wrong number of args for format in Warnf call"
|
|
|
|
f.Warnf(0, "%r", "hello") // ERROR "unrecognized printf verb"
|
|
|
|
f.Warnf(0, "%#s", "hello") // ERROR "unrecognized printf flag"
|
2013-06-18 09:21:06 -06:00
|
|
|
Printf("d%", 2) // ERROR "missing verb at end of format string in Printf call"
|
2013-08-02 06:52:38 -06:00
|
|
|
Printf("%d", percentDV)
|
|
|
|
Printf("%d", &percentDV)
|
|
|
|
Printf("%d", notPercentDV) // ERROR "arg notPercentDV for printf verb %d of wrong type"
|
|
|
|
Printf("%d", ¬PercentDV) // ERROR "arg ¬PercentDV for printf verb %d of wrong type"
|
|
|
|
Printf("%p", ¬PercentDV) // Works regardless: we print it as a pointer.
|
|
|
|
Printf("%s", percentSV)
|
|
|
|
Printf("%s", &percentSV)
|
2013-05-31 14:31:01 -06:00
|
|
|
// Good argument reorderings.
|
|
|
|
Printf("%[1]d", 3)
|
|
|
|
Printf("%[1]*d", 3, 1)
|
|
|
|
Printf("%[2]*[1]d", 1, 3)
|
|
|
|
Printf("%[2]*.[1]*[3]d", 2, 3, 4)
|
|
|
|
fmt.Fprintf(os.Stderr, "%[2]*.[1]*[3]d", 2, 3, 4) // Use Fprintf to make sure we count arguments correctly.
|
|
|
|
// Bad argument reorderings.
|
|
|
|
Printf("%[xd", 3) // ERROR "illegal syntax for printf argument index"
|
|
|
|
Printf("%[x]d", 3) // ERROR "illegal syntax for printf argument index"
|
2013-06-24 13:56:35 -06:00
|
|
|
Printf("%[3]*s", "hi", 2) // ERROR "missing argument for Printf indirect \*: need 3, have 2"
|
|
|
|
fmt.Sprintf("%[3]d", 2) // ERROR "missing argument for Sprintf verb %d: need 3, have 1"
|
2013-05-31 14:31:01 -06:00
|
|
|
Printf("%[2]*.[1]*[3]d", 2, "hi", 4) // ERROR "arg .hi. for \* in printf format not of type int"
|
2013-05-17 14:20:39 -06:00
|
|
|
// Something that satisfies the error interface.
|
|
|
|
var e error
|
|
|
|
fmt.Println(e.Error()) // ok
|
|
|
|
// Something that looks like an error interface but isn't, such as the (*T).Error method
|
|
|
|
// in the testing package.
|
|
|
|
var et1 errorTest1
|
|
|
|
fmt.Println(et1.Error()) // ERROR "no args in Error call"
|
|
|
|
fmt.Println(et1.Error("hi")) // ok
|
|
|
|
fmt.Println(et1.Error("%d", 3)) // ERROR "possible formatting directive in Error call"
|
|
|
|
var et2 errorTest2
|
|
|
|
et2.Error() // ERROR "no args in Error call"
|
|
|
|
et2.Error("hi") // ok, not an error method.
|
|
|
|
et2.Error("%d", 3) // ERROR "possible formatting directive in Error call"
|
|
|
|
var et3 errorTest3
|
|
|
|
et3.Error() // ok, not an error method.
|
|
|
|
var et4 errorTest4
|
|
|
|
et4.Error() // ok, not an error method.
|
|
|
|
var et5 errorTest5
|
|
|
|
et5.error() // ok, not an error method.
|
2013-09-17 20:57:59 -06:00
|
|
|
// Bug: used to recur forever.
|
|
|
|
Printf("%p %x", recursiveStructV, recursiveStructV.next)
|
|
|
|
Printf("%p %x", recursiveStruct1V, recursiveStruct1V.next)
|
|
|
|
Printf("%p %x", recursiveSliceV, recursiveSliceV)
|
|
|
|
Printf("%p %x", recursiveMapV, recursiveMapV)
|
2013-05-17 14:20:39 -06:00
|
|
|
}
|
|
|
|
|
2013-05-31 14:31:01 -06:00
|
|
|
// Printf is used by the test so we must declare it.
|
|
|
|
func Printf(format string, args ...interface{}) {
|
2013-05-17 14:20:39 -06:00
|
|
|
panic("don't call - testing only")
|
|
|
|
}
|
|
|
|
|
2013-06-06 17:05:14 -06:00
|
|
|
// printf is used by the test so we must declare it.
|
|
|
|
func printf(format string, args ...interface{}) {
|
|
|
|
panic("don't call - testing only")
|
|
|
|
}
|
|
|
|
|
2013-05-17 14:20:39 -06:00
|
|
|
// multi is used by the test.
|
|
|
|
func multi() []interface{} {
|
|
|
|
panic("don't call - testing only")
|
|
|
|
}
|
2013-06-06 17:05:14 -06:00
|
|
|
|
|
|
|
type stringer float64
|
|
|
|
|
|
|
|
var stringerv stringer
|
|
|
|
|
|
|
|
func (*stringer) String() string {
|
|
|
|
return "string"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*stringer) Warn(int, ...interface{}) string {
|
|
|
|
return "warn"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*stringer) Warnf(int, string, ...interface{}) string {
|
|
|
|
return "warnf"
|
|
|
|
}
|
2013-07-29 21:15:31 -06:00
|
|
|
|
2013-08-02 06:52:38 -06:00
|
|
|
type notstringer struct {
|
|
|
|
f float64
|
|
|
|
}
|
2013-07-29 21:15:31 -06:00
|
|
|
|
|
|
|
var notstringerv notstringer
|
2013-07-29 22:00:52 -06:00
|
|
|
|
|
|
|
type stringerarray [4]float64
|
|
|
|
|
|
|
|
func (stringerarray) String() string {
|
|
|
|
return "string"
|
|
|
|
}
|
|
|
|
|
|
|
|
var stringerarrayv stringerarray
|
|
|
|
|
|
|
|
type notstringerarray [4]float64
|
|
|
|
|
|
|
|
var notstringerarrayv notstringerarray
|
2013-07-29 23:51:35 -06:00
|
|
|
|
|
|
|
var nonemptyinterface = interface {
|
|
|
|
f()
|
|
|
|
}(nil)
|
2013-08-02 06:52:38 -06:00
|
|
|
|
|
|
|
// A data type we can print with "%d".
|
|
|
|
type percentDStruct struct {
|
|
|
|
a int
|
|
|
|
b []byte
|
|
|
|
c *float64
|
|
|
|
}
|
|
|
|
|
|
|
|
var percentDV percentDStruct
|
|
|
|
|
|
|
|
// A data type we cannot print correctly with "%d".
|
|
|
|
type notPercentDStruct struct {
|
|
|
|
a int
|
|
|
|
b []byte
|
|
|
|
c bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var notPercentDV notPercentDStruct
|
|
|
|
|
|
|
|
// A data type we can print with "%s".
|
|
|
|
type percentSStruct struct {
|
|
|
|
a string
|
|
|
|
b []byte
|
|
|
|
c stringerarray
|
|
|
|
}
|
|
|
|
|
|
|
|
var percentSV percentSStruct
|
2013-08-20 19:07:53 -06:00
|
|
|
|
|
|
|
type recursiveStringer int
|
|
|
|
|
|
|
|
func (s recursiveStringer) String() string {
|
|
|
|
fmt.Sprintf("%d", s)
|
2013-08-21 19:44:39 -06:00
|
|
|
fmt.Sprintf("%#v", s)
|
2013-08-20 19:07:53 -06:00
|
|
|
fmt.Sprintf("%v", s) // ERROR "arg s for printf causes recursive call to String method"
|
2013-08-21 19:44:39 -06:00
|
|
|
fmt.Sprintf("%v", &s) // ERROR "arg &s for printf causes recursive call to String method"
|
2013-08-20 19:07:53 -06:00
|
|
|
return fmt.Sprintln(s) // ERROR "arg s for print causes recursive call to String method"
|
|
|
|
}
|
|
|
|
|
|
|
|
type recursivePtrStringer int
|
|
|
|
|
|
|
|
func (p *recursivePtrStringer) String() string {
|
2013-08-21 19:29:44 -06:00
|
|
|
fmt.Sprintf("%v", *p)
|
2013-08-20 19:07:53 -06:00
|
|
|
return fmt.Sprintln(p) // ERROR "arg p for print causes recursive call to String method"
|
|
|
|
}
|
2013-08-27 19:24:43 -06:00
|
|
|
|
|
|
|
type Formatter bool
|
|
|
|
|
|
|
|
func (*Formatter) Format(fmt.State, rune) {
|
|
|
|
}
|
2013-09-17 20:57:59 -06:00
|
|
|
|
|
|
|
type RecursiveSlice []RecursiveSlice
|
|
|
|
|
|
|
|
var recursiveSliceV = &RecursiveSlice{}
|
|
|
|
|
|
|
|
type RecursiveMap map[int]RecursiveMap
|
|
|
|
|
|
|
|
var recursiveMapV = make(RecursiveMap)
|
|
|
|
|
|
|
|
type RecursiveStruct struct {
|
|
|
|
next *RecursiveStruct
|
|
|
|
}
|
|
|
|
|
|
|
|
var recursiveStructV = &RecursiveStruct{}
|
|
|
|
|
|
|
|
type RecursiveStruct1 struct {
|
|
|
|
next *Recursive2Struct
|
|
|
|
}
|
|
|
|
|
|
|
|
type RecursiveStruct2 struct {
|
|
|
|
next *Recursive1Struct
|
|
|
|
}
|
|
|
|
|
|
|
|
var recursiveStruct1V = &RecursiveStruct1{}
|