liyujie
2025-08-28 d9927380ed7c8366f762049be9f3fee225860833
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
 
package big
 
import (
   "bytes"
   "encoding/gob"
   "encoding/json"
   "encoding/xml"
   "testing"
)
 
var encodingTests = []string{
   "0",
   "1",
   "2",
   "10",
   "1000",
   "1234567890",
   "298472983472983471903246121093472394872319615612417471234712061",
}
 
func TestIntGobEncoding(t *testing.T) {
   var medium bytes.Buffer
   enc := gob.NewEncoder(&medium)
   dec := gob.NewDecoder(&medium)
   for _, test := range encodingTests {
       for _, sign := range []string{"", "+", "-"} {
           x := sign + test
           medium.Reset() // empty buffer for each test case (in case of failures)
           var tx Int
           tx.SetString(x, 10)
           if err := enc.Encode(&tx); err != nil {
               t.Errorf("encoding of %s failed: %s", &tx, err)
               continue
           }
           var rx Int
           if err := dec.Decode(&rx); err != nil {
               t.Errorf("decoding of %s failed: %s", &tx, err)
               continue
           }
           if rx.Cmp(&tx) != 0 {
               t.Errorf("transmission of %s failed: got %s want %s", &tx, &rx, &tx)
           }
       }
   }
}
 
// Sending a nil Int pointer (inside a slice) on a round trip through gob should yield a zero.
// TODO: top-level nils.
func TestGobEncodingNilIntInSlice(t *testing.T) {
   buf := new(bytes.Buffer)
   enc := gob.NewEncoder(buf)
   dec := gob.NewDecoder(buf)
 
   var in = make([]*Int, 1)
   err := enc.Encode(&in)
   if err != nil {
       t.Errorf("gob encode failed: %q", err)
   }
   var out []*Int
   err = dec.Decode(&out)
   if err != nil {
       t.Fatalf("gob decode failed: %q", err)
   }
   if len(out) != 1 {
       t.Fatalf("wrong len; want 1 got %d", len(out))
   }
   var zero Int
   if out[0].Cmp(&zero) != 0 {
       t.Fatalf("transmission of (*Int)(nil) failed: got %s want 0", out)
   }
}
 
func TestIntJSONEncoding(t *testing.T) {
   for _, test := range encodingTests {
       for _, sign := range []string{"", "+", "-"} {
           x := sign + test
           var tx Int
           tx.SetString(x, 10)
           b, err := json.Marshal(&tx)
           if err != nil {
               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 TestIntXMLEncoding(t *testing.T) {
   for _, test := range encodingTests {
       for _, sign := range []string{"", "+", "-"} {
           x := sign + test
           var tx Int
           tx.SetString(x, 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)
           }
       }
   }
}