1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 var encodingTests = []string{
22 "298472983472983471903246121093472394872319615612417471234712061",
25 func TestIntGobEncoding(t *testing.T) {
26 var medium bytes.Buffer
27 enc := gob.NewEncoder(&medium)
28 dec := gob.NewDecoder(&medium)
29 for _, test := range encodingTests {
30 for _, sign := range []string{"", "+", "-"} {
32 medium.Reset() // empty buffer for each test case (in case of failures)
35 if err := enc.Encode(&tx); err != nil {
36 t.Errorf("encoding of %s failed: %s", &tx, err)
40 if err := dec.Decode(&rx); err != nil {
41 t.Errorf("decoding of %s failed: %s", &tx, err)
45 t.Errorf("transmission of %s failed: got %s want %s", &tx, &rx, &tx)
51 // Sending a nil Int pointer (inside a slice) on a round trip through gob should yield a zero.
52 // TODO: top-level nils.
53 func TestGobEncodingNilIntInSlice(t *testing.T) {
54 buf := new(bytes.Buffer)
55 enc := gob.NewEncoder(buf)
56 dec := gob.NewDecoder(buf)
58 var in = make([]*Int, 1)
59 err := enc.Encode(&in)
61 t.Errorf("gob encode failed: %q", err)
64 err = dec.Decode(&out)
66 t.Fatalf("gob decode failed: %q", err)
69 t.Fatalf("wrong len; want 1 got %d", len(out))
72 if out[0].Cmp(&zero) != 0 {
73 t.Fatalf("transmission of (*Int)(nil) failed: got %s want 0", out)
77 func TestIntJSONEncoding(t *testing.T) {
78 for _, test := range encodingTests {
79 for _, sign := range []string{"", "+", "-"} {
83 b, err := json.Marshal(&tx)
85 t.Errorf("marshaling of %s failed: %s", &tx, err)
89 if err := json.Unmarshal(b, &rx); err != nil {
90 t.Errorf("unmarshaling of %s failed: %s", &tx, err)
94 t.Errorf("JSON encoding of %s failed: got %s want %s", &tx, &rx, &tx)
100 func TestIntJSONEncodingNil(t *testing.T) {
102 b, err := x.MarshalJSON()
104 t.Fatalf("marshaling of nil failed: %s", err)
109 t.Fatalf("marshaling of nil failed: got %s want %s", got, want)
113 func TestIntXMLEncoding(t *testing.T) {
114 for _, test := range encodingTests {
115 for _, sign := range []string{"", "+", "-"} {
119 b, err := xml.Marshal(&tx)
121 t.Errorf("marshaling of %s failed: %s", &tx, err)
125 if err := xml.Unmarshal(b, &rx); err != nil {
126 t.Errorf("unmarshaling of %s failed: %s", &tx, err)
129 if rx.Cmp(&tx) != 0 {
130 t.Errorf("XML encoding of %s failed: got %s want %s", &tx, &rx, &tx)