Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
343 changes: 156 additions & 187 deletions contract/p/gnoswap/fuzz/int256.gno

Large diffs are not rendered by default.

59 changes: 59 additions & 0 deletions contract/p/gnoswap/fuzz/int256_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,62 @@ func TestInt256_Int256RangePositiveOnly(t *testing.T) {
}
})
}

func TestUint256Range_PreservesSwapBounds(t *testing.T) {
const (
minPrice = "4295128740"
maxPrice = "1461446703485210103287273052203988822378723970341"
)

gen := Uint256Range(minPrice, maxPrice)
impl, ok := gen.impl.(*uint256Gen)
if !ok {
t.Fatalf("unexpected generator implementation: %T", gen.impl)
}

minBefore := impl.min.ToString()
maxBefore := impl.max.ToString()

CheckN(t, 5, func(ft *T) {
val := gen.Draw(ft, "priceLimit").(*uint256.Uint)

if val.Cmp(impl.min) < 0 || val.Cmp(impl.max) > 0 {
ft.Fatalf("value %s out of range [%s, %s]", val.ToString(), impl.min.ToString(), impl.max.ToString())
}
if val == impl.min || val == impl.max {
ft.Fatalf("generator returned internal bound pointer")
}
})

if impl.min.ToString() != minBefore || impl.max.ToString() != maxBefore {
t.Fatalf("generator bounds mutated during draws")
}
}

func TestInt256Range_SingleValueReturnsClone(t *testing.T) {
const value = "-12345678901234567890"

gen := Int256Range(value, value)
impl, ok := gen.impl.(*int256Gen)
if !ok {
t.Fatalf("unexpected generator implementation: %T", gen.impl)
}

minBefore := impl.min.ToString()
maxBefore := impl.max.ToString()

CheckN(t, 3, func(ft *T) {
val := gen.Draw(ft, "singleVal").(*int256.Int)

if val.ToString() != value {
ft.Fatalf("value %s differs from expected %s", val.ToString(), value)
}
if val == impl.min || val == impl.max {
ft.Fatalf("generator returned internal bound pointer")
}
})

if impl.min.ToString() != minBefore || impl.max.ToString() != maxBefore {
t.Fatalf("generator bounds mutated during draws")
}
}
16 changes: 16 additions & 0 deletions contract/p/gnoswap/fuzzutils/runner.gno
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
"gno.land/p/nt/ufmt"
)

func RunFuzzTest(t *testing.T, iterations int, fn func(*fuzz.T, *Result, int), options ...any) {

Check failure on line 11 in contract/p/gnoswap/fuzzutils/runner.gno

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 23 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=gnoswap-labs_gnoswap&issues=AZrZJF09gd24eZsKAN81&open=AZrZJF09gd24eZsKAN81&pullRequest=993
result := NewFuzzResult(fuzz.BASE_SEED)
isHandlePanic := true
isPrintLog := true
index := 0

if len(options) > 0 {
Expand All @@ -20,6 +21,13 @@
}
}

if len(options) > 1 {
isPrintLogOption, ok := options[1].(bool)
if ok {
isPrintLog = isPrintLogOption
}
}

// Use CheckN instead of CheckWithConfig
fuzz.CheckN(t, iterations, func(ft *fuzz.T) {
index++
Expand All @@ -45,6 +53,14 @@
fn(ft, result, index)
}

if isPrintLog {
if isError {
ufmt.Printf("[LOG-%d] %s (error: %s)\n", index, result.params[index].ToString(), errorMessage)
} else {
ufmt.Printf("[LOG-%d] %s (valid)\n", index, result.params[index].ToString())
}
}

if isError {
message := strings.TrimSpace(errorMessage)
result.AddErrorMessage(index, message)
Expand Down
Loading
Loading