summaryrefslogtreecommitdiff
path: root/src/pkg/fmt/scan_test.go
diff options
context:
space:
mode:
authorOndřej Surý <ondrej@sury.org>2011-01-17 12:40:45 +0100
committerOndřej Surý <ondrej@sury.org>2011-01-17 12:40:45 +0100
commit3e45412327a2654a77944249962b3652e6142299 (patch)
treebc3bf69452afa055423cbe0c5cfa8ca357df6ccf /src/pkg/fmt/scan_test.go
parentc533680039762cacbc37db8dc7eed074c3e497be (diff)
downloadgolang-upstream/2011.01.12.tar.gz
Imported Upstream version 2011.01.12upstream/2011.01.12
Diffstat (limited to 'src/pkg/fmt/scan_test.go')
-rw-r--r--src/pkg/fmt/scan_test.go422
1 files changed, 275 insertions, 147 deletions
diff --git a/src/pkg/fmt/scan_test.go b/src/pkg/fmt/scan_test.go
index 1e0319836..fe5ee1d61 100644
--- a/src/pkg/fmt/scan_test.go
+++ b/src/pkg/fmt/scan_test.go
@@ -5,10 +5,13 @@
package fmt_test
import (
+ "bufio"
. "fmt"
"io"
+ "math"
"os"
"reflect"
+ "regexp"
"strings"
"testing"
"utf8"
@@ -78,6 +81,12 @@ var (
renamedComplex128Val renamedComplex128
)
+type FloatTest struct {
+ text string
+ in float64
+ out float64
+}
+
// Xs accepts any non-empty run of the verb character
type Xs string
@@ -100,7 +109,7 @@ func (x *Xs) Scan(state ScanState, verb int) os.Error {
if err != nil {
return err
}
- if !testing.MustCompile("^" + string(verb) + "+$").MatchString(tok) {
+ if !regexp.MustCompile("^" + string(verb) + "+$").MatchString(tok) {
return os.ErrorString("syntax error for xs")
}
*x = Xs(tok)
@@ -125,154 +134,163 @@ func newReader(s string) *myStringReader {
var scanTests = []ScanTest{
// Numbers
- ScanTest{"T\n", &boolVal, true}, // boolean test vals toggle to be sure they are written
- ScanTest{"F\n", &boolVal, false}, // restored to zero value
- ScanTest{"21\n", &intVal, 21},
- ScanTest{"22\n", &int8Val, int8(22)},
- ScanTest{"23\n", &int16Val, int16(23)},
- ScanTest{"24\n", &int32Val, int32(24)},
- ScanTest{"25\n", &int64Val, int64(25)},
- ScanTest{"127\n", &int8Val, int8(127)},
- ScanTest{"-21\n", &intVal, -21},
- ScanTest{"-22\n", &int8Val, int8(-22)},
- ScanTest{"-23\n", &int16Val, int16(-23)},
- ScanTest{"-24\n", &int32Val, int32(-24)},
- ScanTest{"-25\n", &int64Val, int64(-25)},
- ScanTest{"-128\n", &int8Val, int8(-128)},
- ScanTest{"+21\n", &intVal, +21},
- ScanTest{"+22\n", &int8Val, int8(+22)},
- ScanTest{"+23\n", &int16Val, int16(+23)},
- ScanTest{"+24\n", &int32Val, int32(+24)},
- ScanTest{"+25\n", &int64Val, int64(+25)},
- ScanTest{"+127\n", &int8Val, int8(+127)},
- ScanTest{"26\n", &uintVal, uint(26)},
- ScanTest{"27\n", &uint8Val, uint8(27)},
- ScanTest{"28\n", &uint16Val, uint16(28)},
- ScanTest{"29\n", &uint32Val, uint32(29)},
- ScanTest{"30\n", &uint64Val, uint64(30)},
- ScanTest{"255\n", &uint8Val, uint8(255)},
- ScanTest{"32767\n", &int16Val, int16(32767)},
- ScanTest{"2.3\n", &floatVal, 2.3},
- ScanTest{"2.3e1\n", &float32Val, float32(2.3e1)},
- ScanTest{"2.3e2\n", &float64Val, float64(2.3e2)},
- ScanTest{"2.35\n", &stringVal, "2.35"},
- ScanTest{"2345678\n", &bytesVal, []byte("2345678")},
- ScanTest{"(3.4e1-2i)\n", &complexVal, 3.4e1 - 2i},
- ScanTest{"-3.45e1-3i\n", &complex64Val, complex64(-3.45e1 - 3i)},
- ScanTest{"-.45e1-1e2i\n", &complex128Val, complex128(-.45e1 - 100i)},
- ScanTest{"hello\n", &stringVal, "hello"},
+ {"T\n", &boolVal, true}, // boolean test vals toggle to be sure they are written
+ {"F\n", &boolVal, false}, // restored to zero value
+ {"21\n", &intVal, 21},
+ {"22\n", &int8Val, int8(22)},
+ {"23\n", &int16Val, int16(23)},
+ {"24\n", &int32Val, int32(24)},
+ {"25\n", &int64Val, int64(25)},
+ {"127\n", &int8Val, int8(127)},
+ {"-21\n", &intVal, -21},
+ {"-22\n", &int8Val, int8(-22)},
+ {"-23\n", &int16Val, int16(-23)},
+ {"-24\n", &int32Val, int32(-24)},
+ {"-25\n", &int64Val, int64(-25)},
+ {"-128\n", &int8Val, int8(-128)},
+ {"+21\n", &intVal, +21},
+ {"+22\n", &int8Val, int8(+22)},
+ {"+23\n", &int16Val, int16(+23)},
+ {"+24\n", &int32Val, int32(+24)},
+ {"+25\n", &int64Val, int64(+25)},
+ {"+127\n", &int8Val, int8(+127)},
+ {"26\n", &uintVal, uint(26)},
+ {"27\n", &uint8Val, uint8(27)},
+ {"28\n", &uint16Val, uint16(28)},
+ {"29\n", &uint32Val, uint32(29)},
+ {"30\n", &uint64Val, uint64(30)},
+ {"255\n", &uint8Val, uint8(255)},
+ {"32767\n", &int16Val, int16(32767)},
+ {"2.3\n", &floatVal, 2.3},
+ {"2.3e1\n", &float32Val, float32(2.3e1)},
+ {"2.3e2\n", &float64Val, float64(2.3e2)},
+ {"2.35\n", &stringVal, "2.35"},
+ {"2345678\n", &bytesVal, []byte("2345678")},
+ {"(3.4e1-2i)\n", &complexVal, 3.4e1 - 2i},
+ {"-3.45e1-3i\n", &complex64Val, complex64(-3.45e1 - 3i)},
+ {"-.45e1-1e2i\n", &complex128Val, complex128(-.45e1 - 100i)},
+ {"hello\n", &stringVal, "hello"},
// Renamed types
- ScanTest{"true\n", &renamedBoolVal, renamedBool(true)},
- ScanTest{"F\n", &renamedBoolVal, renamedBool(false)},
- ScanTest{"101\n", &renamedIntVal, renamedInt(101)},
- ScanTest{"102\n", &renamedIntVal, renamedInt(102)},
- ScanTest{"103\n", &renamedUintVal, renamedUint(103)},
- ScanTest{"104\n", &renamedUintVal, renamedUint(104)},
- ScanTest{"105\n", &renamedInt8Val, renamedInt8(105)},
- ScanTest{"106\n", &renamedInt16Val, renamedInt16(106)},
- ScanTest{"107\n", &renamedInt32Val, renamedInt32(107)},
- ScanTest{"108\n", &renamedInt64Val, renamedInt64(108)},
- ScanTest{"109\n", &renamedUint8Val, renamedUint8(109)},
- ScanTest{"110\n", &renamedUint16Val, renamedUint16(110)},
- ScanTest{"111\n", &renamedUint32Val, renamedUint32(111)},
- ScanTest{"112\n", &renamedUint64Val, renamedUint64(112)},
- ScanTest{"113\n", &renamedUintptrVal, renamedUintptr(113)},
- ScanTest{"114\n", &renamedStringVal, renamedString("114")},
- ScanTest{"115\n", &renamedBytesVal, renamedBytes([]byte("115"))},
+ {"true\n", &renamedBoolVal, renamedBool(true)},
+ {"F\n", &renamedBoolVal, renamedBool(false)},
+ {"101\n", &renamedIntVal, renamedInt(101)},
+ {"102\n", &renamedIntVal, renamedInt(102)},
+ {"103\n", &renamedUintVal, renamedUint(103)},
+ {"104\n", &renamedUintVal, renamedUint(104)},
+ {"105\n", &renamedInt8Val, renamedInt8(105)},
+ {"106\n", &renamedInt16Val, renamedInt16(106)},
+ {"107\n", &renamedInt32Val, renamedInt32(107)},
+ {"108\n", &renamedInt64Val, renamedInt64(108)},
+ {"109\n", &renamedUint8Val, renamedUint8(109)},
+ {"110\n", &renamedUint16Val, renamedUint16(110)},
+ {"111\n", &renamedUint32Val, renamedUint32(111)},
+ {"112\n", &renamedUint64Val, renamedUint64(112)},
+ {"113\n", &renamedUintptrVal, renamedUintptr(113)},
+ {"114\n", &renamedStringVal, renamedString("114")},
+ {"115\n", &renamedBytesVal, renamedBytes([]byte("115"))},
// Custom scanner.
- ScanTest{" vvv ", &xVal, Xs("vvv")},
+ {" vvv ", &xVal, Xs("vvv")},
+
+ // Fixed bugs
+ {"2147483648\n", &int64Val, int64(2147483648)}, // was: integer overflow
}
var scanfTests = []ScanfTest{
- ScanfTest{"%v", "TRUE\n", &boolVal, true},
- ScanfTest{"%t", "false\n", &boolVal, false},
- ScanfTest{"%v", "-71\n", &intVal, -71},
- ScanfTest{"%d", "72\n", &intVal, 72},
- ScanfTest{"%c", "a\n", &intVal, 'a'},
- ScanfTest{"%c", "\u5072\n", &intVal, 0x5072},
- ScanfTest{"%c", "\u1234\n", &intVal, '\u1234'},
- ScanfTest{"%d", "73\n", &int8Val, int8(73)},
- ScanfTest{"%d", "+74\n", &int16Val, int16(74)},
- ScanfTest{"%d", "75\n", &int32Val, int32(75)},
- ScanfTest{"%d", "76\n", &int64Val, int64(76)},
- ScanfTest{"%b", "1001001\n", &intVal, 73},
- ScanfTest{"%o", "075\n", &intVal, 075},
- ScanfTest{"%x", "a75\n", &intVal, 0xa75},
- ScanfTest{"%v", "71\n", &uintVal, uint(71)},
- ScanfTest{"%d", "72\n", &uintVal, uint(72)},
- ScanfTest{"%d", "73\n", &uint8Val, uint8(73)},
- ScanfTest{"%d", "74\n", &uint16Val, uint16(74)},
- ScanfTest{"%d", "75\n", &uint32Val, uint32(75)},
- ScanfTest{"%d", "76\n", &uint64Val, uint64(76)},
- ScanfTest{"%b", "1001001\n", &uintVal, uint(73)},
- ScanfTest{"%o", "075\n", &uintVal, uint(075)},
- ScanfTest{"%x", "a75\n", &uintVal, uint(0xa75)},
- ScanfTest{"%x", "A75\n", &uintVal, uint(0xa75)},
+ {"%v", "TRUE\n", &boolVal, true},
+ {"%t", "false\n", &boolVal, false},
+ {"%v", "-71\n", &intVal, -71},
+ {"%d", "72\n", &intVal, 72},
+ {"%c", "a\n", &intVal, 'a'},
+ {"%c", "\u5072\n", &intVal, 0x5072},
+ {"%c", "\u1234\n", &intVal, '\u1234'},
+ {"%d", "73\n", &int8Val, int8(73)},
+ {"%d", "+74\n", &int16Val, int16(74)},
+ {"%d", "75\n", &int32Val, int32(75)},
+ {"%d", "76\n", &int64Val, int64(76)},
+ {"%b", "1001001\n", &intVal, 73},
+ {"%o", "075\n", &intVal, 075},
+ {"%x", "a75\n", &intVal, 0xa75},
+ {"%v", "71\n", &uintVal, uint(71)},
+ {"%d", "72\n", &uintVal, uint(72)},
+ {"%d", "73\n", &uint8Val, uint8(73)},
+ {"%d", "74\n", &uint16Val, uint16(74)},
+ {"%d", "75\n", &uint32Val, uint32(75)},
+ {"%d", "76\n", &uint64Val, uint64(76)},
+ {"%b", "1001001\n", &uintVal, uint(73)},
+ {"%o", "075\n", &uintVal, uint(075)},
+ {"%x", "a75\n", &uintVal, uint(0xa75)},
+ {"%x", "A75\n", &uintVal, uint(0xa75)},
+ {"%U", "U+1234\n", &intVal, int(0x1234)},
+ {"%U", "U+4567\n", &uintVal, uint(0x4567)},
// Strings
- ScanfTest{"%s", "using-%s\n", &stringVal, "using-%s"},
- ScanfTest{"%x", "7573696e672d2578\n", &stringVal, "using-%x"},
- ScanfTest{"%q", `"quoted\twith\\do\u0075bl\x65s"` + "\n", &stringVal, "quoted\twith\\doubles"},
- ScanfTest{"%q", "`quoted with backs`\n", &stringVal, "quoted with backs"},
+ {"%s", "using-%s\n", &stringVal, "using-%s"},
+ {"%x", "7573696e672d2578\n", &stringVal, "using-%x"},
+ {"%q", `"quoted\twith\\do\u0075bl\x65s"` + "\n", &stringVal, "quoted\twith\\doubles"},
+ {"%q", "`quoted with backs`\n", &stringVal, "quoted with backs"},
// Byte slices
- ScanfTest{"%s", "bytes-%s\n", &bytesVal, []byte("bytes-%s")},
- ScanfTest{"%x", "62797465732d2578\n", &bytesVal, []byte("bytes-%x")},
- ScanfTest{"%q", `"bytes\rwith\vdo\u0075bl\x65s"` + "\n", &bytesVal, []byte("bytes\rwith\vdoubles")},
- ScanfTest{"%q", "`bytes with backs`\n", &bytesVal, []byte("bytes with backs")},
+ {"%s", "bytes-%s\n", &bytesVal, []byte("bytes-%s")},
+ {"%x", "62797465732d2578\n", &bytesVal, []byte("bytes-%x")},
+ {"%q", `"bytes\rwith\vdo\u0075bl\x65s"` + "\n", &bytesVal, []byte("bytes\rwith\vdoubles")},
+ {"%q", "`bytes with backs`\n", &bytesVal, []byte("bytes with backs")},
// Renamed types
- ScanfTest{"%v\n", "true\n", &renamedBoolVal, renamedBool(true)},
- ScanfTest{"%t\n", "F\n", &renamedBoolVal, renamedBool(false)},
- ScanfTest{"%v", "101\n", &renamedIntVal, renamedInt(101)},
- ScanfTest{"%c", "\u0101\n", &renamedIntVal, renamedInt('\u0101')},
- ScanfTest{"%o", "0146\n", &renamedIntVal, renamedInt(102)},
- ScanfTest{"%v", "103\n", &renamedUintVal, renamedUint(103)},
- ScanfTest{"%d", "104\n", &renamedUintVal, renamedUint(104)},
- ScanfTest{"%d", "105\n", &renamedInt8Val, renamedInt8(105)},
- ScanfTest{"%d", "106\n", &renamedInt16Val, renamedInt16(106)},
- ScanfTest{"%d", "107\n", &renamedInt32Val, renamedInt32(107)},
- ScanfTest{"%d", "108\n", &renamedInt64Val, renamedInt64(108)},
- ScanfTest{"%x", "6D\n", &renamedUint8Val, renamedUint8(109)},
- ScanfTest{"%o", "0156\n", &renamedUint16Val, renamedUint16(110)},
- ScanfTest{"%d", "111\n", &renamedUint32Val, renamedUint32(111)},
- ScanfTest{"%d", "112\n", &renamedUint64Val, renamedUint64(112)},
- ScanfTest{"%d", "113\n", &renamedUintptrVal, renamedUintptr(113)},
- ScanfTest{"%s", "114\n", &renamedStringVal, renamedString("114")},
- ScanfTest{"%q", "\"1155\"\n", &renamedBytesVal, renamedBytes([]byte("1155"))},
- ScanfTest{"%g", "115.1\n", &renamedFloatVal, renamedFloat(115.1)},
- ScanfTest{"%g", "116e1\n", &renamedFloat32Val, renamedFloat32(116e1)},
- ScanfTest{"%g", "-11.7e+1", &renamedFloat64Val, renamedFloat64(-11.7e+1)},
- ScanfTest{"%g", "11+5.1i\n", &renamedComplexVal, renamedComplex(11 + 5.1i)},
- ScanfTest{"%g", "11+6e1i\n", &renamedComplex64Val, renamedComplex64(11 + 6e1i)},
- ScanfTest{"%g", "-11.+7e+1i", &renamedComplex128Val, renamedComplex128(-11. + 7e+1i)},
+ {"%v\n", "true\n", &renamedBoolVal, renamedBool(true)},
+ {"%t\n", "F\n", &renamedBoolVal, renamedBool(false)},
+ {"%v", "101\n", &renamedIntVal, renamedInt(101)},
+ {"%c", "\u0101\n", &renamedIntVal, renamedInt('\u0101')},
+ {"%o", "0146\n", &renamedIntVal, renamedInt(102)},
+ {"%v", "103\n", &renamedUintVal, renamedUint(103)},
+ {"%d", "104\n", &renamedUintVal, renamedUint(104)},
+ {"%d", "105\n", &renamedInt8Val, renamedInt8(105)},
+ {"%d", "106\n", &renamedInt16Val, renamedInt16(106)},
+ {"%d", "107\n", &renamedInt32Val, renamedInt32(107)},
+ {"%d", "108\n", &renamedInt64Val, renamedInt64(108)},
+ {"%x", "6D\n", &renamedUint8Val, renamedUint8(109)},
+ {"%o", "0156\n", &renamedUint16Val, renamedUint16(110)},
+ {"%d", "111\n", &renamedUint32Val, renamedUint32(111)},
+ {"%d", "112\n", &renamedUint64Val, renamedUint64(112)},
+ {"%d", "113\n", &renamedUintptrVal, renamedUintptr(113)},
+ {"%s", "114\n", &renamedStringVal, renamedString("114")},
+ {"%q", "\"1155\"\n", &renamedBytesVal, renamedBytes([]byte("1155"))},
+ {"%g", "115.1\n", &renamedFloatVal, renamedFloat(115.1)},
+ {"%g", "116e1\n", &renamedFloat32Val, renamedFloat32(116e1)},
+ {"%g", "-11.7e+1", &renamedFloat64Val, renamedFloat64(-11.7e+1)},
+ {"%g", "11+5.1i\n", &renamedComplexVal, renamedComplex(11 + 5.1i)},
+ {"%g", "11+6e1i\n", &renamedComplex64Val, renamedComplex64(11 + 6e1i)},
+ {"%g", "-11.+7e+1i", &renamedComplex128Val, renamedComplex128(-11. + 7e+1i)},
// Interesting formats
- ScanfTest{"here is\tthe value:%d", "here is the\tvalue:118\n", &intVal, 118},
- ScanfTest{"%% %%:%d", "% %:119\n", &intVal, 119},
+ {"here is\tthe value:%d", "here is the\tvalue:118\n", &intVal, 118},
+ {"%% %%:%d", "% %:119\n", &intVal, 119},
// Corner cases
- ScanfTest{"%x", "FFFFFFFF\n", &uint32Val, uint32(0xFFFFFFFF)},
+ {"%x", "FFFFFFFF\n", &uint32Val, uint32(0xFFFFFFFF)},
// Custom scanner.
- ScanfTest{"%s", " sss ", &xVal, Xs("sss")},
- ScanfTest{"%2s", "sssss", &xVal, Xs("ss")},
+ {"%s", " sss ", &xVal, Xs("sss")},
+ {"%2s", "sssss", &xVal, Xs("ss")},
+
+ // Fixed bugs
+ {"%d\n", "27\n", &intVal, 27}, // ok
+ {"%d\n", "28 \n", &intVal, 28}, // was: "unexpected newline"
}
var overflowTests = []ScanTest{
- ScanTest{"128", &int8Val, 0},
- ScanTest{"32768", &int16Val, 0},
- ScanTest{"-129", &int8Val, 0},
- ScanTest{"-32769", &int16Val, 0},
- ScanTest{"256", &uint8Val, 0},
- ScanTest{"65536", &uint16Val, 0},
- ScanTest{"1e100", &float32Val, 0},
- ScanTest{"1e500", &float64Val, 0},
- ScanTest{"(1e100+0i)", &complexVal, 0},
- ScanTest{"(1+1e100i)", &complex64Val, 0},
- ScanTest{"(1-1e500i)", &complex128Val, 0},
+ {"128", &int8Val, 0},
+ {"32768", &int16Val, 0},
+ {"-129", &int8Val, 0},
+ {"-32769", &int16Val, 0},
+ {"256", &uint8Val, 0},
+ {"65536", &uint16Val, 0},
+ {"1e100", &float32Val, 0},
+ {"1e500", &float64Val, 0},
+ {"(1e100+0i)", &complexVal, 0},
+ {"(1+1e100i)", &complex64Val, 0},
+ {"(1-1e500i)", &complex128Val, 0},
}
var i, j, k int
@@ -281,32 +299,30 @@ var s, t string
var c complex
var x, y Xs
-func args(a ...interface{}) []interface{} { return a }
-
var multiTests = []ScanfMultiTest{
- ScanfMultiTest{"", "", nil, nil, ""},
- ScanfMultiTest{"%d", "23", args(&i), args(23), ""},
- ScanfMultiTest{"%2s%3s", "22333", args(&s, &t), args("22", "333"), ""},
- ScanfMultiTest{"%2d%3d", "44555", args(&i, &j), args(44, 555), ""},
- ScanfMultiTest{"%2d.%3d", "66.777", args(&i, &j), args(66, 777), ""},
- ScanfMultiTest{"%d, %d", "23, 18", args(&i, &j), args(23, 18), ""},
- ScanfMultiTest{"%3d22%3d", "33322333", args(&i, &j), args(333, 333), ""},
- ScanfMultiTest{"%6vX=%3fY", "3+2iX=2.5Y", args(&c, &f), args((3 + 2i), float(2.5)), ""},
- ScanfMultiTest{"%d%s", "123abc", args(&i, &s), args(123, "abc"), ""},
- ScanfMultiTest{"%c%c%c", "2\u50c2X", args(&i, &j, &k), args('2', '\u50c2', 'X'), ""},
+ {"", "", nil, nil, ""},
+ {"%d", "23", args(&i), args(23), ""},
+ {"%2s%3s", "22333", args(&s, &t), args("22", "333"), ""},
+ {"%2d%3d", "44555", args(&i, &j), args(44, 555), ""},
+ {"%2d.%3d", "66.777", args(&i, &j), args(66, 777), ""},
+ {"%d, %d", "23, 18", args(&i, &j), args(23, 18), ""},
+ {"%3d22%3d", "33322333", args(&i, &j), args(333, 333), ""},
+ {"%6vX=%3fY", "3+2iX=2.5Y", args(&c, &f), args((3 + 2i), float(2.5)), ""},
+ {"%d%s", "123abc", args(&i, &s), args(123, "abc"), ""},
+ {"%c%c%c", "2\u50c2X", args(&i, &j, &k), args('2', '\u50c2', 'X'), ""},
// Custom scanner.
- ScanfMultiTest{"%2e%f", "eefffff", args(&x, &y), args(Xs("ee"), Xs("fffff")), ""},
+ {"%2e%f", "eefffff", args(&x, &y), args(Xs("ee"), Xs("fffff")), ""},
// Errors
- ScanfMultiTest{"%t", "23 18", args(&i), nil, "bad verb"},
- ScanfMultiTest{"%d %d %d", "23 18", args(&i, &j), args(23, 18), "too few operands"},
- ScanfMultiTest{"%d %d", "23 18 27", args(&i, &j, &k), args(23, 18), "too many operands"},
- ScanfMultiTest{"%c", "\u0100", args(&int8Val), nil, "overflow"},
- ScanfMultiTest{"X%d", "10X", args(&intVal), nil, "input does not match format"},
+ {"%t", "23 18", args(&i), nil, "bad verb"},
+ {"%d %d %d", "23 18", args(&i, &j), args(23, 18), "too few operands"},
+ {"%d %d", "23 18 27", args(&i, &j, &k), args(23, 18), "too many operands"},
+ {"%c", "\u0100", args(&int8Val), nil, "overflow"},
+ {"X%d", "10X", args(&intVal), nil, "input does not match format"},
// Bad UTF-8: should see every byte.
- ScanfMultiTest{"%c%c%c", "\xc2X\xc2", args(&i, &j, &k), args(utf8.RuneError, 'X', utf8.RuneError), ""},
+ {"%c%c%c", "\xc2X\xc2", args(&i, &j, &k), args(utf8.RuneError, 'X', utf8.RuneError), ""},
}
func testScan(name string, t *testing.T, scan func(r io.Reader, a ...interface{}) (int, os.Error)) {
@@ -379,7 +395,7 @@ func TestScanf(t *testing.T) {
func TestScanOverflow(t *testing.T) {
// different machines and different types report errors with different strings.
- re := testing.MustCompile("overflow|too large|out of range|not representable")
+ re := regexp.MustCompile("overflow|too large|out of range|not representable")
for _, test := range overflowTests {
_, err := Sscan(test.text, test.in)
if err == nil {
@@ -392,6 +408,57 @@ func TestScanOverflow(t *testing.T) {
}
}
+func verifyNaN(str string, t *testing.T) {
+ var f float
+ var f32 float32
+ var f64 float64
+ text := str + " " + str + " " + str
+ n, err := Fscan(strings.NewReader(text), &f, &f32, &f64)
+ if err != nil {
+ t.Errorf("got error scanning %q: %s", text, err)
+ }
+ if n != 3 {
+ t.Errorf("count error scanning %q: got %d", text, n)
+ }
+ if !math.IsNaN(float64(f)) || !math.IsNaN(float64(f32)) || !math.IsNaN(f64) {
+ t.Errorf("didn't get NaNs scanning %q: got %g %g %g", text, f, f32, f64)
+ }
+}
+
+func TestNaN(t *testing.T) {
+ for _, s := range []string{"nan", "NAN", "NaN"} {
+ verifyNaN(s, t)
+ }
+}
+
+func verifyInf(str string, t *testing.T) {
+ var f float
+ var f32 float32
+ var f64 float64
+ text := str + " " + str + " " + str
+ n, err := Fscan(strings.NewReader(text), &f, &f32, &f64)
+ if err != nil {
+ t.Errorf("got error scanning %q: %s", text, err)
+ }
+ if n != 3 {
+ t.Errorf("count error scanning %q: got %d", text, n)
+ }
+ sign := 1
+ if str[0] == '-' {
+ sign = -1
+ }
+ if !math.IsInf(float64(f), sign) || !math.IsInf(float64(f32), sign) || !math.IsInf(f64, sign) {
+ t.Errorf("didn't get right Infs scanning %q: got %g %g %g", text, f, f32, f64)
+ }
+}
+
+
+func TestInf(t *testing.T) {
+ for _, s := range []string{"inf", "+inf", "-inf", "INF", "-INF", "+INF", "Inf", "-Inf", "+Inf"} {
+ verifyInf(s, t)
+ }
+}
+
// TODO: there's no conversion from []T to ...T, but we can fake it. These
// functions do the faking. We index the table by the length of the param list.
var fscanf = []func(io.Reader, string, []interface{}) (int, os.Error){
@@ -460,6 +527,46 @@ func TestScanMultiple(t *testing.T) {
if a != 123 || s != "abc" {
t.Errorf("Sscan wrong values: got (%d %q) expected (123 \"abc\")", a, s)
}
+ n, err = Sscan("asdf", &s, &a)
+ if n != 1 {
+ t.Errorf("Sscan count error: expected 1: got %d", n)
+ }
+ if err == nil {
+ t.Errorf("Sscan expected error; got none: %s", err)
+ }
+ if s != "asdf" {
+ t.Errorf("Sscan wrong values: got %q expected \"asdf\"", s)
+ }
+}
+
+// Empty strings are not valid input when scanning a string.
+func TestScanEmpty(t *testing.T) {
+ var s1, s2 string
+ n, err := Sscan("abc", &s1, &s2)
+ if n != 1 {
+ t.Errorf("Sscan count error: expected 1: got %d", n)
+ }
+ if err == nil {
+ t.Error("Sscan <one item> expected error; got none")
+ }
+ if s1 != "abc" {
+ t.Errorf("Sscan wrong values: got %q expected \"abc\"", s1)
+ }
+ n, err = Sscan("", &s1, &s2)
+ if n != 0 {
+ t.Errorf("Sscan count error: expected 0: got %d", n)
+ }
+ if err == nil {
+ t.Error("Sscan <empty> expected error; got none")
+ }
+ // Quoted empty string is OK.
+ n, err = Sscanf(`""`, "%q", &s1)
+ if n != 1 {
+ t.Errorf("Sscanf count error: expected 1: got %d", n)
+ }
+ if err != nil {
+ t.Errorf("Sscanf <empty> expected no error with quoted string; got %s", err)
+ }
}
func TestScanNotPointer(t *testing.T) {
@@ -535,3 +642,24 @@ func TestEOF(t *testing.T) {
t.Error("expected one EOF, got", ec.eofCount)
}
}
+
+// Verify that, at least when using bufio, successive calls to Fscan do not lose runes.
+func TestUnreadRuneWithBufio(t *testing.T) {
+ r := bufio.NewReader(strings.NewReader("123αb"))
+ var i int
+ var a string
+ n, err := Fscanf(r, "%d", &i)
+ if n != 1 || err != nil {
+ t.Errorf("reading int expected one item, no errors; got %d %q", n, err)
+ }
+ if i != 123 {
+ t.Errorf("expected 123; got %d", i)
+ }
+ n, err = Fscanf(r, "%s", &a)
+ if n != 1 || err != nil {
+ t.Errorf("reading string expected one item, no errors; got %d %q", n, err)
+ }
+ if a != "αb" {
+ t.Errorf("expected αb; got %q", a)
+ }
+}