OSDN Git Service

Update to current version of Go library (revision 94d654be2064).
[pf3gnuchains/gcc-fork.git] / libgo / go / encoding / line / line_test.go
1 // Copyright 2010 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 line
6
7 import (
8         "bytes"
9         "io"
10         "io/ioutil"
11         "os"
12         "testing"
13 )
14
15 var testOutput = []byte("0123456789abcdefghijklmnopqrstuvwxy")
16 var testInput = []byte("012\n345\n678\n9ab\ncde\nfgh\nijk\nlmn\nopq\nrst\nuvw\nxy")
17 var testInputrn = []byte("012\r\n345\r\n678\r\n9ab\r\ncde\r\nfgh\r\nijk\r\nlmn\r\nopq\r\nrst\r\nuvw\r\nxy\r\n\n\r\n")
18
19 // TestReader wraps a []byte and returns reads of a specific length.
20 type testReader struct {
21         data   []byte
22         stride int
23 }
24
25 func (t *testReader) Read(buf []byte) (n int, err os.Error) {
26         n = t.stride
27         if n > len(t.data) {
28                 n = len(t.data)
29         }
30         if n > len(buf) {
31                 n = len(buf)
32         }
33         copy(buf, t.data)
34         t.data = t.data[n:]
35         if len(t.data) == 0 {
36                 err = os.EOF
37         }
38         return
39 }
40
41 func testLineReader(t *testing.T, input []byte) {
42         for stride := 1; stride < len(input); stride++ {
43                 done := 0
44                 reader := testReader{input, stride}
45                 l := NewReader(&reader, len(input)+1)
46                 for {
47                         line, isPrefix, err := l.ReadLine()
48                         if len(line) > 0 && err != nil {
49                                 t.Errorf("ReadLine returned both data and error: %s", err)
50                         }
51                         if isPrefix {
52                                 t.Errorf("ReadLine returned prefix")
53                         }
54                         if err != nil {
55                                 if err != os.EOF {
56                                         t.Fatalf("Got unknown error: %s", err)
57                                 }
58                                 break
59                         }
60                         if want := testOutput[done : done+len(line)]; !bytes.Equal(want, line) {
61                                 t.Errorf("Bad line at stride %d: want: %x got: %x", stride, want, line)
62                         }
63                         done += len(line)
64                 }
65                 if done != len(testOutput) {
66                         t.Error("ReadLine didn't return everything")
67                 }
68         }
69 }
70
71 func TestReader(t *testing.T) {
72         testLineReader(t, testInput)
73         testLineReader(t, testInputrn)
74 }
75
76 func TestLineTooLong(t *testing.T) {
77         buf := bytes.NewBuffer([]byte("aaabbbcc\n"))
78         l := NewReader(buf, 3)
79         line, isPrefix, err := l.ReadLine()
80         if !isPrefix || !bytes.Equal(line, []byte("aaa")) || err != nil {
81                 t.Errorf("bad result for first line: %x %s", line, err)
82         }
83         line, isPrefix, err = l.ReadLine()
84         if !isPrefix || !bytes.Equal(line, []byte("bbb")) || err != nil {
85                 t.Errorf("bad result for second line: %x", line)
86         }
87         line, isPrefix, err = l.ReadLine()
88         if isPrefix || !bytes.Equal(line, []byte("cc")) || err != nil {
89                 t.Errorf("bad result for third line: %x", line)
90         }
91 }
92
93 func TestReadAfterLines(t *testing.T) {
94         line1 := "line1"
95         restData := "line2\nline 3\n"
96         inbuf := bytes.NewBuffer([]byte(line1 + "\n" + restData))
97         outbuf := new(bytes.Buffer)
98         maxLineLength := len(line1) + len(restData)/2
99         l := NewReader(inbuf, maxLineLength)
100         line, isPrefix, err := l.ReadLine()
101         if isPrefix || err != nil || string(line) != line1 {
102                 t.Errorf("bad result for first line: isPrefix=%v err=%v line=%q", isPrefix, err, string(line))
103         }
104         n, err := io.Copy(outbuf, l)
105         if int(n) != len(restData) || err != nil {
106                 t.Errorf("bad result for Read: n=%d err=%v", n, err)
107         }
108         if outbuf.String() != restData {
109                 t.Errorf("bad result for Read: got %q; expected %q", outbuf.String(), restData)
110         }
111 }
112
113 func TestReadEmptyBuffer(t *testing.T) {
114         l := NewReader(bytes.NewBuffer(nil), 10)
115         line, isPrefix, err := l.ReadLine()
116         if err != os.EOF {
117                 t.Errorf("expected EOF from ReadLine, got '%s' %t %s", line, isPrefix, err)
118         }
119 }
120
121 func TestLinesAfterRead(t *testing.T) {
122         l := NewReader(bytes.NewBuffer([]byte("foo")), 10)
123         _, err := ioutil.ReadAll(l)
124         if err != nil {
125                 t.Error(err)
126                 return
127         }
128
129         line, isPrefix, err := l.ReadLine()
130         if err != os.EOF {
131                 t.Errorf("expected EOF from ReadLine, got '%s' %t %s", line, isPrefix, err)
132         }
133 }