2 Copyright 2012 Google Inc.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
27 func TestByteView(t *testing.T) {
28 for _, s := range []string{"", "x", "yy"} {
29 for _, v := range []ByteView{of([]byte(s)), of(s)} {
30 name := fmt.Sprintf("string %q, view %+v", s, v)
31 if v.Len() != len(s) {
32 t.Errorf("%s: Len = %d; want %d", name, v.Len(), len(s))
35 t.Errorf("%s: String = %q; want %q", name, v.String(), s)
38 if n := v.Copy(longDest[:]); n != len(s) {
39 t.Errorf("%s: long Copy = %d; want %d", name, n, len(s))
42 if n := v.Copy(shortDest[:]); n != min(len(s), 1) {
43 t.Errorf("%s: short Copy = %d; want %d", name, n, min(len(s), 1))
45 if got, err := ioutil.ReadAll(v.Reader()); err != nil || string(got) != s {
46 t.Errorf("%s: Reader = %q, %v; want %q", name, got, err, s)
48 if got, err := ioutil.ReadAll(io.NewSectionReader(v, 0, int64(len(s)))); err != nil || string(got) != s {
49 t.Errorf("%s: SectionReader of ReaderAt = %q, %v; want %q", name, got, err, s)
52 if _, err := v.WriteTo(&dest); err != nil || !bytes.Equal(dest.Bytes(), []byte(s)) {
53 t.Errorf("%s: WriteTo = %q, %v; want %q", name, dest.Bytes(), err, s)
59 // of returns a byte view of the []byte or string in x.
60 func of(x interface{}) ByteView {
61 if bytes, ok := x.([]byte); ok {
62 return ByteView{b: bytes}
64 return ByteView{s: x.(string)}
67 func TestByteViewEqual(t *testing.T) {
69 a interface{} // string or []byte
70 b interface{} // string or []byte
76 {[]byte("x"), []byte("x"), true},
77 {[]byte("x"), []byte("y"), false},
78 {[]byte("x"), []byte("yy"), false},
79 {[]byte("x"), "x", true},
80 {[]byte("x"), "y", false},
81 {[]byte("x"), "yy", false},
82 {"x", []byte("x"), true},
83 {"x", []byte("y"), false},
84 {"x", []byte("yy"), false},
86 for i, tt := range tests {
88 if bytes, ok := tt.b.([]byte); ok {
89 if got := va.EqualBytes(bytes); got != tt.want {
90 t.Errorf("%d. EqualBytes = %v; want %v", i, got, tt.want)
93 if got := va.EqualString(tt.b.(string)); got != tt.want {
94 t.Errorf("%d. EqualString = %v; want %v", i, got, tt.want)
97 if got := va.Equal(of(tt.b)); got != tt.want {
98 t.Errorf("%d. Equal = %v; want %v", i, got, tt.want)
103 func TestByteViewSlice(t *testing.T) {
107 to interface{} // nil to mean the end (SliceFrom); else int
127 for i, tt := range tests {
128 for _, v := range []ByteView{of([]byte(tt.in)), of(tt.in)} {
129 name := fmt.Sprintf("test %d, view %+v", i, v)
131 v = v.Slice(tt.from, tt.to.(int))
133 v = v.SliceFrom(tt.from)
135 if v.String() != tt.want {
136 t.Errorf("%s: got %q; want %q", name, v.String(), tt.want)
142 func min(a, b int) int {