blob: 058dd96292cdde4e2a4512e6ee222042824dc8bf [file] [log] [blame]
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -07001// Copyright 2009 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.
4
5package big
6
Adam Langley65063bc2009-11-05 15:55:41 -08007import (
Robert Griesemer5a1d3322009-12-15 15:33:31 -08008 "bytes"
Rob Pike45e3bcb2011-11-08 15:41:54 -08009 "encoding/gob"
Robert Griesemer5a1d3322009-12-15 15:33:31 -080010 "encoding/hex"
Robert Griesemer13a59b82012-05-22 17:20:37 -070011 "encoding/json"
Michael T. Jones1dc82d22014-02-14 12:57:03 -080012 "encoding/xml"
Robert Griesemerdbb62322010-05-15 10:23:41 -070013 "fmt"
Robert Griesemerfc78c5a2011-12-22 14:15:41 -080014 "math/rand"
Robert Griesemer5a1d3322009-12-15 15:33:31 -080015 "testing"
16 "testing/quick"
Adam Langley65063bc2009-11-05 15:55:41 -080017)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070018
Robert Griesemerb9caa4a2010-05-03 18:48:05 -070019func isNormalized(x *Int) bool {
20 if len(x.abs) == 0 {
21 return !x.neg
22 }
23 // len(x.abs) > 0
24 return x.abs[len(x.abs)-1] != 0
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070025}
26
Robert Griesemere5874222009-08-15 11:43:54 -070027type funZZ func(z, x, y *Int) *Int
Russ Cox650bff62009-10-06 14:55:39 -070028type argZZ struct {
Robert Griesemer5a1d3322009-12-15 15:33:31 -080029 z, x, y *Int
Russ Cox650bff62009-10-06 14:55:39 -070030}
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070031
32var sumZZ = []argZZ{
Robert Griesemer34788912010-10-22 10:06:33 -070033 {NewInt(0), NewInt(0), NewInt(0)},
34 {NewInt(1), NewInt(1), NewInt(0)},
35 {NewInt(1111111110), NewInt(123456789), NewInt(987654321)},
36 {NewInt(-1), NewInt(-1), NewInt(0)},
37 {NewInt(864197532), NewInt(-123456789), NewInt(987654321)},
38 {NewInt(-1111111110), NewInt(-123456789), NewInt(-987654321)},
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070039}
40
Robert Griesemer88742ef2009-08-18 10:06:15 -070041var prodZZ = []argZZ{
Robert Griesemer34788912010-10-22 10:06:33 -070042 {NewInt(0), NewInt(0), NewInt(0)},
43 {NewInt(0), NewInt(1), NewInt(0)},
44 {NewInt(1), NewInt(1), NewInt(1)},
45 {NewInt(-991 * 991), NewInt(991), NewInt(-991)},
Adam Langley65063bc2009-11-05 15:55:41 -080046 // TODO(gri) add larger products
Robert Griesemer88742ef2009-08-18 10:06:15 -070047}
48
Robert Griesemer97bcf042010-07-12 16:09:27 -070049func TestSignZ(t *testing.T) {
50 var zero Int
51 for _, a := range sumZZ {
52 s := a.z.Sign()
53 e := a.z.Cmp(&zero)
54 if s != e {
55 t.Errorf("got %d; want %d for z = %v", s, e, a.z)
56 }
57 }
58}
59
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070060func TestSetZ(t *testing.T) {
61 for _, a := range sumZZ {
Robert Griesemer5a1d3322009-12-15 15:33:31 -080062 var z Int
63 z.Set(a.z)
Robert Griesemerb9caa4a2010-05-03 18:48:05 -070064 if !isNormalized(&z) {
65 t.Errorf("%v is not normalized", z)
66 }
Adam Langley19418552009-11-11 13:21:37 -080067 if (&z).Cmp(a.z) != 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -080068 t.Errorf("got z = %v; want %v", z, a.z)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070069 }
70 }
71}
72
Robert Griesemer97bcf042010-07-12 16:09:27 -070073func TestAbsZ(t *testing.T) {
74 var zero Int
75 for _, a := range sumZZ {
76 var z Int
77 z.Abs(a.z)
78 var e Int
79 e.Set(a.z)
80 if e.Cmp(&zero) < 0 {
81 e.Sub(&zero, &e)
82 }
83 if z.Cmp(&e) != 0 {
84 t.Errorf("got z = %v; want %v", z, e)
85 }
86 }
87}
88
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070089func testFunZZ(t *testing.T, msg string, f funZZ, a argZZ) {
Robert Griesemer5a1d3322009-12-15 15:33:31 -080090 var z Int
91 f(&z, a.x, a.y)
Robert Griesemerb9caa4a2010-05-03 18:48:05 -070092 if !isNormalized(&z) {
Rob Pikefa7791e2013-09-27 10:09:15 +100093 t.Errorf("%s%v is not normalized", msg, z)
Robert Griesemerb9caa4a2010-05-03 18:48:05 -070094 }
Adam Langley19418552009-11-11 13:21:37 -080095 if (&z).Cmp(a.z) != 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -080096 t.Errorf("%s%+v\n\tgot z = %v; want %v", msg, a, &z, a.z)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070097 }
98}
99
Robert Griesemer88742ef2009-08-18 10:06:15 -0700100func TestSumZZ(t *testing.T) {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800101 AddZZ := func(z, x, y *Int) *Int { return z.Add(x, y) }
102 SubZZ := func(z, x, y *Int) *Int { return z.Sub(x, y) }
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700103 for _, a := range sumZZ {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800104 arg := a
105 testFunZZ(t, "AddZZ", AddZZ, arg)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700106
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800107 arg = argZZ{a.z, a.y, a.x}
108 testFunZZ(t, "AddZZ symmetric", AddZZ, arg)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700109
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800110 arg = argZZ{a.x, a.z, a.y}
111 testFunZZ(t, "SubZZ", SubZZ, arg)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700112
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800113 arg = argZZ{a.y, a.z, a.x}
114 testFunZZ(t, "SubZZ symmetric", SubZZ, arg)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700115 }
116}
Robert Griesemer88742ef2009-08-18 10:06:15 -0700117
Robert Griesemer88742ef2009-08-18 10:06:15 -0700118func TestProdZZ(t *testing.T) {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800119 MulZZ := func(z, x, y *Int) *Int { return z.Mul(x, y) }
Robert Griesemer88742ef2009-08-18 10:06:15 -0700120 for _, a := range prodZZ {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800121 arg := a
122 testFunZZ(t, "MulZZ", MulZZ, arg)
Robert Griesemer88742ef2009-08-18 10:06:15 -0700123
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800124 arg = argZZ{a.z, a.y, a.x}
125 testFunZZ(t, "MulZZ symmetric", MulZZ, arg)
Robert Griesemer88742ef2009-08-18 10:06:15 -0700126 }
127}
128
Robert Griesemerb2183702010-04-27 19:16:08 -0700129// mulBytes returns x*y via grade school multiplication. Both inputs
130// and the result are assumed to be in big-endian representation (to
131// match the semantics of Int.Bytes and Int.SetBytes).
132func mulBytes(x, y []byte) []byte {
133 z := make([]byte, len(x)+len(y))
Robert Griesemer88742ef2009-08-18 10:06:15 -0700134
Robert Griesemerb2183702010-04-27 19:16:08 -0700135 // multiply
136 k0 := len(z) - 1
137 for j := len(y) - 1; j >= 0; j-- {
138 d := int(y[j])
139 if d != 0 {
140 k := k0
141 carry := 0
142 for i := len(x) - 1; i >= 0; i-- {
143 t := int(z[k]) + int(x[i])*d + carry
144 z[k], carry = byte(t), t>>8
145 k--
146 }
147 z[k] = byte(carry)
Robert Griesemer88742ef2009-08-18 10:06:15 -0700148 }
Robert Griesemerb2183702010-04-27 19:16:08 -0700149 k0--
150 }
151
152 // normalize (remove leading 0's)
153 i := 0
154 for i < len(z) && z[i] == 0 {
155 i++
156 }
157
158 return z[i:]
159}
160
Robert Griesemerb2183702010-04-27 19:16:08 -0700161func checkMul(a, b []byte) bool {
162 var x, y, z1 Int
163 x.SetBytes(a)
164 y.SetBytes(b)
165 z1.Mul(&x, &y)
166
167 var z2 Int
168 z2.SetBytes(mulBytes(a, b))
169
170 return z1.Cmp(&z2) == 0
171}
172
Robert Griesemerb2183702010-04-27 19:16:08 -0700173func TestMul(t *testing.T) {
174 if err := quick.Check(checkMul, nil); err != nil {
175 t.Error(err)
Robert Griesemer88742ef2009-08-18 10:06:15 -0700176 }
177}
Adam Langley65063bc2009-11-05 15:55:41 -0800178
Robert Griesemere3515332010-10-25 17:45:43 -0700179var mulRangesZ = []struct {
Robert Griesemerdbb62322010-05-15 10:23:41 -0700180 a, b int64
181 prod string
Robert Griesemere3515332010-10-25 17:45:43 -0700182}{
Robert Griesemerdbb62322010-05-15 10:23:41 -0700183 // entirely positive ranges are covered by mulRangesN
Robert Griesemer34788912010-10-22 10:06:33 -0700184 {-1, 1, "0"},
185 {-2, -1, "2"},
186 {-3, -2, "6"},
187 {-3, -1, "-6"},
188 {1, 3, "6"},
189 {-10, -10, "-10"},
190 {0, -1, "1"}, // empty range
191 {-1, -100, "1"}, // empty range
192 {-1, 1, "0"}, // range includes 0
193 {-1e9, 0, "0"}, // range includes 0
194 {-1e9, 1e9, "0"}, // range includes 0
195 {-10, -1, "3628800"}, // 10!
196 {-20, -2, "-2432902008176640000"}, // -20!
197 {-99, -1,
Robert Griesemerdbb62322010-05-15 10:23:41 -0700198 "-933262154439441526816992388562667004907159682643816214685929" +
199 "638952175999932299156089414639761565182862536979208272237582" +
200 "511852109168640000000000000000000000", // -99!
201 },
202}
203
Robert Griesemerdbb62322010-05-15 10:23:41 -0700204func TestMulRangeZ(t *testing.T) {
205 var tmp Int
206 // test entirely positive ranges
207 for i, r := range mulRangesN {
208 prod := tmp.MulRange(int64(r.a), int64(r.b)).String()
209 if prod != r.prod {
210 t.Errorf("#%da: got %s; want %s", i, prod, r.prod)
211 }
212 }
213 // test other ranges
214 for i, r := range mulRangesZ {
215 prod := tmp.MulRange(r.a, r.b).String()
216 if prod != r.prod {
217 t.Errorf("#%db: got %s; want %s", i, prod, r.prod)
218 }
219 }
220}
221
Robert Griesemere3515332010-10-25 17:45:43 -0700222// Examples from the Go Language Spec, section "Arithmetic operators"
223var divisionSignsTests = []struct {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800224 x, y int64
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700225 q, r int64 // T-division
226 d, m int64 // Euclidian division
Robert Griesemere3515332010-10-25 17:45:43 -0700227}{
Robert Griesemer34788912010-10-22 10:06:33 -0700228 {5, 3, 1, 2, 1, 2},
229 {-5, 3, -1, -2, -2, 1},
230 {5, -3, -1, 2, -1, 2},
231 {-5, -3, 1, -2, 2, 1},
232 {1, 2, 0, 1, 0, 1},
233 {8, 4, 2, 0, 2, 0},
Adam Langley65063bc2009-11-05 15:55:41 -0800234}
235
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700236func TestDivisionSigns(t *testing.T) {
237 for i, test := range divisionSignsTests {
238 x := NewInt(test.x)
239 y := NewInt(test.y)
240 q := NewInt(test.q)
241 r := NewInt(test.r)
242 d := NewInt(test.d)
243 m := NewInt(test.m)
Adam Langley65063bc2009-11-05 15:55:41 -0800244
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700245 q1 := new(Int).Quo(x, y)
246 r1 := new(Int).Rem(x, y)
247 if !isNormalized(q1) {
248 t.Errorf("#%d Quo: %v is not normalized", i, *q1)
249 }
250 if !isNormalized(r1) {
251 t.Errorf("#%d Rem: %v is not normalized", i, *r1)
252 }
253 if q1.Cmp(q) != 0 || r1.Cmp(r) != 0 {
254 t.Errorf("#%d QuoRem: got (%s, %s), want (%s, %s)", i, q1, r1, q, r)
255 }
256
257 q2, r2 := new(Int).QuoRem(x, y, new(Int))
258 if !isNormalized(q2) {
259 t.Errorf("#%d Quo: %v is not normalized", i, *q2)
260 }
261 if !isNormalized(r2) {
262 t.Errorf("#%d Rem: %v is not normalized", i, *r2)
263 }
264 if q2.Cmp(q) != 0 || r2.Cmp(r) != 0 {
265 t.Errorf("#%d QuoRem: got (%s, %s), want (%s, %s)", i, q2, r2, q, r)
266 }
267
268 d1 := new(Int).Div(x, y)
269 m1 := new(Int).Mod(x, y)
270 if !isNormalized(d1) {
271 t.Errorf("#%d Div: %v is not normalized", i, *d1)
272 }
273 if !isNormalized(m1) {
274 t.Errorf("#%d Mod: %v is not normalized", i, *m1)
275 }
276 if d1.Cmp(d) != 0 || m1.Cmp(m) != 0 {
277 t.Errorf("#%d DivMod: got (%s, %s), want (%s, %s)", i, d1, m1, d, m)
278 }
279
280 d2, m2 := new(Int).DivMod(x, y, new(Int))
281 if !isNormalized(d2) {
282 t.Errorf("#%d Div: %v is not normalized", i, *d2)
283 }
284 if !isNormalized(m2) {
285 t.Errorf("#%d Mod: %v is not normalized", i, *m2)
286 }
287 if d2.Cmp(d) != 0 || m2.Cmp(m) != 0 {
288 t.Errorf("#%d DivMod: got (%s, %s), want (%s, %s)", i, d2, m2, d, m)
Adam Langley65063bc2009-11-05 15:55:41 -0800289 }
290 }
291}
292
Robert Griesemerde47e9c2015-01-16 15:11:26 -0800293func norm(x nat) nat {
294 i := len(x)
295 for i > 0 && x[i-1] == 0 {
296 i--
297 }
298 return x[:i]
299}
300
301func TestBits(t *testing.T) {
Robert Griesemerc20a0182015-02-25 10:20:28 -0800302 for _, test := range []nat{
303 nil,
304 {0},
305 {1},
306 {0, 1, 2, 3, 4},
307 {4, 3, 2, 1, 0},
308 {4, 3, 2, 1, 0, 0, 0, 0},
309 } {
Robert Griesemerde47e9c2015-01-16 15:11:26 -0800310 var z Int
311 z.neg = true
312 got := z.SetBits(test)
313 want := norm(test)
314 if got.abs.cmp(want) != 0 {
315 t.Errorf("SetBits(%v) = %v; want %v", test, got.abs, want)
316 }
317
318 if got.neg {
Robert Griesemerc20a0182015-02-25 10:20:28 -0800319 t.Errorf("SetBits(%v): got negative result", test)
Robert Griesemerde47e9c2015-01-16 15:11:26 -0800320 }
321
322 bits := nat(z.Bits())
323 if bits.cmp(want) != 0 {
324 t.Errorf("%v.Bits() = %v; want %v", z.abs, bits, want)
325 }
326 }
327}
328
Adam Langley65063bc2009-11-05 15:55:41 -0800329func checkSetBytes(b []byte) bool {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800330 hex1 := hex.EncodeToString(new(Int).SetBytes(b).Bytes())
331 hex2 := hex.EncodeToString(b)
Adam Langley65063bc2009-11-05 15:55:41 -0800332
333 for len(hex1) < len(hex2) {
Robert Griesemer16989342009-11-09 21:09:34 -0800334 hex1 = "0" + hex1
Adam Langley65063bc2009-11-05 15:55:41 -0800335 }
336
337 for len(hex1) > len(hex2) {
Robert Griesemer16989342009-11-09 21:09:34 -0800338 hex2 = "0" + hex2
Adam Langley65063bc2009-11-05 15:55:41 -0800339 }
340
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800341 return hex1 == hex2
Adam Langley65063bc2009-11-05 15:55:41 -0800342}
343
Adam Langley65063bc2009-11-05 15:55:41 -0800344func TestSetBytes(t *testing.T) {
Robert Griesemerb2183702010-04-27 19:16:08 -0700345 if err := quick.Check(checkSetBytes, nil); err != nil {
Robert Griesemer40621d52009-11-09 12:07:39 -0800346 t.Error(err)
Adam Langley65063bc2009-11-05 15:55:41 -0800347 }
348}
349
Adam Langley65063bc2009-11-05 15:55:41 -0800350func checkBytes(b []byte) bool {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800351 b2 := new(Int).SetBytes(b).Bytes()
Matthew Dempsky46811d22013-01-07 10:03:49 +1100352 return bytes.Equal(b, b2)
Adam Langley65063bc2009-11-05 15:55:41 -0800353}
354
Adam Langley65063bc2009-11-05 15:55:41 -0800355func TestBytes(t *testing.T) {
Jeremy Schlatterff1f3a12015-03-27 03:29:06 +0000356 if err := quick.Check(checkBytes, nil); err != nil {
Robert Griesemer40621d52009-11-09 12:07:39 -0800357 t.Error(err)
Adam Langley65063bc2009-11-05 15:55:41 -0800358 }
359}
360
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700361func checkQuo(x, y []byte) bool {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800362 u := new(Int).SetBytes(x)
363 v := new(Int).SetBytes(y)
Adam Langley65063bc2009-11-05 15:55:41 -0800364
365 if len(v.abs) == 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800366 return true
Adam Langley65063bc2009-11-05 15:55:41 -0800367 }
368
Evan Shaw76cbbc82010-04-20 20:39:36 -0700369 r := new(Int)
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700370 q, r := new(Int).QuoRem(u, v, r)
Adam Langley65063bc2009-11-05 15:55:41 -0800371
Adam Langley19418552009-11-11 13:21:37 -0800372 if r.Cmp(v) >= 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800373 return false
Adam Langley65063bc2009-11-05 15:55:41 -0800374 }
375
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800376 uprime := new(Int).Set(q)
377 uprime.Mul(uprime, v)
378 uprime.Add(uprime, r)
Adam Langley65063bc2009-11-05 15:55:41 -0800379
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800380 return uprime.Cmp(u) == 0
Adam Langley65063bc2009-11-05 15:55:41 -0800381}
382
Robert Griesemere3515332010-10-25 17:45:43 -0700383var quoTests = []struct {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800384 x, y string
385 q, r string
Robert Griesemere3515332010-10-25 17:45:43 -0700386}{
Robert Griesemer34788912010-10-22 10:06:33 -0700387 {
Adam Langleydb4e48e2009-11-06 11:36:21 -0800388 "476217953993950760840509444250624797097991362735329973741718102894495832294430498335824897858659711275234906400899559094370964723884706254265559534144986498357",
389 "9353930466774385905609975137998169297361893554149986716853295022578535724979483772383667534691121982974895531435241089241440253066816724367338287092081996",
390 "50911",
391 "1",
392 },
Robert Griesemer34788912010-10-22 10:06:33 -0700393 {
Adam Langley19418552009-11-11 13:21:37 -0800394 "11510768301994997771168",
395 "1328165573307167369775",
396 "8",
397 "885443715537658812968",
398 },
Adam Langleydb4e48e2009-11-06 11:36:21 -0800399}
400
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700401func TestQuo(t *testing.T) {
402 if err := quick.Check(checkQuo, nil); err != nil {
Robert Griesemer40621d52009-11-09 12:07:39 -0800403 t.Error(err)
Adam Langley65063bc2009-11-05 15:55:41 -0800404 }
Adam Langleydb4e48e2009-11-06 11:36:21 -0800405
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700406 for i, test := range quoTests {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800407 x, _ := new(Int).SetString(test.x, 10)
408 y, _ := new(Int).SetString(test.y, 10)
409 expectedQ, _ := new(Int).SetString(test.q, 10)
410 expectedR, _ := new(Int).SetString(test.r, 10)
Adam Langleydb4e48e2009-11-06 11:36:21 -0800411
Evan Shaw76cbbc82010-04-20 20:39:36 -0700412 r := new(Int)
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700413 q, r := new(Int).QuoRem(x, y, r)
Adam Langleydb4e48e2009-11-06 11:36:21 -0800414
Adam Langley19418552009-11-11 13:21:37 -0800415 if q.Cmp(expectedQ) != 0 || r.Cmp(expectedR) != 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800416 t.Errorf("#%d got (%s, %s) want (%s, %s)", i, q, r, expectedQ, expectedR)
Adam Langleydb4e48e2009-11-06 11:36:21 -0800417 }
418 }
Adam Langley65063bc2009-11-05 15:55:41 -0800419}
420
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700421func TestQuoStepD6(t *testing.T) {
Adam Langley65063bc2009-11-05 15:55:41 -0800422 // See Knuth, Volume 2, section 4.3.1, exercise 21. This code exercises
423 // a code path which only triggers 1 in 10^{-19} cases.
424
Evan Shaw841a32d2010-04-22 16:57:29 -0700425 u := &Int{false, nat{0, 0, 1 + 1<<(_W-1), _M ^ (1 << (_W - 1))}}
426 v := &Int{false, nat{5, 2 + 1<<(_W-1), 1 << (_W - 1)}}
Adam Langley65063bc2009-11-05 15:55:41 -0800427
Evan Shaw76cbbc82010-04-20 20:39:36 -0700428 r := new(Int)
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700429 q, r := new(Int).QuoRem(u, v, r)
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800430 const expectedQ64 = "18446744073709551613"
431 const expectedR64 = "3138550867693340382088035895064302439801311770021610913807"
432 const expectedQ32 = "4294967293"
433 const expectedR32 = "39614081266355540837921718287"
Adam Langley17021f42009-11-06 09:05:19 -0800434 if q.String() != expectedQ64 && q.String() != expectedQ32 ||
Robert Griesemer56eca9d2009-11-06 11:00:06 -0800435 r.String() != expectedR64 && r.String() != expectedR32 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800436 t.Errorf("got (%s, %s) want (%s, %s) or (%s, %s)", q, r, expectedQ64, expectedR64, expectedQ32, expectedR32)
Adam Langley65063bc2009-11-05 15:55:41 -0800437 }
438}
439
Robert Griesemere3515332010-10-25 17:45:43 -0700440var bitLenTests = []struct {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800441 in string
442 out int
Robert Griesemere3515332010-10-25 17:45:43 -0700443}{
Robert Griesemer34788912010-10-22 10:06:33 -0700444 {"-1", 1},
445 {"0", 0},
446 {"1", 1},
447 {"2", 2},
448 {"4", 3},
449 {"0xabc", 12},
450 {"0x8000", 16},
451 {"0x80000000", 32},
452 {"0x800000000000", 48},
453 {"0x8000000000000000", 64},
454 {"0x80000000000000000000", 80},
455 {"-0x4000000000000000000000", 87},
Adam Langley65063bc2009-11-05 15:55:41 -0800456}
457
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700458func TestBitLen(t *testing.T) {
459 for i, test := range bitLenTests {
460 x, ok := new(Int).SetString(test.in, 0)
Adam Langley65063bc2009-11-05 15:55:41 -0800461 if !ok {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800462 t.Errorf("#%d test input invalid: %s", i, test.in)
463 continue
Adam Langley65063bc2009-11-05 15:55:41 -0800464 }
465
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700466 if n := x.BitLen(); n != test.out {
Rob Pike1959c3a2010-09-23 13:48:56 +1000467 t.Errorf("#%d got %d want %d", i, n, test.out)
Adam Langley65063bc2009-11-05 15:55:41 -0800468 }
469 }
470}
471
Robert Griesemere3515332010-10-25 17:45:43 -0700472var expTests = []struct {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800473 x, y, m string
474 out string
Robert Griesemere3515332010-10-25 17:45:43 -0700475}{
Robert Griesemer26533862014-04-21 15:54:51 -0700476 // y <= 0
477 {"0", "0", "", "1"},
478 {"1", "0", "", "1"},
479 {"-10", "0", "", "1"},
480 {"1234", "-1", "", "1"},
481
482 // m == 1
483 {"0", "0", "1", "0"},
484 {"1", "0", "1", "0"},
485 {"-10", "0", "1", "0"},
486 {"1234", "-1", "1", "0"},
487
488 // misc
Robert Griesemer75657262012-10-16 13:46:27 -0700489 {"5", "-7", "", "1"},
490 {"-5", "-7", "", "1"},
Robert Griesemer34788912010-10-22 10:06:33 -0700491 {"5", "0", "", "1"},
Robert Griesemer75657262012-10-16 13:46:27 -0700492 {"-5", "0", "", "1"},
Robert Griesemer34788912010-10-22 10:06:33 -0700493 {"5", "1", "", "5"},
494 {"-5", "1", "", "-5"},
Robert Griesemer28ddfb02014-10-02 13:02:25 -0700495 {"-5", "1", "7", "2"},
Robert Griesemer34788912010-10-22 10:06:33 -0700496 {"-2", "3", "2", "0"},
497 {"5", "2", "", "25"},
498 {"1", "65537", "2", "1"},
499 {"0x8000000000000000", "2", "", "0x40000000000000000000000000000000"},
500 {"0x8000000000000000", "2", "6719", "4944"},
501 {"0x8000000000000000", "3", "6719", "5447"},
502 {"0x8000000000000000", "1000", "6719", "1603"},
503 {"0x8000000000000000", "1000000", "6719", "3199"},
Robert Griesemer75657262012-10-16 13:46:27 -0700504 {"0x8000000000000000", "-1000000", "6719", "1"},
Robert Griesemer34788912010-10-22 10:06:33 -0700505 {
Adam Langley65063bc2009-11-05 15:55:41 -0800506 "2938462938472983472983659726349017249287491026512746239764525612965293865296239471239874193284792387498274256129746192347",
507 "298472983472983471903246121093472394872319615612417471234712061",
508 "29834729834729834729347290846729561262544958723956495615629569234729836259263598127342374289365912465901365498236492183464",
509 "23537740700184054162508175125554701713153216681790245129157191391322321508055833908509185839069455749219131480588829346291",
Adam Langley17021f42009-11-06 09:05:19 -0800510 },
Robert Griesemer28ddfb02014-10-02 13:02:25 -0700511 // test case for issue 8822
512 {
513 "-0x1BCE04427D8032319A89E5C4136456671AC620883F2C4139E57F91307C485AD2D6204F4F87A58262652DB5DBBAC72B0613E51B835E7153BEC6068F5C8D696B74DBD18FEC316AEF73985CF0475663208EB46B4F17DD9DA55367B03323E5491A70997B90C059FB34809E6EE55BCFBD5F2F52233BFE62E6AA9E4E26A1D4C2439883D14F2633D55D8AA66A1ACD5595E778AC3A280517F1157989E70C1A437B849F1877B779CC3CDDEDE2DAA6594A6C66D181A00A5F777EE60596D8773998F6E988DEAE4CCA60E4DDCF9590543C89F74F603259FCAD71660D30294FBBE6490300F78A9D63FA660DC9417B8B9DDA28BEB3977B621B988E23D4D954F322C3540541BC649ABD504C50FADFD9F0987D58A2BF689313A285E773FF02899A6EF887D1D4A0D2",
514 "0xB08FFB20760FFED58FADA86DFEF71AD72AA0FA763219618FE022C197E54708BB1191C66470250FCE8879487507CEE41381CA4D932F81C2B3F1AB20B539D50DCD",
515 "0xAC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF73",
516 "21484252197776302499639938883777710321993113097987201050501182909581359357618579566746556372589385361683610524730509041328855066514963385522570894839035884713051640171474186548713546686476761306436434146475140156284389181808675016576845833340494848283681088886584219750554408060556769486628029028720727393293111678826356480455433909233520504112074401376133077150471237549474149190242010469539006449596611576612573955754349042329130631128234637924786466585703488460540228477440853493392086251021228087076124706778899179648655221663765993962724699135217212118535057766739392069738618682722216712319320435674779146070442",
517 },
Adam Langley65063bc2009-11-05 15:55:41 -0800518}
519
Adam Langley65063bc2009-11-05 15:55:41 -0800520func TestExp(t *testing.T) {
521 for i, test := range expTests {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800522 x, ok1 := new(Int).SetString(test.x, 0)
523 y, ok2 := new(Int).SetString(test.y, 0)
524 out, ok3 := new(Int).SetString(test.out, 0)
Adam Langley65063bc2009-11-05 15:55:41 -0800525
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800526 var ok4 bool
527 var m *Int
Adam Langley65063bc2009-11-05 15:55:41 -0800528
529 if len(test.m) == 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800530 m, ok4 = nil, true
Adam Langley65063bc2009-11-05 15:55:41 -0800531 } else {
Robert Griesemer40621d52009-11-09 12:07:39 -0800532 m, ok4 = new(Int).SetString(test.m, 0)
Adam Langley65063bc2009-11-05 15:55:41 -0800533 }
534
535 if !ok1 || !ok2 || !ok3 || !ok4 {
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700536 t.Errorf("#%d: error in input", i)
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800537 continue
Adam Langley65063bc2009-11-05 15:55:41 -0800538 }
539
Robert Griesemer75657262012-10-16 13:46:27 -0700540 z1 := new(Int).Exp(x, y, m)
541 if !isNormalized(z1) {
542 t.Errorf("#%d: %v is not normalized", i, *z1)
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700543 }
Robert Griesemer75657262012-10-16 13:46:27 -0700544 if z1.Cmp(out) != 0 {
545 t.Errorf("#%d: got %s want %s", i, z1, out)
546 }
547
548 if m == nil {
Robert Griesemer28ddfb02014-10-02 13:02:25 -0700549 // The result should be the same as for m == 0;
550 // specifically, there should be no div-zero panic.
Robert Griesemer75657262012-10-16 13:46:27 -0700551 m = &Int{abs: nat{}} // m != nil && len(m.abs) == 0
552 z2 := new(Int).Exp(x, y, m)
553 if z2.Cmp(z1) != 0 {
Robert Griesemer28ddfb02014-10-02 13:02:25 -0700554 t.Errorf("#%d: got %s want %s", i, z2, z1)
Robert Griesemer75657262012-10-16 13:46:27 -0700555 }
Adam Langley65063bc2009-11-05 15:55:41 -0800556 }
557 }
558}
559
Adam Langley65063bc2009-11-05 15:55:41 -0800560func checkGcd(aBytes, bBytes []byte) bool {
Robert Griesemer10b88882012-06-13 13:54:36 -0700561 x := new(Int)
562 y := new(Int)
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800563 a := new(Int).SetBytes(aBytes)
564 b := new(Int).SetBytes(bBytes)
Adam Langley65063bc2009-11-05 15:55:41 -0800565
Robert Griesemer10b88882012-06-13 13:54:36 -0700566 d := new(Int).GCD(x, y, a, b)
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800567 x.Mul(x, a)
568 y.Mul(y, b)
569 x.Add(x, y)
Adam Langley65063bc2009-11-05 15:55:41 -0800570
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800571 return x.Cmp(d) == 0
Adam Langley65063bc2009-11-05 15:55:41 -0800572}
573
Robert Griesemere3515332010-10-25 17:45:43 -0700574var gcdTests = []struct {
Robert Griesemer10b88882012-06-13 13:54:36 -0700575 d, x, y, a, b string
Robert Griesemere3515332010-10-25 17:45:43 -0700576}{
Robert Griesemer10b88882012-06-13 13:54:36 -0700577 // a <= 0 || b <= 0
578 {"0", "0", "0", "0", "0"},
579 {"0", "0", "0", "0", "7"},
580 {"0", "0", "0", "11", "0"},
581 {"0", "0", "0", "-77", "35"},
582 {"0", "0", "0", "64515", "-24310"},
583 {"0", "0", "0", "-64515", "-24310"},
584
585 {"1", "-9", "47", "120", "23"},
586 {"7", "1", "-2", "77", "35"},
587 {"935", "-3", "8", "64515", "24310"},
588 {"935000000000000000", "-3", "8", "64515000000000000000", "24310000000000000000"},
589 {"1", "-221", "22059940471369027483332068679400581064239780177629666810348940098015901108344", "98920366548084643601728869055592650835572950932266967461790948584315647051443", "991"},
590
591 // test early exit (after one Euclidean iteration) in binaryGCD
592 {"1", "", "", "1", "98920366548084643601728869055592650835572950932266967461790948584315647051443"},
593}
594
595func testGcd(t *testing.T, d, x, y, a, b *Int) {
596 var X *Int
597 if x != nil {
598 X = new(Int)
599 }
600 var Y *Int
601 if y != nil {
602 Y = new(Int)
603 }
604
605 D := new(Int).GCD(X, Y, a, b)
606 if D.Cmp(d) != 0 {
607 t.Errorf("GCD(%s, %s): got d = %s, want %s", a, b, D, d)
608 }
609 if x != nil && X.Cmp(x) != 0 {
610 t.Errorf("GCD(%s, %s): got x = %s, want %s", a, b, X, x)
611 }
612 if y != nil && Y.Cmp(y) != 0 {
613 t.Errorf("GCD(%s, %s): got y = %s, want %s", a, b, Y, y)
614 }
615
616 // binaryGCD requires a > 0 && b > 0
617 if a.Sign() <= 0 || b.Sign() <= 0 {
618 return
619 }
620
621 D.binaryGCD(a, b)
622 if D.Cmp(d) != 0 {
623 t.Errorf("binaryGcd(%s, %s): got d = %s, want %s", a, b, D, d)
624 }
Adam Langley65063bc2009-11-05 15:55:41 -0800625}
626
Adam Langley65063bc2009-11-05 15:55:41 -0800627func TestGcd(t *testing.T) {
Robert Griesemer10b88882012-06-13 13:54:36 -0700628 for _, test := range gcdTests {
629 d, _ := new(Int).SetString(test.d, 0)
630 x, _ := new(Int).SetString(test.x, 0)
631 y, _ := new(Int).SetString(test.y, 0)
632 a, _ := new(Int).SetString(test.a, 0)
633 b, _ := new(Int).SetString(test.b, 0)
Adam Langley65063bc2009-11-05 15:55:41 -0800634
Robert Griesemer10b88882012-06-13 13:54:36 -0700635 testGcd(t, d, nil, nil, a, b)
636 testGcd(t, d, x, nil, a, b)
637 testGcd(t, d, nil, y, a, b)
638 testGcd(t, d, x, y, a, b)
Adam Langley65063bc2009-11-05 15:55:41 -0800639 }
640
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800641 quick.Check(checkGcd, nil)
Adam Langley65063bc2009-11-05 15:55:41 -0800642}
Adam Langley19418552009-11-11 13:21:37 -0800643
Adam Langley19418552009-11-11 13:21:37 -0800644var primes = []string{
645 "2",
646 "3",
647 "5",
648 "7",
649 "11",
Russ Coxcfbee342010-01-05 16:49:05 -0800650
651 "13756265695458089029",
652 "13496181268022124907",
653 "10953742525620032441",
654 "17908251027575790097",
655
Péter Surányi9b6ccb12015-02-06 21:44:39 +0900656 // http://golang.org/issue/638
Adam Langley308064f2010-03-05 15:55:26 -0500657 "18699199384836356663",
658
Adam Langley19418552009-11-11 13:21:37 -0800659 "98920366548084643601728869055592650835572950932266967461790948584315647051443",
660 "94560208308847015747498523884063394671606671904944666360068158221458669711639",
Russ Coxcfbee342010-01-05 16:49:05 -0800661
Adam Langley19418552009-11-11 13:21:37 -0800662 // http://primes.utm.edu/lists/small/small3.html
663 "449417999055441493994709297093108513015373787049558499205492347871729927573118262811508386655998299074566974373711472560655026288668094291699357843464363003144674940345912431129144354948751003607115263071543163",
664 "230975859993204150666423538988557839555560243929065415434980904258310530753006723857139742334640122533598517597674807096648905501653461687601339782814316124971547968912893214002992086353183070342498989426570593",
665 "5521712099665906221540423207019333379125265462121169655563495403888449493493629943498064604536961775110765377745550377067893607246020694972959780839151452457728855382113555867743022746090187341871655890805971735385789993",
666 "203956878356401977405765866929034577280193993314348263094772646453283062722701277632936616063144088173312372882677123879538709400158306567338328279154499698366071906766440037074217117805690872792848149112022286332144876183376326512083574821647933992961249917319836219304274280243803104015000563790123",
667}
668
Adam Langley19418552009-11-11 13:21:37 -0800669var composites = []string{
Robert Griesemerde47e9c2015-01-16 15:11:26 -0800670 "0",
671 "1",
Adam Langley19418552009-11-11 13:21:37 -0800672 "21284175091214687912771199898307297748211672914763848041968395774954376176754",
673 "6084766654921918907427900243509372380954290099172559290432744450051395395951",
674 "84594350493221918389213352992032324280367711247940675652888030554255915464401",
675 "82793403787388584738507275144194252681",
676}
677
Adam Langley19418552009-11-11 13:21:37 -0800678func TestProbablyPrime(t *testing.T) {
Rob Pike7f9acb52011-03-26 11:25:22 -0700679 nreps := 20
680 if testing.Short() {
681 nreps = 1
682 }
Adam Langley19418552009-11-11 13:21:37 -0800683 for i, s := range primes {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800684 p, _ := new(Int).SetString(s, 10)
Robert Griesemerb80c7e52012-02-02 19:21:55 -0800685 if !p.ProbablyPrime(nreps) {
Adam Langley308064f2010-03-05 15:55:26 -0500686 t.Errorf("#%d prime found to be non-prime (%s)", i, s)
Adam Langley19418552009-11-11 13:21:37 -0800687 }
688 }
689
690 for i, s := range composites {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800691 c, _ := new(Int).SetString(s, 10)
Robert Griesemerb80c7e52012-02-02 19:21:55 -0800692 if c.ProbablyPrime(nreps) {
Adam Langley308064f2010-03-05 15:55:26 -0500693 t.Errorf("#%d composite found to be prime (%s)", i, s)
Adam Langley19418552009-11-11 13:21:37 -0800694 }
Rob Pike7f9acb52011-03-26 11:25:22 -0700695 if testing.Short() {
696 break
697 }
Adam Langley19418552009-11-11 13:21:37 -0800698 }
Shenghou Ma43178692015-01-05 16:39:34 -0500699
700 // check that ProbablyPrime panics if n <= 0
701 c := NewInt(11) // a prime
702 for _, n := range []int{-1, 0, 1} {
703 func() {
704 defer func() {
705 if n <= 0 && recover() == nil {
706 t.Fatalf("expected panic from ProbablyPrime(%d)", n)
707 }
708 }()
709 if !c.ProbablyPrime(n) {
710 t.Fatalf("%v should be a prime", c)
711 }
712 }()
713 }
Adam Langley19418552009-11-11 13:21:37 -0800714}
715
Evan Shaw76cbbc82010-04-20 20:39:36 -0700716type intShiftTest struct {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800717 in string
Evan Shaw76cbbc82010-04-20 20:39:36 -0700718 shift uint
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800719 out string
Adam Langley19418552009-11-11 13:21:37 -0800720}
721
Evan Shaw76cbbc82010-04-20 20:39:36 -0700722var rshTests = []intShiftTest{
Robert Griesemer34788912010-10-22 10:06:33 -0700723 {"0", 0, "0"},
724 {"-0", 0, "0"},
725 {"0", 1, "0"},
726 {"0", 2, "0"},
727 {"1", 0, "1"},
728 {"1", 1, "0"},
729 {"1", 2, "0"},
730 {"2", 0, "2"},
731 {"2", 1, "1"},
732 {"-1", 0, "-1"},
733 {"-1", 1, "-1"},
734 {"-1", 10, "-1"},
735 {"-100", 2, "-25"},
736 {"-100", 3, "-13"},
737 {"-100", 100, "-1"},
738 {"4294967296", 0, "4294967296"},
739 {"4294967296", 1, "2147483648"},
740 {"4294967296", 2, "1073741824"},
741 {"18446744073709551616", 0, "18446744073709551616"},
742 {"18446744073709551616", 1, "9223372036854775808"},
743 {"18446744073709551616", 2, "4611686018427387904"},
744 {"18446744073709551616", 64, "1"},
745 {"340282366920938463463374607431768211456", 64, "18446744073709551616"},
746 {"340282366920938463463374607431768211456", 128, "1"},
Adam Langley19418552009-11-11 13:21:37 -0800747}
748
Adam Langley19418552009-11-11 13:21:37 -0800749func TestRsh(t *testing.T) {
750 for i, test := range rshTests {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800751 in, _ := new(Int).SetString(test.in, 10)
752 expected, _ := new(Int).SetString(test.out, 10)
753 out := new(Int).Rsh(in, test.shift)
Adam Langley19418552009-11-11 13:21:37 -0800754
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700755 if !isNormalized(out) {
756 t.Errorf("#%d: %v is not normalized", i, *out)
757 }
Adam Langley19418552009-11-11 13:21:37 -0800758 if out.Cmp(expected) != 0 {
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700759 t.Errorf("#%d: got %s want %s", i, out, expected)
Adam Langley19418552009-11-11 13:21:37 -0800760 }
761 }
762}
Evan Shaw76cbbc82010-04-20 20:39:36 -0700763
Evan Shaw76cbbc82010-04-20 20:39:36 -0700764func TestRshSelf(t *testing.T) {
765 for i, test := range rshTests {
766 z, _ := new(Int).SetString(test.in, 10)
767 expected, _ := new(Int).SetString(test.out, 10)
768 z.Rsh(z, test.shift)
769
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700770 if !isNormalized(z) {
771 t.Errorf("#%d: %v is not normalized", i, *z)
772 }
Evan Shaw76cbbc82010-04-20 20:39:36 -0700773 if z.Cmp(expected) != 0 {
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700774 t.Errorf("#%d: got %s want %s", i, z, expected)
Evan Shaw76cbbc82010-04-20 20:39:36 -0700775 }
776 }
777}
778
Evan Shaw76cbbc82010-04-20 20:39:36 -0700779var lshTests = []intShiftTest{
Robert Griesemer34788912010-10-22 10:06:33 -0700780 {"0", 0, "0"},
781 {"0", 1, "0"},
782 {"0", 2, "0"},
783 {"1", 0, "1"},
784 {"1", 1, "2"},
785 {"1", 2, "4"},
786 {"2", 0, "2"},
787 {"2", 1, "4"},
788 {"2", 2, "8"},
789 {"-87", 1, "-174"},
790 {"4294967296", 0, "4294967296"},
791 {"4294967296", 1, "8589934592"},
792 {"4294967296", 2, "17179869184"},
793 {"18446744073709551616", 0, "18446744073709551616"},
794 {"9223372036854775808", 1, "18446744073709551616"},
795 {"4611686018427387904", 2, "18446744073709551616"},
796 {"1", 64, "18446744073709551616"},
797 {"18446744073709551616", 64, "340282366920938463463374607431768211456"},
798 {"1", 128, "340282366920938463463374607431768211456"},
Evan Shaw76cbbc82010-04-20 20:39:36 -0700799}
800
Evan Shaw76cbbc82010-04-20 20:39:36 -0700801func TestLsh(t *testing.T) {
802 for i, test := range lshTests {
803 in, _ := new(Int).SetString(test.in, 10)
804 expected, _ := new(Int).SetString(test.out, 10)
805 out := new(Int).Lsh(in, test.shift)
806
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700807 if !isNormalized(out) {
808 t.Errorf("#%d: %v is not normalized", i, *out)
809 }
Evan Shaw76cbbc82010-04-20 20:39:36 -0700810 if out.Cmp(expected) != 0 {
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700811 t.Errorf("#%d: got %s want %s", i, out, expected)
Evan Shaw76cbbc82010-04-20 20:39:36 -0700812 }
813 }
814}
815
Evan Shaw76cbbc82010-04-20 20:39:36 -0700816func TestLshSelf(t *testing.T) {
817 for i, test := range lshTests {
818 z, _ := new(Int).SetString(test.in, 10)
819 expected, _ := new(Int).SetString(test.out, 10)
820 z.Lsh(z, test.shift)
821
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700822 if !isNormalized(z) {
823 t.Errorf("#%d: %v is not normalized", i, *z)
824 }
Evan Shaw76cbbc82010-04-20 20:39:36 -0700825 if z.Cmp(expected) != 0 {
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700826 t.Errorf("#%d: got %s want %s", i, z, expected)
Evan Shaw76cbbc82010-04-20 20:39:36 -0700827 }
828 }
829}
830
Evan Shaw76cbbc82010-04-20 20:39:36 -0700831func TestLshRsh(t *testing.T) {
832 for i, test := range rshTests {
833 in, _ := new(Int).SetString(test.in, 10)
834 out := new(Int).Lsh(in, test.shift)
835 out = out.Rsh(out, test.shift)
836
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700837 if !isNormalized(out) {
838 t.Errorf("#%d: %v is not normalized", i, *out)
839 }
Evan Shaw76cbbc82010-04-20 20:39:36 -0700840 if in.Cmp(out) != 0 {
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700841 t.Errorf("#%d: got %s want %s", i, out, in)
Evan Shaw76cbbc82010-04-20 20:39:36 -0700842 }
843 }
844 for i, test := range lshTests {
845 in, _ := new(Int).SetString(test.in, 10)
846 out := new(Int).Lsh(in, test.shift)
847 out.Rsh(out, test.shift)
848
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700849 if !isNormalized(out) {
850 t.Errorf("#%d: %v is not normalized", i, *out)
851 }
Evan Shaw76cbbc82010-04-20 20:39:36 -0700852 if in.Cmp(out) != 0 {
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700853 t.Errorf("#%d: got %s want %s", i, out, in)
Evan Shaw76cbbc82010-04-20 20:39:36 -0700854 }
855 }
856}
857
Evan Shaw76cbbc82010-04-20 20:39:36 -0700858var int64Tests = []int64{
859 0,
860 1,
861 -1,
862 4294967295,
863 -4294967295,
864 4294967296,
865 -4294967296,
866 9223372036854775807,
867 -9223372036854775807,
868 -9223372036854775808,
869}
870
871func TestInt64(t *testing.T) {
872 for i, testVal := range int64Tests {
873 in := NewInt(testVal)
874 out := in.Int64()
875
876 if out != testVal {
877 t.Errorf("#%d got %d want %d", i, out, testVal)
878 }
879 }
880}
Evan Shaw4d1b1572010-05-03 11:20:52 -0700881
Luit van Drongelenf4fc1632012-12-11 12:19:10 -0500882var uint64Tests = []uint64{
883 0,
884 1,
885 4294967295,
886 4294967296,
887 8589934591,
888 8589934592,
889 9223372036854775807,
890 9223372036854775808,
891 18446744073709551615, // 1<<64 - 1
892}
893
894func TestUint64(t *testing.T) {
895 in := new(Int)
896 for i, testVal := range uint64Tests {
897 in.SetUint64(testVal)
898 out := in.Uint64()
899
900 if out != testVal {
901 t.Errorf("#%d got %d want %d", i, out, testVal)
902 }
903
904 str := fmt.Sprint(testVal)
905 strOut := in.String()
906 if strOut != str {
907 t.Errorf("#%d.String got %s want %s", i, strOut, str)
908 }
909 }
910}
911
Robert Griesemere3515332010-10-25 17:45:43 -0700912var bitwiseTests = []struct {
Evan Shaw4d1b1572010-05-03 11:20:52 -0700913 x, y string
914 and, or, xor, andNot string
Robert Griesemere3515332010-10-25 17:45:43 -0700915}{
Robert Griesemer34788912010-10-22 10:06:33 -0700916 {"0x00", "0x00", "0x00", "0x00", "0x00", "0x00"},
917 {"0x00", "0x01", "0x00", "0x01", "0x01", "0x00"},
918 {"0x01", "0x00", "0x00", "0x01", "0x01", "0x01"},
919 {"-0x01", "0x00", "0x00", "-0x01", "-0x01", "-0x01"},
920 {"-0xaf", "-0x50", "-0xf0", "-0x0f", "0xe1", "0x41"},
921 {"0x00", "-0x01", "0x00", "-0x01", "-0x01", "0x00"},
922 {"0x01", "0x01", "0x01", "0x01", "0x00", "0x00"},
923 {"-0x01", "-0x01", "-0x01", "-0x01", "0x00", "0x00"},
924 {"0x07", "0x08", "0x00", "0x0f", "0x0f", "0x07"},
925 {"0x05", "0x0f", "0x05", "0x0f", "0x0a", "0x00"},
Keith Randallc6ddca22015-01-15 20:45:07 -0800926 {"0xff", "-0x0a", "0xf6", "-0x01", "-0xf7", "0x09"},
Robert Griesemer34788912010-10-22 10:06:33 -0700927 {"0x013ff6", "0x9a4e", "0x1a46", "0x01bffe", "0x01a5b8", "0x0125b0"},
928 {"-0x013ff6", "0x9a4e", "0x800a", "-0x0125b2", "-0x01a5bc", "-0x01c000"},
929 {"-0x013ff6", "-0x9a4e", "-0x01bffe", "-0x1a46", "0x01a5b8", "0x8008"},
930 {
Evan Shaw4d1b1572010-05-03 11:20:52 -0700931 "0x1000009dc6e3d9822cba04129bcbe3401",
932 "0xb9bd7d543685789d57cb918e833af352559021483cdb05cc21fd",
933 "0x1000001186210100001000009048c2001",
934 "0xb9bd7d543685789d57cb918e8bfeff7fddb2ebe87dfbbdfe35fd",
935 "0xb9bd7d543685789d57ca918e8ae69d6fcdb2eae87df2b97215fc",
936 "0x8c40c2d8822caa04120b8321400",
937 },
Robert Griesemer34788912010-10-22 10:06:33 -0700938 {
Evan Shaw4d1b1572010-05-03 11:20:52 -0700939 "0x1000009dc6e3d9822cba04129bcbe3401",
940 "-0xb9bd7d543685789d57cb918e833af352559021483cdb05cc21fd",
941 "0x8c40c2d8822caa04120b8321401",
942 "-0xb9bd7d543685789d57ca918e82229142459020483cd2014001fd",
943 "-0xb9bd7d543685789d57ca918e8ae69d6fcdb2eae87df2b97215fe",
944 "0x1000001186210100001000009048c2000",
945 },
Robert Griesemer34788912010-10-22 10:06:33 -0700946 {
Evan Shaw4d1b1572010-05-03 11:20:52 -0700947 "-0x1000009dc6e3d9822cba04129bcbe3401",
948 "-0xb9bd7d543685789d57cb918e833af352559021483cdb05cc21fd",
949 "-0xb9bd7d543685789d57cb918e8bfeff7fddb2ebe87dfbbdfe35fd",
950 "-0x1000001186210100001000009048c2001",
951 "0xb9bd7d543685789d57ca918e8ae69d6fcdb2eae87df2b97215fc",
952 "0xb9bd7d543685789d57ca918e82229142459020483cd2014001fc",
953 },
954}
955
Evan Shaw4d1b1572010-05-03 11:20:52 -0700956type bitFun func(z, x, y *Int) *Int
957
958func testBitFun(t *testing.T, msg string, f bitFun, x, y *Int, exp string) {
959 expected := new(Int)
Evan Shaw28a09712010-08-09 10:21:54 -0700960 expected.SetString(exp, 0)
Evan Shaw4d1b1572010-05-03 11:20:52 -0700961
962 out := f(new(Int), x, y)
963 if out.Cmp(expected) != 0 {
Evan Shaw4d1b1572010-05-03 11:20:52 -0700964 t.Errorf("%s: got %s want %s", msg, out, expected)
965 }
966}
967
Evan Shaw4d1b1572010-05-03 11:20:52 -0700968func testBitFunSelf(t *testing.T, msg string, f bitFun, x, y *Int, exp string) {
Evan Shaw28a09712010-08-09 10:21:54 -0700969 self := new(Int)
970 self.Set(x)
Evan Shaw4d1b1572010-05-03 11:20:52 -0700971 expected := new(Int)
Evan Shaw28a09712010-08-09 10:21:54 -0700972 expected.SetString(exp, 0)
Evan Shaw4d1b1572010-05-03 11:20:52 -0700973
Evan Shaw28a09712010-08-09 10:21:54 -0700974 self = f(self, self, y)
975 if self.Cmp(expected) != 0 {
976 t.Errorf("%s: got %s want %s", msg, self, expected)
Evan Shaw4d1b1572010-05-03 11:20:52 -0700977 }
978}
979
Roger Peppe83fd82b2011-05-17 13:38:21 -0700980func altBit(x *Int, i int) uint {
981 z := new(Int).Rsh(x, uint(i))
982 z = z.And(z, NewInt(1))
983 if z.Cmp(new(Int)) != 0 {
984 return 1
985 }
986 return 0
987}
988
989func altSetBit(z *Int, x *Int, i int, b uint) *Int {
990 one := NewInt(1)
991 m := one.Lsh(one, uint(i))
992 switch b {
993 case 1:
994 return z.Or(x, m)
995 case 0:
996 return z.AndNot(x, m)
997 }
998 panic("set bit is not 0 or 1")
999}
1000
1001func testBitset(t *testing.T, x *Int) {
1002 n := x.BitLen()
1003 z := new(Int).Set(x)
1004 z1 := new(Int).Set(x)
1005 for i := 0; i < n+10; i++ {
1006 old := z.Bit(i)
1007 old1 := altBit(z1, i)
1008 if old != old1 {
1009 t.Errorf("bitset: inconsistent value for Bit(%s, %d), got %v want %v", z1, i, old, old1)
1010 }
1011 z := new(Int).SetBit(z, i, 1)
1012 z1 := altSetBit(new(Int), z1, i, 1)
1013 if z.Bit(i) == 0 {
1014 t.Errorf("bitset: bit %d of %s got 0 want 1", i, x)
1015 }
1016 if z.Cmp(z1) != 0 {
1017 t.Errorf("bitset: inconsistent value after SetBit 1, got %s want %s", z, z1)
1018 }
1019 z.SetBit(z, i, 0)
1020 altSetBit(z1, z1, i, 0)
1021 if z.Bit(i) != 0 {
1022 t.Errorf("bitset: bit %d of %s got 1 want 0", i, x)
1023 }
1024 if z.Cmp(z1) != 0 {
1025 t.Errorf("bitset: inconsistent value after SetBit 0, got %s want %s", z, z1)
1026 }
1027 altSetBit(z1, z1, i, old)
1028 z.SetBit(z, i, old)
1029 if z.Cmp(z1) != 0 {
1030 t.Errorf("bitset: inconsistent value after SetBit old, got %s want %s", z, z1)
1031 }
1032 }
1033 if z.Cmp(x) != 0 {
1034 t.Errorf("bitset: got %s want %s", z, x)
1035 }
1036}
1037
1038var bitsetTests = []struct {
1039 x string
1040 i int
1041 b uint
1042}{
1043 {"0", 0, 0},
1044 {"0", 200, 0},
1045 {"1", 0, 1},
1046 {"1", 1, 0},
1047 {"-1", 0, 1},
1048 {"-1", 200, 1},
1049 {"0x2000000000000000000000000000", 108, 0},
1050 {"0x2000000000000000000000000000", 109, 1},
1051 {"0x2000000000000000000000000000", 110, 0},
1052 {"-0x2000000000000000000000000001", 108, 1},
1053 {"-0x2000000000000000000000000001", 109, 0},
1054 {"-0x2000000000000000000000000001", 110, 1},
1055}
1056
1057func TestBitSet(t *testing.T) {
1058 for _, test := range bitwiseTests {
1059 x := new(Int)
1060 x.SetString(test.x, 0)
1061 testBitset(t, x)
1062 x = new(Int)
1063 x.SetString(test.y, 0)
1064 testBitset(t, x)
1065 }
1066 for i, test := range bitsetTests {
1067 x := new(Int)
1068 x.SetString(test.x, 0)
1069 b := x.Bit(test.i)
1070 if b != test.b {
Roger Peppeca6de002011-11-30 09:29:58 -08001071 t.Errorf("#%d got %v want %v", i, b, test.b)
Roger Peppe83fd82b2011-05-17 13:38:21 -07001072 }
1073 }
Roger Peppeca6de002011-11-30 09:29:58 -08001074 z := NewInt(1)
1075 z.SetBit(NewInt(0), 2, 1)
1076 if z.Cmp(NewInt(4)) != 0 {
1077 t.Errorf("destination leaked into result; got %s want 4", z)
1078 }
Roger Peppe83fd82b2011-05-17 13:38:21 -07001079}
1080
1081func BenchmarkBitset(b *testing.B) {
1082 z := new(Int)
1083 z.SetBit(z, 512, 1)
1084 b.ResetTimer()
1085 b.StartTimer()
1086 for i := b.N - 1; i >= 0; i-- {
1087 z.SetBit(z, i&512, 1)
1088 }
1089}
1090
1091func BenchmarkBitsetNeg(b *testing.B) {
1092 z := NewInt(-1)
1093 z.SetBit(z, 512, 0)
1094 b.ResetTimer()
1095 b.StartTimer()
1096 for i := b.N - 1; i >= 0; i-- {
1097 z.SetBit(z, i&512, 0)
1098 }
1099}
1100
1101func BenchmarkBitsetOrig(b *testing.B) {
1102 z := new(Int)
1103 altSetBit(z, z, 512, 1)
1104 b.ResetTimer()
1105 b.StartTimer()
1106 for i := b.N - 1; i >= 0; i-- {
1107 altSetBit(z, z, i&512, 1)
1108 }
1109}
1110
1111func BenchmarkBitsetNegOrig(b *testing.B) {
1112 z := NewInt(-1)
1113 altSetBit(z, z, 512, 0)
1114 b.ResetTimer()
1115 b.StartTimer()
1116 for i := b.N - 1; i >= 0; i-- {
1117 altSetBit(z, z, i&512, 0)
1118 }
1119}
Evan Shaw4d1b1572010-05-03 11:20:52 -07001120
1121func TestBitwise(t *testing.T) {
1122 x := new(Int)
1123 y := new(Int)
1124 for _, test := range bitwiseTests {
Evan Shaw28a09712010-08-09 10:21:54 -07001125 x.SetString(test.x, 0)
1126 y.SetString(test.y, 0)
Evan Shaw4d1b1572010-05-03 11:20:52 -07001127
1128 testBitFun(t, "and", (*Int).And, x, y, test.and)
1129 testBitFunSelf(t, "and", (*Int).And, x, y, test.and)
1130 testBitFun(t, "andNot", (*Int).AndNot, x, y, test.andNot)
1131 testBitFunSelf(t, "andNot", (*Int).AndNot, x, y, test.andNot)
1132 testBitFun(t, "or", (*Int).Or, x, y, test.or)
1133 testBitFunSelf(t, "or", (*Int).Or, x, y, test.or)
1134 testBitFun(t, "xor", (*Int).Xor, x, y, test.xor)
1135 testBitFunSelf(t, "xor", (*Int).Xor, x, y, test.xor)
1136 }
1137}
1138
Robert Griesemere3515332010-10-25 17:45:43 -07001139var notTests = []struct {
Evan Shaw4d1b1572010-05-03 11:20:52 -07001140 in string
1141 out string
Robert Griesemere3515332010-10-25 17:45:43 -07001142}{
Robert Griesemer34788912010-10-22 10:06:33 -07001143 {"0", "-1"},
1144 {"1", "-2"},
1145 {"7", "-8"},
1146 {"0", "-1"},
1147 {"-81910", "81909"},
1148 {
Evan Shaw4d1b1572010-05-03 11:20:52 -07001149 "298472983472983471903246121093472394872319615612417471234712061",
1150 "-298472983472983471903246121093472394872319615612417471234712062",
1151 },
1152}
1153
1154func TestNot(t *testing.T) {
1155 in := new(Int)
1156 out := new(Int)
1157 expected := new(Int)
1158 for i, test := range notTests {
1159 in.SetString(test.in, 10)
1160 expected.SetString(test.out, 10)
1161 out = out.Not(in)
1162 if out.Cmp(expected) != 0 {
1163 t.Errorf("#%d: got %s want %s", i, out, expected)
1164 }
1165 out = out.Not(out)
1166 if out.Cmp(in) != 0 {
1167 t.Errorf("#%d: got %s want %s", i, out, in)
1168 }
1169 }
1170}
Adam Langleyf199f292010-05-26 15:58:58 -04001171
Robert Griesemere3515332010-10-25 17:45:43 -07001172var modInverseTests = []struct {
Adam Langleyf199f292010-05-26 15:58:58 -04001173 element string
Keith Randall96d1e4a2014-10-14 14:09:56 -07001174 modulus string
Robert Griesemere3515332010-10-25 17:45:43 -07001175}{
Keith Randall96d1e4a2014-10-14 14:09:56 -07001176 {"1234567", "458948883992"},
Robert Griesemer34788912010-10-22 10:06:33 -07001177 {"239487239847", "2410312426921032588552076022197566074856950548502459942654116941958108831682612228890093858261341614673227141477904012196503648957050582631942730706805009223062734745341073406696246014589361659774041027169249453200378729434170325843778659198143763193776859869524088940195577346119843545301547043747207749969763750084308926339295559968882457872412993810129130294592999947926365264059284647209730384947211681434464714438488520940127459844288859336526896320919633919"},
Adam Langleyf199f292010-05-26 15:58:58 -04001178}
1179
1180func TestModInverse(t *testing.T) {
Keith Randall96d1e4a2014-10-14 14:09:56 -07001181 var element, modulus, gcd, inverse Int
Adam Langleyf199f292010-05-26 15:58:58 -04001182 one := NewInt(1)
1183 for i, test := range modInverseTests {
1184 (&element).SetString(test.element, 10)
Keith Randall96d1e4a2014-10-14 14:09:56 -07001185 (&modulus).SetString(test.modulus, 10)
1186 (&inverse).ModInverse(&element, &modulus)
1187 (&inverse).Mul(&inverse, &element)
1188 (&inverse).Mod(&inverse, &modulus)
1189 if (&inverse).Cmp(one) != 0 {
1190 t.Errorf("#%d: failed (e·e^(-1)=%s)", i, &inverse)
1191 }
1192 }
1193 // exhaustive test for small values
1194 for n := 2; n < 100; n++ {
1195 (&modulus).SetInt64(int64(n))
1196 for x := 1; x < n; x++ {
1197 (&element).SetInt64(int64(x))
1198 (&gcd).GCD(nil, nil, &element, &modulus)
1199 if (&gcd).Cmp(one) != 0 {
1200 continue
1201 }
1202 (&inverse).ModInverse(&element, &modulus)
1203 (&inverse).Mul(&inverse, &element)
1204 (&inverse).Mod(&inverse, &modulus)
1205 if (&inverse).Cmp(one) != 0 {
1206 t.Errorf("ModInverse(%d,%d)*%d%%%d=%d, not 1", &element, &modulus, &element, &modulus, &inverse)
1207 }
Adam Langleyf199f292010-05-26 15:58:58 -04001208 }
1209 }
1210}
Robert Griesemer758d0552011-03-08 17:27:44 -08001211
Robert Griesemer13a59b82012-05-22 17:20:37 -07001212var encodingTests = []string{
1213 "-539345864568634858364538753846587364875430589374589",
1214 "-678645873",
1215 "-100",
1216 "-2",
1217 "-1",
Robert Griesemer758d0552011-03-08 17:27:44 -08001218 "0",
1219 "1",
1220 "2",
1221 "10",
1222 "42",
1223 "1234567890",
1224 "298472983472983471903246121093472394872319615612417471234712061",
1225}
1226
Robert Griesemer21032eb2011-06-08 09:10:01 -07001227func TestIntGobEncoding(t *testing.T) {
Robert Griesemer758d0552011-03-08 17:27:44 -08001228 var medium bytes.Buffer
1229 enc := gob.NewEncoder(&medium)
1230 dec := gob.NewDecoder(&medium)
Robert Griesemer13a59b82012-05-22 17:20:37 -07001231 for _, test := range encodingTests {
1232 medium.Reset() // empty buffer for each test case (in case of failures)
1233 var tx Int
1234 tx.SetString(test, 10)
1235 if err := enc.Encode(&tx); err != nil {
1236 t.Errorf("encoding of %s failed: %s", &tx, err)
1237 }
1238 var rx Int
1239 if err := dec.Decode(&rx); err != nil {
1240 t.Errorf("decoding of %s failed: %s", &tx, err)
1241 }
1242 if rx.Cmp(&tx) != 0 {
1243 t.Errorf("transmission of %s failed: got %s want %s", &tx, &rx, &tx)
1244 }
1245 }
1246}
1247
Rob Pikebc6bb3e2013-08-19 11:22:09 +10001248// Sending a nil Int pointer (inside a slice) on a round trip through gob should yield a zero.
1249// TODO: top-level nils.
1250func TestGobEncodingNilIntInSlice(t *testing.T) {
1251 buf := new(bytes.Buffer)
1252 enc := gob.NewEncoder(buf)
1253 dec := gob.NewDecoder(buf)
1254
1255 var in = make([]*Int, 1)
1256 err := enc.Encode(&in)
1257 if err != nil {
1258 t.Errorf("gob encode failed: %q", err)
1259 }
1260 var out []*Int
1261 err = dec.Decode(&out)
1262 if err != nil {
1263 t.Fatalf("gob decode failed: %q", err)
1264 }
1265 if len(out) != 1 {
1266 t.Fatalf("wrong len; want 1 got %d", len(out))
1267 }
1268 var zero Int
1269 if out[0].Cmp(&zero) != 0 {
1270 t.Errorf("transmission of (*Int)(nill) failed: got %s want 0", out)
1271 }
1272}
1273
Robert Griesemer13a59b82012-05-22 17:20:37 -07001274func TestIntJSONEncoding(t *testing.T) {
1275 for _, test := range encodingTests {
1276 var tx Int
1277 tx.SetString(test, 10)
1278 b, err := json.Marshal(&tx)
1279 if err != nil {
1280 t.Errorf("marshaling of %s failed: %s", &tx, err)
1281 }
1282 var rx Int
1283 if err := json.Unmarshal(b, &rx); err != nil {
1284 t.Errorf("unmarshaling of %s failed: %s", &tx, err)
1285 }
1286 if rx.Cmp(&tx) != 0 {
1287 t.Errorf("JSON encoding of %s failed: got %s want %s", &tx, &rx, &tx)
Robert Griesemer758d0552011-03-08 17:27:44 -08001288 }
1289 }
1290}
Robert Griesemerfc78c5a2011-12-22 14:15:41 -08001291
Michael T. Jones1dc82d22014-02-14 12:57:03 -08001292var intVals = []string{
1293 "-141592653589793238462643383279502884197169399375105820974944592307816406286",
1294 "-1415926535897932384626433832795028841971",
1295 "-141592653589793",
1296 "-1",
1297 "0",
1298 "1",
1299 "141592653589793",
1300 "1415926535897932384626433832795028841971",
1301 "141592653589793238462643383279502884197169399375105820974944592307816406286",
1302}
1303
1304func TestIntJSONEncodingTextMarshaller(t *testing.T) {
1305 for _, num := range intVals {
1306 var tx Int
1307 tx.SetString(num, 0)
1308 b, err := json.Marshal(&tx)
1309 if err != nil {
1310 t.Errorf("marshaling of %s failed: %s", &tx, err)
1311 continue
1312 }
1313 var rx Int
1314 if err := json.Unmarshal(b, &rx); err != nil {
1315 t.Errorf("unmarshaling of %s failed: %s", &tx, err)
1316 continue
1317 }
1318 if rx.Cmp(&tx) != 0 {
1319 t.Errorf("JSON encoding of %s failed: got %s want %s", &tx, &rx, &tx)
1320 }
1321 }
1322}
1323
1324func TestIntXMLEncodingTextMarshaller(t *testing.T) {
1325 for _, num := range intVals {
1326 var tx Int
1327 tx.SetString(num, 0)
1328 b, err := xml.Marshal(&tx)
1329 if err != nil {
1330 t.Errorf("marshaling of %s failed: %s", &tx, err)
1331 continue
1332 }
1333 var rx Int
1334 if err := xml.Unmarshal(b, &rx); err != nil {
1335 t.Errorf("unmarshaling of %s failed: %s", &tx, err)
1336 continue
1337 }
1338 if rx.Cmp(&tx) != 0 {
1339 t.Errorf("XML encoding of %s failed: got %s want %s", &tx, &rx, &tx)
1340 }
1341 }
1342}
1343
Robert Griesemerfc78c5a2011-12-22 14:15:41 -08001344func TestIssue2607(t *testing.T) {
1345 // This code sequence used to hang.
1346 n := NewInt(10)
1347 n.Rand(rand.New(rand.NewSource(9)), n)
1348}