value_test.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2014 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 common
  17. import (
  18. "math/big"
  19. checker "gopkg.in/check.v1"
  20. )
  21. type ValueSuite struct{}
  22. var _ = checker.Suite(&ValueSuite{})
  23. func (s *ValueSuite) TestValueCmp(c *checker.C) {
  24. val1 := NewValue("hello")
  25. val2 := NewValue("world")
  26. c.Assert(val1.Cmp(val2), checker.Equals, false)
  27. val3 := NewValue("hello")
  28. val4 := NewValue("hello")
  29. c.Assert(val3.Cmp(val4), checker.Equals, true)
  30. }
  31. func (s *ValueSuite) TestValueTypes(c *checker.C) {
  32. str := NewValue("str")
  33. num := NewValue(1)
  34. inter := NewValue([]interface{}{1})
  35. byt := NewValue([]byte{1, 2, 3, 4})
  36. bigInt := NewValue(big.NewInt(10))
  37. strExp := "str"
  38. numExp := uint64(1)
  39. interExp := []interface{}{1}
  40. bytExp := []byte{1, 2, 3, 4}
  41. bigExp := big.NewInt(10)
  42. c.Assert(str.Str(), checker.Equals, strExp)
  43. c.Assert(num.Uint(), checker.Equals, numExp)
  44. c.Assert(NewValue(inter.Val).Cmp(NewValue(interExp)), checker.Equals, true)
  45. c.Assert(byt.Bytes(), checker.DeepEquals, bytExp)
  46. c.Assert(bigInt.BigInt(), checker.DeepEquals, bigExp)
  47. }
  48. func (s *ValueSuite) TestIterator(c *checker.C) {
  49. value := NewValue([]interface{}{1, 2, 3})
  50. iter := value.NewIterator()
  51. values := []uint64{1, 2, 3}
  52. i := 0
  53. for iter.Next() {
  54. c.Assert(values[i], checker.Equals, iter.Value().Uint())
  55. i++
  56. }
  57. }
  58. func (s *ValueSuite) TestMath(c *checker.C) {
  59. data1 := NewValue(1)
  60. data1.Add(1).Add(1)
  61. exp1 := NewValue(3)
  62. data2 := NewValue(2)
  63. data2.Sub(1).Sub(1)
  64. exp2 := NewValue(0)
  65. c.Assert(data1.DeepCmp(exp1), checker.Equals, true)
  66. c.Assert(data2.DeepCmp(exp2), checker.Equals, true)
  67. }
  68. func (s *ValueSuite) TestString(c *checker.C) {
  69. data := "10"
  70. exp := int64(10)
  71. c.Assert(NewValue(data).Int(), checker.DeepEquals, exp)
  72. }