2008-12-18 16:42:39 -07: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 (
|
2009-12-15 16:35:38 -07:00
|
|
|
"bufio"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2010-09-11 20:02:29 -06:00
|
|
|
"runtime"
|
2008-12-18 16:42:39 -07:00
|
|
|
)
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestReadLine(t *testing.T) {
|
2011-08-17 11:28:29 -06:00
|
|
|
// /etc/services file does not exist on windows and Plan 9.
|
|
|
|
if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
|
2010-09-11 20:02:29 -06:00
|
|
|
return
|
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
filename := "/etc/services" // a nice big file
|
2008-12-18 16:42:39 -07:00
|
|
|
|
2011-04-05 00:42:14 -06:00
|
|
|
fd, err := os.Open(filename)
|
2008-12-18 16:42:39 -07:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Fatalf("open %s: %v", filename, err)
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
br := bufio.NewReader(fd)
|
2008-12-18 16:42:39 -07:00
|
|
|
|
2010-04-29 12:02:10 -06:00
|
|
|
file, err := open(filename)
|
2008-12-18 16:42:39 -07:00
|
|
|
if file == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Fatalf("net.open(%s) = nil", filename)
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
lineno := 1
|
|
|
|
byteno := 0
|
2008-12-18 16:42:39 -07:00
|
|
|
for {
|
2009-12-15 16:35:38 -07:00
|
|
|
bline, berr := br.ReadString('\n')
|
2009-08-27 12:20:15 -06:00
|
|
|
if n := len(bline); n > 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
bline = bline[0 : n-1]
|
2009-08-27 12:20:15 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
line, ok := file.readLine()
|
2008-12-18 16:42:39 -07:00
|
|
|
if (berr != nil) != !ok || bline != line {
|
|
|
|
t.Fatalf("%s:%d (#%d)\nbufio => %q, %v\nnet => %q, %v",
|
2009-11-09 13:07:39 -07:00
|
|
|
filename, lineno, byteno, bline, berr, line, ok)
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
if !ok {
|
2009-11-09 13:07:39 -07:00
|
|
|
break
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
lineno++
|
|
|
|
byteno += len(line) + 1
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
|
|
|
}
|