Skip to content

Commit

Permalink
added BigMin, BigMax utils
Browse files Browse the repository at this point in the history
  • Loading branch information
lmittmann committed Dec 20, 2024
1 parent f0d8138 commit c4e9410
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
16 changes: 16 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,19 @@ func FromWei(wei *big.Int, decimals uint8) string {
func has0xPrefix(hexStr string) bool {
return len(hexStr) >= 2 && hexStr[0] == '0' && (hexStr[1] == 'x' || hexStr[1] == 'X')
}

// BigMin returns the smaller of the two big integers.
func BigMin(a, b *big.Int) *big.Int {
if a.Cmp(b) < 0 {
return a
}
return b
}

// BigMax returns the larger of the two big integers.
func BigMax(a, b *big.Int) *big.Int {
if a.Cmp(b) > 0 {
return a
}
return b
}
38 changes: 38 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,41 @@ func TestFromWei(t *testing.T) {
})
}
}

func TestBigMin(t *testing.T) {
tests := []struct {
A, B, Want *big.Int
}{
{big.NewInt(0), big.NewInt(0), big.NewInt(0)},
{big.NewInt(0), big.NewInt(1), big.NewInt(0)},
{big.NewInt(1), big.NewInt(0), big.NewInt(0)},
}

for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
got := w3.BigMin(test.A, test.B)
if test.Want.Cmp(got) != 0 {
t.Fatalf("want: %v, got: %v", test.Want, got)
}
})
}
}

func TestBigMax(t *testing.T) {
tests := []struct {
A, B, Want *big.Int
}{
{big.NewInt(0), big.NewInt(0), big.NewInt(0)},
{big.NewInt(0), big.NewInt(1), big.NewInt(1)},
{big.NewInt(1), big.NewInt(0), big.NewInt(1)},
}

for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
got := w3.BigMax(test.A, test.B)
if test.Want.Cmp(got) != 0 {
t.Fatalf("want: %v, got: %v", test.Want, got)
}
})
}
}

0 comments on commit c4e9410

Please sign in to comment.