primitives_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package ndr
  2. import (
  3. "bufio"
  4. "bytes"
  5. "encoding/binary"
  6. "encoding/hex"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestReadFloat32(t *testing.T) {
  11. tests := []struct {
  12. hexStr string
  13. value float32
  14. order binary.ByteOrder
  15. }{
  16. {"3E200000", 0.15625, binary.BigEndian},
  17. {"00000000", 0.0, binary.BigEndian},
  18. {"3F800000", 1.0, binary.BigEndian},
  19. {"BF800000", -1.0, binary.BigEndian},
  20. {"00000001", 1.4e-45, binary.BigEndian},
  21. {"00400000", 5.877472e-39, binary.BigEndian},
  22. {"007FFFFF", 1.1754942e-38, binary.BigEndian},
  23. {"00800000", 1.1754944e-38, binary.BigEndian},
  24. {"7F7FFFFF", 3.4028235e38, binary.BigEndian},
  25. //TODO need some littleendian test vectors
  26. }
  27. for i, test := range tests {
  28. b, _ := hex.DecodeString(test.hexStr)
  29. //t.Logf("%s %08b\n", test.hexStr,b)
  30. r := bufio.NewReader(bytes.NewReader(b))
  31. dec := Decoder{
  32. r: r,
  33. ch: CommonHeader{Endianness: test.order},
  34. }
  35. f, err := dec.readFloat32()
  36. if err != nil {
  37. t.Errorf("could not read float32 test %d: %v", i, err)
  38. }
  39. assert.Equal(t, test.value, f, "float32 not as expect for test %d: %s", i, test.hexStr)
  40. }
  41. }