Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MalekLahbib committed Oct 4, 2024
1 parent f53dab1 commit be92319
Showing 1 changed file with 115 additions and 1 deletion.
116 changes: 115 additions & 1 deletion gnovm/stdlibs/math/overflow/overflow_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
// sample all possibilities of 8 bit numbers
// by checking against 64 bit numbers

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

for a64 := int64(math.MinInt8); a64 <= int64(math.MaxInt8); a64++ {
Expand Down Expand Up @@ -93,6 +93,92 @@ func TestAlgorithms(t *testing.T) {
}
}

func TestAlgorithmsUint(t *testing.T) {

errors := 0

for a64 := uint64(0); a64 <= uint64(math.MaxUint8); a64++ {

for b64 := uint64(0); b64 <= uint64(math.MaxUint8) && errors < 10; b64++ {

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

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

// ADDITION
{
r64 := a64 + b64

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

// SUBTRACTION
{
r64 := a64 - b64

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

// MULTIPLICATION
{
r64 := a64 * b64

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

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

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

}

func TestQuotient(t *testing.T) {
q, r, ok := Quo(100, 3)
if r != 1 || q != 33 || !ok {
Expand All @@ -101,6 +187,34 @@ func TestQuotient(t *testing.T) {
if _, _, ok = Quo(1, 0); ok {
t.Error("unexpected lack of failure")
}
if _, _, ok = Quo(math.MinInt, -1); ok {
t.Error("unexpected lack of failure")
}
uq, ur, ok := UQuo(100, 3)
if ur != 1 || uq != 33 || !ok {
t.Errorf("expected 100/3 => 33, r=1")
}
if _, _, ok = UQuo(1, 0); ok {
t.Error("unexpected lack of failure")
}
}

func TestAbs(t *testing.T) {
if _, ok := Abs(math.MinInt); ok {
t.Error("unexpected ok, absolute value overflow")
}
if _, ok := Abs64(math.MinInt64); ok {
t.Error("unexpected ok, absolute value overflow")
}
if _, ok := Abs32(math.MinInt32); ok {
t.Error("unexpected ok, absolute value overflow")
}
if _, ok := Abs16(math.MinInt16); ok {
t.Error("unexpected ok, absolute value overflow")
}
if _, ok := Abs8(math.MinInt8); ok {
t.Error("unexpected ok, absolute value overflow")
}
}

func TestLong(t *testing.T) {
Expand Down

0 comments on commit be92319

Please sign in to comment.