diff options
| author | Michael T. Jones <mtj@google.com> | 2014-02-13 08:42:19 -0800 |
|---|---|---|
| committer | Robert Griesemer <gri@golang.org> | 2014-02-13 08:42:19 -0800 |
| commit | eea28f6701ac888d5613470a88d09b634efc1d75 (patch) | |
| tree | e0ef06bb8b8d8a15b6932ef171cb4897dc911d92 /src | |
| parent | e0dee49688c1c767e2f81a4dab824ba0be36dcbb (diff) | |
| download | go-eea28f6701ac888d5613470a88d09b634efc1d75.tar.xz | |
math/big: add support for general encoding interfaces
TextMarshaller and TextUnmarshaller to ease transport of
unlimited precision rational numbers.
Fixes #7287.
Consists of encode and decode functions and two test
functions, one using JSON and one using XML. Each
verifies round trips for integers (rationals with
denominator == 1) and for fractional vaues.
LGTM=gri
R=gri, cookieo9, bradfitz, mtj
CC=golang-codereviews
https://golang.org/cl/61180043
Diffstat (limited to 'src')
| -rw-r--r-- | src/pkg/math/big/rat.go | 13 | ||||
| -rw-r--r-- | src/pkg/math/big/rat_test.go | 65 |
2 files changed, 78 insertions, 0 deletions
diff --git a/src/pkg/math/big/rat.go b/src/pkg/math/big/rat.go index 7faee61a46..3cdb1d807f 100644 --- a/src/pkg/math/big/rat.go +++ b/src/pkg/math/big/rat.go @@ -585,3 +585,16 @@ func (z *Rat) GobDecode(buf []byte) error { z.b.abs = z.b.abs.setBytes(buf[i:]) return nil } + +// MarshalText implements the encoding.TextMarshaler interface +func (r *Rat) MarshalText() (text []byte, err error) { + return []byte(r.RatString()), nil +} + +// UnmarshalText implements the encoding.TextUnmarshaler interface +func (r *Rat) UnmarshalText(text []byte) error { + if _, ok := r.SetString(string(text)); !ok { + return fmt.Errorf("math/big: cannot unmarshal %q into a *big.Rat", text) + } + return nil +} diff --git a/src/pkg/math/big/rat_test.go b/src/pkg/math/big/rat_test.go index 0d432637ba..414a67d419 100644 --- a/src/pkg/math/big/rat_test.go +++ b/src/pkg/math/big/rat_test.go @@ -7,6 +7,8 @@ package big import ( "bytes" "encoding/gob" + "encoding/json" + "encoding/xml" "fmt" "math" "strconv" @@ -433,6 +435,69 @@ func TestGobEncodingNilRatInSlice(t *testing.T) { } } +var ratNums = []string{ + "-141592653589793238462643383279502884197169399375105820974944592307816406286", + "-1415926535897932384626433832795028841971", + "-141592653589793", + "-1", + "0", + "1", + "141592653589793", + "1415926535897932384626433832795028841971", + "141592653589793238462643383279502884197169399375105820974944592307816406286", +} + +var ratDenoms = []string{ + "1", + "718281828459045", + "7182818284590452353602874713526624977572", + "718281828459045235360287471352662497757247093699959574966967627724076630353", +} + +func TestRatJSONEncoding(t *testing.T) { + for _, num := range ratNums { + for _, denom := range ratDenoms { + var tx Rat + tx.SetString(num + "/" + denom) + b, err := json.Marshal(&tx) + if err != nil { + t.Errorf("marshaling of %s failed: %s", &tx, err) + continue + } + var rx Rat + if err := json.Unmarshal(b, &rx); err != nil { + t.Errorf("unmarshaling of %s failed: %s", &tx, err) + continue + } + if rx.Cmp(&tx) != 0 { + t.Errorf("JSON encoding of %s failed: got %s want %s", &tx, &rx, &tx) + } + } + } +} + +func TestRatXMLEncoding(t *testing.T) { + for _, num := range ratNums { + for _, denom := range ratDenoms { + var tx Rat + tx.SetString(num + "/" + denom) + b, err := xml.Marshal(&tx) + if err != nil { + t.Errorf("marshaling of %s failed: %s", &tx, err) + continue + } + var rx Rat + if err := xml.Unmarshal(b, &rx); err != nil { + t.Errorf("unmarshaling of %s failed: %s", &tx, err) + continue + } + if rx.Cmp(&tx) != 0 { + t.Errorf("XML encoding of %s failed: got %s want %s", &tx, &rx, &tx) + } + } + } +} + func TestIssue2379(t *testing.T) { // 1) no aliasing q := NewRat(3, 2) |
