Add Safe Sub64 Method (#9993)

* add sub 64

* add sub

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
Raul Jordan
2021-12-08 04:44:54 -05:00
committed by GitHub
parent 424c8f6b46
commit 886332c8fa
2 changed files with 43 additions and 0 deletions

View File

@@ -112,3 +112,12 @@ func Add64(a, b uint64) (uint64, error) {
}
return res, nil
}
// Sub64 subtracts two 64-bit unsigned integers and checks for errors.
func Sub64(a, b uint64) (uint64, error) {
res, borrow := bits.Sub64(a, b, 0 /* borrow */)
if borrow > 0 {
return 0, errors.New("subtraction underflow")
}
return res, nil
}

View File

@@ -332,3 +332,37 @@ func TestAdd64(t *testing.T) {
}
}
}
func TestMath_Sub64(t *testing.T) {
type args struct {
a uint64
b uint64
}
tests := []struct {
args args
res uint64
err bool
}{
{args: args{1, 0}, res: 1},
{args: args{0, 1}, res: 0, err: true},
{args: args{1 << 32, 1}, res: 4294967295},
{args: args{1 << 32, 100}, res: 4294967196},
{args: args{1 << 31, 1 << 31}, res: 0},
{args: args{1 << 63, 1 << 63}, res: 0},
{args: args{1 << 63, 1}, res: 9223372036854775807},
{args: args{stdmath.MaxUint64, stdmath.MaxUint64}, res: 0},
{args: args{stdmath.MaxUint64 - 1, stdmath.MaxUint64}, res: 0, err: true},
{args: args{stdmath.MaxUint64, 0}, res: stdmath.MaxUint64},
{args: args{1 << 63, 2}, res: 9223372036854775806},
}
for _, tt := range tests {
got, err := math.Sub64(tt.args.a, tt.args.b)
if tt.err && err == nil {
t.Errorf("Sub64() Expected Error = %v, want error", tt.err)
continue
}
if tt.res != got {
t.Errorf("Sub64() %v, want %v", got, tt.res)
}
}
}