Skip to content

Commit fdbaf62

Browse files
committed
rearrange tests file to match math/big version
1 parent f7aedbc commit fdbaf62

File tree

1 file changed

+100
-95
lines changed

1 file changed

+100
-95
lines changed

int_test.go

+100-95
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// 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+
15
package gmp
26

37
import (
@@ -219,6 +223,101 @@ func TestMulRangeZ(t *testing.T) {
219223
}
220224
}
221225

226+
var stringTests = []struct {
227+
in string
228+
out string
229+
base int
230+
val int64
231+
ok bool
232+
}{
233+
{in: "", ok: false},
234+
{in: "a", ok: false},
235+
{in: "z", ok: false},
236+
{in: "+", ok: false},
237+
{in: "-", ok: false},
238+
{in: "0b", ok: false},
239+
{in: "0x", ok: false},
240+
{in: "2", base: 2, ok: false},
241+
{in: "0b2", base: 0, ok: false},
242+
{in: "08", ok: false},
243+
{in: "8", base: 8, ok: false},
244+
{in: "0xg", base: 0, ok: false},
245+
{in: "g", base: 16, ok: false},
246+
{"0", "0", 0, 0, true},
247+
{"0", "0", 10, 0, true},
248+
{"0", "0", 16, 0, true},
249+
{"+0", "0", 0, 0, true},
250+
{"-0", "0", 0, 0, true},
251+
{"10", "10", 0, 10, true},
252+
{"10", "10", 10, 10, true},
253+
{"10", "10", 16, 16, true},
254+
{"-10", "-10", 16, -16, true},
255+
{"+10", "10", 16, 16, true},
256+
{"0x10", "16", 0, 16, true},
257+
{in: "0x10", base: 16, ok: false},
258+
{"-0x10", "-16", 0, -16, true},
259+
{"+0x10", "16", 0, 16, true},
260+
{"00", "0", 0, 0, true},
261+
{"0", "0", 8, 0, true},
262+
{"07", "7", 0, 7, true},
263+
{"7", "7", 8, 7, true},
264+
{"023", "19", 0, 19, true},
265+
{"23", "23", 8, 19, true},
266+
{"cafebabe", "cafebabe", 16, 0xcafebabe, true},
267+
{"0b0", "0", 0, 0, true},
268+
{"-111", "-111", 2, -7, true},
269+
{"-0b111", "-7", 0, -7, true},
270+
{"0b1001010111", "599", 0, 0x257, true},
271+
{"1001010111", "1001010111", 2, 0x257, true},
272+
}
273+
274+
func format(base int) string {
275+
switch base {
276+
case 2:
277+
return "%b"
278+
case 8:
279+
return "%o"
280+
case 16:
281+
return "%x"
282+
}
283+
return "%d"
284+
}
285+
286+
func TestSetString(t *testing.T) {
287+
tmp := new(Int)
288+
for i, test := range stringTests {
289+
// initialize to a non-zero value so that issues with parsing
290+
// 0 are detected
291+
tmp.SetInt64(1234567890)
292+
n1, ok1 := new(Int).SetString(test.in, test.base)
293+
n2, ok2 := tmp.SetString(test.in, test.base)
294+
expected := NewInt(test.val)
295+
if ok1 != test.ok || ok2 != test.ok {
296+
t.Errorf("#%d (input '%s') ok incorrect (should be %t)", i, test.in, test.ok)
297+
continue
298+
}
299+
if !ok1 {
300+
if n1 != nil {
301+
t.Errorf("#%d (input '%s') n1 != nil", i, test.in)
302+
}
303+
continue
304+
}
305+
if !ok2 {
306+
if n2 != nil {
307+
t.Errorf("#%d (input '%s') n2 != nil", i, test.in)
308+
}
309+
continue
310+
}
311+
312+
if n1.Cmp(expected) != 0 {
313+
t.Errorf("#%d (input '%s') got: %s want: %d", i, test.in, n1, test.val)
314+
}
315+
if n2.Cmp(expected) != 0 {
316+
t.Errorf("#%d (input '%s') got: %s want: %d", i, test.in, n2, test.val)
317+
}
318+
}
319+
}
320+
222321
// Examples from the Go Language Spec, section "Arithmetic operators"
223322
var divisionSignsTests = []struct {
224323
x, y int64
@@ -520,6 +619,7 @@ func testGcd(t *testing.T, d, x, y, a, b *Int) {
520619
if y != nil {
521620
Y = new(Int)
522621
}
622+
523623
D := new(Int).GCD(X, Y, a, b)
524624
if D.Cmp(d) != 0 {
525625
t.Errorf("GCD(%s, %s): got d = %s, want %s", a, b, D, d)
@@ -549,101 +649,6 @@ func TestGcd(t *testing.T) {
549649
quick.Check(checkGcd, nil)
550650
}
551651

552-
var stringTests = []struct {
553-
in string
554-
out string
555-
base int
556-
val int64
557-
ok bool
558-
}{
559-
{in: "", ok: false},
560-
{in: "a", ok: false},
561-
{in: "z", ok: false},
562-
{in: "+", ok: false},
563-
{in: "-", ok: false},
564-
{in: "0b", ok: false},
565-
{in: "0x", ok: false},
566-
{in: "2", base: 2, ok: false},
567-
{in: "0b2", base: 0, ok: false},
568-
{in: "08", ok: false},
569-
{in: "8", base: 8, ok: false},
570-
{in: "0xg", base: 0, ok: false},
571-
{in: "g", base: 16, ok: false},
572-
{"0", "0", 0, 0, true},
573-
{"0", "0", 10, 0, true},
574-
{"0", "0", 16, 0, true},
575-
{"+0", "0", 0, 0, true},
576-
{"-0", "0", 0, 0, true},
577-
{"10", "10", 0, 10, true},
578-
{"10", "10", 10, 10, true},
579-
{"10", "10", 16, 16, true},
580-
{"-10", "-10", 16, -16, true},
581-
{"+10", "10", 16, 16, true},
582-
{"0x10", "16", 0, 16, true},
583-
{in: "0x10", base: 16, ok: false},
584-
{"-0x10", "-16", 0, -16, true},
585-
{"+0x10", "16", 0, 16, true},
586-
{"00", "0", 0, 0, true},
587-
{"0", "0", 8, 0, true},
588-
{"07", "7", 0, 7, true},
589-
{"7", "7", 8, 7, true},
590-
{"023", "19", 0, 19, true},
591-
{"23", "23", 8, 19, true},
592-
{"cafebabe", "cafebabe", 16, 0xcafebabe, true},
593-
{"0b0", "0", 0, 0, true},
594-
{"-111", "-111", 2, -7, true},
595-
{"-0b111", "-7", 0, -7, true},
596-
{"0b1001010111", "599", 0, 0x257, true},
597-
{"1001010111", "1001010111", 2, 0x257, true},
598-
}
599-
600-
func format(base int) string {
601-
switch base {
602-
case 2:
603-
return "%b"
604-
case 8:
605-
return "%o"
606-
case 16:
607-
return "%x"
608-
}
609-
return "%d"
610-
}
611-
612-
func TestSetString(t *testing.T) {
613-
tmp := new(Int)
614-
for i, test := range stringTests {
615-
// initialize to a non-zero value so that issues with parsing
616-
// 0 are detected
617-
tmp.SetInt64(1234567890)
618-
n1, ok1 := new(Int).SetString(test.in, test.base)
619-
n2, ok2 := tmp.SetString(test.in, test.base)
620-
expected := NewInt(test.val)
621-
if ok1 != test.ok || ok2 != test.ok {
622-
t.Errorf("#%d (input '%s') ok incorrect (should be %t)", i, test.in, test.ok)
623-
continue
624-
}
625-
if !ok1 {
626-
if n1 != nil {
627-
t.Errorf("#%d (input '%s') n1 != nil", i, test.in)
628-
}
629-
continue
630-
}
631-
if !ok2 {
632-
if n2 != nil {
633-
t.Errorf("#%d (input '%s') n2 != nil", i, test.in)
634-
}
635-
continue
636-
}
637-
638-
if n1.Cmp(expected) != 0 {
639-
t.Errorf("#%d (input '%s') got: %s want: %d", i, test.in, n1, test.val)
640-
}
641-
if n2.Cmp(expected) != 0 {
642-
t.Errorf("#%d (input '%s') got: %s want: %d", i, test.in, n2, test.val)
643-
}
644-
}
645-
}
646-
647652
var primes = []string{
648653
"2",
649654
"3",

0 commit comments

Comments
 (0)