data_writer_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package network
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "testing"
  6. )
  7. const (
  8. writerByteValue byte = 1
  9. writerUint8Value uint8 = 5
  10. writerUint16Value uint16 = 6
  11. writerUint32Value uint32 = 7
  12. writerUint64Value uint64 = 8
  13. writerStringValue = "91011"
  14. )
  15. var (
  16. writerBytesValue = []byte{2, 3, 4}
  17. )
  18. func TestDataWriter(t *testing.T) {
  19. buffer := &bytes.Buffer{}
  20. writer := NewDataWriter(buffer)
  21. err := writer.Byte(writerByteValue)
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. err = writer.Bytes(writerBytesValue)
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. err = writer.Uint8(writerUint8Value)
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. err = writer.Uint16(writerUint16Value)
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. err = writer.Uint32(writerUint32Value)
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. err = writer.Uint64(writerUint64Value)
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. err = writer.String(writerStringValue)
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. var retByteValue byte
  50. err = binary.Read(buffer, binary.BigEndian, &retByteValue)
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. if retByteValue != writerByteValue {
  55. t.Fatal("byte不一致")
  56. }
  57. retBytesValue := make([]byte, len(writerBytesValue))
  58. err = binary.Read(buffer, binary.BigEndian, &retBytesValue)
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. if len(retBytesValue) != len(readerBytesValue) {
  63. t.Fatal("bytes长度不正确")
  64. }
  65. for i := 0; i < len(retBytesValue); i++ {
  66. if retBytesValue[i] != readerBytesValue[i] {
  67. t.Fatal("bytes不正确")
  68. }
  69. }
  70. var retUint8Value uint8
  71. err = binary.Read(buffer, binary.BigEndian, &retUint8Value)
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. if retUint8Value != writerUint8Value {
  76. t.Fatal("uint8不一致")
  77. }
  78. var retUint16Value uint16
  79. err = binary.Read(buffer, binary.BigEndian, &retUint16Value)
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. if retUint16Value != writerUint16Value {
  84. t.Fatal("uint16不一致")
  85. }
  86. var retUint32Value uint32
  87. err = binary.Read(buffer, binary.BigEndian, &retUint32Value)
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. if retUint32Value != writerUint32Value {
  92. t.Fatal("uint32不一致")
  93. }
  94. var retUint64Value uint64
  95. err = binary.Read(buffer, binary.BigEndian, &retUint64Value)
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. if retUint64Value != writerUint64Value {
  100. t.Fatal("uint64不一致")
  101. }
  102. retStringByteValue := make([]byte, len(writerStringValue))
  103. err = binary.Read(buffer, binary.BigEndian, &retStringByteValue)
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. if string(retStringByteValue) != writerStringValue {
  108. t.Fatal("string不一致")
  109. }
  110. }