* gcc.dg/guality/guality.exp: Skip on AIX.
[official-gcc.git] / libgo / go / strings / reader_test.go
blob4fdddcdb58e47719271fc1e23ec4c4468d1eb8d9
1 // Copyright 2012 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.
5 package strings_test
7 import (
8 "bytes"
9 "fmt"
10 "io"
11 "os"
12 "strings"
13 "testing"
16 func TestReader(t *testing.T) {
17 r := strings.NewReader("0123456789")
18 tests := []struct {
19 off int64
20 seek int
21 n int
22 want string
23 wantpos int64
24 seekerr string
26 {seek: os.SEEK_SET, off: 0, n: 20, want: "0123456789"},
27 {seek: os.SEEK_SET, off: 1, n: 1, want: "1"},
28 {seek: os.SEEK_CUR, off: 1, wantpos: 3, n: 2, want: "34"},
29 {seek: os.SEEK_SET, off: -1, seekerr: "strings: negative position"},
30 {seek: os.SEEK_SET, off: 1<<31 - 1},
31 {seek: os.SEEK_CUR, off: 1, seekerr: "strings: position out of range"},
32 {seek: os.SEEK_SET, n: 5, want: "01234"},
33 {seek: os.SEEK_CUR, n: 5, want: "56789"},
34 {seek: os.SEEK_END, off: -1, n: 1, wantpos: 9, want: "9"},
37 for i, tt := range tests {
38 pos, err := r.Seek(tt.off, tt.seek)
39 if err == nil && tt.seekerr != "" {
40 t.Errorf("%d. want seek error %q", i, tt.seekerr)
41 continue
43 if err != nil && err.Error() != tt.seekerr {
44 t.Errorf("%d. seek error = %q; want %q", i, err.Error(), tt.seekerr)
45 continue
47 if tt.wantpos != 0 && tt.wantpos != pos {
48 t.Errorf("%d. pos = %d, want %d", i, pos, tt.wantpos)
50 buf := make([]byte, tt.n)
51 n, err := r.Read(buf)
52 if err != nil {
53 t.Errorf("%d. read = %v", i, err)
54 continue
56 got := string(buf[:n])
57 if got != tt.want {
58 t.Errorf("%d. got %q; want %q", i, got, tt.want)
63 func TestReaderAt(t *testing.T) {
64 r := strings.NewReader("0123456789")
65 tests := []struct {
66 off int64
67 n int
68 want string
69 wanterr interface{}
71 {0, 10, "0123456789", nil},
72 {1, 10, "123456789", io.EOF},
73 {1, 9, "123456789", nil},
74 {11, 10, "", io.EOF},
75 {0, 0, "", nil},
76 {-1, 0, "", "strings: invalid offset"},
78 for i, tt := range tests {
79 b := make([]byte, tt.n)
80 rn, err := r.ReadAt(b, tt.off)
81 got := string(b[:rn])
82 if got != tt.want {
83 t.Errorf("%d. got %q; want %q", i, got, tt.want)
85 if fmt.Sprintf("%v", err) != fmt.Sprintf("%v", tt.wanterr) {
86 t.Errorf("%d. got error = %v; want %v", i, err, tt.wanterr)
91 func TestWriteTo(t *testing.T) {
92 const str = "0123456789"
93 for i := 0; i <= len(str); i++ {
94 s := str[i:]
95 r := strings.NewReader(s)
96 var b bytes.Buffer
97 n, err := r.WriteTo(&b)
98 if expect := int64(len(s)); n != expect {
99 t.Errorf("got %v; want %v", n, expect)
101 if err != nil {
102 t.Errorf("for length %d: got error = %v; want nil", len(s), err)
104 if b.String() != s {
105 t.Errorf("got string %q; want %q", b.String(), s)
107 if r.Len() != 0 {
108 t.Errorf("reader contains %v bytes; want 0", r.Len())