Commit 18563f8a authored by Robert Griesemer's avatar Robert Griesemer

math/big: clean up *Int encoding tests

- more uniform naming
- test sign more deliberately
- remove superfluous test (JSON encoder always uses the JSON marshaler if present)

Change-Id: I37b1e367c01fc8bae1e06adbdb72dd366c08d5ce
Reviewed-on: https://go-review.googlesource.com/15110Reviewed-by: default avatarAlan Donovan <adonovan@google.com>
parent 38c5fd5c
...@@ -59,6 +59,10 @@ func (z *Int) UnmarshalText(text []byte) error { ...@@ -59,6 +59,10 @@ func (z *Int) UnmarshalText(text []byte) error {
return nil return nil
} }
// The JSON marshallers are only here for API backward compatibility
// (programs that explicitly look for these two methods). JSON works
// fine with the TextMarshaler only.
// MarshalJSON implements the json.Marshaler interface. // MarshalJSON implements the json.Marshaler interface.
func (x *Int) MarshalJSON() ([]byte, error) { func (x *Int) MarshalJSON() ([]byte, error) {
return x.MarshalText() return x.MarshalText()
......
...@@ -13,16 +13,11 @@ import ( ...@@ -13,16 +13,11 @@ import (
) )
var encodingTests = []string{ var encodingTests = []string{
"-539345864568634858364538753846587364875430589374589",
"-678645873",
"-100",
"-2",
"-1",
"0", "0",
"1", "1",
"2", "2",
"10", "10",
"42", "1000",
"1234567890", "1234567890",
"298472983472983471903246121093472394872319615612417471234712061", "298472983472983471903246121093472394872319615612417471234712061",
} }
...@@ -32,20 +27,23 @@ func TestIntGobEncoding(t *testing.T) { ...@@ -32,20 +27,23 @@ func TestIntGobEncoding(t *testing.T) {
enc := gob.NewEncoder(&medium) enc := gob.NewEncoder(&medium)
dec := gob.NewDecoder(&medium) dec := gob.NewDecoder(&medium)
for _, test := range encodingTests { for _, test := range encodingTests {
medium.Reset() // empty buffer for each test case (in case of failures) for _, sign := range []string{"", "+", "-"} {
var tx Int x := sign + test
tx.SetString(test, 10) medium.Reset() // empty buffer for each test case (in case of failures)
if err := enc.Encode(&tx); err != nil { var tx Int
t.Errorf("encoding of %s failed: %s", &tx, err) tx.SetString(x, 10)
continue if err := enc.Encode(&tx); err != nil {
} t.Errorf("encoding of %s failed: %s", &tx, err)
var rx Int continue
if err := dec.Decode(&rx); err != nil { }
t.Errorf("decoding of %s failed: %s", &tx, err) var rx Int
continue if err := dec.Decode(&rx); err != nil {
} t.Errorf("decoding of %s failed: %s", &tx, err)
if rx.Cmp(&tx) != 0 { continue
t.Errorf("transmission of %s failed: got %s want %s", &tx, &rx, &tx) }
if rx.Cmp(&tx) != 0 {
t.Errorf("transmission of %s failed: got %s want %s", &tx, &rx, &tx)
}
} }
} }
} }
...@@ -78,72 +76,46 @@ func TestGobEncodingNilIntInSlice(t *testing.T) { ...@@ -78,72 +76,46 @@ func TestGobEncodingNilIntInSlice(t *testing.T) {
func TestIntJSONEncoding(t *testing.T) { func TestIntJSONEncoding(t *testing.T) {
for _, test := range encodingTests { for _, test := range encodingTests {
var tx Int for _, sign := range []string{"", "+", "-"} {
tx.SetString(test, 10) x := sign + test
b, err := json.Marshal(&tx) var tx Int
if err != nil { tx.SetString(x, 10)
t.Errorf("marshaling of %s failed: %s", &tx, err) b, err := json.Marshal(&tx)
continue if err != nil {
} t.Errorf("marshaling of %s failed: %s", &tx, err)
var rx Int continue
if err := json.Unmarshal(b, &rx); err != nil { }
t.Errorf("unmarshaling of %s failed: %s", &tx, err) var rx Int
continue if err := json.Unmarshal(b, &rx); err != nil {
} t.Errorf("unmarshaling of %s failed: %s", &tx, err)
if rx.Cmp(&tx) != 0 { continue
t.Errorf("JSON encoding of %s failed: got %s want %s", &tx, &rx, &tx) }
if rx.Cmp(&tx) != 0 {
t.Errorf("JSON encoding of %s failed: got %s want %s", &tx, &rx, &tx)
}
} }
} }
} }
var intVals = []string{ func TestIntXMLEncoding(t *testing.T) {
"-141592653589793238462643383279502884197169399375105820974944592307816406286", for _, test := range encodingTests {
"-1415926535897932384626433832795028841971", for _, sign := range []string{"", "+", "-"} {
"-141592653589793", x := sign + test
"-1", var tx Int
"0", tx.SetString(x, 0)
"1", b, err := xml.Marshal(&tx)
"141592653589793", if err != nil {
"1415926535897932384626433832795028841971", t.Errorf("marshaling of %s failed: %s", &tx, err)
"141592653589793238462643383279502884197169399375105820974944592307816406286", continue
} }
var rx Int
func TestIntJSONEncodingTextMarshaller(t *testing.T) { if err := xml.Unmarshal(b, &rx); err != nil {
for _, num := range intVals { t.Errorf("unmarshaling of %s failed: %s", &tx, err)
var tx Int continue
tx.SetString(num, 0) }
b, err := json.Marshal(&tx) if rx.Cmp(&tx) != 0 {
if err != nil { t.Errorf("XML encoding of %s failed: got %s want %s", &tx, &rx, &tx)
t.Errorf("marshaling of %s failed: %s", &tx, err) }
continue
}
var rx Int
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 TestIntXMLEncodingTextMarshaller(t *testing.T) {
for _, num := range intVals {
var tx Int
tx.SetString(num, 0)
b, err := xml.Marshal(&tx)
if err != nil {
t.Errorf("marshaling of %s failed: %s", &tx, err)
continue
}
var rx Int
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)
} }
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment