integer_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library 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. // The go-ethereum library 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 the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package math
  17. import (
  18. "testing"
  19. )
  20. type operation byte
  21. const (
  22. sub operation = iota
  23. add
  24. mul
  25. )
  26. func TestOverflow(t *testing.T) {
  27. for i, test := range []struct {
  28. x uint64
  29. y uint64
  30. overflow bool
  31. op operation
  32. }{
  33. // add operations
  34. {MaxUint64, 1, true, add},
  35. {MaxUint64 - 1, 1, false, add},
  36. // sub operations
  37. {0, 1, true, sub},
  38. {0, 0, false, sub},
  39. // mul operations
  40. {0, 0, false, mul},
  41. {10, 10, false, mul},
  42. {MaxUint64, 2, true, mul},
  43. {MaxUint64, 1, false, mul},
  44. } {
  45. var overflows bool
  46. switch test.op {
  47. case sub:
  48. _, overflows = SafeSub(test.x, test.y)
  49. case add:
  50. _, overflows = SafeAdd(test.x, test.y)
  51. case mul:
  52. _, overflows = SafeMul(test.x, test.y)
  53. }
  54. if test.overflow != overflows {
  55. t.Errorf("%d failed. Expected test to be %v, got %v", i, test.overflow, overflows)
  56. }
  57. }
  58. }
  59. func TestHexOrDecimal64(t *testing.T) {
  60. tests := []struct {
  61. input string
  62. num uint64
  63. ok bool
  64. }{
  65. {"", 0, true},
  66. {"0", 0, true},
  67. {"0x0", 0, true},
  68. {"12345678", 12345678, true},
  69. {"0x12345678", 0x12345678, true},
  70. {"0X12345678", 0x12345678, true},
  71. // Tests for leading zero behaviour:
  72. {"0123456789", 123456789, true}, // note: not octal
  73. {"0x00", 0, true},
  74. {"0x012345678abc", 0x12345678abc, true},
  75. // Invalid syntax:
  76. {"abcdef", 0, false},
  77. {"0xgg", 0, false},
  78. // Doesn't fit into 64 bits:
  79. {"18446744073709551617", 0, false},
  80. }
  81. for _, test := range tests {
  82. var num HexOrDecimal64
  83. err := num.UnmarshalText([]byte(test.input))
  84. if (err == nil) != test.ok {
  85. t.Errorf("ParseUint64(%q) -> (err == nil) = %t, want %t", test.input, err == nil, test.ok)
  86. continue
  87. }
  88. if err == nil && uint64(num) != test.num {
  89. t.Errorf("ParseUint64(%q) -> %d, want %d", test.input, num, test.num)
  90. }
  91. }
  92. }
  93. func TestMustParseUint64(t *testing.T) {
  94. if v := MustParseUint64("12345"); v != 12345 {
  95. t.Errorf(`MustParseUint64("12345") = %d, want 12345`, v)
  96. }
  97. }
  98. func TestMustParseUint64Panic(t *testing.T) {
  99. defer func() {
  100. if recover() == nil {
  101. t.Error("MustParseBig should've panicked")
  102. }
  103. }()
  104. MustParseUint64("ggg")
  105. }