data_writer_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. writer := NewDataWriter()
  20. err := writer.Byte(writerByteValue)
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. err = writer.Bytes(writerBytesValue)
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. err = writer.Uint8(writerUint8Value)
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. err = writer.Uint16(writerUint16Value)
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. err = writer.Uint32(writerUint32Value)
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. err = writer.Uint64(writerUint64Value)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. err = writer.String(writerStringValue)
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. readBuffer := bytes.NewReader(writer.GetBytes())
  49. var retByteValue byte
  50. err = binary.Read(readBuffer, 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(readBuffer, 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(readBuffer, 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(readBuffer, 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(readBuffer, 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(readBuffer, 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(readBuffer, binary.BigEndian, &retStringByteValue)
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. if string(retStringByteValue) != writerStringValue {
  108. t.Fatal("string不一致")
  109. }
  110. }