Source file src/fmt/scan_test.go

Documentation: fmt

     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  
     5  package fmt_test
     6  
     7  import (
     8  	"bufio"
     9  	"bytes"
    10  	"errors"
    11  	. "fmt"
    12  	"io"
    13  	"math"
    14  	"reflect"
    15  	"regexp"
    16  	"strings"
    17  	"testing"
    18  	"testing/iotest"
    19  	"unicode/utf8"
    20  )
    21  
    22  type ScanTest struct {
    23  	text string
    24  	in   interface{}
    25  	out  interface{}
    26  }
    27  
    28  type ScanfTest struct {
    29  	format string
    30  	text   string
    31  	in     interface{}
    32  	out    interface{}
    33  }
    34  
    35  type ScanfMultiTest struct {
    36  	format string
    37  	text   string
    38  	in     []interface{}
    39  	out    []interface{}
    40  	err    string
    41  }
    42  
    43  var (
    44  	boolVal              bool
    45  	intVal               int
    46  	int8Val              int8
    47  	int16Val             int16
    48  	int32Val             int32
    49  	int64Val             int64
    50  	uintVal              uint
    51  	uint8Val             uint8
    52  	uint16Val            uint16
    53  	uint32Val            uint32
    54  	uint64Val            uint64
    55  	float32Val           float32
    56  	float64Val           float64
    57  	stringVal            string
    58  	bytesVal             []byte
    59  	runeVal              rune
    60  	complex64Val         complex64
    61  	complex128Val        complex128
    62  	renamedBoolVal       renamedBool
    63  	renamedIntVal        renamedInt
    64  	renamedInt8Val       renamedInt8
    65  	renamedInt16Val      renamedInt16
    66  	renamedInt32Val      renamedInt32
    67  	renamedInt64Val      renamedInt64
    68  	renamedUintVal       renamedUint
    69  	renamedUint8Val      renamedUint8
    70  	renamedUint16Val     renamedUint16
    71  	renamedUint32Val     renamedUint32
    72  	renamedUint64Val     renamedUint64
    73  	renamedUintptrVal    renamedUintptr
    74  	renamedStringVal     renamedString
    75  	renamedBytesVal      renamedBytes
    76  	renamedFloat32Val    renamedFloat32
    77  	renamedFloat64Val    renamedFloat64
    78  	renamedComplex64Val  renamedComplex64
    79  	renamedComplex128Val renamedComplex128
    80  )
    81  
    82  // Xs accepts any non-empty run of the verb character
    83  type Xs string
    84  
    85  func (x *Xs) Scan(state ScanState, verb rune) error {
    86  	tok, err := state.Token(true, func(r rune) bool { return r == verb })
    87  	if err != nil {
    88  		return err
    89  	}
    90  	s := string(tok)
    91  	if !regexp.MustCompile("^" + string(verb) + "+$").MatchString(s) {
    92  		return errors.New("syntax error for xs")
    93  	}
    94  	*x = Xs(s)
    95  	return nil
    96  }
    97  
    98  var xVal Xs
    99  
   100  // IntString accepts an integer followed immediately by a string.
   101  // It tests the embedding of a scan within a scan.
   102  type IntString struct {
   103  	i int
   104  	s string
   105  }
   106  
   107  func (s *IntString) Scan(state ScanState, verb rune) error {
   108  	if _, err := Fscan(state, &s.i); err != nil {
   109  		return err
   110  	}
   111  
   112  	tok, err := state.Token(true, nil)
   113  	if err != nil {
   114  		return err
   115  	}
   116  	s.s = string(tok)
   117  	return nil
   118  }
   119  
   120  var intStringVal IntString
   121  
   122  var scanTests = []ScanTest{
   123  	// Basic types
   124  	{"T\n", &boolVal, true},  // boolean test vals toggle to be sure they are written
   125  	{"F\n", &boolVal, false}, // restored to zero value
   126  	{"21\n", &intVal, 21},
   127  	{"2_1\n", &intVal, 21},
   128  	{"0\n", &intVal, 0},
   129  	{"000\n", &intVal, 0},
   130  	{"0x10\n", &intVal, 0x10},
   131  	{"0x_1_0\n", &intVal, 0x10},
   132  	{"-0x10\n", &intVal, -0x10},
   133  	{"0377\n", &intVal, 0377},
   134  	{"0_3_7_7\n", &intVal, 0377},
   135  	{"0o377\n", &intVal, 0377},
   136  	{"0o_3_7_7\n", &intVal, 0377},
   137  	{"-0377\n", &intVal, -0377},
   138  	{"-0o377\n", &intVal, -0377},
   139  	{"0\n", &uintVal, uint(0)},
   140  	{"000\n", &uintVal, uint(0)},
   141  	{"0x10\n", &uintVal, uint(0x10)},
   142  	{"0377\n", &uintVal, uint(0377)},
   143  	{"22\n", &int8Val, int8(22)},
   144  	{"23\n", &int16Val, int16(23)},
   145  	{"24\n", &int32Val, int32(24)},
   146  	{"25\n", &int64Val, int64(25)},
   147  	{"127\n", &int8Val, int8(127)},
   148  	{"-21\n", &intVal, -21},
   149  	{"-22\n", &int8Val, int8(-22)},
   150  	{"-23\n", &int16Val, int16(-23)},
   151  	{"-24\n", &int32Val, int32(-24)},
   152  	{"-25\n", &int64Val, int64(-25)},
   153  	{"-128\n", &int8Val, int8(-128)},
   154  	{"+21\n", &intVal, +21},
   155  	{"+22\n", &int8Val, int8(+22)},
   156  	{"+23\n", &int16Val, int16(+23)},
   157  	{"+24\n", &int32Val, int32(+24)},
   158  	{"+25\n", &int64Val, int64(+25)},
   159  	{"+127\n", &int8Val, int8(+127)},
   160  	{"26\n", &uintVal, uint(26)},
   161  	{"27\n", &uint8Val, uint8(27)},
   162  	{"28\n", &uint16Val, uint16(28)},
   163  	{"29\n", &uint32Val, uint32(29)},
   164  	{"30\n", &uint64Val, uint64(30)},
   165  	{"255\n", &uint8Val, uint8(255)},
   166  	{"32767\n", &int16Val, int16(32767)},
   167  	{"2.3\n", &float64Val, 2.3},
   168  	{"2.3e1\n", &float32Val, float32(2.3e1)},
   169  	{"2.3e2\n", &float64Val, 2.3e2},
   170  	{"2.3p2\n", &float64Val, 2.3 * 4},
   171  	{"2.3p+2\n", &float64Val, 2.3 * 4},
   172  	{"2.3p+66\n", &float64Val, 2.3 * (1 << 66)},
   173  	{"2.3p-66\n", &float64Val, 2.3 / (1 << 66)},
   174  	{"0x2.3p-66\n", &float64Val, float64(0x23) / (1 << 70)},
   175  	{"2_3.4_5\n", &float64Val, 23.45},
   176  	{"2.35\n", &stringVal, "2.35"},
   177  	{"2345678\n", &bytesVal, []byte("2345678")},
   178  	{"(3.4e1-2i)\n", &complex128Val, 3.4e1 - 2i},
   179  	{"-3.45e1-3i\n", &complex64Val, complex64(-3.45e1 - 3i)},
   180  	{"-.45e1-1e2i\n", &complex128Val, complex128(-.45e1 - 100i)},
   181  	{"-.4_5e1-1E2i\n", &complex128Val, complex128(-.45e1 - 100i)},
   182  	{"0x1.0p1+0x1.0P2i\n", &complex128Val, complex128(2 + 4i)},
   183  	{"-0x1p1-0x1p2i\n", &complex128Val, complex128(-2 - 4i)},
   184  	{"-0x1ep-1-0x1p2i\n", &complex128Val, complex128(-15 - 4i)},
   185  	{"-0x1_Ep-1-0x1p0_2i\n", &complex128Val, complex128(-15 - 4i)},
   186  	{"hello\n", &stringVal, "hello"},
   187  
   188  	// Carriage-return followed by newline. (We treat \r\n as \n always.)
   189  	{"hello\r\n", &stringVal, "hello"},
   190  	{"27\r\n", &uint8Val, uint8(27)},
   191  
   192  	// Renamed types
   193  	{"true\n", &renamedBoolVal, renamedBool(true)},
   194  	{"F\n", &renamedBoolVal, renamedBool(false)},
   195  	{"101\n", &renamedIntVal, renamedInt(101)},
   196  	{"102\n", &renamedIntVal, renamedInt(102)},
   197  	{"103\n", &renamedUintVal, renamedUint(103)},
   198  	{"104\n", &renamedUintVal, renamedUint(104)},
   199  	{"105\n", &renamedInt8Val, renamedInt8(105)},
   200  	{"106\n", &renamedInt16Val, renamedInt16(106)},
   201  	{"107\n", &renamedInt32Val, renamedInt32(107)},
   202  	{"108\n", &renamedInt64Val, renamedInt64(108)},
   203  	{"109\n", &renamedUint8Val, renamedUint8(109)},
   204  	{"110\n", &renamedUint16Val, renamedUint16(110)},
   205  	{"111\n", &renamedUint32Val, renamedUint32(111)},
   206  	{"112\n", &renamedUint64Val, renamedUint64(112)},
   207  	{"113\n", &renamedUintptrVal, renamedUintptr(113)},
   208  	{"114\n", &renamedStringVal, renamedString("114")},
   209  	{"115\n", &renamedBytesVal, renamedBytes([]byte("115"))},
   210  
   211  	// Custom scanners.
   212  	{"  vvv ", &xVal, Xs("vvv")},
   213  	{" 1234hello", &intStringVal, IntString{1234, "hello"}},
   214  
   215  	// Fixed bugs
   216  	{"2147483648\n", &int64Val, int64(2147483648)}, // was: integer overflow
   217  }
   218  
   219  var scanfTests = []ScanfTest{
   220  	{"%v", "TRUE\n", &boolVal, true},
   221  	{"%t", "false\n", &boolVal, false},
   222  	{"%v", "-71\n", &intVal, -71},
   223  	{"%v", "-7_1\n", &intVal, -71},
   224  	{"%v", "0b111\n", &intVal, 7},
   225  	{"%v", "0b_1_1_1\n", &intVal, 7},
   226  	{"%v", "0377\n", &intVal, 0377},
   227  	{"%v", "0_3_7_7\n", &intVal, 0377},
   228  	{"%v", "0o377\n", &intVal, 0377},
   229  	{"%v", "0o_3_7_7\n", &intVal, 0377},
   230  	{"%v", "0x44\n", &intVal, 0x44},
   231  	{"%v", "0x_4_4\n", &intVal, 0x44},
   232  	{"%d", "72\n", &intVal, 72},
   233  	{"%c", "a\n", &runeVal, 'a'},
   234  	{"%c", "\u5072\n", &runeVal, '\u5072'},
   235  	{"%c", "\u1234\n", &runeVal, '\u1234'},
   236  	{"%d", "73\n", &int8Val, int8(73)},
   237  	{"%d", "+74\n", &int16Val, int16(74)},
   238  	{"%d", "75\n", &int32Val, int32(75)},
   239  	{"%d", "76\n", &int64Val, int64(76)},
   240  	{"%b", "1001001\n", &intVal, 73},
   241  	{"%o", "075\n", &intVal, 075},
   242  	{"%x", "a75\n", &intVal, 0xa75},
   243  	{"%v", "71\n", &uintVal, uint(71)},
   244  	{"%d", "72\n", &uintVal, uint(72)},
   245  	{"%d", "7_2\n", &uintVal, uint(7)}, // only %v takes underscores
   246  	{"%d", "73\n", &uint8Val, uint8(73)},
   247  	{"%d", "74\n", &uint16Val, uint16(74)},
   248  	{"%d", "75\n", &uint32Val, uint32(75)},
   249  	{"%d", "76\n", &uint64Val, uint64(76)},
   250  	{"%b", "1001001\n", &uintVal, uint(73)},
   251  	{"%b", "100_1001\n", &uintVal, uint(4)},
   252  	{"%o", "075\n", &uintVal, uint(075)},
   253  	{"%o", "07_5\n", &uintVal, uint(07)}, // only %v takes underscores
   254  	{"%x", "a75\n", &uintVal, uint(0xa75)},
   255  	{"%x", "A75\n", &uintVal, uint(0xa75)},
   256  	{"%x", "A7_5\n", &uintVal, uint(0xa7)}, // only %v takes underscores
   257  	{"%U", "U+1234\n", &intVal, int(0x1234)},
   258  	{"%U", "U+4567\n", &uintVal, uint(0x4567)},
   259  
   260  	{"%e", "2.3\n", &float64Val, 2.3},
   261  	{"%E", "2.3e1\n", &float32Val, float32(2.3e1)},
   262  	{"%f", "2.3e2\n", &float64Val, 2.3e2},
   263  	{"%g", "2.3p2\n", &float64Val, 2.3 * 4},
   264  	{"%G", "2.3p+2\n", &float64Val, 2.3 * 4},
   265  	{"%v", "2.3p+66\n", &float64Val, 2.3 * (1 << 66)},
   266  	{"%f", "2.3p-66\n", &float64Val, 2.3 / (1 << 66)},
   267  	{"%G", "0x2.3p-66\n", &float64Val, float64(0x23) / (1 << 70)},
   268  	{"%E", "2_3.4_5\n", &float64Val, 23.45},
   269  
   270  	// Strings
   271  	{"%s", "using-%s\n", &stringVal, "using-%s"},
   272  	{"%x", "7573696e672d2578\n", &stringVal, "using-%x"},
   273  	{"%X", "7573696E672D2558\n", &stringVal, "using-%X"},
   274  	{"%q", `"quoted\twith\\do\u0075bl\x65s"` + "\n", &stringVal, "quoted\twith\\doubles"},
   275  	{"%q", "`quoted with backs`\n", &stringVal, "quoted with backs"},
   276  
   277  	// Byte slices
   278  	{"%s", "bytes-%s\n", &bytesVal, []byte("bytes-%s")},
   279  	{"%x", "62797465732d2578\n", &bytesVal, []byte("bytes-%x")},
   280  	{"%X", "62797465732D2558\n", &bytesVal, []byte("bytes-%X")},
   281  	{"%q", `"bytes\rwith\vdo\u0075bl\x65s"` + "\n", &bytesVal, []byte("bytes\rwith\vdoubles")},
   282  	{"%q", "`bytes with backs`\n", &bytesVal, []byte("bytes with backs")},
   283  
   284  	// Renamed types
   285  	{"%v\n", "true\n", &renamedBoolVal, renamedBool(true)},
   286  	{"%t\n", "F\n", &renamedBoolVal, renamedBool(false)},
   287  	{"%v", "101\n", &renamedIntVal, renamedInt(101)},
   288  	{"%c", "\u0101\n", &renamedIntVal, renamedInt('\u0101')},
   289  	{"%o", "0146\n", &renamedIntVal, renamedInt(102)},
   290  	{"%v", "103\n", &renamedUintVal, renamedUint(103)},
   291  	{"%d", "104\n", &renamedUintVal, renamedUint(104)},
   292  	{"%d", "105\n", &renamedInt8Val, renamedInt8(105)},
   293  	{"%d", "106\n", &renamedInt16Val, renamedInt16(106)},
   294  	{"%d", "107\n", &renamedInt32Val, renamedInt32(107)},
   295  	{"%d", "108\n", &renamedInt64Val, renamedInt64(108)},
   296  	{"%x", "6D\n", &renamedUint8Val, renamedUint8(109)},
   297  	{"%o", "0156\n", &renamedUint16Val, renamedUint16(110)},
   298  	{"%d", "111\n", &renamedUint32Val, renamedUint32(111)},
   299  	{"%d", "112\n", &renamedUint64Val, renamedUint64(112)},
   300  	{"%d", "113\n", &renamedUintptrVal, renamedUintptr(113)},
   301  	{"%s", "114\n", &renamedStringVal, renamedString("114")},
   302  	{"%q", "\"1155\"\n", &renamedBytesVal, renamedBytes([]byte("1155"))},
   303  	{"%g", "116e1\n", &renamedFloat32Val, renamedFloat32(116e1)},
   304  	{"%g", "-11.7e+1", &renamedFloat64Val, renamedFloat64(-11.7e+1)},
   305  	{"%g", "11+6e1i\n", &renamedComplex64Val, renamedComplex64(11 + 6e1i)},
   306  	{"%g", "-11.+7e+1i", &renamedComplex128Val, renamedComplex128(-11. + 7e+1i)},
   307  
   308  	// Interesting formats
   309  	{"here is\tthe value:%d", "here is   the\tvalue:118\n", &intVal, 118},
   310  	{"%% %%:%d", "% %:119\n", &intVal, 119},
   311  	{"%d%%", "42%", &intVal, 42}, // %% at end of string.
   312  
   313  	// Corner cases
   314  	{"%x", "FFFFFFFF\n", &uint32Val, uint32(0xFFFFFFFF)},
   315  
   316  	// Custom scanner.
   317  	{"%s", "  sss ", &xVal, Xs("sss")},
   318  	{"%2s", "sssss", &xVal, Xs("ss")},
   319  
   320  	// Fixed bugs
   321  	{"%d\n", "27\n", &intVal, 27},      // ok
   322  	{"%d\n", "28 \n", &intVal, 28},     // was: "unexpected newline"
   323  	{"%v", "0", &intVal, 0},            // was: "EOF"; 0 was taken as base prefix and not counted.
   324  	{"%v", "0", &uintVal, uint(0)},     // was: "EOF"; 0 was taken as base prefix and not counted.
   325  	{"%c", " ", &uintVal, uint(' ')},   // %c must accept a blank.
   326  	{"%c", "\t", &uintVal, uint('\t')}, // %c must accept any space.
   327  	{"%c", "\n", &uintVal, uint('\n')}, // %c must accept any space.
   328  
   329  	// space handling
   330  	{"%d", "27", &intVal, 27},
   331  	{"%d", "27 ", &intVal, 27},
   332  	{"%d", " 27", &intVal, 27},
   333  	{"%d", " 27 ", &intVal, 27},
   334  
   335  	{"X%d", "X27", &intVal, 27},
   336  	{"X%d", "X27 ", &intVal, 27},
   337  	{"X%d", "X 27", &intVal, 27},
   338  	{"X%d", "X 27 ", &intVal, 27},
   339  
   340  	{"X %d", "X27", &intVal, nil},  // expected space in input to match format
   341  	{"X %d", "X27 ", &intVal, nil}, // expected space in input to match format
   342  	{"X %d", "X 27", &intVal, 27},
   343  	{"X %d", "X 27 ", &intVal, 27},
   344  
   345  	{"%dX", "27X", &intVal, 27},
   346  	{"%dX", "27 X", &intVal, nil}, // input does not match format
   347  	{"%dX", " 27X", &intVal, 27},
   348  	{"%dX", " 27 X", &intVal, nil}, // input does not match format
   349  
   350  	{"%d X", "27X", &intVal, nil}, // expected space in input to match format
   351  	{"%d X", "27 X", &intVal, 27},
   352  	{"%d X", " 27X", &intVal, nil}, // expected space in input to match format
   353  	{"%d X", " 27 X", &intVal, 27},
   354  
   355  	{"X %d X", "X27X", &intVal, nil},  // expected space in input to match format
   356  	{"X %d X", "X27 X", &intVal, nil}, // expected space in input to match format
   357  	{"X %d X", "X 27X", &intVal, nil}, // expected space in input to match format
   358  	{"X %d X", "X 27 X", &intVal, 27},
   359  
   360  	{"X %s X", "X27X", &stringVal, nil},  // expected space in input to match format
   361  	{"X %s X", "X27 X", &stringVal, nil}, // expected space in input to match format
   362  	{"X %s X", "X 27X", &stringVal, nil}, // unexpected EOF
   363  	{"X %s X", "X 27 X", &stringVal, "27"},
   364  
   365  	{"X%sX", "X27X", &stringVal, nil},   // unexpected EOF
   366  	{"X%sX", "X27 X", &stringVal, nil},  // input does not match format
   367  	{"X%sX", "X 27X", &stringVal, nil},  // unexpected EOF
   368  	{"X%sX", "X 27 X", &stringVal, nil}, // input does not match format
   369  
   370  	{"X%s", "X27", &stringVal, "27"},
   371  	{"X%s", "X27 ", &stringVal, "27"},
   372  	{"X%s", "X 27", &stringVal, "27"},
   373  	{"X%s", "X 27 ", &stringVal, "27"},
   374  
   375  	{"X%dX", "X27X", &intVal, 27},
   376  	{"X%dX", "X27 X", &intVal, nil}, // input does not match format
   377  	{"X%dX", "X 27X", &intVal, 27},
   378  	{"X%dX", "X 27 X", &intVal, nil}, // input does not match format
   379  
   380  	{"X%dX", "X27X", &intVal, 27},
   381  	{"X%dX", "X27X ", &intVal, 27},
   382  	{"X%dX", " X27X", &intVal, nil},  // input does not match format
   383  	{"X%dX", " X27X ", &intVal, nil}, // input does not match format
   384  
   385  	{"X%dX\n", "X27X", &intVal, 27},
   386  	{"X%dX \n", "X27X ", &intVal, 27},
   387  	{"X%dX\n", "X27X\n", &intVal, 27},
   388  	{"X%dX\n", "X27X \n", &intVal, 27},
   389  
   390  	{"X%dX \n", "X27X", &intVal, 27},
   391  	{"X%dX \n", "X27X ", &intVal, 27},
   392  	{"X%dX \n", "X27X\n", &intVal, 27},
   393  	{"X%dX \n", "X27X \n", &intVal, 27},
   394  
   395  	{"X%c", "X\n", &runeVal, '\n'},
   396  	{"X%c", "X \n", &runeVal, ' '},
   397  	{"X %c", "X!", &runeVal, nil},  // expected space in input to match format
   398  	{"X %c", "X\n", &runeVal, nil}, // newline in input does not match format
   399  	{"X %c", "X !", &runeVal, '!'},
   400  	{"X %c", "X \n", &runeVal, '\n'},
   401  
   402  	{" X%dX", "X27X", &intVal, nil},  // expected space in input to match format
   403  	{" X%dX", "X27X ", &intVal, nil}, // expected space in input to match format
   404  	{" X%dX", " X27X", &intVal, 27},
   405  	{" X%dX", " X27X ", &intVal, 27},
   406  
   407  	{"X%dX ", "X27X", &intVal, 27},
   408  	{"X%dX ", "X27X ", &intVal, 27},
   409  	{"X%dX ", " X27X", &intVal, nil},  // input does not match format
   410  	{"X%dX ", " X27X ", &intVal, nil}, // input does not match format
   411  
   412  	{" X%dX ", "X27X", &intVal, nil},  // expected space in input to match format
   413  	{" X%dX ", "X27X ", &intVal, nil}, // expected space in input to match format
   414  	{" X%dX ", " X27X", &intVal, 27},
   415  	{" X%dX ", " X27X ", &intVal, 27},
   416  
   417  	{"%d\nX", "27\nX", &intVal, 27},
   418  	{"%dX\n X", "27X\n X", &intVal, 27},
   419  }
   420  
   421  var overflowTests = []ScanTest{
   422  	{"128", &int8Val, 0},
   423  	{"32768", &int16Val, 0},
   424  	{"-129", &int8Val, 0},
   425  	{"-32769", &int16Val, 0},
   426  	{"256", &uint8Val, 0},
   427  	{"65536", &uint16Val, 0},
   428  	{"1e100", &float32Val, 0},
   429  	{"1e500", &float64Val, 0},
   430  	{"(1e100+0i)", &complex64Val, 0},
   431  	{"(1+1e100i)", &complex64Val, 0},
   432  	{"(1-1e500i)", &complex128Val, 0},
   433  }
   434  
   435  var truth bool
   436  var i, j, k int
   437  var f float64
   438  var s, t string
   439  var c complex128
   440  var x, y Xs
   441  var z IntString
   442  var r1, r2, r3 rune
   443  
   444  var multiTests = []ScanfMultiTest{
   445  	{"", "", []interface{}{}, []interface{}{}, ""},
   446  	{"%d", "23", args(&i), args(23), ""},
   447  	{"%2s%3s", "22333", args(&s, &t), args("22", "333"), ""},
   448  	{"%2d%3d", "44555", args(&i, &j), args(44, 555), ""},
   449  	{"%2d.%3d", "66.777", args(&i, &j), args(66, 777), ""},
   450  	{"%d, %d", "23, 18", args(&i, &j), args(23, 18), ""},
   451  	{"%3d22%3d", "33322333", args(&i, &j), args(333, 333), ""},
   452  	{"%6vX=%3fY", "3+2iX=2.5Y", args(&c, &f), args((3 + 2i), 2.5), ""},
   453  	{"%d%s", "123abc", args(&i, &s), args(123, "abc"), ""},
   454  	{"%c%c%c", "2\u50c2X", args(&r1, &r2, &r3), args('2', '\u50c2', 'X'), ""},
   455  	{"%5s%d", " 1234567 ", args(&s, &i), args("12345", 67), ""},
   456  	{"%5s%d", " 12 34 567 ", args(&s, &i), args("12", 34), ""},
   457  
   458  	// Custom scanners.
   459  	{"%e%f", "eefffff", args(&x, &y), args(Xs("ee"), Xs("fffff")), ""},
   460  	{"%4v%s", "12abcd", args(&z, &s), args(IntString{12, "ab"}, "cd"), ""},
   461  
   462  	// Errors
   463  	{"%t", "23 18", args(&i), nil, "bad verb"},
   464  	{"%d %d %d", "23 18", args(&i, &j), args(23, 18), "too few operands"},
   465  	{"%d %d", "23 18 27", args(&i, &j, &k), args(23, 18), "too many operands"},
   466  	{"%c", "\u0100", args(&int8Val), nil, "overflow"},
   467  	{"X%d", "10X", args(&intVal), nil, "input does not match format"},
   468  	{"%d%", "42%", args(&intVal), args(42), "missing verb: % at end of format string"},
   469  	{"%d% ", "42%", args(&intVal), args(42), "too few operands for format '% '"}, // Slightly odd error, but correct.
   470  
   471  	// Bad UTF-8: should see every byte.
   472  	{"%c%c%c", "\xc2X\xc2", args(&r1, &r2, &r3), args(utf8.RuneError, 'X', utf8.RuneError), ""},
   473  
   474  	// Fixed bugs
   475  	{"%v%v", "FALSE23", args(&truth, &i), args(false, 23), ""},
   476  }
   477  
   478  var readers = []struct {
   479  	name string
   480  	f    func(string) io.Reader
   481  }{
   482  	{"StringReader", func(s string) io.Reader {
   483  		return strings.NewReader(s)
   484  	}},
   485  	{"ReaderOnly", func(s string) io.Reader {
   486  		return struct{ io.Reader }{strings.NewReader(s)}
   487  	}},
   488  	{"OneByteReader", func(s string) io.Reader {
   489  		return iotest.OneByteReader(strings.NewReader(s))
   490  	}},
   491  	{"DataErrReader", func(s string) io.Reader {
   492  		return iotest.DataErrReader(strings.NewReader(s))
   493  	}},
   494  }
   495  
   496  func testScan(t *testing.T, f func(string) io.Reader, scan func(r io.Reader, a ...interface{}) (int, error)) {
   497  	for _, test := range scanTests {
   498  		r := f(test.text)
   499  		n, err := scan(r, test.in)
   500  		if err != nil {
   501  			m := ""
   502  			if n > 0 {
   503  				m = Sprintf(" (%d fields ok)", n)
   504  			}
   505  			t.Errorf("got error scanning %q: %s%s", test.text, err, m)
   506  			continue
   507  		}
   508  		if n != 1 {
   509  			t.Errorf("count error on entry %q: got %d", test.text, n)
   510  			continue
   511  		}
   512  		// The incoming value may be a pointer
   513  		v := reflect.ValueOf(test.in)
   514  		if p := v; p.Kind() == reflect.Ptr {
   515  			v = p.Elem()
   516  		}
   517  		val := v.Interface()
   518  		if !reflect.DeepEqual(val, test.out) {
   519  			t.Errorf("scanning %q: expected %#v got %#v, type %T", test.text, test.out, val, val)
   520  		}
   521  	}
   522  }
   523  
   524  func TestScan(t *testing.T) {
   525  	for _, r := range readers {
   526  		t.Run(r.name, func(t *testing.T) {
   527  			testScan(t, r.f, Fscan)
   528  		})
   529  	}
   530  }
   531  
   532  func TestScanln(t *testing.T) {
   533  	for _, r := range readers {
   534  		t.Run(r.name, func(t *testing.T) {
   535  			testScan(t, r.f, Fscanln)
   536  		})
   537  	}
   538  }
   539  
   540  func TestScanf(t *testing.T) {
   541  	for _, test := range scanfTests {
   542  		n, err := Sscanf(test.text, test.format, test.in)
   543  		if err != nil {
   544  			if test.out != nil {
   545  				t.Errorf("Sscanf(%q, %q): unexpected error: %v", test.text, test.format, err)
   546  			}
   547  			continue
   548  		}
   549  		if test.out == nil {
   550  			t.Errorf("Sscanf(%q, %q): unexpected success", test.text, test.format)
   551  			continue
   552  		}
   553  		if n != 1 {
   554  			t.Errorf("Sscanf(%q, %q): parsed %d field, want 1", test.text, test.format, n)
   555  			continue
   556  		}
   557  		// The incoming value may be a pointer
   558  		v := reflect.ValueOf(test.in)
   559  		if p := v; p.Kind() == reflect.Ptr {
   560  			v = p.Elem()
   561  		}
   562  		val := v.Interface()
   563  		if !reflect.DeepEqual(val, test.out) {
   564  			t.Errorf("Sscanf(%q, %q): parsed value %T(%#v), want %T(%#v)", test.text, test.format, val, val, test.out, test.out)
   565  		}
   566  	}
   567  }
   568  
   569  func TestScanOverflow(t *testing.T) {
   570  	// different machines and different types report errors with different strings.
   571  	re := regexp.MustCompile("overflow|too large|out of range|not representable")
   572  	for _, test := range overflowTests {
   573  		_, err := Sscan(test.text, test.in)
   574  		if err == nil {
   575  			t.Errorf("expected overflow scanning %q", test.text)
   576  			continue
   577  		}
   578  		if !re.MatchString(err.Error()) {
   579  			t.Errorf("expected overflow error scanning %q: %s", test.text, err)
   580  		}
   581  	}
   582  }
   583  
   584  func verifyNaN(str string, t *testing.T) {
   585  	var f float64
   586  	var f32 float32
   587  	var f64 float64
   588  	text := str + " " + str + " " + str
   589  	n, err := Fscan(strings.NewReader(text), &f, &f32, &f64)
   590  	if err != nil {
   591  		t.Errorf("got error scanning %q: %s", text, err)
   592  	}
   593  	if n != 3 {
   594  		t.Errorf("count error scanning %q: got %d", text, n)
   595  	}
   596  	if !math.IsNaN(float64(f)) || !math.IsNaN(float64(f32)) || !math.IsNaN(f64) {
   597  		t.Errorf("didn't get NaNs scanning %q: got %g %g %g", text, f, f32, f64)
   598  	}
   599  }
   600  
   601  func TestNaN(t *testing.T) {
   602  	for _, s := range []string{"nan", "NAN", "NaN"} {
   603  		verifyNaN(s, t)
   604  	}
   605  }
   606  
   607  func verifyInf(str string, t *testing.T) {
   608  	var f float64
   609  	var f32 float32
   610  	var f64 float64
   611  	text := str + " " + str + " " + str
   612  	n, err := Fscan(strings.NewReader(text), &f, &f32, &f64)
   613  	if err != nil {
   614  		t.Errorf("got error scanning %q: %s", text, err)
   615  	}
   616  	if n != 3 {
   617  		t.Errorf("count error scanning %q: got %d", text, n)
   618  	}
   619  	sign := 1
   620  	if str[0] == '-' {
   621  		sign = -1
   622  	}
   623  	if !math.IsInf(float64(f), sign) || !math.IsInf(float64(f32), sign) || !math.IsInf(f64, sign) {
   624  		t.Errorf("didn't get right Infs scanning %q: got %g %g %g", text, f, f32, f64)
   625  	}
   626  }
   627  
   628  func TestInf(t *testing.T) {
   629  	for _, s := range []string{"inf", "+inf", "-inf", "INF", "-INF", "+INF", "Inf", "-Inf", "+Inf"} {
   630  		verifyInf(s, t)
   631  	}
   632  }
   633  
   634  func testScanfMulti(t *testing.T, f func(string) io.Reader) {
   635  	sliceType := reflect.TypeOf(make([]interface{}, 1))
   636  	for _, test := range multiTests {
   637  		r := f(test.text)
   638  		n, err := Fscanf(r, test.format, test.in...)
   639  		if err != nil {
   640  			if test.err == "" {
   641  				t.Errorf("got error scanning (%q, %q): %q", test.format, test.text, err)
   642  			} else if !strings.Contains(err.Error(), test.err) {
   643  				t.Errorf("got wrong error scanning (%q, %q): %q; expected %q", test.format, test.text, err, test.err)
   644  			}
   645  			continue
   646  		}
   647  		if test.err != "" {
   648  			t.Errorf("expected error %q error scanning (%q, %q)", test.err, test.format, test.text)
   649  		}
   650  		if n != len(test.out) {
   651  			t.Errorf("count error on entry (%q, %q): expected %d got %d", test.format, test.text, len(test.out), n)
   652  			continue
   653  		}
   654  		// Convert the slice of pointers into a slice of values
   655  		resultVal := reflect.MakeSlice(sliceType, n, n)
   656  		for i := 0; i < n; i++ {
   657  			v := reflect.ValueOf(test.in[i]).Elem()
   658  			resultVal.Index(i).Set(v)
   659  		}
   660  		result := resultVal.Interface()
   661  		if !reflect.DeepEqual(result, test.out) {
   662  			t.Errorf("scanning (%q, %q): expected %#v got %#v", test.format, test.text, test.out, result)
   663  		}
   664  	}
   665  }
   666  
   667  func TestScanfMulti(t *testing.T) {
   668  	for _, r := range readers {
   669  		t.Run(r.name, func(t *testing.T) {
   670  			testScanfMulti(t, r.f)
   671  		})
   672  	}
   673  }
   674  
   675  func TestScanMultiple(t *testing.T) {
   676  	var a int
   677  	var s string
   678  	n, err := Sscan("123abc", &a, &s)
   679  	if n != 2 {
   680  		t.Errorf("Sscan count error: expected 2: got %d", n)
   681  	}
   682  	if err != nil {
   683  		t.Errorf("Sscan expected no error; got %s", err)
   684  	}
   685  	if a != 123 || s != "abc" {
   686  		t.Errorf("Sscan wrong values: got (%d %q) expected (123 \"abc\")", a, s)
   687  	}
   688  	n, err = Sscan("asdf", &s, &a)
   689  	if n != 1 {
   690  		t.Errorf("Sscan count error: expected 1: got %d", n)
   691  	}
   692  	if err == nil {
   693  		t.Errorf("Sscan expected error; got none: %s", err)
   694  	}
   695  	if s != "asdf" {
   696  		t.Errorf("Sscan wrong values: got %q expected \"asdf\"", s)
   697  	}
   698  }
   699  
   700  // Empty strings are not valid input when scanning a string.
   701  func TestScanEmpty(t *testing.T) {
   702  	var s1, s2 string
   703  	n, err := Sscan("abc", &s1, &s2)
   704  	if n != 1 {
   705  		t.Errorf("Sscan count error: expected 1: got %d", n)
   706  	}
   707  	if err == nil {
   708  		t.Error("Sscan <one item> expected error; got none")
   709  	}
   710  	if s1 != "abc" {
   711  		t.Errorf("Sscan wrong values: got %q expected \"abc\"", s1)
   712  	}
   713  	n, err = Sscan("", &s1, &s2)
   714  	if n != 0 {
   715  		t.Errorf("Sscan count error: expected 0: got %d", n)
   716  	}
   717  	if err == nil {
   718  		t.Error("Sscan <empty> expected error; got none")
   719  	}
   720  	// Quoted empty string is OK.
   721  	n, err = Sscanf(`""`, "%q", &s1)
   722  	if n != 1 {
   723  		t.Errorf("Sscanf count error: expected 1: got %d", n)
   724  	}
   725  	if err != nil {
   726  		t.Errorf("Sscanf <empty> expected no error with quoted string; got %s", err)
   727  	}
   728  }
   729  
   730  func TestScanNotPointer(t *testing.T) {
   731  	r := strings.NewReader("1")
   732  	var a int
   733  	_, err := Fscan(r, a)
   734  	if err == nil {
   735  		t.Error("expected error scanning non-pointer")
   736  	} else if !strings.Contains(err.Error(), "pointer") {
   737  		t.Errorf("expected pointer error scanning non-pointer, got: %s", err)
   738  	}
   739  }
   740  
   741  func TestScanlnNoNewline(t *testing.T) {
   742  	var a int
   743  	_, err := Sscanln("1 x\n", &a)
   744  	if err == nil {
   745  		t.Error("expected error scanning string missing newline")
   746  	} else if !strings.Contains(err.Error(), "newline") {
   747  		t.Errorf("expected newline error scanning string missing newline, got: %s", err)
   748  	}
   749  }
   750  
   751  func TestScanlnWithMiddleNewline(t *testing.T) {
   752  	r := strings.NewReader("123\n456\n")
   753  	var a, b int
   754  	_, err := Fscanln(r, &a, &b)
   755  	if err == nil {
   756  		t.Error("expected error scanning string with extra newline")
   757  	} else if !strings.Contains(err.Error(), "newline") {
   758  		t.Errorf("expected newline error scanning string with extra newline, got: %s", err)
   759  	}
   760  }
   761  
   762  // eofCounter is a special Reader that counts reads at end of file.
   763  type eofCounter struct {
   764  	reader   *strings.Reader
   765  	eofCount int
   766  }
   767  
   768  func (ec *eofCounter) Read(b []byte) (n int, err error) {
   769  	n, err = ec.reader.Read(b)
   770  	if n == 0 {
   771  		ec.eofCount++
   772  	}
   773  	return
   774  }
   775  
   776  // TestEOF verifies that when we scan, we see at most EOF once per call to a
   777  // Scan function, and then only when it's really an EOF.
   778  func TestEOF(t *testing.T) {
   779  	ec := &eofCounter{strings.NewReader("123\n"), 0}
   780  	var a int
   781  	n, err := Fscanln(ec, &a)
   782  	if err != nil {
   783  		t.Error("unexpected error", err)
   784  	}
   785  	if n != 1 {
   786  		t.Error("expected to scan one item, got", n)
   787  	}
   788  	if ec.eofCount != 0 {
   789  		t.Error("expected zero EOFs", ec.eofCount)
   790  		ec.eofCount = 0 // reset for next test
   791  	}
   792  	n, err = Fscanln(ec, &a)
   793  	if err == nil {
   794  		t.Error("expected error scanning empty string")
   795  	}
   796  	if n != 0 {
   797  		t.Error("expected to scan zero items, got", n)
   798  	}
   799  	if ec.eofCount != 1 {
   800  		t.Error("expected one EOF, got", ec.eofCount)
   801  	}
   802  }
   803  
   804  // TestEOFAtEndOfInput verifies that we see an EOF error if we run out of input.
   805  // This was a buglet: we used to get "expected integer".
   806  func TestEOFAtEndOfInput(t *testing.T) {
   807  	var i, j int
   808  	n, err := Sscanf("23", "%d %d", &i, &j)
   809  	if n != 1 || i != 23 {
   810  		t.Errorf("Sscanf expected one value of 23; got %d %d", n, i)
   811  	}
   812  	if err != io.EOF {
   813  		t.Errorf("Sscanf expected EOF; got %q", err)
   814  	}
   815  	n, err = Sscan("234", &i, &j)
   816  	if n != 1 || i != 234 {
   817  		t.Errorf("Sscan expected one value of 234; got %d %d", n, i)
   818  	}
   819  	if err != io.EOF {
   820  		t.Errorf("Sscan expected EOF; got %q", err)
   821  	}
   822  	// Trailing space is tougher.
   823  	n, err = Sscan("234 ", &i, &j)
   824  	if n != 1 || i != 234 {
   825  		t.Errorf("Sscan expected one value of 234; got %d %d", n, i)
   826  	}
   827  	if err != io.EOF {
   828  		t.Errorf("Sscan expected EOF; got %q", err)
   829  	}
   830  }
   831  
   832  var eofTests = []struct {
   833  	format string
   834  	v      interface{}
   835  }{
   836  	{"%s", &stringVal},
   837  	{"%q", &stringVal},
   838  	{"%x", &stringVal},
   839  	{"%v", &stringVal},
   840  	{"%v", &bytesVal},
   841  	{"%v", &intVal},
   842  	{"%v", &uintVal},
   843  	{"%v", &boolVal},
   844  	{"%v", &float32Val},
   845  	{"%v", &complex64Val},
   846  	{"%v", &renamedStringVal},
   847  	{"%v", &renamedBytesVal},
   848  	{"%v", &renamedIntVal},
   849  	{"%v", &renamedUintVal},
   850  	{"%v", &renamedBoolVal},
   851  	{"%v", &renamedFloat32Val},
   852  	{"%v", &renamedComplex64Val},
   853  }
   854  
   855  func TestEOFAllTypes(t *testing.T) {
   856  	for i, test := range eofTests {
   857  		if _, err := Sscanf("", test.format, test.v); err != io.EOF {
   858  			t.Errorf("#%d: %s %T not eof on empty string: %s", i, test.format, test.v, err)
   859  		}
   860  		if _, err := Sscanf("   ", test.format, test.v); err != io.EOF {
   861  			t.Errorf("#%d: %s %T not eof on trailing blanks: %s", i, test.format, test.v, err)
   862  		}
   863  	}
   864  }
   865  
   866  // TestUnreadRuneWithBufio verifies that, at least when using bufio, successive
   867  // calls to Fscan do not lose runes.
   868  func TestUnreadRuneWithBufio(t *testing.T) {
   869  	r := bufio.NewReader(strings.NewReader("123αb"))
   870  	var i int
   871  	var a string
   872  	n, err := Fscanf(r, "%d", &i)
   873  	if n != 1 || err != nil {
   874  		t.Errorf("reading int expected one item, no errors; got %d %q", n, err)
   875  	}
   876  	if i != 123 {
   877  		t.Errorf("expected 123; got %d", i)
   878  	}
   879  	n, err = Fscanf(r, "%s", &a)
   880  	if n != 1 || err != nil {
   881  		t.Errorf("reading string expected one item, no errors; got %d %q", n, err)
   882  	}
   883  	if a != "αb" {
   884  		t.Errorf("expected αb; got %q", a)
   885  	}
   886  }
   887  
   888  type TwoLines string
   889  
   890  // Scan attempts to read two lines into the object. Scanln should prevent this
   891  // because it stops at newline; Scan and Scanf should be fine.
   892  func (t *TwoLines) Scan(state ScanState, verb rune) error {
   893  	chars := make([]rune, 0, 100)
   894  	for nlCount := 0; nlCount < 2; {
   895  		c, _, err := state.ReadRune()
   896  		if err != nil {
   897  			return err
   898  		}
   899  		chars = append(chars, c)
   900  		if c == '\n' {
   901  			nlCount++
   902  		}
   903  	}
   904  	*t = TwoLines(string(chars))
   905  	return nil
   906  }
   907  
   908  func TestMultiLine(t *testing.T) {
   909  	input := "abc\ndef\n"
   910  	// Sscan should work
   911  	var tscan TwoLines
   912  	n, err := Sscan(input, &tscan)
   913  	if n != 1 {
   914  		t.Errorf("Sscan: expected 1 item; got %d", n)
   915  	}
   916  	if err != nil {
   917  		t.Errorf("Sscan: expected no error; got %s", err)
   918  	}
   919  	if string(tscan) != input {
   920  		t.Errorf("Sscan: expected %q; got %q", input, tscan)
   921  	}
   922  	// Sscanf should work
   923  	var tscanf TwoLines
   924  	n, err = Sscanf(input, "%s", &tscanf)
   925  	if n != 1 {
   926  		t.Errorf("Sscanf: expected 1 item; got %d", n)
   927  	}
   928  	if err != nil {
   929  		t.Errorf("Sscanf: expected no error; got %s", err)
   930  	}
   931  	if string(tscanf) != input {
   932  		t.Errorf("Sscanf: expected %q; got %q", input, tscanf)
   933  	}
   934  	// Sscanln should not work
   935  	var tscanln TwoLines
   936  	n, err = Sscanln(input, &tscanln)
   937  	if n != 0 {
   938  		t.Errorf("Sscanln: expected 0 items; got %d: %q", n, tscanln)
   939  	}
   940  	if err == nil {
   941  		t.Error("Sscanln: expected error; got none")
   942  	} else if err != io.ErrUnexpectedEOF {
   943  		t.Errorf("Sscanln: expected io.ErrUnexpectedEOF (ha!); got %s", err)
   944  	}
   945  }
   946  
   947  // TestLineByLineFscanf tests that Fscanf does not read past newline. Issue
   948  // 3481.
   949  func TestLineByLineFscanf(t *testing.T) {
   950  	r := struct{ io.Reader }{strings.NewReader("1\n2\n")}
   951  	var i, j int
   952  	n, err := Fscanf(r, "%v\n", &i)
   953  	if n != 1 || err != nil {
   954  		t.Fatalf("first read: %d %q", n, err)
   955  	}
   956  	n, err = Fscanf(r, "%v\n", &j)
   957  	if n != 1 || err != nil {
   958  		t.Fatalf("second read: %d %q", n, err)
   959  	}
   960  	if i != 1 || j != 2 {
   961  		t.Errorf("wrong values; wanted 1 2 got %d %d", i, j)
   962  	}
   963  }
   964  
   965  // TestScanStateCount verifies the correct byte count is returned. Issue 8512.
   966  
   967  // runeScanner implements the Scanner interface for TestScanStateCount.
   968  type runeScanner struct {
   969  	rune rune
   970  	size int
   971  }
   972  
   973  func (rs *runeScanner) Scan(state ScanState, verb rune) error {
   974  	r, size, err := state.ReadRune()
   975  	rs.rune = r
   976  	rs.size = size
   977  	return err
   978  }
   979  
   980  func TestScanStateCount(t *testing.T) {
   981  	var a, b, c runeScanner
   982  	n, err := Sscanf("12➂", "%c%c%c", &a, &b, &c)
   983  	if err != nil {
   984  		t.Fatal(err)
   985  	}
   986  	if n != 3 {
   987  		t.Fatalf("expected 3 items consumed, got %d", n)
   988  	}
   989  	if a.rune != '1' || b.rune != '2' || c.rune != '➂' {
   990  		t.Errorf("bad scan rune: %q %q %q should be '1' '2' '➂'", a.rune, b.rune, c.rune)
   991  	}
   992  	if a.size != 1 || b.size != 1 || c.size != 3 {
   993  		t.Errorf("bad scan size: %q %q %q should be 1 1 3", a.size, b.size, c.size)
   994  	}
   995  }
   996  
   997  // RecursiveInt accepts a string matching %d.%d.%d....
   998  // and parses it into a linked list.
   999  // It allows us to benchmark recursive descent style scanners.
  1000  type RecursiveInt struct {
  1001  	i    int
  1002  	next *RecursiveInt
  1003  }
  1004  
  1005  func (r *RecursiveInt) Scan(state ScanState, verb rune) (err error) {
  1006  	_, err = Fscan(state, &r.i)
  1007  	if err != nil {
  1008  		return
  1009  	}
  1010  	next := new(RecursiveInt)
  1011  	_, err = Fscanf(state, ".%v", next)
  1012  	if err != nil {
  1013  		if err == io.ErrUnexpectedEOF {
  1014  			err = nil
  1015  		}
  1016  		return
  1017  	}
  1018  	r.next = next
  1019  	return
  1020  }
  1021  
  1022  // scanInts performs the same scanning task as RecursiveInt.Scan
  1023  // but without recurring through scanner, so we can compare
  1024  // performance more directly.
  1025  func scanInts(r *RecursiveInt, b *bytes.Buffer) (err error) {
  1026  	r.next = nil
  1027  	_, err = Fscan(b, &r.i)
  1028  	if err != nil {
  1029  		return
  1030  	}
  1031  	c, _, err := b.ReadRune()
  1032  	if err != nil {
  1033  		if err == io.EOF {
  1034  			err = nil
  1035  		}
  1036  		return
  1037  	}
  1038  	if c != '.' {
  1039  		return
  1040  	}
  1041  	next := new(RecursiveInt)
  1042  	err = scanInts(next, b)
  1043  	if err == nil {
  1044  		r.next = next
  1045  	}
  1046  	return
  1047  }
  1048  
  1049  func makeInts(n int) []byte {
  1050  	var buf bytes.Buffer
  1051  	Fprintf(&buf, "1")
  1052  	for i := 1; i < n; i++ {
  1053  		Fprintf(&buf, ".%d", i+1)
  1054  	}
  1055  	return buf.Bytes()
  1056  }
  1057  
  1058  func TestScanInts(t *testing.T) {
  1059  	testScanInts(t, scanInts)
  1060  	testScanInts(t, func(r *RecursiveInt, b *bytes.Buffer) (err error) {
  1061  		_, err = Fscan(b, r)
  1062  		return
  1063  	})
  1064  }
  1065  
  1066  // 800 is small enough to not overflow the stack when using gccgo on a
  1067  // platform that does not support split stack.
  1068  const intCount = 800
  1069  
  1070  func testScanInts(t *testing.T, scan func(*RecursiveInt, *bytes.Buffer) error) {
  1071  	r := new(RecursiveInt)
  1072  	ints := makeInts(intCount)
  1073  	buf := bytes.NewBuffer(ints)
  1074  	err := scan(r, buf)
  1075  	if err != nil {
  1076  		t.Error("unexpected error", err)
  1077  	}
  1078  	i := 1
  1079  	for ; r != nil; r = r.next {
  1080  		if r.i != i {
  1081  			t.Fatalf("bad scan: expected %d got %d", i, r.i)
  1082  		}
  1083  		i++
  1084  	}
  1085  	if i-1 != intCount {
  1086  		t.Fatalf("bad scan count: expected %d got %d", intCount, i-1)
  1087  	}
  1088  }
  1089  
  1090  func BenchmarkScanInts(b *testing.B) {
  1091  	b.ResetTimer()
  1092  	ints := makeInts(intCount)
  1093  	var r RecursiveInt
  1094  	for i := b.N - 1; i >= 0; i-- {
  1095  		buf := bytes.NewBuffer(ints)
  1096  		b.StartTimer()
  1097  		scanInts(&r, buf)
  1098  		b.StopTimer()
  1099  	}
  1100  }
  1101  
  1102  func BenchmarkScanRecursiveInt(b *testing.B) {
  1103  	b.ResetTimer()
  1104  	ints := makeInts(intCount)
  1105  	var r RecursiveInt
  1106  	for i := b.N - 1; i >= 0; i-- {
  1107  		buf := bytes.NewBuffer(ints)
  1108  		b.StartTimer()
  1109  		Fscan(buf, &r)
  1110  		b.StopTimer()
  1111  	}
  1112  }
  1113  
  1114  func BenchmarkScanRecursiveIntReaderWrapper(b *testing.B) {
  1115  	b.ResetTimer()
  1116  	ints := makeInts(intCount)
  1117  	var r RecursiveInt
  1118  	for i := b.N - 1; i >= 0; i-- {
  1119  		buf := struct{ io.Reader }{strings.NewReader(string(ints))}
  1120  		b.StartTimer()
  1121  		Fscan(buf, &r)
  1122  		b.StopTimer()
  1123  	}
  1124  }
  1125  
  1126  // Issue 9124.
  1127  // %x on bytes couldn't handle non-space bytes terminating the scan.
  1128  func TestHexBytes(t *testing.T) {
  1129  	var a, b []byte
  1130  	n, err := Sscanf("00010203", "%x", &a)
  1131  	if n != 1 || err != nil {
  1132  		t.Errorf("simple: got count, err = %d, %v; expected 1, nil", n, err)
  1133  	}
  1134  	check := func(msg string, x []byte) {
  1135  		if len(x) != 4 {
  1136  			t.Errorf("%s: bad length %d", msg, len(x))
  1137  		}
  1138  		for i, b := range x {
  1139  			if int(b) != i {
  1140  				t.Errorf("%s: bad x[%d] = %x", msg, i, x[i])
  1141  			}
  1142  		}
  1143  	}
  1144  	check("simple", a)
  1145  	a = nil
  1146  
  1147  	n, err = Sscanf("00010203 00010203", "%x %x", &a, &b)
  1148  	if n != 2 || err != nil {
  1149  		t.Errorf("simple pair: got count, err = %d, %v; expected 2, nil", n, err)
  1150  	}
  1151  	check("simple pair a", a)
  1152  	check("simple pair b", b)
  1153  	a = nil
  1154  	b = nil
  1155  
  1156  	n, err = Sscanf("00010203:", "%x", &a)
  1157  	if n != 1 || err != nil {
  1158  		t.Errorf("colon: got count, err = %d, %v; expected 1, nil", n, err)
  1159  	}
  1160  	check("colon", a)
  1161  	a = nil
  1162  
  1163  	n, err = Sscanf("00010203:00010203", "%x:%x", &a, &b)
  1164  	if n != 2 || err != nil {
  1165  		t.Errorf("colon pair: got count, err = %d, %v; expected 2, nil", n, err)
  1166  	}
  1167  	check("colon pair a", a)
  1168  	check("colon pair b", b)
  1169  	a = nil
  1170  	b = nil
  1171  
  1172  	// This one fails because there is a hex byte after the data,
  1173  	// that is, an odd number of hex input bytes.
  1174  	n, err = Sscanf("000102034:", "%x", &a)
  1175  	if n != 0 || err == nil {
  1176  		t.Errorf("odd count: got count, err = %d, %v; expected 0, error", n, err)
  1177  	}
  1178  }
  1179  
  1180  func TestScanNewlinesAreSpaces(t *testing.T) {
  1181  	var a, b int
  1182  	var tests = []struct {
  1183  		name  string
  1184  		text  string
  1185  		count int
  1186  	}{
  1187  		{"newlines", "1\n2\n", 2},
  1188  		{"no final newline", "1\n2", 2},
  1189  		{"newlines with spaces ", "1  \n  2  \n", 2},
  1190  		{"no final newline with spaces", "1  \n  2", 2},
  1191  	}
  1192  	for _, test := range tests {
  1193  		n, err := Sscan(test.text, &a, &b)
  1194  		if n != test.count {
  1195  			t.Errorf("%s: expected to scan %d item(s), scanned %d", test.name, test.count, n)
  1196  		}
  1197  		if err != nil {
  1198  			t.Errorf("%s: unexpected error: %s", test.name, err)
  1199  		}
  1200  	}
  1201  }
  1202  
  1203  func TestScanlnNewlinesTerminate(t *testing.T) {
  1204  	var a, b int
  1205  	var tests = []struct {
  1206  		name  string
  1207  		text  string
  1208  		count int
  1209  		ok    bool
  1210  	}{
  1211  		{"one line one item", "1\n", 1, false},
  1212  		{"one line two items with spaces ", "   1 2    \n", 2, true},
  1213  		{"one line two items no newline", "   1 2", 2, true},
  1214  		{"two lines two items", "1\n2\n", 1, false},
  1215  	}
  1216  	for _, test := range tests {
  1217  		n, err := Sscanln(test.text, &a, &b)
  1218  		if n != test.count {
  1219  			t.Errorf("%s: expected to scan %d item(s), scanned %d", test.name, test.count, n)
  1220  		}
  1221  		if test.ok && err != nil {
  1222  			t.Errorf("%s: unexpected error: %s", test.name, err)
  1223  		}
  1224  		if !test.ok && err == nil {
  1225  			t.Errorf("%s: expected error; got none", test.name)
  1226  		}
  1227  	}
  1228  }
  1229  
  1230  func TestScanfNewlineMatchFormat(t *testing.T) {
  1231  	var a, b int
  1232  	var tests = []struct {
  1233  		name   string
  1234  		text   string
  1235  		format string
  1236  		count  int
  1237  		ok     bool
  1238  	}{
  1239  		{"newline in both", "1\n2", "%d\n%d\n", 2, true},
  1240  		{"newline in input", "1\n2", "%d %d", 1, false},
  1241  		{"space-newline in input", "1 \n2", "%d %d", 1, false},
  1242  		{"newline in format", "1 2", "%d\n%d", 1, false},
  1243  		{"space-newline in format", "1 2", "%d \n%d", 1, false},
  1244  		{"space-newline in both", "1 \n2", "%d \n%d", 2, true},
  1245  		{"extra space in format", "1\n2", "%d\n %d", 2, true},
  1246  		{"two extra spaces in format", "1\n2", "%d \n %d", 2, true},
  1247  		{"space vs newline 0000", "1\n2", "%d\n%d", 2, true},
  1248  		{"space vs newline 0001", "1\n2", "%d\n %d", 2, true},
  1249  		{"space vs newline 0010", "1\n2", "%d \n%d", 2, true},
  1250  		{"space vs newline 0011", "1\n2", "%d \n %d", 2, true},
  1251  		{"space vs newline 0100", "1\n 2", "%d\n%d", 2, true},
  1252  		{"space vs newline 0101", "1\n 2", "%d\n%d ", 2, true},
  1253  		{"space vs newline 0110", "1\n 2", "%d \n%d", 2, true},
  1254  		{"space vs newline 0111", "1\n 2", "%d \n %d", 2, true},
  1255  		{"space vs newline 1000", "1 \n2", "%d\n%d", 2, true},
  1256  		{"space vs newline 1001", "1 \n2", "%d\n %d", 2, true},
  1257  		{"space vs newline 1010", "1 \n2", "%d \n%d", 2, true},
  1258  		{"space vs newline 1011", "1 \n2", "%d \n %d", 2, true},
  1259  		{"space vs newline 1100", "1 \n 2", "%d\n%d", 2, true},
  1260  		{"space vs newline 1101", "1 \n 2", "%d\n %d", 2, true},
  1261  		{"space vs newline 1110", "1 \n 2", "%d \n%d", 2, true},
  1262  		{"space vs newline 1111", "1 \n 2", "%d \n %d", 2, true},
  1263  		{"space vs newline no-percent 0000", "1\n2", "1\n2", 0, true},
  1264  		{"space vs newline no-percent 0001", "1\n2", "1\n 2", 0, true},
  1265  		{"space vs newline no-percent 0010", "1\n2", "1 \n2", 0, true},
  1266  		{"space vs newline no-percent 0011", "1\n2", "1 \n 2", 0, true},
  1267  		{"space vs newline no-percent 0100", "1\n 2", "1\n2", 0, false},  // fails: space after nl in input but not pattern
  1268  		{"space vs newline no-percent 0101", "1\n 2", "1\n2 ", 0, false}, // fails: space after nl in input but not pattern
  1269  		{"space vs newline no-percent 0110", "1\n 2", "1 \n2", 0, false}, // fails: space after nl in input but not pattern
  1270  		{"space vs newline no-percent 0111", "1\n 2", "1 \n 2", 0, true},
  1271  		{"space vs newline no-percent 1000", "1 \n2", "1\n2", 0, true},
  1272  		{"space vs newline no-percent 1001", "1 \n2", "1\n 2", 0, true},
  1273  		{"space vs newline no-percent 1010", "1 \n2", "1 \n2", 0, true},
  1274  		{"space vs newline no-percent 1011", "1 \n2", "1 \n 2", 0, true},
  1275  		{"space vs newline no-percent 1100", "1 \n 2", "1\n2", 0, false}, // fails: space after nl in input but not pattern
  1276  		{"space vs newline no-percent 1101", "1 \n 2", "1\n 2", 0, true},
  1277  		{"space vs newline no-percent 1110", "1 \n 2", "1 \n2", 0, false}, // fails: space after nl in input but not pattern
  1278  		{"space vs newline no-percent 1111", "1 \n 2", "1 \n 2", 0, true},
  1279  	}
  1280  	for _, test := range tests {
  1281  		var n int
  1282  		var err error
  1283  		if strings.Contains(test.format, "%") {
  1284  			n, err = Sscanf(test.text, test.format, &a, &b)
  1285  		} else {
  1286  			n, err = Sscanf(test.text, test.format)
  1287  		}
  1288  		if n != test.count {
  1289  			t.Errorf("%s: expected to scan %d item(s), scanned %d", test.name, test.count, n)
  1290  		}
  1291  		if test.ok && err != nil {
  1292  			t.Errorf("%s: unexpected error: %s", test.name, err)
  1293  		}
  1294  		if !test.ok && err == nil {
  1295  			t.Errorf("%s: expected error; got none", test.name)
  1296  		}
  1297  	}
  1298  }
  1299  
  1300  // Test for issue 12090: Was unreading at EOF, double-scanning a byte.
  1301  
  1302  type hexBytes [2]byte
  1303  
  1304  func (h *hexBytes) Scan(ss ScanState, verb rune) error {
  1305  	var b []byte
  1306  	_, err := Fscanf(ss, "%4x", &b)
  1307  	if err != nil {
  1308  		panic(err) // Really shouldn't happen.
  1309  	}
  1310  	copy((*h)[:], b)
  1311  	return err
  1312  }
  1313  
  1314  func TestHexByte(t *testing.T) {
  1315  	var h hexBytes
  1316  	n, err := Sscanln("0123\n", &h)
  1317  	if err != nil {
  1318  		t.Fatal(err)
  1319  	}
  1320  	if n != 1 {
  1321  		t.Fatalf("expected 1 item; scanned %d", n)
  1322  	}
  1323  	if h[0] != 0x01 || h[1] != 0x23 {
  1324  		t.Fatalf("expected 0123 got %x", h)
  1325  	}
  1326  }
  1327  

View as plain text