Skip to content

Commit 2e39f39

Browse files
increase code coverage in common pkg
1 parent c900f0a commit 2e39f39

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

common/common_test.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,3 +512,112 @@ func TestParseUint64HexOrDecimal(t *testing.T) {
512512
})
513513
}
514514
}
515+
516+
func TestSafeUint64(t *testing.T) {
517+
tests := []struct {
518+
name string
519+
input *big.Int
520+
want uint64
521+
expectErr bool
522+
}{
523+
{
524+
name: "nil value",
525+
input: nil,
526+
expectErr: true,
527+
},
528+
{
529+
name: "zero",
530+
input: big.NewInt(0),
531+
want: 0,
532+
expectErr: false,
533+
},
534+
{
535+
name: "small positive number",
536+
input: big.NewInt(42),
537+
want: 42,
538+
expectErr: false,
539+
},
540+
{
541+
name: "max uint64",
542+
input: new(big.Int).SetUint64(math.MaxUint64),
543+
want: math.MaxUint64,
544+
expectErr: false,
545+
},
546+
{
547+
name: "negative value",
548+
input: big.NewInt(-1),
549+
expectErr: true,
550+
},
551+
{
552+
name: "overflow uint64",
553+
input: new(big.Int).Add(new(big.Int).SetUint64(math.MaxUint64), big.NewInt(1)),
554+
expectErr: true,
555+
},
556+
}
557+
558+
for _, tt := range tests {
559+
t.Run(tt.name, func(t *testing.T) {
560+
got, err := SafeUint64(tt.input)
561+
562+
if tt.expectErr {
563+
require.Error(t, err)
564+
return
565+
}
566+
567+
require.NoError(t, err)
568+
require.Equal(t, tt.want, got)
569+
})
570+
}
571+
}
572+
573+
func TestSafeUint32(t *testing.T) {
574+
tests := []struct {
575+
name string
576+
input uint64
577+
want uint32
578+
expectErr bool
579+
}{
580+
{
581+
name: "zero",
582+
input: 0,
583+
want: 0,
584+
expectErr: false,
585+
},
586+
{
587+
name: "small value",
588+
input: 123,
589+
want: 123,
590+
expectErr: false,
591+
},
592+
{
593+
name: "max uint32",
594+
input: math.MaxUint32,
595+
want: math.MaxUint32,
596+
expectErr: false,
597+
},
598+
{
599+
name: "just above max uint32",
600+
input: uint64(math.MaxUint32) + 1,
601+
expectErr: true,
602+
},
603+
{
604+
name: "max uint64",
605+
input: math.MaxUint64,
606+
expectErr: true,
607+
},
608+
}
609+
610+
for _, tt := range tests {
611+
t.Run(tt.name, func(t *testing.T) {
612+
got, err := SafeUint32(tt.input)
613+
614+
if tt.expectErr {
615+
require.Error(t, err)
616+
return
617+
}
618+
619+
require.NoError(t, err)
620+
require.Equal(t, tt.want, got)
621+
})
622+
}
623+
}

0 commit comments

Comments
 (0)