math/big: implement Rat.SetUint64

Implemented via the underlying Int.SetUint64.
Added tests for Rat.SetInt64 and Rat.SetUint64.

Fixes #29579

Change-Id: I03faaffc93e36873b202b58ae72b139dea5c40f9
Reviewed-on: https://go-review.googlesource.com/c/go/+/160682
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Brian Kessler 2019-01-31 22:28:21 -07:00 committed by Robert Griesemer
parent 90a3ce02dc
commit 5ee2290420
2 changed files with 58 additions and 0 deletions

View File

@ -339,6 +339,13 @@ func (z *Rat) SetInt64(x int64) *Rat {
return z
}
// SetUint64 sets z to x and returns z.
func (z *Rat) SetUint64(x uint64) *Rat {
z.a.SetUint64(x)
z.b.abs = z.b.abs[:0]
return z
}
// Set sets z to x (by making a copy of x) and returns z.
func (z *Rat) Set(x *Rat) *Rat {
if z != x {

View File

@ -620,3 +620,54 @@ func TestIsFinite(t *testing.T) {
}
}
}
func TestRatSetInt64(t *testing.T) {
var testCases = []int64{
0,
1,
-1,
12345,
-98765,
math.MaxInt64,
math.MinInt64,
}
var r = new(Rat)
for i, want := range testCases {
r.SetInt64(want)
if !r.IsInt() {
t.Errorf("#%d: Rat.SetInt64(%d) is not an integer", i, want)
}
num := r.Num()
if !num.IsInt64() {
t.Errorf("#%d: Rat.SetInt64(%d) numerator is not an int64", i, want)
}
got := num.Int64()
if got != want {
t.Errorf("#%d: Rat.SetInt64(%d) = %d, but expected %d", i, want, got, want)
}
}
}
func TestRatSetUint64(t *testing.T) {
var testCases = []uint64{
0,
1,
12345,
^uint64(0),
}
var r = new(Rat)
for i, want := range testCases {
r.SetUint64(want)
if !r.IsInt() {
t.Errorf("#%d: Rat.SetUint64(%d) is not an integer", i, want)
}
num := r.Num()
if !num.IsUint64() {
t.Errorf("#%d: Rat.SetUint64(%d) numerator is not a uint64", i, want)
}
got := num.Uint64()
if got != want {
t.Errorf("#%d: Rat.SetUint64(%d) = %d, but expected %d", i, want, got, want)
}
}
}