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.

58 lines
1.7 KiB

  1. package common
  2. import (
  3. "fmt"
  4. "math/big"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestFeePercentage(t *testing.T) {
  9. assert.InEpsilon(t, 2.68E-18, FeeSelector(1).Percentage(), 0.002)
  10. assert.InEpsilon(t, 6.76E-14, FeeSelector(10).Percentage(), 0.002)
  11. assert.InEpsilon(t, 3.91E-03, FeeSelector(32).Percentage(), 0.002)
  12. assert.InEpsilon(t, 7.29E-03, FeeSelector(50).Percentage(), 0.002)
  13. assert.InEpsilon(t, 4.12E-02, FeeSelector(100).Percentage(), 0.002)
  14. assert.InEpsilon(t, 2.33E-01, FeeSelector(150).Percentage(), 0.002)
  15. assert.InEpsilon(t, 1.00E+00, FeeSelector(192).Percentage(), 0.002)
  16. assert.InEpsilon(t, 2.56E+02, FeeSelector(200).Percentage(), 0.002)
  17. assert.InEpsilon(t, 2.88E+17, FeeSelector(250).Percentage(), 0.002)
  18. }
  19. func TestCalcFeeAmount(t *testing.T) {
  20. v := big.NewInt(1000)
  21. feeAmount, err := CalcFeeAmount(v, FeeSelector(195)) // 800%
  22. assert.Nil(t, err)
  23. assert.Equal(t, "8000", feeAmount.String())
  24. feeAmount, err = CalcFeeAmount(v, FeeSelector(192)) // 100%
  25. assert.Nil(t, err)
  26. assert.Equal(t, "1000", feeAmount.String())
  27. feeAmount, err = CalcFeeAmount(v, FeeSelector(172)) // 50%
  28. assert.Nil(t, err)
  29. assert.Equal(t, "500", feeAmount.String())
  30. feeAmount, err = CalcFeeAmount(v, FeeSelector(126)) // 10.2%
  31. assert.Nil(t, err)
  32. assert.Equal(t, "101", feeAmount.String())
  33. feeAmount, err = CalcFeeAmount(v, FeeSelector(60)) // 1.03%
  34. assert.Nil(t, err)
  35. assert.Equal(t, "10", feeAmount.String())
  36. feeAmount, err = CalcFeeAmount(v, FeeSelector(31)) // 0.127%
  37. assert.Nil(t, err)
  38. assert.Equal(t, "1", feeAmount.String())
  39. }
  40. func TestFeePrintSQLSwitch(t *testing.T) {
  41. debug := false
  42. for i := 0; i < 256; i++ {
  43. f := FeeSelector(i).Percentage()
  44. if debug {
  45. fmt.Printf(" WHEN $1 = %03d THEN %.6e\n", i, f)
  46. }
  47. }
  48. }