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