Skip to content

Commit

Permalink
overflow checker (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon authored Jan 3, 2025
1 parent e138837 commit f2badf4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions _deploy/p/gnoswap/uint256/arithmetic.gno
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,10 @@ func (z *Uint) isBitSet(n uint) bool {
return (z.arr[n/64] & (1 << (n % 64))) != 0
}

func (z *Uint) IsOverflow() bool {
return z.isBitSet(255)
}

// addTo computes x += y.
// Requires len(x) >= len(y).
func addTo(x, y []uint64) uint64 {
Expand Down
43 changes: 43 additions & 0 deletions _deploy/p/gnoswap/uint256/arithmetic_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,49 @@ type binOp2Test struct {
x, y, want string
}

func TestIsOverflow(t *testing.T) {
tests := []struct {
name string
input *Uint
expected bool
}{
{
name: "Number greater than max value",
input: &Uint{arr: [4]uint64{
^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0),
}},
expected: true,
},
{
name: "Max value",
input: &Uint{arr: [4]uint64{
^uint64(0), ^uint64(0), ^uint64(0), ^uint64(0) >> 1,
}},
expected: false,
},
{
name: "0",
input: &Uint{arr: [4]uint64{0, 0, 0, 0}},
expected: false,
},
{
name: "Only 255th bit set",
input: &Uint{arr: [4]uint64{
0, 0, 0, uint64(1) << 63,
}},
expected: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.input.IsOverflow(); got != tt.expected {
t.Errorf("IsOverflow() = %v, expected %v", got, tt.expected)
}
})
}
}

func TestAdd(t *testing.T) {
tests := []binOp2Test{
{"0", "1", "1"},
Expand Down

0 comments on commit f2badf4

Please sign in to comment.