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.

95 lines
2.8 KiB

  1. package common
  2. import (
  3. "math/big"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. )
  8. func TestConversionsFloat40(t *testing.T) {
  9. testVector := map[Float40]string{
  10. 6*0x800000000 + 123: "123000000",
  11. 2*0x800000000 + 4545: "454500",
  12. 30*0x800000000 + 10235: "10235000000000000000000000000000000",
  13. 0x000000000: "0",
  14. 0x800000000: "0",
  15. 0x0001: "1",
  16. 0x0401: "1025",
  17. 0x800000000 + 1: "10",
  18. 0xFFFFFFFFFF: "343597383670000000000000000000000000000000",
  19. }
  20. for test := range testVector {
  21. fix, err := test.BigInt()
  22. require.NoError(t, err)
  23. assert.Equal(t, fix.String(), testVector[test])
  24. bi, ok := new(big.Int).SetString(testVector[test], 10)
  25. require.True(t, ok)
  26. fl, err := NewFloat40(bi)
  27. assert.NoError(t, err)
  28. fx2, err := fl.BigInt()
  29. require.NoError(t, err)
  30. assert.Equal(t, fx2.String(), testVector[test])
  31. }
  32. }
  33. func TestExpectError(t *testing.T) {
  34. testVector := map[string]error{
  35. "9922334455000000000000000000000000000000": nil,
  36. "9922334455000000000000000000000000000001": ErrFloat40NotEnoughPrecission,
  37. "9922334454999999999999999999999999999999": ErrFloat40NotEnoughPrecission,
  38. "42949672950000000000000000000000000000000": nil,
  39. "99223344556573838487575": ErrFloat40NotEnoughPrecission,
  40. "992233445500000000000000000000000000000000": ErrFloat40E31,
  41. "343597383670000000000000000000000000000000": nil,
  42. "343597383680000000000000000000000000000000": ErrFloat40NotEnoughPrecission,
  43. "343597383690000000000000000000000000000000": ErrFloat40NotEnoughPrecission,
  44. "343597383700000000000000000000000000000000": ErrFloat40E31,
  45. }
  46. for test := range testVector {
  47. bi, ok := new(big.Int).SetString(test, 10)
  48. require.True(t, ok)
  49. _, err := NewFloat40(bi)
  50. assert.Equal(t, testVector[test], err)
  51. }
  52. }
  53. func BenchmarkFloat40(b *testing.B) {
  54. newBigInt := func(s string) *big.Int {
  55. bigInt, ok := new(big.Int).SetString(s, 10)
  56. if !ok {
  57. panic("Can not convert string to *big.Int")
  58. }
  59. return bigInt
  60. }
  61. type pair struct {
  62. Float40 Float40
  63. BigInt *big.Int
  64. }
  65. testVector := []pair{
  66. {6*0x800000000 + 123, newBigInt("123000000")},
  67. {2*0x800000000 + 4545, newBigInt("454500")},
  68. {30*0x800000000 + 10235, newBigInt("10235000000000000000000000000000000")},
  69. {0x000000000, newBigInt("0")},
  70. {0x800000000, newBigInt("0")},
  71. {0x0001, newBigInt("1")},
  72. {0x0401, newBigInt("1025")},
  73. {0x800000000 + 1, newBigInt("10")},
  74. {0xFFFFFFFFFF, newBigInt("343597383670000000000000000000000000000000")},
  75. }
  76. b.Run("NewFloat40()", func(b *testing.B) {
  77. for i := 0; i < b.N; i++ {
  78. _, _ = NewFloat40(testVector[i%len(testVector)].BigInt)
  79. }
  80. })
  81. b.Run("Float40.BigInt()", func(b *testing.B) {
  82. for i := 0; i < b.N; i++ {
  83. _, _ = testVector[i%len(testVector)].Float40.BigInt()
  84. }
  85. })
  86. }