gitee.com/mirrors_u-root/u-root@v7.0.0+incompatible/pkg/uio/cached_test.go (about) 1 // Copyright 2018 the u-root 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. 4 5 package uio 6 7 import ( 8 "bytes" 9 "fmt" 10 "io" 11 "testing" 12 ) 13 14 func TestCachingReaderRead(t *testing.T) { 15 type read struct { 16 // Buffer sizes to call Read with. 17 size int 18 19 // Buffer value corresponding Read(size) we want. 20 want []byte 21 22 // Error corresponding to Read(size) we want. 23 err error 24 } 25 26 for i, tt := range []struct { 27 // Content of the underlying io.Reader. 28 content []byte 29 30 // Read calls to make in order. 31 reads []read 32 }{ 33 { 34 content: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}, 35 reads: []read{ 36 { 37 size: 0, 38 }, 39 { 40 size: 1, 41 want: []byte{0x11}, 42 }, 43 { 44 size: 2, 45 want: []byte{0x22, 0x33}, 46 }, 47 { 48 size: 0, 49 }, 50 { 51 size: 3, 52 want: []byte{0x44, 0x55, 0x66}, 53 }, 54 { 55 size: 4, 56 want: []byte{0x77, 0x88, 0x99}, 57 err: io.EOF, 58 }, 59 }, 60 }, 61 { 62 content: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}, 63 reads: []read{ 64 { 65 size: 11, 66 want: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}, 67 err: io.EOF, 68 }, 69 }, 70 }, 71 { 72 content: nil, 73 reads: []read{ 74 { 75 size: 2, 76 err: io.EOF, 77 }, 78 { 79 size: 0, 80 }, 81 }, 82 }, 83 { 84 content: []byte{0x33, 0x22, 0x11}, 85 reads: []read{ 86 { 87 size: 3, 88 want: []byte{0x33, 0x22, 0x11}, 89 err: nil, 90 }, 91 { 92 size: 0, 93 }, 94 { 95 size: 1, 96 err: io.EOF, 97 }, 98 }, 99 }, 100 } { 101 t.Run(fmt.Sprintf("Test [%02d]", i), func(t *testing.T) { 102 buf := NewCachingReader(bytes.NewBuffer(tt.content)) 103 for j, r := range tt.reads { 104 p := make([]byte, r.size) 105 m, err := buf.Read(p) 106 if err != r.err { 107 t.Errorf("Read#%d(%d) = %v, want %v", j, r.size, err, r.err) 108 } 109 if m != len(r.want) { 110 t.Errorf("Read#%d(%d) = %d, want %d", j, r.size, m, len(r.want)) 111 } 112 if !bytes.Equal(r.want, p[:m]) { 113 t.Errorf("Read#%d(%d) = %v, want %v", j, r.size, p[:m], r.want) 114 } 115 } 116 }) 117 } 118 } 119 120 func TestCachingReaderReadAt(t *testing.T) { 121 type readAt struct { 122 // Buffer sizes to call Read with. 123 size int 124 125 // Offset to read from. 126 off int64 127 128 // Buffer value corresponding Read(size) we want. 129 want []byte 130 131 // Error corresponding to Read(size) we want. 132 err error 133 } 134 135 for i, tt := range []struct { 136 // Content of the underlying io.Reader. 137 content []byte 138 139 // Read calls to make in order. 140 reads []readAt 141 }{ 142 { 143 content: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}, 144 reads: []readAt{ 145 { 146 off: 0, 147 size: 0, 148 }, 149 { 150 off: 0, 151 size: 1, 152 want: []byte{0x11}, 153 }, 154 { 155 off: 1, 156 size: 2, 157 want: []byte{0x22, 0x33}, 158 }, 159 { 160 off: 0, 161 size: 0, 162 }, 163 { 164 off: 3, 165 size: 3, 166 want: []byte{0x44, 0x55, 0x66}, 167 }, 168 { 169 off: 6, 170 size: 4, 171 want: []byte{0x77, 0x88, 0x99}, 172 err: io.EOF, 173 }, 174 { 175 off: 0, 176 size: 9, 177 want: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}, 178 }, 179 { 180 off: 0, 181 size: 10, 182 want: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}, 183 err: io.EOF, 184 }, 185 { 186 off: 0, 187 size: 8, 188 want: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}, 189 }, 190 }, 191 }, 192 { 193 content: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}, 194 reads: []readAt{ 195 { 196 off: 10, 197 size: 10, 198 err: io.EOF, 199 }, 200 { 201 off: 5, 202 size: 4, 203 want: []byte{0x66, 0x77, 0x88, 0x99}, 204 }, 205 }, 206 }, 207 { 208 content: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}, 209 reads: []readAt{ 210 { 211 size: 9, 212 want: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}, 213 }, 214 { 215 off: 5, 216 size: 4, 217 want: []byte{0x66, 0x77, 0x88, 0x99}, 218 }, 219 { 220 off: 9, 221 size: 1, 222 err: io.EOF, 223 }, 224 }, 225 }, 226 } { 227 t.Run(fmt.Sprintf("Test [%02d]", i), func(t *testing.T) { 228 buf := NewCachingReader(bytes.NewBuffer(tt.content)) 229 for j, r := range tt.reads { 230 p := make([]byte, r.size) 231 m, err := buf.ReadAt(p, r.off) 232 if err != r.err { 233 t.Errorf("Read#%d(%d) = %v, want %v", j, r.size, err, r.err) 234 } 235 if m != len(r.want) { 236 t.Errorf("Read#%d(%d) = %d, want %d", j, r.size, m, len(r.want)) 237 } 238 if !bytes.Equal(r.want, p[:m]) { 239 t.Errorf("Read#%d(%d) = %v, want %v", j, r.size, p[:m], r.want) 240 } 241 } 242 }) 243 } 244 }