]> Cypherpunks repositories - gostls13.git/commitdiff
math/big: add SetUint64 and Uint64 functions to *Int
authorLuit van Drongelen <luitvd@gmail.com>
Tue, 11 Dec 2012 17:19:10 +0000 (12:19 -0500)
committerRuss Cox <rsc@golang.org>
Tue, 11 Dec 2012 17:19:10 +0000 (12:19 -0500)
Implementation is mostly identical to passing a non-negative int64 to
SetInt64, and calling Int64 with a non-negative value in the *Int.
Fixes #4389.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/6929048

src/pkg/math/big/int.go
src/pkg/math/big/int_test.go

index 95c0d58ee9798568819a4d92c297b3d9774bd1f6..63a4536e2a0b387f4b77a4f535d92485014bf1a2 100644 (file)
@@ -51,6 +51,13 @@ func (z *Int) SetInt64(x int64) *Int {
        return z
 }
 
+// SetUint64 sets z to x and returns z.
+func (z *Int) SetUint64(x uint64) *Int {
+       z.abs = z.abs.setUint64(uint64(x))
+       z.neg = false
+       return z
+}
+
 // NewInt allocates and returns a new Int set to x.
 func NewInt(x int64) *Int {
        return new(Int).SetInt64(x)
@@ -519,6 +526,19 @@ func (x *Int) Int64() int64 {
        return v
 }
 
+// Uint64 returns the int64 representation of x.
+// If x cannot be represented in an uint64, the result is undefined.
+func (x *Int) Uint64() uint64 {
+       if len(x.abs) == 0 {
+               return 0
+       }
+       v := uint64(x.abs[0])
+       if _W == 32 && len(x.abs) > 1 {
+               v |= uint64(x.abs[1]) << 32
+       }
+       return v
+}
+
 // SetString sets z to the value of s, interpreted in the given base,
 // and returns z and a boolean indicating success. If SetString fails,
 // the value of z is undefined but the returned value is nil.
index d3c5a0e8bfe7481a4dd444d20bad7d49b2b8f54e..fd6d152b39de8516dd40269085bb691b5656aaf5 100644 (file)
@@ -1135,6 +1135,36 @@ func TestInt64(t *testing.T) {
        }
 }
 
+var uint64Tests = []uint64{
+       0,
+       1,
+       4294967295,
+       4294967296,
+       8589934591,
+       8589934592,
+       9223372036854775807,
+       9223372036854775808,
+       18446744073709551615, // 1<<64 - 1
+}
+
+func TestUint64(t *testing.T) {
+       in := new(Int)
+       for i, testVal := range uint64Tests {
+               in.SetUint64(testVal)
+               out := in.Uint64()
+
+               if out != testVal {
+                       t.Errorf("#%d got %d want %d", i, out, testVal)
+               }
+
+               str := fmt.Sprint(testVal)
+               strOut := in.String()
+               if strOut != str {
+                       t.Errorf("#%d.String got %s want %s", i, strOut, str)
+               }
+       }
+}
+
 var bitwiseTests = []struct {
        x, y                 string
        and, or, xor, andNot string