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.

132 lines
2.8 KiB

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