uint_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package number
  2. import (
  3. "math/big"
  4. "testing"
  5. "github.com/ethereum/go-ethereum/common"
  6. )
  7. func TestSet(t *testing.T) {
  8. a := Uint(0)
  9. b := Uint(10)
  10. a.Set(b)
  11. if a.num.Cmp(b.num) != 0 {
  12. t.Error("didn't compare", a, b)
  13. }
  14. c := Uint(0).SetBytes(common.Hex2Bytes("0a"))
  15. if c.num.Cmp(big.NewInt(10)) != 0 {
  16. t.Error("c set bytes failed.")
  17. }
  18. }
  19. func TestInitialiser(t *testing.T) {
  20. check := false
  21. init := NewInitialiser(func(x *Number) *Number {
  22. check = true
  23. return x
  24. })
  25. a := init(0).Add(init(1), init(2))
  26. if a.Cmp(init(3)) != 0 {
  27. t.Error("expected 3. got", a)
  28. }
  29. if !check {
  30. t.Error("expected limiter to be called")
  31. }
  32. }
  33. func TestGet(t *testing.T) {
  34. a := Uint(10)
  35. if a.Uint64() != 10 {
  36. t.Error("expected to get 10. got", a.Uint64())
  37. }
  38. a = Uint(10)
  39. if a.Int64() != 10 {
  40. t.Error("expected to get 10. got", a.Int64())
  41. }
  42. }
  43. func TestCmp(t *testing.T) {
  44. a := Uint(10)
  45. b := Uint(10)
  46. c := Uint(11)
  47. if a.Cmp(b) != 0 {
  48. t.Error("a b == 0 failed", a, b)
  49. }
  50. if a.Cmp(c) >= 0 {
  51. t.Error("a c < 0 failed", a, c)
  52. }
  53. if c.Cmp(b) <= 0 {
  54. t.Error("c b > 0 failed", c, b)
  55. }
  56. }
  57. func TestMaxArith(t *testing.T) {
  58. a := Uint(0).Add(MaxUint256, One)
  59. if a.Cmp(Zero) != 0 {
  60. t.Error("expected max256 + 1 = 0 got", a)
  61. }
  62. a = Uint(0).Sub(Uint(0), One)
  63. if a.Cmp(MaxUint256) != 0 {
  64. t.Error("expected 0 - 1 = max256 got", a)
  65. }
  66. a = Int(0).Sub(Int(0), One)
  67. if a.Cmp(MinOne) != 0 {
  68. t.Error("expected 0 - 1 = -1 got", a)
  69. }
  70. }
  71. func TestConversion(t *testing.T) {
  72. a := Int(-1)
  73. b := a.Uint256()
  74. if b.Cmp(MaxUint256) != 0 {
  75. t.Error("expected -1 => unsigned to return max. got", b)
  76. }
  77. }