Source file
src/math/big/intmarsh_test.go
1
2
3
4
5 package big
6
7 import (
8 "bytes"
9 "encoding/gob"
10 "encoding/json"
11 "encoding/xml"
12 "testing"
13 )
14
15 var encodingTests = []string{
16 "0",
17 "1",
18 "2",
19 "10",
20 "1000",
21 "1234567890",
22 "298472983472983471903246121093472394872319615612417471234712061",
23 }
24
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{"", "+", "-"} {
31 x := sign + test
32 medium.Reset()
33 var tx Int
34 tx.SetString(x, 10)
35 if err := enc.Encode(&tx); err != nil {
36 t.Errorf("encoding of %s failed: %s", &tx, err)
37 continue
38 }
39 var rx Int
40 if err := dec.Decode(&rx); err != nil {
41 t.Errorf("decoding of %s failed: %s", &tx, err)
42 continue
43 }
44 if rx.Cmp(&tx) != 0 {
45 t.Errorf("transmission of %s failed: got %s want %s", &tx, &rx, &tx)
46 }
47 }
48 }
49 }
50
51
52
53 func TestGobEncodingNilIntInSlice(t *testing.T) {
54 buf := new(bytes.Buffer)
55 enc := gob.NewEncoder(buf)
56 dec := gob.NewDecoder(buf)
57
58 var in = make([]*Int, 1)
59 err := enc.Encode(&in)
60 if err != nil {
61 t.Errorf("gob encode failed: %q", err)
62 }
63 var out []*Int
64 err = dec.Decode(&out)
65 if err != nil {
66 t.Fatalf("gob decode failed: %q", err)
67 }
68 if len(out) != 1 {
69 t.Fatalf("wrong len; want 1 got %d", len(out))
70 }
71 var zero Int
72 if out[0].Cmp(&zero) != 0 {
73 t.Fatalf("transmission of (*Int)(nil) failed: got %s want 0", out)
74 }
75 }
76
77 func TestIntJSONEncoding(t *testing.T) {
78 for _, test := range encodingTests {
79 for _, sign := range []string{"", "+", "-"} {
80 x := sign + test
81 var tx Int
82 tx.SetString(x, 10)
83 b, err := json.Marshal(&tx)
84 if err != nil {
85 t.Errorf("marshaling of %s failed: %s", &tx, err)
86 continue
87 }
88 var rx Int
89 if err := json.Unmarshal(b, &rx); err != nil {
90 t.Errorf("unmarshaling of %s failed: %s", &tx, err)
91 continue
92 }
93 if rx.Cmp(&tx) != 0 {
94 t.Errorf("JSON encoding of %s failed: got %s want %s", &tx, &rx, &tx)
95 }
96 }
97 }
98 }
99
100 func TestIntJSONEncodingNil(t *testing.T) {
101 var x *Int
102 b, err := x.MarshalJSON()
103 if err != nil {
104 t.Fatalf("marshaling of nil failed: %s", err)
105 }
106 got := string(b)
107 want := "null"
108 if got != want {
109 t.Fatalf("marshaling of nil failed: got %s want %s", got, want)
110 }
111 }
112
113 func TestIntXMLEncoding(t *testing.T) {
114 for _, test := range encodingTests {
115 for _, sign := range []string{"", "+", "-"} {
116 x := sign + test
117 var tx Int
118 tx.SetString(x, 0)
119 b, err := xml.Marshal(&tx)
120 if err != nil {
121 t.Errorf("marshaling of %s failed: %s", &tx, err)
122 continue
123 }
124 var rx Int
125 if err := xml.Unmarshal(b, &rx); err != nil {
126 t.Errorf("unmarshaling of %s failed: %s", &tx, err)
127 continue
128 }
129 if rx.Cmp(&tx) != 0 {
130 t.Errorf("XML encoding of %s failed: got %s want %s", &tx, &rx, &tx)
131 }
132 }
133 }
134 }
135
136 func TestIntAppendText(t *testing.T) {
137 for _, test := range encodingTests {
138 for _, sign := range []string{"", "+", "-"} {
139 x := sign + test
140 var tx Int
141 tx.SetString(x, 10)
142 buf := make([]byte, 4, 32)
143 b, err := tx.AppendText(buf)
144 if err != nil {
145 t.Errorf("marshaling of %s failed: %s", &tx, err)
146 continue
147 }
148 var rx Int
149 if err := rx.UnmarshalText(b[4:]); err != nil {
150 t.Errorf("unmarshaling of %s failed: %s", &tx, err)
151 continue
152 }
153 if rx.Cmp(&tx) != 0 {
154 t.Errorf("AppendText of %s failed: got %s want %s", &tx, &rx, &tx)
155 }
156 }
157 }
158 }
159
160 func TestIntAppendTextNil(t *testing.T) {
161 var x *Int
162 buf := make([]byte, 4, 16)
163 data, _ := x.AppendText(buf)
164 if string(data[4:]) != "<nil>" {
165 t.Errorf("got %q, want <nil>", data[4:])
166 }
167 }
168
View as plain text