Skip to content

Commit

Permalink
test: fix issues flagged by deepsource.io + formatting (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman authored Aug 25, 2021
1 parent bb10789 commit 4420c2b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 40 deletions.
62 changes: 41 additions & 21 deletions benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,31 +645,51 @@ func BenchmarkAddMod(b *testing.B) {
residue256Big [len(int256Samples)]big.Int
)

for i := 0; i < len( int32Samples); i++ { residue32Int[i] = *(I.Mod( &int32Samples[i], &int32SamplesLt[i])) }
for i := 0; i < len( big32Samples); i++ { residue32Big[i] = *(B.Mod( &big32Samples[i], &big32SamplesLt[i])) }
for i := 0; i < len(int32Samples); i++ {
residue32Int[i] = *(I.Mod(&int32Samples[i], &int32SamplesLt[i]))
}
for i := 0; i < len(big32Samples); i++ {
residue32Big[i] = *(B.Mod(&big32Samples[i], &big32SamplesLt[i]))
}

for i := 0; i < len( int64Samples); i++ { residue64Int[i] = *(I.Mod( &int64Samples[i], &int64Samples[i])) }
for i := 0; i < len( big64Samples); i++ { residue64Big[i] = *(B.Mod( &big64Samples[i], &big64Samples[i])) }
for i := 0; i < len(int64Samples); i++ {
residue64Int[i] = *(I.Mod(&int64Samples[i], &int64Samples[i]))
}
for i := 0; i < len(big64Samples); i++ {
residue64Big[i] = *(B.Mod(&big64Samples[i], &big64Samples[i]))
}

for i := 0; i < len(int128Samples); i++ { residue128Int[i] = *(I.Mod(&int128Samples[i], &int128Samples[i])) }
for i := 0; i < len(big128Samples); i++ { residue128Big[i] = *(B.Mod(&big128Samples[i], &big128Samples[i])) }
for i := 0; i < len(int128Samples); i++ {
residue128Int[i] = *(I.Mod(&int128Samples[i], &int128Samples[i]))
}
for i := 0; i < len(big128Samples); i++ {
residue128Big[i] = *(B.Mod(&big128Samples[i], &big128Samples[i]))
}

for i := 0; i < len(int192Samples); i++ { residue192Int[i] = *(I.Mod(&int192Samples[i], &int192Samples[i])) }
for i := 0; i < len(big192Samples); i++ { residue192Big[i] = *(B.Mod(&big192Samples[i], &big192Samples[i])) }
for i := 0; i < len(int192Samples); i++ {
residue192Int[i] = *(I.Mod(&int192Samples[i], &int192Samples[i]))
}
for i := 0; i < len(big192Samples); i++ {
residue192Big[i] = *(B.Mod(&big192Samples[i], &big192Samples[i]))
}

for i := 0; i < len(int256Samples); i++ { residue256Int[i] = *(I.Mod(&int256Samples[i], &int256SamplesLt[i])) }
for i := 0; i < len(big256Samples); i++ { residue256Big[i] = *(B.Mod(&big256Samples[i], &big256SamplesLt[i])) }
for i := 0; i < len(int256Samples); i++ {
residue256Int[i] = *(I.Mod(&int256Samples[i], &int256SamplesLt[i]))
}
for i := 0; i < len(big256Samples); i++ {
residue256Big[i] = *(B.Mod(&big256Samples[i], &big256SamplesLt[i]))
}

b.Run("small/uint256", func(b *testing.B) { benchmarkAddModUint256 (b, &residue32Int, &int32SamplesLt) })
b.Run("small/big", func(b *testing.B) { benchmarkAddModBig (b, &residue32Big, &big32SamplesLt) })
b.Run("mod64/uint256", func(b *testing.B) { benchmarkAddModUint256 (b, &residue256Int, &int64Samples) })
b.Run("mod64/big", func(b *testing.B) { benchmarkAddModBig (b, &residue256Big, &big64Samples) })
b.Run("mod128/uint256", func(b *testing.B) { benchmarkAddModUint256 (b, &residue256Int, &int128Samples) })
b.Run("mod128/big", func(b *testing.B) { benchmarkAddModBig (b, &residue256Big, &big128Samples) })
b.Run("mod192/uint256", func(b *testing.B) { benchmarkAddModUint256 (b, &residue256Int, &int192Samples) })
b.Run("mod192/big", func(b *testing.B) { benchmarkAddModBig (b, &residue256Big, &big192Samples) })
b.Run("mod256/uint256", func(b *testing.B) { benchmarkAddModUint256 (b, &residue256Int, &int256SamplesLt) })
b.Run("mod256/big", func(b *testing.B) { benchmarkAddModBig (b, &residue256Big, &big256SamplesLt) })
b.Run("small/uint256", func(b *testing.B) { benchmarkAddModUint256(b, &residue32Int, &int32SamplesLt) })
b.Run("small/big", func(b *testing.B) { benchmarkAddModBig(b, &residue32Big, &big32SamplesLt) })
b.Run("mod64/uint256", func(b *testing.B) { benchmarkAddModUint256(b, &residue256Int, &int64Samples) })
b.Run("mod64/big", func(b *testing.B) { benchmarkAddModBig(b, &residue256Big, &big64Samples) })
b.Run("mod128/uint256", func(b *testing.B) { benchmarkAddModUint256(b, &residue256Int, &int128Samples) })
b.Run("mod128/big", func(b *testing.B) { benchmarkAddModBig(b, &residue256Big, &big128Samples) })
b.Run("mod192/uint256", func(b *testing.B) { benchmarkAddModUint256(b, &residue256Int, &int192Samples) })
b.Run("mod192/big", func(b *testing.B) { benchmarkAddModBig(b, &residue256Big, &big192Samples) })
b.Run("mod256/uint256", func(b *testing.B) { benchmarkAddModUint256(b, &residue256Int, &int256SamplesLt) })
b.Run("mod256/big", func(b *testing.B) { benchmarkAddModBig(b, &residue256Big, &big256SamplesLt) })
}
}

