tlv_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package tlv
  2. import (
  3. "bytes"
  4. "reflect"
  5. "testing"
  6. )
  7. func TestTlvLen(t *testing.T) {
  8. float64Tlv, _ := MakeTLV(float64(0.12))
  9. if float64Tlv.Length() != 10 {
  10. t.Errorf("float64 len is not right\n")
  11. }
  12. int64Tlv, _ := MakeTLV(int64(100))
  13. if int64Tlv.Length() != 10 {
  14. t.Errorf("int64 len is not right\n")
  15. }
  16. uint64Tlv, _ := MakeTLV(uint64(100))
  17. if uint64Tlv.Length() != 10 {
  18. t.Errorf("uint64 len is not right\n")
  19. }
  20. float32Tlv, _ := MakeTLV(float32(0.12))
  21. if float32Tlv.Length() != 6 {
  22. t.Errorf("float32 len is not right\n")
  23. }
  24. int32Tlv, _ := MakeTLV(int32(100))
  25. if int32Tlv.Length() != 6 {
  26. t.Errorf("int32 len is not right\n")
  27. }
  28. uint32Tlv, _ := MakeTLV(uint32(100))
  29. if uint32Tlv.Length() != 6 {
  30. t.Errorf("uint32 len is not right\n")
  31. }
  32. int16Tlv, _ := MakeTLV(int16(100))
  33. if int16Tlv.Length() != 4 {
  34. t.Errorf("int16 len is not right\n")
  35. }
  36. uint16Tlv, _ := MakeTLV(uint16(100))
  37. if uint16Tlv.Length() != 4 {
  38. t.Errorf("uint16 len is not right\n")
  39. }
  40. int8Tlv, _ := MakeTLV(int8(100))
  41. if int8Tlv.Length() != 3 {
  42. t.Errorf("int8 len is not right\n")
  43. }
  44. uint8Tlv, _ := MakeTLV(uint8(100))
  45. if uint8Tlv.Length() != 3 {
  46. t.Errorf("uint8 len is not right\n")
  47. }
  48. byteValue := []byte{'1', '0', '0'}
  49. byteTLV, _ := MakeTLV(byteValue)
  50. if byteTLV.Length() != len(byteValue)+4 {
  51. t.Errorf("byte len is not right\n")
  52. }
  53. str := "100"
  54. strTLV, _ := MakeTLV(str)
  55. if strTLV.Length() != len(str)+4 {
  56. t.Errorf("string len is not right\n")
  57. }
  58. }
  59. func TestUintAndByte(t *testing.T) {
  60. value := uint16(100)
  61. byteValue := Uint16ToByte(value)
  62. newValue := ByteToUint16(byteValue)
  63. if value != newValue {
  64. t.Errorf("origin: %d, now: %d\n", value, newValue)
  65. }
  66. }
  67. func TestTlvs(t *testing.T) {
  68. str := "itachili"
  69. params := []interface{}{float64(0.1), int64(100), uint64(200), uint32(300), int32(16), float32(3.2), int16(20), uint16(30), int8(1), uint8(2), []byte{'1', '2', '3'}, str}
  70. tlvs, err := MakeTLVs(params)
  71. if err != nil {
  72. t.Error(err)
  73. }
  74. newParams, err := ReadTLVs(tlvs)
  75. if err != nil {
  76. t.Error(err)
  77. }
  78. if !reflect.DeepEqual(params, newParams) {
  79. t.Errorf("the origin:\n%x\n, now:\n%x\n", params, newParams)
  80. }
  81. }
  82. func TestTlvBinary(t *testing.T) {
  83. str := "itachili"
  84. params := []interface{}{float64(0.1), int64(100), uint64(200), uint32(300), int32(16), float32(3.2), int16(20), uint16(30), int8(1), uint8(2), []byte{'1', '2', '3'}, str}
  85. tlv, err := MakeTLV(params[0])
  86. if err != nil {
  87. t.Error(err)
  88. }
  89. bin := tlv.ToBinary()
  90. buf := bytes.NewReader(bin)
  91. newTlv := &TLV{}
  92. newTlv.FromBinary(buf)
  93. if !reflect.DeepEqual(tlv, newTlv) {
  94. t.Errorf("the origin:\n%x\n, now:\n%x\n", tlv, newTlv)
  95. }
  96. }