-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix signed saturating math functions
- Loading branch information
1 parent
6f73839
commit 4f72ebb
Showing
3 changed files
with
250 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright 2024, Offchain Labs, Inc. | ||
// For license information, see https://github.com/nitro/blob/master/LICENSE | ||
|
||
package arbmath | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
) | ||
|
||
func toBig[T Signed](a T) *big.Int { | ||
return big.NewInt(int64(a)) | ||
} | ||
|
||
func saturatingBigToInt[T Signed](a *big.Int) T { | ||
// MinIntValue and MaxIntValue are already separately tested | ||
if a.Cmp(toBig(MaxIntValue[T]())) > 0 { | ||
return MaxIntValue[T]() | ||
} | ||
if a.Cmp(toBig(MinIntValue[T]())) < 0 { | ||
return MinIntValue[T]() | ||
} | ||
return T(a.Int64()) | ||
} | ||
|
||
func fuzzSaturatingAdd[T Signed](f *testing.F) { | ||
f.Fuzz(func(t *testing.T, a, b T) { | ||
got := SaturatingAdd(a, b) | ||
expected := saturatingBigToInt[T](new(big.Int).Add(toBig(a), toBig(b))) | ||
if got != expected { | ||
t.Errorf("SaturatingAdd(%v, %v) = %v, expected %v", a, b, got, expected) | ||
} | ||
}) | ||
} | ||
|
||
func fuzzSaturatingMul[T Signed](f *testing.F) { | ||
f.Fuzz(func(t *testing.T, a, b T) { | ||
got := SaturatingMul(a, b) | ||
expected := saturatingBigToInt[T](new(big.Int).Mul(toBig(a), toBig(b))) | ||
if got != expected { | ||
t.Errorf("SaturatingMul(%v, %v) = %v, expected %v", a, b, got, expected) | ||
} | ||
}) | ||
} | ||
|
||
func fuzzSaturatingNeg[T Signed](f *testing.F) { | ||
f.Fuzz(func(t *testing.T, a T) { | ||
got := SaturatingNeg(a) | ||
expected := saturatingBigToInt[T](new(big.Int).Neg(toBig(a))) | ||
if got != expected { | ||
t.Errorf("SaturatingNeg(%v) = %v, expected %v", a, got, expected) | ||
} | ||
}) | ||
} | ||
|
||
func FuzzSaturatingAddInt8(f *testing.F) { | ||
fuzzSaturatingAdd[int8](f) | ||
} | ||
|
||
func FuzzSaturatingAddInt16(f *testing.F) { | ||
fuzzSaturatingAdd[int16](f) | ||
} | ||
|
||
func FuzzSaturatingAddInt32(f *testing.F) { | ||
fuzzSaturatingAdd[int32](f) | ||
} | ||
|
||
func FuzzSaturatingAddInt64(f *testing.F) { | ||
fuzzSaturatingAdd[int64](f) | ||
} | ||
|
||
func FuzzSaturatingSub(f *testing.F) { | ||
f.Fuzz(func(t *testing.T, a, b int64) { | ||
got := SaturatingSub(a, b) | ||
expected := saturatingBigToInt[int64](new(big.Int).Sub(toBig(a), toBig(b))) | ||
if got != expected { | ||
t.Errorf("SaturatingSub(%v, %v) = %v, expected %v", a, b, got, expected) | ||
} | ||
}) | ||
} | ||
|
||
func FuzzSaturatingMulInt8(f *testing.F) { | ||
fuzzSaturatingMul[int8](f) | ||
} | ||
|
||
func FuzzSaturatingMulInt16(f *testing.F) { | ||
fuzzSaturatingMul[int16](f) | ||
} | ||
|
||
func FuzzSaturatingMulInt32(f *testing.F) { | ||
fuzzSaturatingMul[int32](f) | ||
} | ||
|
||
func FuzzSaturatingMulInt64(f *testing.F) { | ||
fuzzSaturatingMul[int64](f) | ||
} | ||
|
||
func FuzzSaturatingNegInt8(f *testing.F) { | ||
fuzzSaturatingNeg[int8](f) | ||
} | ||
|
||
func FuzzSaturatingNegInt16(f *testing.F) { | ||
fuzzSaturatingNeg[int16](f) | ||
} | ||
|
||
func FuzzSaturatingNegInt32(f *testing.F) { | ||
fuzzSaturatingNeg[int32](f) | ||
} | ||
|
||
func FuzzSaturatingNegInt64(f *testing.F) { | ||
fuzzSaturatingNeg[int64](f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters