OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [io/] [io_test.go] - Blame information for rev 867

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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 io_test
6
 
7
import (
8
        "bytes"
9
        . "io"
10
        "strings"
11
        "testing"
12
)
13
 
14
// An version of bytes.Buffer without ReadFrom and WriteTo
15
type Buffer struct {
16
        bytes.Buffer
17
        ReaderFrom // conflicts with and hides bytes.Buffer's ReaderFrom.
18
        WriterTo   // conflicts with and hides bytes.Buffer's WriterTo.
19
}
20
 
21
// Simple tests, primarily to verify the ReadFrom and WriteTo callouts inside Copy and CopyN.
22
 
23
func TestCopy(t *testing.T) {
24
        rb := new(Buffer)
25
        wb := new(Buffer)
26
        rb.WriteString("hello, world.")
27
        Copy(wb, rb)
28
        if wb.String() != "hello, world." {
29
                t.Errorf("Copy did not work properly")
30
        }
31
}
32
 
33
func TestCopyReadFrom(t *testing.T) {
34
        rb := new(Buffer)
35
        wb := new(bytes.Buffer) // implements ReadFrom.
36
        rb.WriteString("hello, world.")
37
        Copy(wb, rb)
38
        if wb.String() != "hello, world." {
39
                t.Errorf("Copy did not work properly")
40
        }
41
}
42
 
43
func TestCopyWriteTo(t *testing.T) {
44
        rb := new(bytes.Buffer) // implements WriteTo.
45
        wb := new(Buffer)
46
        rb.WriteString("hello, world.")
47
        Copy(wb, rb)
48
        if wb.String() != "hello, world." {
49
                t.Errorf("Copy did not work properly")
50
        }
51
}
52
 
53
func TestCopyN(t *testing.T) {
54
        rb := new(Buffer)
55
        wb := new(Buffer)
56
        rb.WriteString("hello, world.")
57
        CopyN(wb, rb, 5)
58
        if wb.String() != "hello" {
59
                t.Errorf("CopyN did not work properly")
60
        }
61
}
62
 
63
func TestCopyNReadFrom(t *testing.T) {
64
        rb := new(Buffer)
65
        wb := new(bytes.Buffer) // implements ReadFrom.
66
        rb.WriteString("hello")
67
        CopyN(wb, rb, 5)
68
        if wb.String() != "hello" {
69
                t.Errorf("CopyN did not work properly")
70
        }
71
}
72
 
73
func TestCopyNWriteTo(t *testing.T) {
74
        rb := new(bytes.Buffer) // implements WriteTo.
75
        wb := new(Buffer)
76
        rb.WriteString("hello, world.")
77
        CopyN(wb, rb, 5)
78
        if wb.String() != "hello" {
79
                t.Errorf("CopyN did not work properly")
80
        }
81
}
82
 
83
type noReadFrom struct {
84
        w Writer
85
}
86
 
87
func (w *noReadFrom) Write(p []byte) (n int, err error) {
88
        return w.w.Write(p)
89
}
90
 
91
func TestCopyNEOF(t *testing.T) {
92
        // Test that EOF behavior is the same regardless of whether
93
        // argument to CopyN has ReadFrom.
94
 
95
        b := new(bytes.Buffer)
96
 
97
        n, err := CopyN(&noReadFrom{b}, strings.NewReader("foo"), 3)
98
        if n != 3 || err != nil {
99
                t.Errorf("CopyN(noReadFrom, foo, 3) = %d, %v; want 3, nil", n, err)
100
        }
101
 
102
        n, err = CopyN(&noReadFrom{b}, strings.NewReader("foo"), 4)
103
        if n != 3 || err != EOF {
104
                t.Errorf("CopyN(noReadFrom, foo, 4) = %d, %v; want 3, EOF", n, err)
105
        }
106
 
107
        n, err = CopyN(b, strings.NewReader("foo"), 3) // b has read from
108
        if n != 3 || err != nil {
109
                t.Errorf("CopyN(bytes.Buffer, foo, 3) = %d, %v; want 3, nil", n, err)
110
        }
111
 
112
        n, err = CopyN(b, strings.NewReader("foo"), 4) // b has read from
113
        if n != 3 || err != EOF {
114
                t.Errorf("CopyN(bytes.Buffer, foo, 4) = %d, %v; want 3, EOF", n, err)
115
        }
116
}
117
 
118
func TestReadAtLeast(t *testing.T) {
119
        var rb bytes.Buffer
120
        testReadAtLeast(t, &rb)
121
}
122
 
123
// A version of bytes.Buffer that returns n > 0, EOF on Read
124
// when the input is exhausted.
125
type dataAndEOFBuffer struct {
126
        bytes.Buffer
127
}
128
 
129
func (r *dataAndEOFBuffer) Read(p []byte) (n int, err error) {
130
        n, err = r.Buffer.Read(p)
131
        if n > 0 && r.Buffer.Len() == 0 && err == nil {
132
                err = EOF
133
        }
134
        return
135
}
136
 
137
func TestReadAtLeastWithDataAndEOF(t *testing.T) {
138
        var rb dataAndEOFBuffer
139
        testReadAtLeast(t, &rb)
140
}
141
 
142
func testReadAtLeast(t *testing.T, rb ReadWriter) {
143
        rb.Write([]byte("0123"))
144
        buf := make([]byte, 2)
145
        n, err := ReadAtLeast(rb, buf, 2)
146
        if err != nil {
147
                t.Error(err)
148
        }
149
        n, err = ReadAtLeast(rb, buf, 4)
150
        if err != ErrShortBuffer {
151
                t.Errorf("expected ErrShortBuffer got %v", err)
152
        }
153
        if n != 0 {
154
                t.Errorf("expected to have read 0 bytes, got %v", n)
155
        }
156
        n, err = ReadAtLeast(rb, buf, 1)
157
        if err != nil {
158
                t.Error(err)
159
        }
160
        if n != 2 {
161
                t.Errorf("expected to have read 2 bytes, got %v", n)
162
        }
163
        n, err = ReadAtLeast(rb, buf, 2)
164
        if err != EOF {
165
                t.Errorf("expected EOF, got %v", err)
166
        }
167
        if n != 0 {
168
                t.Errorf("expected to have read 0 bytes, got %v", n)
169
        }
170
        rb.Write([]byte("4"))
171
        n, err = ReadAtLeast(rb, buf, 2)
172
        if err != ErrUnexpectedEOF {
173
                t.Errorf("expected ErrUnexpectedEOF, got %v", err)
174
        }
175
        if n != 1 {
176
                t.Errorf("expected to have read 1 bytes, got %v", n)
177
        }
178
}
179
 
180
func TestTeeReader(t *testing.T) {
181
        src := []byte("hello, world")
182
        dst := make([]byte, len(src))
183
        rb := bytes.NewBuffer(src)
184
        wb := new(bytes.Buffer)
185
        r := TeeReader(rb, wb)
186
        if n, err := ReadFull(r, dst); err != nil || n != len(src) {
187
                t.Fatalf("ReadFull(r, dst) = %d, %v; want %d, nil", n, err, len(src))
188
        }
189
        if !bytes.Equal(dst, src) {
190
                t.Errorf("bytes read = %q want %q", dst, src)
191
        }
192
        if !bytes.Equal(wb.Bytes(), src) {
193
                t.Errorf("bytes written = %q want %q", wb.Bytes(), src)
194
        }
195
        if n, err := r.Read(dst); n != 0 || err != EOF {
196
                t.Errorf("r.Read at EOF = %d, %v want 0, EOF", n, err)
197
        }
198
        rb = bytes.NewBuffer(src)
199
        pr, pw := Pipe()
200
        pr.Close()
201
        r = TeeReader(rb, pw)
202
        if n, err := ReadFull(r, dst); n != 0 || err != ErrClosedPipe {
203
                t.Errorf("closed tee: ReadFull(r, dst) = %d, %v; want 0, EPIPE", n, err)
204
        }
205
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.