You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
2.8 KiB

  1. package utils
  2. import (
  3. "math/big"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestConversions(t *testing.T) {
  8. testVector := map[Float16]string{
  9. 0x307B: "123000000",
  10. 0x1DC6: "454500",
  11. 0xFFFF: "10235000000000000000000000000000000",
  12. 0x0000: "0",
  13. 0x0400: "0",
  14. 0x0001: "1",
  15. 0x0401: "1",
  16. 0x0800: "0",
  17. 0x0c00: "5",
  18. 0x0801: "10",
  19. 0x0c01: "15",
  20. }
  21. for test := range testVector {
  22. fix := test.BigInt()
  23. assert.Equal(t, fix.String(), testVector[test])
  24. bi := big.NewInt(0)
  25. bi.SetString(testVector[test], 10)
  26. fl, err := NewFloat16(bi)
  27. assert.Equal(t, nil, err)
  28. fx2 := fl.BigInt()
  29. assert.Equal(t, fx2.String(), testVector[test])
  30. }
  31. }
  32. func TestFloorFix2Float(t *testing.T) {
  33. testVector := map[string]Float16{
  34. "87999990000000000": 0x776f,
  35. "87950000000000001": 0x776f,
  36. "87950000000000000": 0x776f,
  37. "87949999999999999": 0x736f,
  38. }
  39. for test := range testVector {
  40. bi := big.NewInt(0)
  41. bi.SetString(test, 10)
  42. testFloat := NewFloat16Floor(bi)
  43. assert.Equal(t, testFloat, testVector[test])
  44. }
  45. }
  46. func TestConversionLosses(t *testing.T) {
  47. a := big.NewInt(1000)
  48. b, err := NewFloat16(a)
  49. assert.Equal(t, nil, err)
  50. c := b.BigInt()
  51. assert.Equal(t, c, a)
  52. a = big.NewInt(1024)
  53. b, err = NewFloat16(a)
  54. assert.Equal(t, ErrRoundingLoss, err)
  55. c = b.BigInt()
  56. assert.NotEqual(t, c, a)
  57. a = big.NewInt(32767)
  58. b, err = NewFloat16(a)
  59. assert.Equal(t, ErrRoundingLoss, err)
  60. c = b.BigInt()
  61. assert.NotEqual(t, c, a)
  62. a = big.NewInt(32768)
  63. b, err = NewFloat16(a)
  64. assert.Equal(t, ErrRoundingLoss, err)
  65. c = b.BigInt()
  66. assert.NotEqual(t, c, a)
  67. a = big.NewInt(65536000)
  68. b, err = NewFloat16(a)
  69. assert.Equal(t, ErrRoundingLoss, err)
  70. c = b.BigInt()
  71. assert.NotEqual(t, c, a)
  72. }
  73. func BenchmarkFloat16(b *testing.B) {
  74. newBigInt := func(s string) *big.Int {
  75. bigInt, ok := new(big.Int).SetString(s, 10)
  76. if !ok {
  77. panic("Bad big int")
  78. }
  79. return bigInt
  80. }
  81. type pair struct {
  82. Float16 Float16
  83. BigInt *big.Int
  84. }
  85. testVector := []pair{
  86. pair{0x307B, newBigInt("123000000")},
  87. pair{0x1DC6, newBigInt("454500")},
  88. pair{0xFFFF, newBigInt("10235000000000000000000000000000000")},
  89. pair{0x0000, newBigInt("0")},
  90. pair{0x0400, newBigInt("0")},
  91. pair{0x0001, newBigInt("1")},
  92. pair{0x0401, newBigInt("1")},
  93. pair{0x0800, newBigInt("0")},
  94. pair{0x0c00, newBigInt("5")},
  95. pair{0x0801, newBigInt("10")},
  96. pair{0x0c01, newBigInt("15")},
  97. }
  98. b.Run("floorFix2Float()", func(b *testing.B) {
  99. for i := 0; i < b.N; i++ {
  100. NewFloat16Floor(testVector[i%len(testVector)].BigInt)
  101. }
  102. })
  103. b.Run("NewFloat16()", func(b *testing.B) {
  104. for i := 0; i < b.N; i++ {
  105. NewFloat16(testVector[i%len(testVector)].BigInt) //nolint:errcheck
  106. }
  107. })
  108. b.Run("Float16.BigInt()", func(b *testing.B) {
  109. for i := 0; i < b.N; i++ {
  110. testVector[i%len(testVector)].Float16.BigInt()
  111. }
  112. })
  113. }