OSDN Git Service

versoin1.1.9 (#594)
[bytom/vapor.git] / vendor / github.com / golang / groupcache / byteview_test.go
1 /*
2 Copyright 2012 Google Inc.
3
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
7
8      http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 */
16
17 package groupcache
18
19 import (
20         "bytes"
21         "fmt"
22         "io"
23         "io/ioutil"
24         "testing"
25 )
26
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))
33                         }
34                         if v.String() != s {
35                                 t.Errorf("%s: String = %q; want %q", name, v.String(), s)
36                         }
37                         var longDest [3]byte
38                         if n := v.Copy(longDest[:]); n != len(s) {
39                                 t.Errorf("%s: long Copy = %d; want %d", name, n, len(s))
40                         }
41                         var shortDest [1]byte
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))
44                         }
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)
47                         }
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)
50                         }
51                         var dest bytes.Buffer
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)
54                         }
55                 }
56         }
57 }
58
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}
63         }
64         return ByteView{s: x.(string)}
65 }
66
67 func TestByteViewEqual(t *testing.T) {
68         tests := []struct {
69                 a    interface{} // string or []byte
70                 b    interface{} // string or []byte
71                 want bool
72         }{
73                 {"x", "x", true},
74                 {"x", "y", false},
75                 {"x", "yy", false},
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},
85         }
86         for i, tt := range tests {
87                 va := of(tt.a)
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)
91                         }
92                 } else {
93                         if got := va.EqualString(tt.b.(string)); got != tt.want {
94                                 t.Errorf("%d. EqualString = %v; want %v", i, got, tt.want)
95                         }
96                 }
97                 if got := va.Equal(of(tt.b)); got != tt.want {
98                         t.Errorf("%d. Equal = %v; want %v", i, got, tt.want)
99                 }
100         }
101 }
102
103 func TestByteViewSlice(t *testing.T) {
104         tests := []struct {
105                 in   string
106                 from int
107                 to   interface{} // nil to mean the end (SliceFrom); else int
108                 want string
109         }{
110                 {
111                         in:   "abc",
112                         from: 1,
113                         to:   2,
114                         want: "b",
115                 },
116                 {
117                         in:   "abc",
118                         from: 1,
119                         want: "bc",
120                 },
121                 {
122                         in:   "abc",
123                         to:   2,
124                         want: "ab",
125                 },
126         }
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)
130                         if tt.to != nil {
131                                 v = v.Slice(tt.from, tt.to.(int))
132                         } else {
133                                 v = v.SliceFrom(tt.from)
134                         }
135                         if v.String() != tt.want {
136                                 t.Errorf("%s: got %q; want %q", name, v.String(), tt.want)
137                         }
138                 }
139         }
140 }
141
142 func min(a, b int) int {
143         if a < b {
144                 return a
145         }
146         return b
147 }