Expand Down Expand Up @@ -764,7 +784,7 @@ func Benchmark_EncodeHex(b *testing.B) {
func Benchmark_DecodeHex(b *testing.B) {

var hexStrings []string
for _, z := range int256Samples {
for _, z := range &int256Samples {
hexStrings = append(hexStrings, (&z).Hex())
}

Expand Down
6 changes: 2 additions & 4 deletions conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,10 +709,8 @@ func TestDecode(t *testing.T) {
}
if err := json.Unmarshal([]byte(`{"Foo":"0x1"}`), &jsonDecoded); err != nil {
t.Fatalf("Expected no error, got %v", err)
} else {
if jsonDecoded.Foo.Uint64() != 1 {
t.Fatal("Expected 1")
}
} else if jsonDecoded.Foo.Uint64() != 1 {
t.Fatal("Expected 1")
}
}

Expand Down
21 changes: 6 additions & 15 deletions uint256_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,6 @@ func checkOverflow(b *big.Int, f *Int, overflow bool) error {
return nil
}

func checkUnderflow(b *big.Int, f *Int, underflow bool) error {
shouldUnderflow := b.Cmp(big.NewInt(0)) < 0
if underflow != shouldUnderflow {
return fmt.Errorf("Undeflow should be %v, was %v\nf= %x\nb= %x\b", shouldUnderflow, underflow, f, b)
}
return nil
}

func randNums() (*big.Int, *Int, error) {
//How many bits? 0-256
nbits, _ := rand.Int(rand.Reader, big.NewInt(256))
Expand All @@ -141,8 +133,7 @@ func randNums() (*big.Int, *Int, error) {
max.Exp(big.NewInt(2), big.NewInt(nbits.Int64()), nil)
b, _ := rand.Int(rand.Reader, max)
f, overflow := FromBig(b)
err := checkOverflow(b, f, overflow)
return b, f, err
return b, f, checkOverflow(b, f, overflow)
}

func randHighNums() (*big.Int, *Int, error) {
Expand All @@ -154,9 +145,7 @@ func randHighNums() (*big.Int, *Int, error) {
//Generate cryptographically strong pseudo-random between 0 - max
b, _ := rand.Int(rand.Reader, max)
f, overflow := FromBig(b)
//fmt.Printf("f %v\n", f.Hex())
err := checkOverflow(b, f, overflow)
return b, f, err
return b, f, checkOverflow(b, f, overflow)
}
func checkEq(b *big.Int, f *Int) bool {
f2, _ := FromBig(b)
Expand Down Expand Up @@ -239,8 +228,10 @@ func TestRandomSubOverflow(t *testing.T) {
f1a, f2a := f1.Clone(), f2.Clone()
_, overflow := f1.SubOverflow(f1, f2)
b.Sub(b, b2)
if err := checkUnderflow(b, f1, overflow); err != nil {
t.Fatal(err)

// check overflow
if have, want := overflow, b.Cmp(big.NewInt(0)) < 0; have != want {
t.Fatalf("underflow should be %v, was %v\nf= %x\nb= %x\b", have, want, f1, b)
}
if eq := checkEq(b, f1); !eq {
t.Fatalf("Expected equality:\nf1= %x\nf2= %x\n[ - ]==\nf= %x\nb= %x\n", f1a, f2a, f1, b)
Expand Down

0 comments on commit 4420c2b

Please sign in to comment.