github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/golang/groupcache/byteview_test.go (about) 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 "fmt" 21 "io" 22 "io/ioutil" 23 "testing" 24 ) 25 26 func TestByteView(t *testing.T) { 27 for _, s := range []string{"", "x", "yy"} { 28 for _, v := range []ByteView{of([]byte(s)), of(s)} { 29 name := fmt.Sprintf("string %q, view %+v", s, v) 30 if v.Len() != len(s) { 31 t.Errorf("%s: Len = %d; want %d", name, v.Len(), len(s)) 32 } 33 if v.String() != s { 34 t.Errorf("%s: String = %q; want %q", name, v.String(), s) 35 } 36 var longDest [3]byte 37 if n := v.Copy(longDest[:]); n != len(s) { 38 t.Errorf("%s: long Copy = %d; want %d", name, n, len(s)) 39 } 40 var shortDest [1]byte 41 if n := v.Copy(shortDest[:]); n != min(len(s), 1) { 42 t.Errorf("%s: short Copy = %d; want %d", name, n, min(len(s), 1)) 43 } 44 if got, err := ioutil.ReadAll(v.Reader()); err != nil || string(got) != s { 45 t.Errorf("%s: Reader = %q, %v; want %q", name, got, err, s) 46 } 47 if got, err := ioutil.ReadAll(io.NewSectionReader(v, 0, int64(len(s)))); err != nil || string(got) != s { 48 t.Errorf("%s: SectionReader of ReaderAt = %q, %v; want %q", name, got, err, s) 49 } 50 } 51 } 52 } 53 54 // of returns a byte view of the []byte or string in x. 55 func of(x interface{}) ByteView { 56 if bytes, ok := x.([]byte); ok { 57 return ByteView{b: bytes} 58 } 59 return ByteView{s: x.(string)} 60 } 61 62 func TestByteViewEqual(t *testing.T) { 63 tests := []struct { 64 a interface{} // string or []byte 65 b interface{} // string or []byte 66 want bool 67 }{ 68 {"x", "x", true}, 69 {"x", "y", false}, 70 {"x", "yy", false}, 71 {[]byte("x"), []byte("x"), true}, 72 {[]byte("x"), []byte("y"), false}, 73 {[]byte("x"), []byte("yy"), false}, 74 {[]byte("x"), "x", true}, 75 {[]byte("x"), "y", false}, 76 {[]byte("x"), "yy", false}, 77 {"x", []byte("x"), true}, 78 {"x", []byte("y"), false}, 79 {"x", []byte("yy"), false}, 80 } 81 for i, tt := range tests { 82 va := of(tt.a) 83 if bytes, ok := tt.b.([]byte); ok { 84 if got := va.EqualBytes(bytes); got != tt.want { 85 t.Errorf("%d. EqualBytes = %v; want %v", i, got, tt.want) 86 } 87 } else { 88 if got := va.EqualString(tt.b.(string)); got != tt.want { 89 t.Errorf("%d. EqualString = %v; want %v", i, got, tt.want) 90 } 91 } 92 if got := va.Equal(of(tt.b)); got != tt.want { 93 t.Errorf("%d. Equal = %v; want %v", i, got, tt.want) 94 } 95 } 96 } 97 98 func TestByteViewSlice(t *testing.T) { 99 tests := []struct { 100 in string 101 from int 102 to interface{} // nil to mean the end (SliceFrom); else int 103 want string 104 }{ 105 { 106 in: "abc", 107 from: 1, 108 to: 2, 109 want: "b", 110 }, 111 { 112 in: "abc", 113 from: 1, 114 want: "bc", 115 }, 116 { 117 in: "abc", 118 to: 2, 119 want: "ab", 120 }, 121 } 122 for i, tt := range tests { 123 for _, v := range []ByteView{of([]byte(tt.in)), of(tt.in)} { 124 name := fmt.Sprintf("test %d, view %+v", i, v) 125 if tt.to != nil { 126 v = v.Slice(tt.from, tt.to.(int)) 127 } else { 128 v = v.SliceFrom(tt.from) 129 } 130 if v.String() != tt.want { 131 t.Errorf("%s: got %q; want %q", name, v.String(), tt.want) 132 } 133 } 134 } 135 } 136 137 func min(a, b int) int { 138 if a < b { 139 return a 140 } 141 return b 142 }