parse_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package net
  5. import (
  6. "bufio"
  7. "os"
  8. "runtime"
  9. "testing"
  10. )
  11. func TestReadLine(t *testing.T) {
  12. // /etc/services file does not exist on android, plan9, windows.
  13. switch runtime.GOOS {
  14. case "android", "plan9", "windows":
  15. t.Skipf("skipping test on %q", runtime.GOOS)
  16. }
  17. filename := "/etc/services" // a nice big file
  18. fd, err := os.Open(filename)
  19. if err != nil {
  20. t.Fatalf("open %s: %v", filename, err)
  21. }
  22. defer fd.Close()
  23. br := bufio.NewReader(fd)
  24. file, err := open(filename)
  25. if file == nil {
  26. t.Fatalf("net.open(%s) = nil", filename)
  27. }
  28. defer file.close()
  29. lineno := 1
  30. byteno := 0
  31. for {
  32. bline, berr := br.ReadString('\n')
  33. if n := len(bline); n > 0 {
  34. bline = bline[0 : n-1]
  35. }
  36. line, ok := file.readLine()
  37. if (berr != nil) != !ok || bline != line {
  38. t.Fatalf("%s:%d (#%d)\nbufio => %q, %v\nnet => %q, %v",
  39. filename, lineno, byteno, bline, berr, line, ok)
  40. }
  41. if !ok {
  42. break
  43. }
  44. lineno++
  45. byteno += len(line) + 1
  46. }
  47. }