Retro68/gcc/libgo/go/net/parse_test.go

74 lines
1.5 KiB
Go
Raw Normal View History

2012-03-27 23:13:14 +00:00
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package net
import (
"bufio"
"os"
"runtime"
"testing"
)
func TestReadLine(t *testing.T) {
2015-08-28 15:33:40 +00:00
// /etc/services file does not exist on android, plan9, windows.
2014-09-21 17:33:12 +00:00
switch runtime.GOOS {
2015-08-28 15:33:40 +00:00
case "android", "plan9", "windows":
2017-04-10 11:32:00 +00:00
t.Skipf("not supported on %s", runtime.GOOS)
2012-03-27 23:13:14 +00:00
}
filename := "/etc/services" // a nice big file
fd, err := os.Open(filename)
if err != nil {
2017-04-10 11:32:00 +00:00
t.Fatal(err)
2012-03-27 23:13:14 +00:00
}
2014-09-21 17:33:12 +00:00
defer fd.Close()
2012-03-27 23:13:14 +00:00
br := bufio.NewReader(fd)
file, err := open(filename)
if file == nil {
2017-04-10 11:32:00 +00:00
t.Fatal(err)
2012-03-27 23:13:14 +00:00
}
2014-09-21 17:33:12 +00:00
defer file.close()
2012-03-27 23:13:14 +00:00
lineno := 1
byteno := 0
for {
bline, berr := br.ReadString('\n')
if n := len(bline); n > 0 {
bline = bline[0 : n-1]
}
line, ok := file.readLine()
if (berr != nil) != !ok || bline != line {
2017-04-10 11:32:00 +00:00
t.Fatalf("%s:%d (#%d)\nbufio => %q, %v\nnet => %q, %v", filename, lineno, byteno, bline, berr, line, ok)
2012-03-27 23:13:14 +00:00
}
if !ok {
break
}
lineno++
byteno += len(line) + 1
}
}
2017-04-10 11:32:00 +00:00
func TestDtoi(t *testing.T) {
for _, tt := range []struct {
in string
out int
off int
ok bool
}{
{"", 0, 0, false},
{"0", 0, 1, true},
{"65536", 65536, 5, true},
{"123456789", big, 8, false},
{"-0", 0, 0, false},
{"-1234", 0, 0, false},
2017-04-10 11:32:00 +00:00
} {
n, i, ok := dtoi(tt.in)
2017-04-10 11:32:00 +00:00
if n != tt.out || i != tt.off || ok != tt.ok {
t.Errorf("got %d, %d, %v; want %d, %d, %v", n, i, ok, tt.out, tt.off, tt.ok)
}
}
}