Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(stdlibs): add math/overflow #1698

Merged
merged 23 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// NOTE: there was a bug with the original Quotient* functions, and
// testing method. These have been fixed here, and tests ported to
// tests/files/maths_int*.go respectively.
// TODO: make PR upstream.
thehowl marked this conversation as resolved.
Show resolved Hide resolved
package maths
// Note: copied over from p/demo/maths & deprecated over there.
thehowl marked this conversation as resolved.
Show resolved Hide resolved
package overflow

import "math"

/*
Package overflow offers overflow-checked integer arithmetic operations
thehowl marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -29,7 +31,7 @@ and new tests. */
//go:generate ./overflow_template.sh

func _is64Bit() bool {
maxU32 := uint(MaxUint32)
maxU32 := uint(math.MaxUint32)
return ((maxU32 << 1) >> 1) == maxU32
}

Expand Down Expand Up @@ -220,7 +222,7 @@ func Div8p(a, b int8) int8 {
func Quo8(a, b int8) (int8, int8, bool) {
if b == 0 {
return 0, 0, false
} else if b == -1 && a == MinInt8 {
} else if b == -1 && a == math.MinInt8 {
return 0, 0, false
}
c := a / b
Expand Down Expand Up @@ -310,7 +312,7 @@ func Div16p(a, b int16) int16 {
func Quo16(a, b int16) (int16, int16, bool) {
if b == 0 {
return 0, 0, false
} else if b == -1 && a == MinInt16 {
} else if b == -1 && a == math.MinInt16 {
return 0, 0, false
}
c := a / b
Expand Down Expand Up @@ -400,7 +402,7 @@ func Div32p(a, b int32) int32 {
func Quo32(a, b int32) (int32, int32, bool) {
if b == 0 {
return 0, 0, false
} else if b == -1 && a == MinInt32 {
} else if b == -1 && a == math.MinInt32 {
return 0, 0, false
}
c := a / b
Expand Down Expand Up @@ -490,7 +492,7 @@ func Div64p(a, b int64) int64 {
func Quo64(a, b int64) (int64, int64, bool) {
if b == 0 {
return 0, 0, false
} else if b == -1 && a == MinInt64 {
} else if b == -1 && a == math.MinInt64 {
return 0, 0, false
}
c := a / b
Expand Down
102 changes: 102 additions & 0 deletions gnovm/stdlibs/math/overflow/overflow_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package overflow

import (
"math"
"testing"
)

// sample all possibilities of 8 bit numbers
// by checking against 64 bit numbers

func TestAlgorithms(t *testing.T) {
errors := 0

for a64 := int64(math.MinInt8); a64 <= int64(math.MaxInt8); a64++ {
for b64 := int64(math.MinInt8); b64 <= int64(math.MaxInt8) && errors < 10; b64++ {

a8 := int8(a64)
b8 := int8(b64)

if int64(a8) != a64 || int64(b8) != b64 {
t.Fatal("LOGIC FAILURE IN TEST")
}

// ADDITION
{
r64 := a64 + b64

// now the verification
result, ok := Add8(a8, b8)
if ok && int64(result) != r64 {
t.Errorf("failed to fail on %v + %v = %v instead of %v\n",
a8, b8, result, r64)
errors++
}
if !ok && int64(result) == r64 {
t.Fail()
errors++
}
}

// SUBTRACTION
{
r64 := a64 - b64

// now the verification
result, ok := Sub8(a8, b8)
if ok && int64(result) != r64 {
t.Errorf("failed to fail on %v - %v = %v instead of %v\n",
a8, b8, result, r64)
}
if !ok && int64(result) == r64 {
t.Fail()
errors++
}
}

// MULTIPLICATION
{
r64 := a64 * b64

// now the verification
result, ok := Mul8(a8, b8)
if ok && int64(result) != r64 {
t.Errorf("failed to fail on %v * %v = %v instead of %v\n",
a8, b8, result, r64)
errors++
}
if !ok && int64(result) == r64 {
t.Fail()
errors++
}
}

// DIVISION
if b8 != 0 {
r64 := a64 / b64

// now the verification
result, _, ok := Quo8(a8, b8)
thehowl marked this conversation as resolved.
Show resolved Hide resolved
if ok && int64(result) != r64 {
t.Errorf("failed to fail on %v / %v = %v instead of %v\n",
a8, b8, result, r64)
errors++
}
if !ok && result != 0 && int64(result) == r64 {
t.Fail()
errors++
}
}
}
}
}

func TestQuotient(t *testing.T) {
q, r, ok := Quo(100, 3)
if r != 1 || q != 33 || !ok {
t.Errorf("expected 100/3 => 33, r=1")
}
if _, _, ok = Quo(1, 0); ok {
t.Error("unexpected lack of failure")
}
}
Loading