2009-08-14 12:53:27 -06:00
|
|
|
// Copyright 2009 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 "testing"
|
|
|
|
|
2010-04-21 18:12:36 -06:00
|
|
|
type cmpTest struct {
|
2010-04-22 17:57:29 -06:00
|
|
|
x, y nat
|
2010-04-21 18:12:36 -06:00
|
|
|
r int
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var cmpTests = []cmpTest{
|
|
|
|
cmpTest{nil, nil, 0},
|
2010-04-22 17:57:29 -06:00
|
|
|
cmpTest{nil, nat{}, 0},
|
|
|
|
cmpTest{nat{}, nil, 0},
|
|
|
|
cmpTest{nat{}, nat{}, 0},
|
|
|
|
cmpTest{nat{0}, nat{0}, 0},
|
|
|
|
cmpTest{nat{0}, nat{1}, -1},
|
|
|
|
cmpTest{nat{1}, nat{0}, 1},
|
|
|
|
cmpTest{nat{1}, nat{1}, 0},
|
|
|
|
cmpTest{nat{0, _M}, nat{1}, 1},
|
|
|
|
cmpTest{nat{1}, nat{0, _M}, -1},
|
|
|
|
cmpTest{nat{1, _M}, nat{0, _M}, 1},
|
|
|
|
cmpTest{nat{0, _M}, nat{1, _M}, -1},
|
|
|
|
cmpTest{nat{16, 571956, 8794, 68}, nat{837, 9146, 1, 754489}, -1},
|
|
|
|
cmpTest{nat{34986, 41, 105, 1957}, nat{56, 7458, 104, 1957}, 1},
|
2010-04-21 18:12:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-22 17:57:29 -06:00
|
|
|
func TestCmp(t *testing.T) {
|
2010-04-21 18:12:36 -06:00
|
|
|
for i, a := range cmpTests {
|
2010-04-22 17:57:29 -06:00
|
|
|
r := a.x.cmp(a.y)
|
2010-04-21 18:12:36 -06:00
|
|
|
if r != a.r {
|
|
|
|
t.Errorf("#%d got r = %v; want %v", i, r, a.r)
|
|
|
|
}
|
|
|
|
}
|
2009-08-14 12:53:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-22 17:57:29 -06:00
|
|
|
type funNN func(z, x, y nat) nat
|
2009-10-06 12:42:55 -06:00
|
|
|
type argNN struct {
|
2010-04-22 17:57:29 -06:00
|
|
|
z, x, y nat
|
2009-10-06 12:42:55 -06:00
|
|
|
}
|
2009-08-14 12:53:27 -06:00
|
|
|
|
2009-11-05 16:55:41 -07:00
|
|
|
|
2009-08-14 12:53:27 -06:00
|
|
|
var sumNN = []argNN{
|
|
|
|
argNN{},
|
2010-04-22 17:57:29 -06:00
|
|
|
argNN{nat{1}, nil, nat{1}},
|
|
|
|
argNN{nat{1111111110}, nat{123456789}, nat{987654321}},
|
|
|
|
argNN{nat{0, 0, 0, 1}, nil, nat{0, 0, 0, 1}},
|
|
|
|
argNN{nat{0, 0, 0, 1111111110}, nat{0, 0, 0, 123456789}, nat{0, 0, 0, 987654321}},
|
|
|
|
argNN{nat{0, 0, 0, 1}, nat{0, 0, _M}, nat{0, 0, 1}},
|
2009-08-14 12:53:27 -06:00
|
|
|
}
|
|
|
|
|
2009-11-05 16:55:41 -07:00
|
|
|
|
2009-10-06 12:42:55 -06:00
|
|
|
var prodNN = []argNN{
|
2009-08-18 11:06:15 -06:00
|
|
|
argNN{},
|
|
|
|
argNN{nil, nil, nil},
|
2010-04-22 17:57:29 -06:00
|
|
|
argNN{nil, nat{991}, nil},
|
|
|
|
argNN{nat{991}, nat{991}, nat{1}},
|
|
|
|
argNN{nat{991 * 991}, nat{991}, nat{991}},
|
|
|
|
argNN{nat{0, 0, 991 * 991}, nat{0, 991}, nat{0, 991}},
|
|
|
|
argNN{nat{1 * 991, 2 * 991, 3 * 991, 4 * 991}, nat{1, 2, 3, 4}, nat{991}},
|
|
|
|
argNN{nat{4, 11, 20, 30, 20, 11, 4}, nat{1, 2, 3, 4}, nat{4, 3, 2, 1}},
|
2009-08-18 11:06:15 -06:00
|
|
|
}
|
|
|
|
|
2009-08-14 12:53:27 -06:00
|
|
|
|
2010-04-22 17:57:29 -06:00
|
|
|
func TestSet(t *testing.T) {
|
2009-08-14 12:53:27 -06:00
|
|
|
for _, a := range sumNN {
|
2010-04-22 17:57:29 -06:00
|
|
|
z := nat(nil).set(a.z)
|
|
|
|
if z.cmp(a.z) != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("got z = %v; want %v", z, a.z)
|
2009-08-14 12:53:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func testFunNN(t *testing.T, msg string, f funNN, a argNN) {
|
2009-12-15 16:33:31 -07:00
|
|
|
z := f(nil, a.x, a.y)
|
2010-04-22 17:57:29 -06:00
|
|
|
if z.cmp(a.z) != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("%s%+v\n\tgot z = %v; want %v", msg, a, z, a.z)
|
2009-08-14 12:53:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestFunNN(t *testing.T) {
|
|
|
|
for _, a := range sumNN {
|
2009-12-15 16:33:31 -07:00
|
|
|
arg := a
|
2010-04-22 17:57:29 -06:00
|
|
|
testFunNN(t, "add", nat.add, arg)
|
2009-08-14 12:53:27 -06:00
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
arg = argNN{a.z, a.y, a.x}
|
2010-04-22 17:57:29 -06:00
|
|
|
testFunNN(t, "add symmetric", nat.add, arg)
|
2009-08-14 12:53:27 -06:00
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
arg = argNN{a.x, a.z, a.y}
|
2010-04-22 17:57:29 -06:00
|
|
|
testFunNN(t, "sub", nat.sub, arg)
|
2009-08-14 12:53:27 -06:00
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
arg = argNN{a.y, a.z, a.x}
|
2010-04-22 17:57:29 -06:00
|
|
|
testFunNN(t, "sub symmetric", nat.sub, arg)
|
2009-08-14 12:53:27 -06:00
|
|
|
}
|
2009-08-18 11:06:15 -06:00
|
|
|
|
|
|
|
for _, a := range prodNN {
|
2009-12-15 16:33:31 -07:00
|
|
|
arg := a
|
2010-04-22 17:57:29 -06:00
|
|
|
testFunNN(t, "mul", nat.mul, arg)
|
2009-08-18 11:06:15 -06:00
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
arg = argNN{a.z, a.y, a.x}
|
2010-04-22 17:57:29 -06:00
|
|
|
testFunNN(t, "mul symmetric", nat.mul, arg)
|
2009-08-18 11:06:15 -06:00
|
|
|
}
|
2009-08-14 12:53:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-27 20:16:08 -06:00
|
|
|
type mulRange struct {
|
|
|
|
a, b uint64
|
|
|
|
prod string
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var mulRanges = []mulRange{
|
|
|
|
mulRange{0, 0, "0"},
|
|
|
|
mulRange{1, 1, "1"},
|
|
|
|
mulRange{1, 2, "2"},
|
|
|
|
mulRange{1, 3, "6"},
|
|
|
|
mulRange{1, 3, "6"},
|
|
|
|
mulRange{10, 10, "10"},
|
|
|
|
mulRange{0, 100, "0"},
|
|
|
|
mulRange{0, 1e9, "0"},
|
|
|
|
mulRange{100, 1, "1"}, // empty range
|
|
|
|
mulRange{1, 10, "3628800"}, // 10!
|
|
|
|
mulRange{1, 20, "2432902008176640000"}, // 20!
|
|
|
|
mulRange{1, 100,
|
|
|
|
"933262154439441526816992388562667004907159682643816214685929" +
|
|
|
|
"638952175999932299156089414639761565182862536979208272237582" +
|
|
|
|
"51185210916864000000000000000000000000", // 100!
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestMulRange(t *testing.T) {
|
|
|
|
for i, r := range mulRanges {
|
|
|
|
prod := nat(nil).mulRange(r.a, r.b).string(10)
|
|
|
|
if prod != r.prod {
|
|
|
|
t.Errorf("%d: got %s; want %s", i, prod, r.prod)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-30 12:54:27 -06:00
|
|
|
var mulArg, mulTmp nat
|
2010-04-27 20:16:08 -06:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
const n = 1000
|
|
|
|
mulArg = make(nat, n)
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
mulArg[i] = _M
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-30 12:54:27 -06:00
|
|
|
func benchmarkMulLoad() {
|
|
|
|
for j := 1; j <= 10; j++ {
|
|
|
|
x := mulArg[0 : j*100]
|
|
|
|
mulTmp.mul(x, x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-27 20:16:08 -06:00
|
|
|
func BenchmarkMul(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
2010-04-30 12:54:27 -06:00
|
|
|
benchmarkMulLoad()
|
2010-04-27 20:16:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-06 12:42:55 -06:00
|
|
|
type strN struct {
|
2010-04-22 17:57:29 -06:00
|
|
|
x nat
|
2009-12-15 16:33:31 -07:00
|
|
|
b int
|
|
|
|
s string
|
2009-10-06 12:42:55 -06:00
|
|
|
}
|
2009-10-06 15:55:39 -06:00
|
|
|
|
2009-11-05 16:55:41 -07:00
|
|
|
|
2009-08-14 12:53:27 -06:00
|
|
|
var tabN = []strN{
|
2009-10-06 12:42:55 -06:00
|
|
|
strN{nil, 10, "0"},
|
2010-04-22 17:57:29 -06:00
|
|
|
strN{nat{1}, 10, "1"},
|
|
|
|
strN{nat{10}, 10, "10"},
|
|
|
|
strN{nat{1234567890}, 10, "1234567890"},
|
2009-08-14 12:53:27 -06:00
|
|
|
}
|
|
|
|
|
2009-11-05 16:55:41 -07:00
|
|
|
|
2010-04-22 17:57:29 -06:00
|
|
|
func TestString(t *testing.T) {
|
2009-08-14 12:53:27 -06:00
|
|
|
for _, a := range tabN {
|
2010-04-22 17:57:29 -06:00
|
|
|
s := a.x.string(a.b)
|
2009-08-14 12:53:27 -06:00
|
|
|
if s != a.s {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("stringN%+v\n\tgot s = %s; want %s", a, s, a.s)
|
2009-08-14 12:53:27 -06:00
|
|
|
}
|
2009-08-15 12:43:54 -06:00
|
|
|
|
2010-04-22 17:57:29 -06:00
|
|
|
x, b, n := nat(nil).scan(a.s, a.b)
|
|
|
|
if x.cmp(a.x) != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("scanN%+v\n\tgot z = %v; want %v", a, x, a.x)
|
2009-08-15 12:43:54 -06:00
|
|
|
}
|
|
|
|
if b != a.b {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("scanN%+v\n\tgot b = %d; want %d", a, b, a.b)
|
2009-08-15 12:43:54 -06:00
|
|
|
}
|
|
|
|
if n != len(a.s) {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("scanN%+v\n\tgot n = %d; want %d", a, n, len(a.s))
|
2009-08-15 12:43:54 -06:00
|
|
|
}
|
2009-08-14 12:53:27 -06:00
|
|
|
}
|
|
|
|
}
|
2009-11-05 16:55:41 -07:00
|
|
|
|
|
|
|
|
|
|
|
func TestLeadingZeroBits(t *testing.T) {
|
2009-12-15 16:33:31 -07:00
|
|
|
var x Word = 1 << (_W - 1)
|
2009-11-11 14:21:37 -07:00
|
|
|
for i := 0; i <= _W; i++ {
|
2009-11-05 16:55:41 -07:00
|
|
|
if leadingZeroBits(x) != i {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("failed at %x: got %d want %d", x, leadingZeroBits(x), i)
|
2009-11-05 16:55:41 -07:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
x >>= 1
|
2009-11-05 16:55:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type shiftTest struct {
|
2010-04-22 17:57:29 -06:00
|
|
|
in nat
|
2010-04-20 21:39:36 -06:00
|
|
|
shift uint
|
2010-04-22 17:57:29 -06:00
|
|
|
out nat
|
2009-11-05 16:55:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var leftShiftTests = []shiftTest{
|
|
|
|
shiftTest{nil, 0, nil},
|
|
|
|
shiftTest{nil, 1, nil},
|
2010-04-30 22:25:48 -06:00
|
|
|
shiftTest{natOne, 0, natOne},
|
|
|
|
shiftTest{natOne, 1, natTwo},
|
2010-04-22 17:57:29 -06:00
|
|
|
shiftTest{nat{1 << (_W - 1)}, 1, nat{0}},
|
|
|
|
shiftTest{nat{1 << (_W - 1), 0}, 1, nat{0, 1}},
|
2009-11-05 16:55:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestShiftLeft(t *testing.T) {
|
|
|
|
for i, test := range leftShiftTests {
|
2010-04-30 22:25:48 -06:00
|
|
|
var z nat
|
|
|
|
z = z.shl(test.in, test.shift)
|
|
|
|
for j, d := range test.out {
|
|
|
|
if j >= len(z) || z[j] != d {
|
|
|
|
t.Errorf("#%d: got: %v want: %v", i, z, test.out)
|
2009-12-15 16:33:31 -07:00
|
|
|
break
|
2009-11-05 16:55:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var rightShiftTests = []shiftTest{
|
|
|
|
shiftTest{nil, 0, nil},
|
|
|
|
shiftTest{nil, 1, nil},
|
2010-04-30 22:25:48 -06:00
|
|
|
shiftTest{natOne, 0, natOne},
|
|
|
|
shiftTest{natOne, 1, nil},
|
|
|
|
shiftTest{natTwo, 1, natOne},
|
|
|
|
shiftTest{nat{0, 1}, 1, nat{1 << (_W - 1)}},
|
|
|
|
shiftTest{nat{2, 1, 1}, 1, nat{1<<(_W-1) + 1, 1 << (_W - 1)}},
|
2009-11-05 16:55:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestShiftRight(t *testing.T) {
|
|
|
|
for i, test := range rightShiftTests {
|
2010-04-30 22:25:48 -06:00
|
|
|
var z nat
|
|
|
|
z = z.shr(test.in, test.shift)
|
|
|
|
for j, d := range test.out {
|
|
|
|
if j >= len(z) || z[j] != d {
|
|
|
|
t.Errorf("#%d: got: %v want: %v", i, z, test.out)
|
2009-12-15 16:33:31 -07:00
|
|
|
break
|
2009-11-05 16:55:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-11 14:21:37 -07:00
|
|
|
|
|
|
|
|
2010-04-22 17:57:29 -06:00
|
|
|
type modWTest struct {
|
2009-12-15 16:33:31 -07:00
|
|
|
in string
|
|
|
|
dividend string
|
|
|
|
out string
|
2009-11-11 14:21:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-22 17:57:29 -06:00
|
|
|
var modWTests32 = []modWTest{
|
|
|
|
modWTest{"23492635982634928349238759823742", "252341", "220170"},
|
2009-11-11 14:21:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-22 17:57:29 -06:00
|
|
|
var modWTests64 = []modWTest{
|
|
|
|
modWTest{"6527895462947293856291561095690465243862946", "524326975699234", "375066989628668"},
|
2009-11-11 14:21:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-22 17:57:29 -06:00
|
|
|
func runModWTests(t *testing.T, tests []modWTest) {
|
2009-11-11 14:21:37 -07:00
|
|
|
for i, test := range tests {
|
2009-12-15 16:33:31 -07:00
|
|
|
in, _ := new(Int).SetString(test.in, 10)
|
|
|
|
d, _ := new(Int).SetString(test.dividend, 10)
|
|
|
|
out, _ := new(Int).SetString(test.out, 10)
|
2009-11-11 14:21:37 -07:00
|
|
|
|
2010-04-22 17:57:29 -06:00
|
|
|
r := in.abs.modW(d.abs[0])
|
2009-11-11 14:21:37 -07:00
|
|
|
if r != out.abs[0] {
|
|
|
|
t.Errorf("#%d failed: got %s want %s\n", i, r, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-22 17:57:29 -06:00
|
|
|
func TestModW(t *testing.T) {
|
2009-11-11 14:21:37 -07:00
|
|
|
if _W >= 32 {
|
2010-04-22 17:57:29 -06:00
|
|
|
runModWTests(t, modWTests32)
|
2009-11-11 14:21:37 -07:00
|
|
|
}
|
|
|
|
if _W >= 64 {
|
2010-04-22 17:57:29 -06:00
|
|
|
runModWTests(t, modWTests64)
|
2009-11-11 14:21:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestTrailingZeroBits(t *testing.T) {
|
2009-12-15 16:33:31 -07:00
|
|
|
var x Word
|
|
|
|
x--
|
2009-11-11 14:21:37 -07:00
|
|
|
for i := 0; i < _W; i++ {
|
|
|
|
if trailingZeroBits(x) != i {
|
|
|
|
t.Errorf("Failed at step %d: x: %x got: %d\n", i, x, trailingZeroBits(x))
|
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
x <<= 1
|
2009-11-11 14:21:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-22 17:57:29 -06:00
|
|
|
type expNNTest struct {
|
2009-12-15 16:33:31 -07:00
|
|
|
x, y, m string
|
|
|
|
out string
|
2009-11-11 14:21:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-22 17:57:29 -06:00
|
|
|
var expNNTests = []expNNTest{
|
|
|
|
expNNTest{"0x8000000000000000", "2", "", "0x40000000000000000000000000000000"},
|
|
|
|
expNNTest{"0x8000000000000000", "2", "6719", "4944"},
|
|
|
|
expNNTest{"0x8000000000000000", "3", "6719", "5447"},
|
|
|
|
expNNTest{"0x8000000000000000", "1000", "6719", "1603"},
|
|
|
|
expNNTest{"0x8000000000000000", "1000000", "6719", "3199"},
|
|
|
|
expNNTest{
|
2009-11-11 14:21:37 -07:00
|
|
|
"2938462938472983472983659726349017249287491026512746239764525612965293865296239471239874193284792387498274256129746192347",
|
|
|
|
"298472983472983471903246121093472394872319615612417471234712061",
|
|
|
|
"29834729834729834729347290846729561262544958723956495615629569234729836259263598127342374289365912465901365498236492183464",
|
|
|
|
"23537740700184054162508175125554701713153216681790245129157191391322321508055833908509185839069455749219131480588829346291",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-22 17:57:29 -06:00
|
|
|
func TestExpNN(t *testing.T) {
|
|
|
|
for i, test := range expNNTests {
|
|
|
|
x, _, _ := nat(nil).scan(test.x, 0)
|
|
|
|
y, _, _ := nat(nil).scan(test.y, 0)
|
|
|
|
out, _, _ := nat(nil).scan(test.out, 0)
|
2009-11-11 14:21:37 -07:00
|
|
|
|
2010-04-22 17:57:29 -06:00
|
|
|
var m nat
|
2009-11-11 14:21:37 -07:00
|
|
|
|
|
|
|
if len(test.m) > 0 {
|
2010-04-22 17:57:29 -06:00
|
|
|
m, _, _ = nat(nil).scan(test.m, 0)
|
2009-11-11 14:21:37 -07:00
|
|
|
}
|
|
|
|
|
2010-04-22 17:57:29 -06:00
|
|
|
z := nat(nil).expNN(x, y, m)
|
|
|
|
if z.cmp(out) != 0 {
|
2009-11-11 14:21:37 -07:00
|
|
|
t.Errorf("#%d got %v want %v", i, z, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|