scan_test.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package fmt_test
  5. import (
  6. "bufio"
  7. "bytes"
  8. "errors"
  9. . "fmt"
  10. "io"
  11. "math"
  12. "reflect"
  13. "regexp"
  14. "strings"
  15. "testing"
  16. "unicode/utf8"
  17. )
  18. type ScanTest struct {
  19. text string
  20. in interface{}
  21. out interface{}
  22. }
  23. type ScanfTest struct {
  24. format string
  25. text string
  26. in interface{}
  27. out interface{}
  28. }
  29. type ScanfMultiTest struct {
  30. format string
  31. text string
  32. in []interface{}
  33. out []interface{}
  34. err string
  35. }
  36. var (
  37. boolVal bool
  38. intVal int
  39. int8Val int8
  40. int16Val int16
  41. int32Val int32
  42. int64Val int64
  43. uintVal uint
  44. uint8Val uint8
  45. uint16Val uint16
  46. uint32Val uint32
  47. uint64Val uint64
  48. float32Val float32
  49. float64Val float64
  50. stringVal string
  51. bytesVal []byte
  52. runeVal rune
  53. complex64Val complex64
  54. complex128Val complex128
  55. renamedBoolVal renamedBool
  56. renamedIntVal renamedInt
  57. renamedInt8Val renamedInt8
  58. renamedInt16Val renamedInt16
  59. renamedInt32Val renamedInt32
  60. renamedInt64Val renamedInt64
  61. renamedUintVal renamedUint
  62. renamedUint8Val renamedUint8
  63. renamedUint16Val renamedUint16
  64. renamedUint32Val renamedUint32
  65. renamedUint64Val renamedUint64
  66. renamedUintptrVal renamedUintptr
  67. renamedStringVal renamedString
  68. renamedBytesVal renamedBytes
  69. renamedFloat32Val renamedFloat32
  70. renamedFloat64Val renamedFloat64
  71. renamedComplex64Val renamedComplex64
  72. renamedComplex128Val renamedComplex128
  73. )
  74. type FloatTest struct {
  75. text string
  76. in float64
  77. out float64
  78. }
  79. // Xs accepts any non-empty run of the verb character
  80. type Xs string
  81. func (x *Xs) Scan(state ScanState, verb rune) error {
  82. tok, err := state.Token(true, func(r rune) bool { return r == verb })
  83. if err != nil {
  84. return err
  85. }
  86. s := string(tok)
  87. if !regexp.MustCompile("^" + string(verb) + "+$").MatchString(s) {
  88. return errors.New("syntax error for xs")
  89. }
  90. *x = Xs(s)
  91. return nil
  92. }
  93. var xVal Xs
  94. // IntString accepts an integer followed immediately by a string.
  95. // It tests the embedding of a scan within a scan.
  96. type IntString struct {
  97. i int
  98. s string
  99. }
  100. func (s *IntString) Scan(state ScanState, verb rune) error {
  101. if _, err := Fscan(state, &s.i); err != nil {
  102. return err
  103. }
  104. tok, err := state.Token(true, nil)
  105. if err != nil {
  106. return err
  107. }
  108. s.s = string(tok)
  109. return nil
  110. }
  111. var intStringVal IntString
  112. // myStringReader implements Read but not ReadRune, allowing us to test our readRune wrapper
  113. // type that creates something that can read runes given only Read().
  114. type myStringReader struct {
  115. r *strings.Reader
  116. }
  117. func (s *myStringReader) Read(p []byte) (n int, err error) {
  118. return s.r.Read(p)
  119. }
  120. func newReader(s string) *myStringReader {
  121. return &myStringReader{strings.NewReader(s)}
  122. }
  123. var scanTests = []ScanTest{
  124. // Basic types
  125. {"T\n", &boolVal, true}, // boolean test vals toggle to be sure they are written
  126. {"F\n", &boolVal, false}, // restored to zero value
  127. {"21\n", &intVal, 21},
  128. {"0\n", &intVal, 0},
  129. {"000\n", &intVal, 0},
  130. {"0x10\n", &intVal, 0x10},
  131. {"-0x10\n", &intVal, -0x10},
  132. {"0377\n", &intVal, 0377},
  133. {"-0377\n", &intVal, -0377},
  134. {"0\n", &uintVal, uint(0)},
  135. {"000\n", &uintVal, uint(0)},
  136. {"0x10\n", &uintVal, uint(0x10)},
  137. {"0377\n", &uintVal, uint(0377)},
  138. {"22\n", &int8Val, int8(22)},
  139. {"23\n", &int16Val, int16(23)},
  140. {"24\n", &int32Val, int32(24)},
  141. {"25\n", &int64Val, int64(25)},
  142. {"127\n", &int8Val, int8(127)},
  143. {"-21\n", &intVal, -21},
  144. {"-22\n", &int8Val, int8(-22)},
  145. {"-23\n", &int16Val, int16(-23)},
  146. {"-24\n", &int32Val, int32(-24)},
  147. {"-25\n", &int64Val, int64(-25)},
  148. {"-128\n", &int8Val, int8(-128)},
  149. {"+21\n", &intVal, +21},
  150. {"+22\n", &int8Val, int8(+22)},
  151. {"+23\n", &int16Val, int16(+23)},
  152. {"+24\n", &int32Val, int32(+24)},
  153. {"+25\n", &int64Val, int64(+25)},
  154. {"+127\n", &int8Val, int8(+127)},
  155. {"26\n", &uintVal, uint(26)},
  156. {"27\n", &uint8Val, uint8(27)},
  157. {"28\n", &uint16Val, uint16(28)},
  158. {"29\n", &uint32Val, uint32(29)},
  159. {"30\n", &uint64Val, uint64(30)},
  160. {"255\n", &uint8Val, uint8(255)},
  161. {"32767\n", &int16Val, int16(32767)},
  162. {"2.3\n", &float64Val, 2.3},
  163. {"2.3e1\n", &float32Val, float32(2.3e1)},
  164. {"2.3e2\n", &float64Val, 2.3e2},
  165. {"2.3p2\n", &float64Val, 2.3 * 4},
  166. {"2.3p+2\n", &float64Val, 2.3 * 4},
  167. {"2.3p+66\n", &float64Val, 2.3 * (1 << 32) * (1 << 32) * 4},
  168. {"2.3p-66\n", &float64Val, 2.3 / ((1 << 32) * (1 << 32) * 4)},
  169. {"2.35\n", &stringVal, "2.35"},
  170. {"2345678\n", &bytesVal, []byte("2345678")},
  171. {"(3.4e1-2i)\n", &complex128Val, 3.4e1 - 2i},
  172. {"-3.45e1-3i\n", &complex64Val, complex64(-3.45e1 - 3i)},
  173. {"-.45e1-1e2i\n", &complex128Val, complex128(-.45e1 - 100i)},
  174. {"hello\n", &stringVal, "hello"},
  175. // Carriage-return followed by newline. (We treat \r\n as \n always.)
  176. {"hello\r\n", &stringVal, "hello"},
  177. {"27\r\n", &uint8Val, uint8(27)},
  178. // Renamed types
  179. {"true\n", &renamedBoolVal, renamedBool(true)},
  180. {"F\n", &renamedBoolVal, renamedBool(false)},
  181. {"101\n", &renamedIntVal, renamedInt(101)},
  182. {"102\n", &renamedIntVal, renamedInt(102)},
  183. {"103\n", &renamedUintVal, renamedUint(103)},
  184. {"104\n", &renamedUintVal, renamedUint(104)},
  185. {"105\n", &renamedInt8Val, renamedInt8(105)},
  186. {"106\n", &renamedInt16Val, renamedInt16(106)},
  187. {"107\n", &renamedInt32Val, renamedInt32(107)},
  188. {"108\n", &renamedInt64Val, renamedInt64(108)},
  189. {"109\n", &renamedUint8Val, renamedUint8(109)},
  190. {"110\n", &renamedUint16Val, renamedUint16(110)},
  191. {"111\n", &renamedUint32Val, renamedUint32(111)},
  192. {"112\n", &renamedUint64Val, renamedUint64(112)},
  193. {"113\n", &renamedUintptrVal, renamedUintptr(113)},
  194. {"114\n", &renamedStringVal, renamedString("114")},
  195. {"115\n", &renamedBytesVal, renamedBytes([]byte("115"))},
  196. // Custom scanners.
  197. {" vvv ", &xVal, Xs("vvv")},
  198. {" 1234hello", &intStringVal, IntString{1234, "hello"}},
  199. // Fixed bugs
  200. {"2147483648\n", &int64Val, int64(2147483648)}, // was: integer overflow
  201. }
  202. var scanfTests = []ScanfTest{
  203. {"%v", "TRUE\n", &boolVal, true},
  204. {"%t", "false\n", &boolVal, false},
  205. {"%v", "-71\n", &intVal, -71},
  206. {"%v", "0377\n", &intVal, 0377},
  207. {"%v", "0x44\n", &intVal, 0x44},
  208. {"%d", "72\n", &intVal, 72},
  209. {"%c", "a\n", &runeVal, 'a'},
  210. {"%c", "\u5072\n", &runeVal, '\u5072'},
  211. {"%c", "\u1234\n", &runeVal, '\u1234'},
  212. {"%d", "73\n", &int8Val, int8(73)},
  213. {"%d", "+74\n", &int16Val, int16(74)},
  214. {"%d", "75\n", &int32Val, int32(75)},
  215. {"%d", "76\n", &int64Val, int64(76)},
  216. {"%b", "1001001\n", &intVal, 73},
  217. {"%o", "075\n", &intVal, 075},
  218. {"%x", "a75\n", &intVal, 0xa75},
  219. {"%v", "71\n", &uintVal, uint(71)},
  220. {"%d", "72\n", &uintVal, uint(72)},
  221. {"%d", "73\n", &uint8Val, uint8(73)},
  222. {"%d", "74\n", &uint16Val, uint16(74)},
  223. {"%d", "75\n", &uint32Val, uint32(75)},
  224. {"%d", "76\n", &uint64Val, uint64(76)},
  225. {"%b", "1001001\n", &uintVal, uint(73)},
  226. {"%o", "075\n", &uintVal, uint(075)},
  227. {"%x", "a75\n", &uintVal, uint(0xa75)},
  228. {"%x", "A75\n", &uintVal, uint(0xa75)},
  229. {"%U", "U+1234\n", &intVal, int(0x1234)},
  230. {"%U", "U+4567\n", &uintVal, uint(0x4567)},
  231. // Strings
  232. {"%s", "using-%s\n", &stringVal, "using-%s"},
  233. {"%x", "7573696e672d2578\n", &stringVal, "using-%x"},
  234. {"%q", `"quoted\twith\\do\u0075bl\x65s"` + "\n", &stringVal, "quoted\twith\\doubles"},
  235. {"%q", "`quoted with backs`\n", &stringVal, "quoted with backs"},
  236. // Byte slices
  237. {"%s", "bytes-%s\n", &bytesVal, []byte("bytes-%s")},
  238. {"%x", "62797465732d2578\n", &bytesVal, []byte("bytes-%x")},
  239. {"%q", `"bytes\rwith\vdo\u0075bl\x65s"` + "\n", &bytesVal, []byte("bytes\rwith\vdoubles")},
  240. {"%q", "`bytes with backs`\n", &bytesVal, []byte("bytes with backs")},
  241. // Renamed types
  242. {"%v\n", "true\n", &renamedBoolVal, renamedBool(true)},
  243. {"%t\n", "F\n", &renamedBoolVal, renamedBool(false)},
  244. {"%v", "101\n", &renamedIntVal, renamedInt(101)},
  245. {"%c", "\u0101\n", &renamedIntVal, renamedInt('\u0101')},
  246. {"%o", "0146\n", &renamedIntVal, renamedInt(102)},
  247. {"%v", "103\n", &renamedUintVal, renamedUint(103)},
  248. {"%d", "104\n", &renamedUintVal, renamedUint(104)},
  249. {"%d", "105\n", &renamedInt8Val, renamedInt8(105)},
  250. {"%d", "106\n", &renamedInt16Val, renamedInt16(106)},
  251. {"%d", "107\n", &renamedInt32Val, renamedInt32(107)},
  252. {"%d", "108\n", &renamedInt64Val, renamedInt64(108)},
  253. {"%x", "6D\n", &renamedUint8Val, renamedUint8(109)},
  254. {"%o", "0156\n", &renamedUint16Val, renamedUint16(110)},
  255. {"%d", "111\n", &renamedUint32Val, renamedUint32(111)},
  256. {"%d", "112\n", &renamedUint64Val, renamedUint64(112)},
  257. {"%d", "113\n", &renamedUintptrVal, renamedUintptr(113)},
  258. {"%s", "114\n", &renamedStringVal, renamedString("114")},
  259. {"%q", "\"1155\"\n", &renamedBytesVal, renamedBytes([]byte("1155"))},
  260. {"%g", "116e1\n", &renamedFloat32Val, renamedFloat32(116e1)},
  261. {"%g", "-11.7e+1", &renamedFloat64Val, renamedFloat64(-11.7e+1)},
  262. {"%g", "11+6e1i\n", &renamedComplex64Val, renamedComplex64(11 + 6e1i)},
  263. {"%g", "-11.+7e+1i", &renamedComplex128Val, renamedComplex128(-11. + 7e+1i)},
  264. // Interesting formats
  265. {"here is\tthe value:%d", "here is the\tvalue:118\n", &intVal, 118},
  266. {"%% %%:%d", "% %:119\n", &intVal, 119},
  267. // Corner cases
  268. {"%x", "FFFFFFFF\n", &uint32Val, uint32(0xFFFFFFFF)},
  269. // Custom scanner.
  270. {"%s", " sss ", &xVal, Xs("sss")},
  271. {"%2s", "sssss", &xVal, Xs("ss")},
  272. // Fixed bugs
  273. {"%d\n", "27\n", &intVal, 27}, // ok
  274. {"%d\n", "28 \n", &intVal, 28}, // was: "unexpected newline"
  275. {"%v", "0", &intVal, 0}, // was: "EOF"; 0 was taken as base prefix and not counted.
  276. {"%v", "0", &uintVal, uint(0)}, // was: "EOF"; 0 was taken as base prefix and not counted.
  277. }
  278. var overflowTests = []ScanTest{
  279. {"128", &int8Val, 0},
  280. {"32768", &int16Val, 0},
  281. {"-129", &int8Val, 0},
  282. {"-32769", &int16Val, 0},
  283. {"256", &uint8Val, 0},
  284. {"65536", &uint16Val, 0},
  285. {"1e100", &float32Val, 0},
  286. {"1e500", &float64Val, 0},
  287. {"(1e100+0i)", &complex64Val, 0},
  288. {"(1+1e100i)", &complex64Val, 0},
  289. {"(1-1e500i)", &complex128Val, 0},
  290. }
  291. var truth bool
  292. var i, j, k int
  293. var f float64
  294. var s, t string
  295. var c complex128
  296. var x, y Xs
  297. var z IntString
  298. var r1, r2, r3 rune
  299. var multiTests = []ScanfMultiTest{
  300. {"", "", []interface{}{}, []interface{}{}, ""},
  301. {"%d", "23", args(&i), args(23), ""},
  302. {"%2s%3s", "22333", args(&s, &t), args("22", "333"), ""},
  303. {"%2d%3d", "44555", args(&i, &j), args(44, 555), ""},
  304. {"%2d.%3d", "66.777", args(&i, &j), args(66, 777), ""},
  305. {"%d, %d", "23, 18", args(&i, &j), args(23, 18), ""},
  306. {"%3d22%3d", "33322333", args(&i, &j), args(333, 333), ""},
  307. {"%6vX=%3fY", "3+2iX=2.5Y", args(&c, &f), args((3 + 2i), 2.5), ""},
  308. {"%d%s", "123abc", args(&i, &s), args(123, "abc"), ""},
  309. {"%c%c%c", "2\u50c2X", args(&r1, &r2, &r3), args('2', '\u50c2', 'X'), ""},
  310. // Custom scanners.
  311. {"%e%f", "eefffff", args(&x, &y), args(Xs("ee"), Xs("fffff")), ""},
  312. {"%4v%s", "12abcd", args(&z, &s), args(IntString{12, "ab"}, "cd"), ""},
  313. // Errors
  314. {"%t", "23 18", args(&i), nil, "bad verb"},
  315. {"%d %d %d", "23 18", args(&i, &j), args(23, 18), "too few operands"},
  316. {"%d %d", "23 18 27", args(&i, &j, &k), args(23, 18), "too many operands"},
  317. {"%c", "\u0100", args(&int8Val), nil, "overflow"},
  318. {"X%d", "10X", args(&intVal), nil, "input does not match format"},
  319. // Bad UTF-8: should see every byte.
  320. {"%c%c%c", "\xc2X\xc2", args(&r1, &r2, &r3), args(utf8.RuneError, 'X', utf8.RuneError), ""},
  321. // Fixed bugs
  322. {"%v%v", "FALSE23", args(&truth, &i), args(false, 23), ""},
  323. }
  324. func testScan(name string, t *testing.T, scan func(r io.Reader, a ...interface{}) (int, error)) {
  325. for _, test := range scanTests {
  326. var r io.Reader
  327. if name == "StringReader" {
  328. r = strings.NewReader(test.text)
  329. } else {
  330. r = newReader(test.text)
  331. }
  332. n, err := scan(r, test.in)
  333. if err != nil {
  334. m := ""
  335. if n > 0 {
  336. m = Sprintf(" (%d fields ok)", n)
  337. }
  338. t.Errorf("%s got error scanning %q: %s%s", name, test.text, err, m)
  339. continue
  340. }
  341. if n != 1 {
  342. t.Errorf("%s count error on entry %q: got %d", name, test.text, n)
  343. continue
  344. }
  345. // The incoming value may be a pointer
  346. v := reflect.ValueOf(test.in)
  347. if p := v; p.Kind() == reflect.Ptr {
  348. v = p.Elem()
  349. }
  350. val := v.Interface()
  351. if !reflect.DeepEqual(val, test.out) {
  352. t.Errorf("%s scanning %q: expected %#v got %#v, type %T", name, test.text, test.out, val, val)
  353. }
  354. }
  355. }
  356. func TestScan(t *testing.T) {
  357. testScan("StringReader", t, Fscan)
  358. }
  359. func TestMyReaderScan(t *testing.T) {
  360. testScan("myStringReader", t, Fscan)
  361. }
  362. func TestScanln(t *testing.T) {
  363. testScan("StringReader", t, Fscanln)
  364. }
  365. func TestMyReaderScanln(t *testing.T) {
  366. testScan("myStringReader", t, Fscanln)
  367. }
  368. func TestScanf(t *testing.T) {
  369. for _, test := range scanfTests {
  370. n, err := Sscanf(test.text, test.format, test.in)
  371. if err != nil {
  372. t.Errorf("got error scanning (%q, %q): %s", test.format, test.text, err)
  373. continue
  374. }
  375. if n != 1 {
  376. t.Errorf("count error on entry (%q, %q): got %d", test.format, test.text, n)
  377. continue
  378. }
  379. // The incoming value may be a pointer
  380. v := reflect.ValueOf(test.in)
  381. if p := v; p.Kind() == reflect.Ptr {
  382. v = p.Elem()
  383. }
  384. val := v.Interface()
  385. if !reflect.DeepEqual(val, test.out) {
  386. t.Errorf("scanning (%q, %q): expected %#v got %#v, type %T", test.format, test.text, test.out, val, val)
  387. }
  388. }
  389. }
  390. func TestScanOverflow(t *testing.T) {
  391. // different machines and different types report errors with different strings.
  392. re := regexp.MustCompile("overflow|too large|out of range|not representable")
  393. for _, test := range overflowTests {
  394. _, err := Sscan(test.text, test.in)
  395. if err == nil {
  396. t.Errorf("expected overflow scanning %q", test.text)
  397. continue
  398. }
  399. if !re.MatchString(err.Error()) {
  400. t.Errorf("expected overflow error scanning %q: %s", test.text, err)
  401. }
  402. }
  403. }
  404. func verifyNaN(str string, t *testing.T) {
  405. var f float64
  406. var f32 float32
  407. var f64 float64
  408. text := str + " " + str + " " + str
  409. n, err := Fscan(strings.NewReader(text), &f, &f32, &f64)
  410. if err != nil {
  411. t.Errorf("got error scanning %q: %s", text, err)
  412. }
  413. if n != 3 {
  414. t.Errorf("count error scanning %q: got %d", text, n)
  415. }
  416. if !math.IsNaN(float64(f)) || !math.IsNaN(float64(f32)) || !math.IsNaN(f64) {
  417. t.Errorf("didn't get NaNs scanning %q: got %g %g %g", text, f, f32, f64)
  418. }
  419. }
  420. func TestNaN(t *testing.T) {
  421. for _, s := range []string{"nan", "NAN", "NaN"} {
  422. verifyNaN(s, t)
  423. }
  424. }
  425. func verifyInf(str string, t *testing.T) {
  426. var f float64
  427. var f32 float32
  428. var f64 float64
  429. text := str + " " + str + " " + str
  430. n, err := Fscan(strings.NewReader(text), &f, &f32, &f64)
  431. if err != nil {
  432. t.Errorf("got error scanning %q: %s", text, err)
  433. }
  434. if n != 3 {
  435. t.Errorf("count error scanning %q: got %d", text, n)
  436. }
  437. sign := 1
  438. if str[0] == '-' {
  439. sign = -1
  440. }
  441. if !math.IsInf(float64(f), sign) || !math.IsInf(float64(f32), sign) || !math.IsInf(f64, sign) {
  442. t.Errorf("didn't get right Infs scanning %q: got %g %g %g", text, f, f32, f64)
  443. }
  444. }
  445. func TestInf(t *testing.T) {
  446. for _, s := range []string{"inf", "+inf", "-inf", "INF", "-INF", "+INF", "Inf", "-Inf", "+Inf"} {
  447. verifyInf(s, t)
  448. }
  449. }
  450. func testScanfMulti(name string, t *testing.T) {
  451. sliceType := reflect.TypeOf(make([]interface{}, 1))
  452. for _, test := range multiTests {
  453. var r io.Reader
  454. if name == "StringReader" {
  455. r = strings.NewReader(test.text)
  456. } else {
  457. r = newReader(test.text)
  458. }
  459. n, err := Fscanf(r, test.format, test.in...)
  460. if err != nil {
  461. if test.err == "" {
  462. t.Errorf("got error scanning (%q, %q): %q", test.format, test.text, err)
  463. } else if strings.Index(err.Error(), test.err) < 0 {
  464. t.Errorf("got wrong error scanning (%q, %q): %q; expected %q", test.format, test.text, err, test.err)
  465. }
  466. continue
  467. }
  468. if test.err != "" {
  469. t.Errorf("expected error %q error scanning (%q, %q)", test.err, test.format, test.text)
  470. }
  471. if n != len(test.out) {
  472. t.Errorf("count error on entry (%q, %q): expected %d got %d", test.format, test.text, len(test.out), n)
  473. continue
  474. }
  475. // Convert the slice of pointers into a slice of values
  476. resultVal := reflect.MakeSlice(sliceType, n, n)
  477. for i := 0; i < n; i++ {
  478. v := reflect.ValueOf(test.in[i]).Elem()
  479. resultVal.Index(i).Set(v)
  480. }
  481. result := resultVal.Interface()
  482. if !reflect.DeepEqual(result, test.out) {
  483. t.Errorf("scanning (%q, %q): expected %#v got %#v", test.format, test.text, test.out, result)
  484. }
  485. }
  486. }
  487. func TestScanfMulti(t *testing.T) {
  488. testScanfMulti("StringReader", t)
  489. }
  490. func TestMyReaderScanfMulti(t *testing.T) {
  491. testScanfMulti("myStringReader", t)
  492. }
  493. func TestScanMultiple(t *testing.T) {
  494. var a int
  495. var s string
  496. n, err := Sscan("123abc", &a, &s)
  497. if n != 2 {
  498. t.Errorf("Sscan count error: expected 2: got %d", n)
  499. }
  500. if err != nil {
  501. t.Errorf("Sscan expected no error; got %s", err)
  502. }
  503. if a != 123 || s != "abc" {
  504. t.Errorf("Sscan wrong values: got (%d %q) expected (123 \"abc\")", a, s)
  505. }
  506. n, err = Sscan("asdf", &s, &a)
  507. if n != 1 {
  508. t.Errorf("Sscan count error: expected 1: got %d", n)
  509. }
  510. if err == nil {
  511. t.Errorf("Sscan expected error; got none: %s", err)
  512. }
  513. if s != "asdf" {
  514. t.Errorf("Sscan wrong values: got %q expected \"asdf\"", s)
  515. }
  516. }
  517. // Empty strings are not valid input when scanning a string.
  518. func TestScanEmpty(t *testing.T) {
  519. var s1, s2 string
  520. n, err := Sscan("abc", &s1, &s2)
  521. if n != 1 {
  522. t.Errorf("Sscan count error: expected 1: got %d", n)
  523. }
  524. if err == nil {
  525. t.Error("Sscan <one item> expected error; got none")
  526. }
  527. if s1 != "abc" {
  528. t.Errorf("Sscan wrong values: got %q expected \"abc\"", s1)
  529. }
  530. n, err = Sscan("", &s1, &s2)
  531. if n != 0 {
  532. t.Errorf("Sscan count error: expected 0: got %d", n)
  533. }
  534. if err == nil {
  535. t.Error("Sscan <empty> expected error; got none")
  536. }
  537. // Quoted empty string is OK.
  538. n, err = Sscanf(`""`, "%q", &s1)
  539. if n != 1 {
  540. t.Errorf("Sscanf count error: expected 1: got %d", n)
  541. }
  542. if err != nil {
  543. t.Errorf("Sscanf <empty> expected no error with quoted string; got %s", err)
  544. }
  545. }
  546. func TestScanNotPointer(t *testing.T) {
  547. r := strings.NewReader("1")
  548. var a int
  549. _, err := Fscan(r, a)
  550. if err == nil {
  551. t.Error("expected error scanning non-pointer")
  552. } else if strings.Index(err.Error(), "pointer") < 0 {
  553. t.Errorf("expected pointer error scanning non-pointer, got: %s", err)
  554. }
  555. }
  556. func TestScanlnNoNewline(t *testing.T) {
  557. var a int
  558. _, err := Sscanln("1 x\n", &a)
  559. if err == nil {
  560. t.Error("expected error scanning string missing newline")
  561. } else if strings.Index(err.Error(), "newline") < 0 {
  562. t.Errorf("expected newline error scanning string missing newline, got: %s", err)
  563. }
  564. }
  565. func TestScanlnWithMiddleNewline(t *testing.T) {
  566. r := strings.NewReader("123\n456\n")
  567. var a, b int
  568. _, err := Fscanln(r, &a, &b)
  569. if err == nil {
  570. t.Error("expected error scanning string with extra newline")
  571. } else if strings.Index(err.Error(), "newline") < 0 {
  572. t.Errorf("expected newline error scanning string with extra newline, got: %s", err)
  573. }
  574. }
  575. // eofCounter is a special Reader that counts reads at end of file.
  576. type eofCounter struct {
  577. reader *strings.Reader
  578. eofCount int
  579. }
  580. func (ec *eofCounter) Read(b []byte) (n int, err error) {
  581. n, err = ec.reader.Read(b)
  582. if n == 0 {
  583. ec.eofCount++
  584. }
  585. return
  586. }
  587. // TestEOF verifies that when we scan, we see at most EOF once per call to a
  588. // Scan function, and then only when it's really an EOF.
  589. func TestEOF(t *testing.T) {
  590. ec := &eofCounter{strings.NewReader("123\n"), 0}
  591. var a int
  592. n, err := Fscanln(ec, &a)
  593. if err != nil {
  594. t.Error("unexpected error", err)
  595. }
  596. if n != 1 {
  597. t.Error("expected to scan one item, got", n)
  598. }
  599. if ec.eofCount != 0 {
  600. t.Error("expected zero EOFs", ec.eofCount)
  601. ec.eofCount = 0 // reset for next test
  602. }
  603. n, err = Fscanln(ec, &a)
  604. if err == nil {
  605. t.Error("expected error scanning empty string")
  606. }
  607. if n != 0 {
  608. t.Error("expected to scan zero items, got", n)
  609. }
  610. if ec.eofCount != 1 {
  611. t.Error("expected one EOF, got", ec.eofCount)
  612. }
  613. }
  614. // TestEOFAtEndOfInput verifies that we see an EOF error if we run out of input.
  615. // This was a buglet: we used to get "expected integer".
  616. func TestEOFAtEndOfInput(t *testing.T) {
  617. var i, j int
  618. n, err := Sscanf("23", "%d %d", &i, &j)
  619. if n != 1 || i != 23 {
  620. t.Errorf("Sscanf expected one value of 23; got %d %d", n, i)
  621. }
  622. if err != io.EOF {
  623. t.Errorf("Sscanf expected EOF; got %q", err)
  624. }
  625. n, err = Sscan("234", &i, &j)
  626. if n != 1 || i != 234 {
  627. t.Errorf("Sscan expected one value of 234; got %d %d", n, i)
  628. }
  629. if err != io.EOF {
  630. t.Errorf("Sscan expected EOF; got %q", err)
  631. }
  632. // Trailing space is tougher.
  633. n, err = Sscan("234 ", &i, &j)
  634. if n != 1 || i != 234 {
  635. t.Errorf("Sscan expected one value of 234; got %d %d", n, i)
  636. }
  637. if err != io.EOF {
  638. t.Errorf("Sscan expected EOF; got %q", err)
  639. }
  640. }
  641. var eofTests = []struct {
  642. format string
  643. v interface{}
  644. }{
  645. {"%s", &stringVal},
  646. {"%q", &stringVal},
  647. {"%x", &stringVal},
  648. {"%v", &stringVal},
  649. {"%v", &bytesVal},
  650. {"%v", &intVal},
  651. {"%v", &uintVal},
  652. {"%v", &boolVal},
  653. {"%v", &float32Val},
  654. {"%v", &complex64Val},
  655. {"%v", &renamedStringVal},
  656. {"%v", &renamedBytesVal},
  657. {"%v", &renamedIntVal},
  658. {"%v", &renamedUintVal},
  659. {"%v", &renamedBoolVal},
  660. {"%v", &renamedFloat32Val},
  661. {"%v", &renamedComplex64Val},
  662. }
  663. func TestEOFAllTypes(t *testing.T) {
  664. for i, test := range eofTests {
  665. if _, err := Sscanf("", test.format, test.v); err != io.EOF {
  666. t.Errorf("#%d: %s %T not eof on empty string: %s", i, test.format, test.v, err)
  667. }
  668. if _, err := Sscanf(" ", test.format, test.v); err != io.EOF {
  669. t.Errorf("#%d: %s %T not eof on trailing blanks: %s", i, test.format, test.v, err)
  670. }
  671. }
  672. }
  673. // TestUnreadRuneWithBufio verifies that, at least when using bufio, successive
  674. // calls to Fscan do not lose runes.
  675. func TestUnreadRuneWithBufio(t *testing.T) {
  676. r := bufio.NewReader(strings.NewReader("123αb"))
  677. var i int
  678. var a string
  679. n, err := Fscanf(r, "%d", &i)
  680. if n != 1 || err != nil {
  681. t.Errorf("reading int expected one item, no errors; got %d %q", n, err)
  682. }
  683. if i != 123 {
  684. t.Errorf("expected 123; got %d", i)
  685. }
  686. n, err = Fscanf(r, "%s", &a)
  687. if n != 1 || err != nil {
  688. t.Errorf("reading string expected one item, no errors; got %d %q", n, err)
  689. }
  690. if a != "αb" {
  691. t.Errorf("expected αb; got %q", a)
  692. }
  693. }
  694. type TwoLines string
  695. // Scan attempts to read two lines into the object. Scanln should prevent this
  696. // because it stops at newline; Scan and Scanf should be fine.
  697. func (t *TwoLines) Scan(state ScanState, verb rune) error {
  698. chars := make([]rune, 0, 100)
  699. for nlCount := 0; nlCount < 2; {
  700. c, _, err := state.ReadRune()
  701. if err != nil {
  702. return err
  703. }
  704. chars = append(chars, c)
  705. if c == '\n' {
  706. nlCount++
  707. }
  708. }
  709. *t = TwoLines(string(chars))
  710. return nil
  711. }
  712. func TestMultiLine(t *testing.T) {
  713. input := "abc\ndef\n"
  714. // Sscan should work
  715. var tscan TwoLines
  716. n, err := Sscan(input, &tscan)
  717. if n != 1 {
  718. t.Errorf("Sscan: expected 1 item; got %d", n)
  719. }
  720. if err != nil {
  721. t.Errorf("Sscan: expected no error; got %s", err)
  722. }
  723. if string(tscan) != input {
  724. t.Errorf("Sscan: expected %q; got %q", input, tscan)
  725. }
  726. // Sscanf should work
  727. var tscanf TwoLines
  728. n, err = Sscanf(input, "%s", &tscanf)
  729. if n != 1 {
  730. t.Errorf("Sscanf: expected 1 item; got %d", n)
  731. }
  732. if err != nil {
  733. t.Errorf("Sscanf: expected no error; got %s", err)
  734. }
  735. if string(tscanf) != input {
  736. t.Errorf("Sscanf: expected %q; got %q", input, tscanf)
  737. }
  738. // Sscanln should not work
  739. var tscanln TwoLines
  740. n, err = Sscanln(input, &tscanln)
  741. if n != 0 {
  742. t.Errorf("Sscanln: expected 0 items; got %d: %q", n, tscanln)
  743. }
  744. if err == nil {
  745. t.Error("Sscanln: expected error; got none")
  746. } else if err != io.ErrUnexpectedEOF {
  747. t.Errorf("Sscanln: expected io.ErrUnexpectedEOF (ha!); got %s", err)
  748. }
  749. }
  750. // simpleReader is a strings.Reader that implements only Read, not ReadRune.
  751. // Good for testing readahead.
  752. type simpleReader struct {
  753. sr *strings.Reader
  754. }
  755. func (s *simpleReader) Read(b []byte) (n int, err error) {
  756. return s.sr.Read(b)
  757. }
  758. // TestLineByLineFscanf tests that Fscanf does not read past newline. Issue
  759. // 3481.
  760. func TestLineByLineFscanf(t *testing.T) {
  761. r := &simpleReader{strings.NewReader("1\n2\n")}
  762. var i, j int
  763. n, err := Fscanf(r, "%v\n", &i)
  764. if n != 1 || err != nil {
  765. t.Fatalf("first read: %d %q", n, err)
  766. }
  767. n, err = Fscanf(r, "%v\n", &j)
  768. if n != 1 || err != nil {
  769. t.Fatalf("second read: %d %q", n, err)
  770. }
  771. if i != 1 || j != 2 {
  772. t.Errorf("wrong values; wanted 1 2 got %d %d", i, j)
  773. }
  774. }
  775. // TestScanStateCount verifies the correct byte count is returned. Issue 8512.
  776. // runeScanner implements the Scanner interface for TestScanStateCount.
  777. type runeScanner struct {
  778. rune rune
  779. size int
  780. }
  781. func (rs *runeScanner) Scan(state ScanState, verb rune) error {
  782. r, size, err := state.ReadRune()
  783. rs.rune = r
  784. rs.size = size
  785. return err
  786. }
  787. func TestScanStateCount(t *testing.T) {
  788. var a, b, c runeScanner
  789. n, err := Sscanf("12➂", "%c%c%c", &a, &b, &c)
  790. if err != nil {
  791. t.Fatal(err)
  792. }
  793. if n != 3 {
  794. t.Fatalf("expected 3 items consumed, got %d")
  795. }
  796. if a.rune != '1' || b.rune != '2' || c.rune != '➂' {
  797. t.Errorf("bad scan rune: %q %q %q should be '1' '2' '➂'", a.rune, b.rune, c.rune)
  798. }
  799. if a.size != 1 || b.size != 1 || c.size != 3 {
  800. t.Errorf("bad scan size: %q %q %q should be 1 1 3", a.size, b.size, c.size)
  801. }
  802. }
  803. // RecursiveInt accepts a string matching %d.%d.%d....
  804. // and parses it into a linked list.
  805. // It allows us to benchmark recursive descent style scanners.
  806. type RecursiveInt struct {
  807. i int
  808. next *RecursiveInt
  809. }
  810. func (r *RecursiveInt) Scan(state ScanState, verb rune) (err error) {
  811. _, err = Fscan(state, &r.i)
  812. if err != nil {
  813. return
  814. }
  815. next := new(RecursiveInt)
  816. _, err = Fscanf(state, ".%v", next)
  817. if err != nil {
  818. if err == io.ErrUnexpectedEOF {
  819. err = nil
  820. }
  821. return
  822. }
  823. r.next = next
  824. return
  825. }
  826. // scanInts performs the same scanning task as RecursiveInt.Scan
  827. // but without recurring through scanner, so we can compare
  828. // performance more directly.
  829. func scanInts(r *RecursiveInt, b *bytes.Buffer) (err error) {
  830. r.next = nil
  831. _, err = Fscan(b, &r.i)
  832. if err != nil {
  833. return
  834. }
  835. c, _, err := b.ReadRune()
  836. if err != nil {
  837. if err == io.EOF {
  838. err = nil
  839. }
  840. return
  841. }
  842. if c != '.' {
  843. return
  844. }
  845. next := new(RecursiveInt)
  846. err = scanInts(next, b)
  847. if err == nil {
  848. r.next = next
  849. }
  850. return
  851. }
  852. func makeInts(n int) []byte {
  853. var buf bytes.Buffer
  854. Fprintf(&buf, "1")
  855. for i := 1; i < n; i++ {
  856. Fprintf(&buf, ".%d", i+1)
  857. }
  858. return buf.Bytes()
  859. }
  860. func TestScanInts(t *testing.T) {
  861. testScanInts(t, scanInts)
  862. testScanInts(t, func(r *RecursiveInt, b *bytes.Buffer) (err error) {
  863. _, err = Fscan(b, r)
  864. return
  865. })
  866. }
  867. // 800 is small enough to not overflow the stack when using gccgo on a
  868. // platform that does not support split stack.
  869. const intCount = 800
  870. func testScanInts(t *testing.T, scan func(*RecursiveInt, *bytes.Buffer) error) {
  871. r := new(RecursiveInt)
  872. ints := makeInts(intCount)
  873. buf := bytes.NewBuffer(ints)
  874. err := scan(r, buf)
  875. if err != nil {
  876. t.Error("unexpected error", err)
  877. }
  878. i := 1
  879. for ; r != nil; r = r.next {
  880. if r.i != i {
  881. t.Fatalf("bad scan: expected %d got %d", i, r.i)
  882. }
  883. i++
  884. }
  885. if i-1 != intCount {
  886. t.Fatalf("bad scan count: expected %d got %d", intCount, i-1)
  887. }
  888. }
  889. func BenchmarkScanInts(b *testing.B) {
  890. b.ResetTimer()
  891. ints := makeInts(intCount)
  892. var r RecursiveInt
  893. for i := b.N - 1; i >= 0; i-- {
  894. buf := bytes.NewBuffer(ints)
  895. b.StartTimer()
  896. scanInts(&r, buf)
  897. b.StopTimer()
  898. }
  899. }
  900. func BenchmarkScanRecursiveInt(b *testing.B) {
  901. b.ResetTimer()
  902. ints := makeInts(intCount)
  903. var r RecursiveInt
  904. for i := b.N - 1; i >= 0; i-- {
  905. buf := bytes.NewBuffer(ints)
  906. b.StartTimer()
  907. Fscan(buf, &r)
  908. b.StopTimer()
  909. }
  910. }