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.

133 lines
4.2 KiB

  1. package common
  2. import (
  3. "math/big"
  4. "testing"
  5. "github.com/hermeznetwork/tracerr"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. )
  9. func TestConversionsFloat40(t *testing.T) {
  10. testVector := map[Float40]string{
  11. 6*0x800000000 + 123: "123000000",
  12. 2*0x800000000 + 4545: "454500",
  13. 30*0x800000000 + 10235: "10235000000000000000000000000000000",
  14. 0x000000000: "0",
  15. 0x800000000: "0",
  16. 0x0001: "1",
  17. 0x0401: "1025",
  18. 0x800000000 + 1: "10",
  19. 0xFFFFFFFFFF: "343597383670000000000000000000000000000000",
  20. }
  21. for test := range testVector {
  22. fix, err := test.BigInt()
  23. require.NoError(t, err)
  24. assert.Equal(t, fix.String(), testVector[test])
  25. bi, ok := new(big.Int).SetString(testVector[test], 10)
  26. require.True(t, ok)
  27. fl, err := NewFloat40(bi)
  28. assert.NoError(t, err)
  29. fx2, err := fl.BigInt()
  30. require.NoError(t, err)
  31. assert.Equal(t, fx2.String(), testVector[test])
  32. }
  33. }
  34. func TestExpectError(t *testing.T) {
  35. testVector := map[string]error{
  36. "9922334455000000000000000000000000000000": nil,
  37. "9922334455000000000000000000000000000001": ErrFloat40NotEnoughPrecission,
  38. "9922334454999999999999999999999999999999": ErrFloat40NotEnoughPrecission,
  39. "42949672950000000000000000000000000000000": nil,
  40. "99223344556573838487575": ErrFloat40NotEnoughPrecission,
  41. "992233445500000000000000000000000000000000": ErrFloat40E31,
  42. "343597383670000000000000000000000000000000": nil,
  43. "343597383680000000000000000000000000000000": ErrFloat40NotEnoughPrecission,
  44. "343597383690000000000000000000000000000000": ErrFloat40NotEnoughPrecission,
  45. "343597383700000000000000000000000000000000": ErrFloat40E31,
  46. }
  47. for test := range testVector {
  48. bi, ok := new(big.Int).SetString(test, 10)
  49. require.True(t, ok)
  50. _, err := NewFloat40(bi)
  51. assert.Equal(t, testVector[test], tracerr.Unwrap(err))
  52. }
  53. }
  54. func TestNewFloat40Floor(t *testing.T) {
  55. testVector := map[string][]uint64{
  56. // []int contains [Float40 value, Flot40 Floor value], when
  57. // Float40 value is expected to be 0, is because is expected to
  58. // be an error
  59. "9922334455000000000000000000000000000000": {1040714485495, 1040714485495},
  60. "9922334455000000000000000000000000000001": {0, 6846188881046405121},
  61. "9922334454999999999999999999999999999999": {0, 6846188881046405119},
  62. "42949672950000000000000000000000000000000": {1069446856703, 1069446856703},
  63. "99223344556573838487575": {0, 16754928163869896727},
  64. "992233445500000000000000000000000000000000": {0, 0},
  65. "343597383670000000000000000000000000000000": {1099511627775, 1099511627775},
  66. "343597383680000000000000000000000000000000": {0, 1099511627776},
  67. "343597383690000000000000000000000000000000": {0, 1099511627777},
  68. "343597383700000000000000000000000000000000": {0, 0},
  69. }
  70. for test := range testVector {
  71. bi, ok := new(big.Int).SetString(test, 10)
  72. require.True(t, ok)
  73. f40, err := NewFloat40(bi)
  74. if f40 == 0 {
  75. assert.Error(t, err)
  76. } else {
  77. assert.NoError(t, err)
  78. }
  79. assert.Equal(t, testVector[test][0], uint64(f40))
  80. f40, err = NewFloat40Floor(bi)
  81. if f40 == 0 {
  82. assert.Equal(t, ErrFloat40E31, tracerr.Unwrap(err))
  83. } else {
  84. assert.NoError(t, err)
  85. }
  86. assert.Equal(t, testVector[test][1], uint64(f40))
  87. }
  88. }
  89. func BenchmarkFloat40(b *testing.B) {
  90. newBigInt := func(s string) *big.Int {
  91. bigInt, ok := new(big.Int).SetString(s, 10)
  92. if !ok {
  93. panic("Can not convert string to *big.Int")
  94. }
  95. return bigInt
  96. }
  97. type pair struct {
  98. Float40 Float40
  99. BigInt *big.Int
  100. }
  101. testVector := []pair{
  102. {6*0x800000000 + 123, newBigInt("123000000")},
  103. {2*0x800000000 + 4545, newBigInt("454500")},
  104. {30*0x800000000 + 10235, newBigInt("10235000000000000000000000000000000")},
  105. {0x000000000, newBigInt("0")},
  106. {0x800000000, newBigInt("0")},
  107. {0x0001, newBigInt("1")},
  108. {0x0401, newBigInt("1025")},
  109. {0x800000000 + 1, newBigInt("10")},
  110. {0xFFFFFFFFFF, newBigInt("343597383670000000000000000000000000000000")},
  111. }
  112. b.Run("NewFloat40()", func(b *testing.B) {
  113. for i := 0; i < b.N; i++ {
  114. _, _ = NewFloat40(testVector[i%len(testVector)].BigInt)
  115. }
  116. })
  117. b.Run("Float40.BigInt()", func(b *testing.B) {
  118. for i := 0; i < b.N; i++ {
  119. _, _ = testVector[i%len(testVector)].Float40.BigInt()
  120. }
  121. })
  122. }