github.com/huandu/go@v0.0.0-20151114150818-04e615e41150/src/fmt/scan_test.go (about)

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