]> Cypherpunks repositories - gostls13.git/commitdiff
math/big: implement Rat.SetUint64
authorBrian Kessler <brian.m.kessler@gmail.com>
Fri, 1 Feb 2019 05:28:21 +0000 (22:28 -0700)
committerRobert Griesemer <gri@golang.org>
Wed, 27 Mar 2019 15:20:28 +0000 (15:20 +0000)
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>
src/math/big/rat.go
src/math/big/rat_test.go

index 5d0800ca936b2952016f60b90bcd760a674d6c6a..675889f33b18f1f254151288b555d56976c3a206 100644 (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 {
index afda68658f7c17e36c3357c57d8673429d686cf0..b169477e239e012e546d43ce5aab71caba0a02b6 100644 (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)
+               }
+       }
+}