The Go Programming Language

Source file src/pkg/net/parse_test.go

     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	
     5	package net
     6	
     7	import (
     8		"bufio"
     9		"os"
    10		"testing"
    11		"runtime"
    12	)
    13	
    14	func TestReadLine(t *testing.T) {
    15		// /etc/services file does not exist on windows and Plan 9.
    16		if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
    17			return
    18		}
    19		filename := "/etc/services" // a nice big file
    20	
    21		fd, err := os.Open(filename)
    22		if err != nil {
    23			t.Fatalf("open %s: %v", filename, err)
    24		}
    25		br := bufio.NewReader(fd)
    26	
    27		file, err := open(filename)
    28		if file == nil {
    29			t.Fatalf("net.open(%s) = nil", filename)
    30		}
    31	
    32		lineno := 1
    33		byteno := 0
    34		for {
    35			bline, berr := br.ReadString('\n')
    36			if n := len(bline); n > 0 {
    37				bline = bline[0 : n-1]
    38			}
    39			line, ok := file.readLine()
    40			if (berr != nil) != !ok || bline != line {
    41				t.Fatalf("%s:%d (#%d)\nbufio => %q, %v\nnet => %q, %v",
    42					filename, lineno, byteno, bline, berr, line, ok)
    43			}
    44			if !ok {
    45				break
    46			}
    47			lineno++
    48			byteno += len(line) + 1
    49		}
    50	}

release.r60.3. Except as noted, this content is licensed under a Creative Commons Attribution 3.0 License.