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

View as plain text