MCPcopy
hub / github.com/TarsCloud/TarsGo / TestReader_unreadHead

Function TestReader_unreadHead

tars/protocol/codec/codec_test.go:544–605  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

542}
543
544func TestReader_unreadHead(t *testing.T) {
545 writer := NewBuffer()
546 err := writer.WriteString("hello", 0)
547 if err != nil {
548 t.Errorf("Write buffer failed.err:%v\n", err)
549 }
550 err = writer.WriteUint8(1, 1)
551 if err != nil {
552 t.Errorf("Write buffer failed.err:%v\n", err)
553 }
554 err = writer.WriteFloat32(1.2, 2)
555 if err != nil {
556 t.Errorf("Write buffer failed.err:%v\n", err)
557 }
558
559 // string type read head
560 reader := r(writer)
561 wantType, wantTag := STRING1, byte(0)
562 gotType, gotTag, err := reader.readHead()
563 if err != nil {
564 t.Errorf("Read buffer failed.err:%v\n", err)
565 }
566 if gotType != wantType || gotTag != wantTag {
567 t.Errorf("Failed to readHead. wantType:%v, wantTag:%v, gotType:%v, gotTag:%v\n",
568 wantType, wantTag, gotType, gotTag)
569 }
570
571 // string type unread head
572 reader.unreadHead(gotTag)
573 gotType, gotTag, err = reader.readHead()
574 if err != nil {
575 t.Errorf("Read head failed.err:%v\n", err)
576 }
577 // skip next 6 byte. 1 byte for string length, 5 byte for string itself.
578 reader.Skip(6)
579 if gotType != wantType || gotTag != wantTag {
580 t.Errorf("Failed to readHead. wantType:%v, wantTag:%v, gotType:%v, gotTag:%v\n",
581 wantType, wantTag, gotType, gotTag)
582 }
583
584 // uint8 read head
585 wantType, wantTag = BYTE, byte(1)
586 gotType, gotTag, err = reader.readHead()
587 if err != nil {
588 t.Errorf("Read buffer failed.err:%v\n", err)
589 }
590 if gotType != wantType || gotTag != wantTag {
591 t.Errorf("Failed to readHead. wantType:%v, wantTag:%v, gotType:%v, gotTag:%v\n",
592 wantType, wantTag, gotType, gotTag)
593 }
594
595 // uint8 unread head
596 reader.unreadHead(gotTag)
597 gotType, gotTag, err = reader.readHead()
598 if err != nil {
599 t.Errorf("Read head failed.err:%v\n", err)
600 }
601 if gotType != wantType || gotTag != wantTag {

Callers

nothing calls this directly

Calls 9

WriteStringMethod · 0.95
WriteUint8Method · 0.95
WriteFloat32Method · 0.95
NewBufferFunction · 0.85
rFunction · 0.85
readHeadMethod · 0.80
unreadHeadMethod · 0.80
SkipMethod · 0.80
ErrorfMethod · 0.45

Tested by

no test coverage detected