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"
|
2010-09-11 20:02:29 -06:00
|
|
|
"runtime"
|
2011-11-02 13:54:16 -06:00
|
|
|
"testing"
|
2008-12-18 16:42:39 -07:00
|
|
|
)
|
|
|
|
|
2009-01-20 15:40:40 -07:00
|
|
|
func TestReadLine(t *testing.T) {
|
2014-07-08 11:41:18 -06:00
|
|
|
// /etc/services file does not exist on android, plan9, windows.
|
2012-03-05 23:41:17 -07:00
|
|
|
switch runtime.GOOS {
|
2014-07-08 11:41:18 -06:00
|
|
|
case "android", "plan9", "windows":
|
2015-04-30 21:38:42 -06:00
|
|
|
t.Skipf("not supported on %s", runtime.GOOS)
|
2010-09-11 20:02:29 -06:00
|
|
|
}
|
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 {
|
2015-04-30 21:38:42 -06:00
|
|
|
t.Fatal(err)
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
2013-06-20 19:13:14 -06:00
|
|
|
defer fd.Close()
|
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 {
|
2015-04-30 21:38:42 -06:00
|
|
|
t.Fatal(err)
|
2008-12-18 16:42:39 -07:00
|
|
|
}
|
2013-06-20 19:13:14 -06:00
|
|
|
defer file.close()
|
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 {
|
2015-04-30 21:38:42 -06:00
|
|
|
t.Fatalf("%s:%d (#%d)\nbufio => %q, %v\nnet => %q, %v", 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
|
|
|
}
|
|
|
|
}
|