uint_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package number
  17. import (
  18. "math/big"
  19. "testing"
  20. "github.com/ethereum/go-ethereum/common"
  21. )
  22. func TestSet(t *testing.T) {
  23. a := Uint(0)
  24. b := Uint(10)
  25. a.Set(b)
  26. if a.num.Cmp(b.num) != 0 {
  27. t.Error("didn't compare", a, b)
  28. }
  29. c := Uint(0).SetBytes(common.Hex2Bytes("0a"))
  30. if c.num.Cmp(big.NewInt(10)) != 0 {
  31. t.Error("c set bytes failed.")
  32. }
  33. }
  34. func TestInitialiser(t *testing.T) {
  35. check := false
  36. init := NewInitialiser(func(x *Number) *Number {
  37. check = true
  38. return x
  39. })
  40. a := init(0).Add(init(1), init(2))
  41. if a.Cmp(init(3)) != 0 {
  42. t.Error("expected 3. got", a)
  43. }
  44. if !check {
  45. t.Error("expected limiter to be called")
  46. }
  47. }
  48. func TestGet(t *testing.T) {
  49. a := Uint(10)
  50. if a.Uint64() != 10 {
  51. t.Error("expected to get 10. got", a.Uint64())
  52. }
  53. a = Uint(10)
  54. if a.Int64() != 10 {
  55. t.Error("expected to get 10. got", a.Int64())
  56. }
  57. }
  58. func TestCmp(t *testing.T) {
  59. a := Uint(10)
  60. b := Uint(10)
  61. c := Uint(11)
  62. if a.Cmp(b) != 0 {
  63. t.Error("a b == 0 failed", a, b)
  64. }
  65. if a.Cmp(c) >= 0 {
  66. t.Error("a c < 0 failed", a, c)
  67. }
  68. if c.Cmp(b) <= 0 {
  69. t.Error("c b > 0 failed", c, b)
  70. }
  71. }
  72. func TestMaxArith(t *testing.T) {
  73. a := Uint(0).Add(MaxUint256, One)
  74. if a.Cmp(Zero) != 0 {
  75. t.Error("expected max256 + 1 = 0 got", a)
  76. }
  77. a = Uint(0).Sub(Uint(0), One)
  78. if a.Cmp(MaxUint256) != 0 {
  79. t.Error("expected 0 - 1 = max256 got", a)
  80. }
  81. a = Int(0).Sub(Int(0), One)
  82. if a.Cmp(MinOne) != 0 {
  83. t.Error("expected 0 - 1 = -1 got", a)
  84. }
  85. }
  86. func TestConversion(t *testing.T) {
  87. a := Int(-1)
  88. b := a.Uint256()
  89. if b.Cmp(MaxUint256) != 0 {
  90. t.Error("expected -1 => unsigned to return max. got", b)
  91. }
  92. }