integer_test.go 875 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package math
  2. import (
  3. gmath "math"
  4. "testing"
  5. )
  6. type operation byte
  7. const (
  8. sub operation = iota
  9. add
  10. mul
  11. )
  12. func TestOverflow(t *testing.T) {
  13. for i, test := range []struct {
  14. x uint64
  15. y uint64
  16. overflow bool
  17. op operation
  18. }{
  19. // add operations
  20. {gmath.MaxUint64, 1, true, add},
  21. {gmath.MaxUint64 - 1, 1, false, add},
  22. // sub operations
  23. {0, 1, true, sub},
  24. {0, 0, false, sub},
  25. // mul operations
  26. {10, 10, false, mul},
  27. {gmath.MaxUint64, 2, true, mul},
  28. {gmath.MaxUint64, 1, false, mul},
  29. } {
  30. var overflows bool
  31. switch test.op {
  32. case sub:
  33. _, overflows = SafeSub(test.x, test.y)
  34. case add:
  35. _, overflows = SafeAdd(test.x, test.y)
  36. case mul:
  37. _, overflows = SafeMul(test.x, test.y)
  38. }
  39. if test.overflow != overflows {
  40. t.Errorf("%d failed. Expected test to be %v, got %v", i, test.overflow, overflows)
  41. }
  42. }
  43. }