1
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
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{}
70 b interface{}
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{}
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 }
148
View as plain text