Source file src/net/parse_test.go

     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 net
     6  
     7  import (
     8  	"bufio"
     9  	"os"
    10  	"runtime"
    11  	"testing"
    12  )
    13  
    14  func TestReadLine(t *testing.T) {
    15  	// /etc/services file does not exist on android, plan9, windows, or wasip1
    16  	// where it would be required to be mounted from the host file system.
    17  	switch runtime.GOOS {
    18  	case "android", "plan9", "windows", "wasip1":
    19  		t.Skipf("not supported on %s", runtime.GOOS)
    20  	}
    21  	filename := "/etc/services" // a nice big file
    22  
    23  	fd, err := os.Open(filename)
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  	defer fd.Close()
    28  	br := bufio.NewReader(fd)
    29  
    30  	file, err := open(filename)
    31  	if file == nil {
    32  		t.Fatal(err)
    33  	}
    34  	defer file.close()
    35  
    36  	lineno := 1
    37  	byteno := 0
    38  	for {
    39  		bline, berr := br.ReadString('\n')
    40  		if n := len(bline); n > 0 {
    41  			bline = bline[0 : n-1]
    42  		}
    43  		line, ok := file.readLine()
    44  		if (berr != nil) != !ok || bline != line {
    45  			t.Fatalf("%s:%d (#%d)\nbufio => %q, %v\nnet => %q, %v", filename, lineno, byteno, bline, berr, line, ok)
    46  		}
    47  		if !ok {
    48  			break
    49  		}
    50  		lineno++
    51  		byteno += len(line) + 1
    52  	}
    53  }
    54  
    55  func TestDtoi(t *testing.T) {
    56  	for _, tt := range []struct {
    57  		in  string
    58  		out int
    59  		off int
    60  		ok  bool
    61  	}{
    62  		{"", 0, 0, false},
    63  		{"0", 0, 1, true},
    64  		{"65536", 65536, 5, true},
    65  		{"123456789", big, 8, false},
    66  		{"-0", 0, 0, false},
    67  		{"-1234", 0, 0, false},
    68  	} {
    69  		n, i, ok := dtoi(tt.in)
    70  		if n != tt.out || i != tt.off || ok != tt.ok {
    71  			t.Errorf("got %d, %d, %v; want %d, %d, %v", n, i, ok, tt.out, tt.off, tt.ok)
    72  		}
    73  	}
    74  }
    75  

View as plain text