2010-12-03 05:34:57 +01: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"
|
2011-12-07 02:11:29 +01:00
|
|
|
"testing"
|
2010-12-03 05:34:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestReadLine(t *testing.T) {
|
2015-01-15 01:27:56 +01:00
|
|
|
// /etc/services file does not exist on android, plan9, windows.
|
2012-03-30 23:27:11 +02:00
|
|
|
switch runtime.GOOS {
|
2015-01-15 01:27:56 +01:00
|
|
|
case "android", "plan9", "windows":
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Skipf("not supported on %s", runtime.GOOS)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
filename := "/etc/services" // a nice big file
|
|
|
|
|
2011-04-07 19:09:10 +02:00
|
|
|
fd, err := os.Open(filename)
|
2010-12-03 05:34:57 +01:00
|
|
|
if err != nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Fatal(err)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
defer fd.Close()
|
2010-12-03 05:34:57 +01:00
|
|
|
br := bufio.NewReader(fd)
|
|
|
|
|
|
|
|
file, err := open(filename)
|
|
|
|
if file == nil {
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Fatal(err)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
defer file.close()
|
2010-12-03 05:34:57 +01: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 {
|
2015-10-31 01:59:47 +01:00
|
|
|
t.Fatalf("%s:%d (#%d)\nbufio => %q, %v\nnet => %q, %v", filename, lineno, byteno, bline, berr, line, ok)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
lineno++
|
|
|
|
byteno += len(line) + 1
|
|
|
|
}
|
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
|
|
|
|
func TestGoDebugString(t *testing.T) {
|
|
|
|
defer os.Setenv("GODEBUG", os.Getenv("GODEBUG"))
|
|
|
|
tests := []struct {
|
|
|
|
godebug string
|
|
|
|
key string
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{"", "foo", ""},
|
|
|
|
{"foo=", "foo", ""},
|
|
|
|
{"foo=bar", "foo", "bar"},
|
|
|
|
{"foo=bar,", "foo", "bar"},
|
|
|
|
{"foo,foo=bar,", "foo", "bar"},
|
|
|
|
{"foo1=bar,foo=bar,", "foo", "bar"},
|
|
|
|
{"foo=bar,foo=bar,", "foo", "bar"},
|
|
|
|
{"foo=", "foo", ""},
|
|
|
|
{"foo", "foo", ""},
|
|
|
|
{",foo", "foo", ""},
|
|
|
|
{"foo=bar,baz", "loooooooong", ""},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
os.Setenv("GODEBUG", tt.godebug)
|
|
|
|
if got := goDebugString(tt.key); got != tt.want {
|
|
|
|
t.Errorf("for %q, goDebugString(%q) = %q; want %q", tt.godebug, tt.key, got, tt.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-03 22:58:02 +01: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},
|
2017-01-14 01:05:42 +01:00
|
|
|
{"-0", 0, 0, false},
|
|
|
|
{"-1234", 0, 0, false},
|
2016-02-03 22:58:02 +01:00
|
|
|
} {
|
2017-01-14 01:05:42 +01:00
|
|
|
n, i, ok := dtoi(tt.in)
|
2016-02-03 22:58:02 +01: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|