MCPcopy Create free account
hub / github.com/chain/Core / TestExtensibleString

Function TestExtensibleString

encoding/blockchain/blockchain_test.go:259–315  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

257}
258
259func TestExtensibleString(t *testing.T) {
260 for i := 0; i < 4; i++ {
261 // make a string of length i+1
262 str := make([]byte, 0, i+1)
263 for j := 0; j <= i; j++ {
264 str = append(str, byte(i))
265 }
266 var buf bytes.Buffer
267 _, err := WriteExtensibleString(&buf, nil, func(w io.Writer) error {
268 _, err := w.Write(str)
269 return err
270 })
271 if err != nil {
272 t.Fatal(err)
273 }
274 var str2 []byte
275 b := buf.Bytes()
276 suffix, err := ReadExtensibleString(NewReader(b), func(r *Reader) error {
277 str2, err = ioutil.ReadAll(r)
278 return err
279 })
280 if err != nil {
281 t.Fatal(err)
282 }
283 if len(suffix) > 0 {
284 t.Errorf("got suffix %x, want empty suffix", suffix)
285 }
286 if !bytes.Equal(str, str2) {
287 t.Errorf("got %x, want %x", str2, str)
288 }
289 _, err = ReadExtensibleString(NewReader(b[:i]), func(r *Reader) error {
290 return nil
291 })
292 switch err {
293 case nil:
294 t.Errorf("got no error, want io.EOF")
295 case io.EOF, io.ErrUnexpectedEOF:
296 default:
297 t.Errorf("got error %s, want io.EOF", err)
298 }
299 _, err = ReadExtensibleString(NewReader(b), func(r *Reader) error {
300 return nil
301 })
302 if err != nil {
303 t.Error(err)
304 }
305 suffix, err = ReadExtensibleString(NewReader(b), func(r *Reader) error {
306 return nil
307 })
308 if err != nil {
309 t.Error(err)
310 }
311 if !bytes.Equal(str, suffix) {
312 t.Errorf("got suffix %x, want %x", suffix, str)
313 }
314 }
315}
316

Callers

nothing calls this directly

Calls 7

WriteExtensibleStringFunction · 0.85
ReadExtensibleStringFunction · 0.85
NewReaderFunction · 0.85
WriteMethod · 0.45
BytesMethod · 0.45
EqualMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected