rsc.io/go@v0.0.0-20150416155037-e040fd465409/src/math/big/nat_test.go (about)

     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  
     5  package big
     6  
     7  import (
     8  	"runtime"
     9  	"strings"
    10  	"testing"
    11  )
    12  
    13  var cmpTests = []struct {
    14  	x, y nat
    15  	r    int
    16  }{
    17  	{nil, nil, 0},
    18  	{nil, nat(nil), 0},
    19  	{nat(nil), nil, 0},
    20  	{nat(nil), nat(nil), 0},
    21  	{nat{0}, nat{0}, 0},
    22  	{nat{0}, nat{1}, -1},
    23  	{nat{1}, nat{0}, 1},
    24  	{nat{1}, nat{1}, 0},
    25  	{nat{0, _M}, nat{1}, 1},
    26  	{nat{1}, nat{0, _M}, -1},
    27  	{nat{1, _M}, nat{0, _M}, 1},
    28  	{nat{0, _M}, nat{1, _M}, -1},
    29  	{nat{16, 571956, 8794, 68}, nat{837, 9146, 1, 754489}, -1},
    30  	{nat{34986, 41, 105, 1957}, nat{56, 7458, 104, 1957}, 1},
    31  }
    32  
    33  func TestCmp(t *testing.T) {
    34  	for i, a := range cmpTests {
    35  		r := a.x.cmp(a.y)
    36  		if r != a.r {
    37  			t.Errorf("#%d got r = %v; want %v", i, r, a.r)
    38  		}
    39  	}
    40  }
    41  
    42  type funNN func(z, x, y nat) nat
    43  type argNN struct {
    44  	z, x, y nat
    45  }
    46  
    47  var sumNN = []argNN{
    48  	{},
    49  	{nat{1}, nil, nat{1}},
    50  	{nat{1111111110}, nat{123456789}, nat{987654321}},
    51  	{nat{0, 0, 0, 1}, nil, nat{0, 0, 0, 1}},
    52  	{nat{0, 0, 0, 1111111110}, nat{0, 0, 0, 123456789}, nat{0, 0, 0, 987654321}},
    53  	{nat{0, 0, 0, 1}, nat{0, 0, _M}, nat{0, 0, 1}},
    54  }
    55  
    56  var prodNN = []argNN{
    57  	{},
    58  	{nil, nil, nil},
    59  	{nil, nat{991}, nil},
    60  	{nat{991}, nat{991}, nat{1}},
    61  	{nat{991 * 991}, nat{991}, nat{991}},
    62  	{nat{0, 0, 991 * 991}, nat{0, 991}, nat{0, 991}},
    63  	{nat{1 * 991, 2 * 991, 3 * 991, 4 * 991}, nat{1, 2, 3, 4}, nat{991}},
    64  	{nat{4, 11, 20, 30, 20, 11, 4}, nat{1, 2, 3, 4}, nat{4, 3, 2, 1}},
    65  	// 3^100 * 3^28 = 3^128
    66  	{
    67  		natFromString("11790184577738583171520872861412518665678211592275841109096961"),
    68  		natFromString("515377520732011331036461129765621272702107522001"),
    69  		natFromString("22876792454961"),
    70  	},
    71  	// z = 111....1 (70000 digits)
    72  	// x = 10^(99*700) + ... + 10^1400 + 10^700 + 1
    73  	// y = 111....1 (700 digits, larger than Karatsuba threshold on 32-bit and 64-bit)
    74  	{
    75  		natFromString(strings.Repeat("1", 70000)),
    76  		natFromString("1" + strings.Repeat(strings.Repeat("0", 699)+"1", 99)),
    77  		natFromString(strings.Repeat("1", 700)),
    78  	},
    79  	// z = 111....1 (20000 digits)
    80  	// x = 10^10000 + 1
    81  	// y = 111....1 (10000 digits)
    82  	{
    83  		natFromString(strings.Repeat("1", 20000)),
    84  		natFromString("1" + strings.Repeat("0", 9999) + "1"),
    85  		natFromString(strings.Repeat("1", 10000)),
    86  	},
    87  }
    88  
    89  func natFromString(s string) nat {
    90  	x, _, _, err := nat(nil).scan(strings.NewReader(s), 0, false)
    91  	if err != nil {
    92  		panic(err)
    93  	}
    94  	return x
    95  }
    96  
    97  func TestSet(t *testing.T) {
    98  	for _, a := range sumNN {
    99  		z := nat(nil).set(a.z)
   100  		if z.cmp(a.z) != 0 {
   101  			t.Errorf("got z = %v; want %v", z, a.z)
   102  		}
   103  	}
   104  }
   105  
   106  func testFunNN(t *testing.T, msg string, f funNN, a argNN) {
   107  	z := f(nil, a.x, a.y)
   108  	if z.cmp(a.z) != 0 {
   109  		t.Errorf("%s%+v\n\tgot z = %v; want %v", msg, a, z, a.z)
   110  	}
   111  }
   112  
   113  func TestFunNN(t *testing.T) {
   114  	for _, a := range sumNN {
   115  		arg := a
   116  		testFunNN(t, "add", nat.add, arg)
   117  
   118  		arg = argNN{a.z, a.y, a.x}
   119  		testFunNN(t, "add symmetric", nat.add, arg)
   120  
   121  		arg = argNN{a.x, a.z, a.y}
   122  		testFunNN(t, "sub", nat.sub, arg)
   123  
   124  		arg = argNN{a.y, a.z, a.x}
   125  		testFunNN(t, "sub symmetric", nat.sub, arg)
   126  	}
   127  
   128  	for _, a := range prodNN {
   129  		arg := a
   130  		testFunNN(t, "mul", nat.mul, arg)
   131  
   132  		arg = argNN{a.z, a.y, a.x}
   133  		testFunNN(t, "mul symmetric", nat.mul, arg)
   134  	}
   135  }
   136  
   137  var mulRangesN = []struct {
   138  	a, b uint64
   139  	prod string
   140  }{
   141  	{0, 0, "0"},
   142  	{1, 1, "1"},
   143  	{1, 2, "2"},
   144  	{1, 3, "6"},
   145  	{10, 10, "10"},
   146  	{0, 100, "0"},
   147  	{0, 1e9, "0"},
   148  	{1, 0, "1"},                    // empty range
   149  	{100, 1, "1"},                  // empty range
   150  	{1, 10, "3628800"},             // 10!
   151  	{1, 20, "2432902008176640000"}, // 20!
   152  	{1, 100,
   153  		"933262154439441526816992388562667004907159682643816214685929" +
   154  			"638952175999932299156089414639761565182862536979208272237582" +
   155  			"51185210916864000000000000000000000000", // 100!
   156  	},
   157  }
   158  
   159  func TestMulRangeN(t *testing.T) {
   160  	for i, r := range mulRangesN {
   161  		prod := nat(nil).mulRange(r.a, r.b).decimalString()
   162  		if prod != r.prod {
   163  			t.Errorf("#%d: got %s; want %s", i, prod, r.prod)
   164  		}
   165  	}
   166  }
   167  
   168  // allocBytes returns the number of bytes allocated by invoking f.
   169  func allocBytes(f func()) uint64 {
   170  	var stats runtime.MemStats
   171  	runtime.ReadMemStats(&stats)
   172  	t := stats.TotalAlloc
   173  	f()
   174  	runtime.ReadMemStats(&stats)
   175  	return stats.TotalAlloc - t
   176  }
   177  
   178  // TestMulUnbalanced tests that multiplying numbers of different lengths
   179  // does not cause deep recursion and in turn allocate too much memory.
   180  // Test case for issue 3807.
   181  func TestMulUnbalanced(t *testing.T) {
   182  	defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
   183  	x := rndNat(50000)
   184  	y := rndNat(40)
   185  	allocSize := allocBytes(func() {
   186  		nat(nil).mul(x, y)
   187  	})
   188  	inputSize := uint64(len(x)+len(y)) * _S
   189  	if ratio := allocSize / uint64(inputSize); ratio > 10 {
   190  		t.Errorf("multiplication uses too much memory (%d > %d times the size of inputs)", allocSize, ratio)
   191  	}
   192  }
   193  
   194  func rndNat(n int) nat {
   195  	return nat(rndV(n)).norm()
   196  }
   197  
   198  func BenchmarkMul(b *testing.B) {
   199  	mulx := rndNat(1e4)
   200  	muly := rndNat(1e4)
   201  	b.ResetTimer()
   202  	for i := 0; i < b.N; i++ {
   203  		var z nat
   204  		z.mul(mulx, muly)
   205  	}
   206  }
   207  
   208  func TestLeadingZeros(t *testing.T) {
   209  	var x Word = _B >> 1
   210  	for i := 0; i <= _W; i++ {
   211  		if int(leadingZeros(x)) != i {
   212  			t.Errorf("failed at %x: got %d want %d", x, leadingZeros(x), i)
   213  		}
   214  		x >>= 1
   215  	}
   216  }
   217  
   218  type shiftTest struct {
   219  	in    nat
   220  	shift uint
   221  	out   nat
   222  }
   223  
   224  var leftShiftTests = []shiftTest{
   225  	{nil, 0, nil},
   226  	{nil, 1, nil},
   227  	{natOne, 0, natOne},
   228  	{natOne, 1, natTwo},
   229  	{nat{1 << (_W - 1)}, 1, nat{0}},
   230  	{nat{1 << (_W - 1), 0}, 1, nat{0, 1}},
   231  }
   232  
   233  func TestShiftLeft(t *testing.T) {
   234  	for i, test := range leftShiftTests {
   235  		var z nat
   236  		z = z.shl(test.in, test.shift)
   237  		for j, d := range test.out {
   238  			if j >= len(z) || z[j] != d {
   239  				t.Errorf("#%d: got: %v want: %v", i, z, test.out)
   240  				break
   241  			}
   242  		}
   243  	}
   244  }
   245  
   246  var rightShiftTests = []shiftTest{
   247  	{nil, 0, nil},
   248  	{nil, 1, nil},
   249  	{natOne, 0, natOne},
   250  	{natOne, 1, nil},
   251  	{natTwo, 1, natOne},
   252  	{nat{0, 1}, 1, nat{1 << (_W - 1)}},
   253  	{nat{2, 1, 1}, 1, nat{1<<(_W-1) + 1, 1 << (_W - 1)}},
   254  }
   255  
   256  func TestShiftRight(t *testing.T) {
   257  	for i, test := range rightShiftTests {
   258  		var z nat
   259  		z = z.shr(test.in, test.shift)
   260  		for j, d := range test.out {
   261  			if j >= len(z) || z[j] != d {
   262  				t.Errorf("#%d: got: %v want: %v", i, z, test.out)
   263  				break
   264  			}
   265  		}
   266  	}
   267  }
   268  
   269  type modWTest struct {
   270  	in       string
   271  	dividend string
   272  	out      string
   273  }
   274  
   275  var modWTests32 = []modWTest{
   276  	{"23492635982634928349238759823742", "252341", "220170"},
   277  }
   278  
   279  var modWTests64 = []modWTest{
   280  	{"6527895462947293856291561095690465243862946", "524326975699234", "375066989628668"},
   281  }
   282  
   283  func runModWTests(t *testing.T, tests []modWTest) {
   284  	for i, test := range tests {
   285  		in, _ := new(Int).SetString(test.in, 10)
   286  		d, _ := new(Int).SetString(test.dividend, 10)
   287  		out, _ := new(Int).SetString(test.out, 10)
   288  
   289  		r := in.abs.modW(d.abs[0])
   290  		if r != out.abs[0] {
   291  			t.Errorf("#%d failed: got %d want %s", i, r, out)
   292  		}
   293  	}
   294  }
   295  
   296  func TestModW(t *testing.T) {
   297  	if _W >= 32 {
   298  		runModWTests(t, modWTests32)
   299  	}
   300  	if _W >= 64 {
   301  		runModWTests(t, modWTests64)
   302  	}
   303  }
   304  
   305  func TestTrailingZeroBits(t *testing.T) {
   306  	// test 0 case explicitly
   307  	if n := trailingZeroBits(0); n != 0 {
   308  		t.Errorf("got trailingZeroBits(0) = %d; want 0", n)
   309  	}
   310  
   311  	x := Word(1)
   312  	for i := uint(0); i < _W; i++ {
   313  		n := trailingZeroBits(x)
   314  		if n != i {
   315  			t.Errorf("got trailingZeroBits(%#x) = %d; want %d", x, n, i%_W)
   316  		}
   317  		x <<= 1
   318  	}
   319  
   320  	// test 0 case explicitly
   321  	if n := nat(nil).trailingZeroBits(); n != 0 {
   322  		t.Errorf("got nat(nil).trailingZeroBits() = %d; want 0", n)
   323  	}
   324  
   325  	y := nat(nil).set(natOne)
   326  	for i := uint(0); i <= 3*_W; i++ {
   327  		n := y.trailingZeroBits()
   328  		if n != i {
   329  			t.Errorf("got 0x%s.trailingZeroBits() = %d; want %d", y.hexString(), n, i)
   330  		}
   331  		y = y.shl(y, 1)
   332  	}
   333  }
   334  
   335  var expNNTests = []struct {
   336  	x, y, m string
   337  	out     string
   338  }{
   339  	{"0", "0", "0", "1"},
   340  	{"0", "0", "1", "0"},
   341  	{"1", "1", "1", "0"},
   342  	{"2", "1", "1", "0"},
   343  	{"2", "2", "1", "0"},
   344  	{"10", "100000000000", "1", "0"},
   345  	{"0x8000000000000000", "2", "", "0x40000000000000000000000000000000"},
   346  	{"0x8000000000000000", "2", "6719", "4944"},
   347  	{"0x8000000000000000", "3", "6719", "5447"},
   348  	{"0x8000000000000000", "1000", "6719", "1603"},
   349  	{"0x8000000000000000", "1000000", "6719", "3199"},
   350  	{
   351  		"2938462938472983472983659726349017249287491026512746239764525612965293865296239471239874193284792387498274256129746192347",
   352  		"298472983472983471903246121093472394872319615612417471234712061",
   353  		"29834729834729834729347290846729561262544958723956495615629569234729836259263598127342374289365912465901365498236492183464",
   354  		"23537740700184054162508175125554701713153216681790245129157191391322321508055833908509185839069455749219131480588829346291",
   355  	},
   356  }
   357  
   358  func TestExpNN(t *testing.T) {
   359  	for i, test := range expNNTests {
   360  		x := natFromString(test.x)
   361  		y := natFromString(test.y)
   362  		out := natFromString(test.out)
   363  
   364  		var m nat
   365  		if len(test.m) > 0 {
   366  			m = natFromString(test.m)
   367  		}
   368  
   369  		z := nat(nil).expNN(x, y, m)
   370  		if z.cmp(out) != 0 {
   371  			t.Errorf("#%d got %s want %s", i, z.decimalString(), out.decimalString())
   372  		}
   373  	}
   374  }
   375  
   376  func ExpHelper(b *testing.B, x, y Word) {
   377  	var z nat
   378  	for i := 0; i < b.N; i++ {
   379  		z.expWW(x, y)
   380  	}
   381  }
   382  
   383  func BenchmarkExp3Power0x10(b *testing.B)     { ExpHelper(b, 3, 0x10) }
   384  func BenchmarkExp3Power0x40(b *testing.B)     { ExpHelper(b, 3, 0x40) }
   385  func BenchmarkExp3Power0x100(b *testing.B)    { ExpHelper(b, 3, 0x100) }
   386  func BenchmarkExp3Power0x400(b *testing.B)    { ExpHelper(b, 3, 0x400) }
   387  func BenchmarkExp3Power0x1000(b *testing.B)   { ExpHelper(b, 3, 0x1000) }
   388  func BenchmarkExp3Power0x4000(b *testing.B)   { ExpHelper(b, 3, 0x4000) }
   389  func BenchmarkExp3Power0x10000(b *testing.B)  { ExpHelper(b, 3, 0x10000) }
   390  func BenchmarkExp3Power0x40000(b *testing.B)  { ExpHelper(b, 3, 0x40000) }
   391  func BenchmarkExp3Power0x100000(b *testing.B) { ExpHelper(b, 3, 0x100000) }
   392  func BenchmarkExp3Power0x400000(b *testing.B) { ExpHelper(b, 3, 0x400000) }
   393  
   394  func fibo(n int) nat {
   395  	switch n {
   396  	case 0:
   397  		return nil
   398  	case 1:
   399  		return nat{1}
   400  	}
   401  	f0 := fibo(0)
   402  	f1 := fibo(1)
   403  	var f2 nat
   404  	for i := 1; i < n; i++ {
   405  		f2 = f2.add(f0, f1)
   406  		f0, f1, f2 = f1, f2, f0
   407  	}
   408  	return f1
   409  }
   410  
   411  var fiboNums = []string{
   412  	"0",
   413  	"55",
   414  	"6765",
   415  	"832040",
   416  	"102334155",
   417  	"12586269025",
   418  	"1548008755920",
   419  	"190392490709135",
   420  	"23416728348467685",
   421  	"2880067194370816120",
   422  	"354224848179261915075",
   423  }
   424  
   425  func TestFibo(t *testing.T) {
   426  	for i, want := range fiboNums {
   427  		n := i * 10
   428  		got := fibo(n).decimalString()
   429  		if got != want {
   430  			t.Errorf("fibo(%d) failed: got %s want %s", n, got, want)
   431  		}
   432  	}
   433  }
   434  
   435  func BenchmarkFibo(b *testing.B) {
   436  	for i := 0; i < b.N; i++ {
   437  		fibo(1e0)
   438  		fibo(1e1)
   439  		fibo(1e2)
   440  		fibo(1e3)
   441  		fibo(1e4)
   442  		fibo(1e5)
   443  	}
   444  }
   445  
   446  var bitTests = []struct {
   447  	x    string
   448  	i    uint
   449  	want uint
   450  }{
   451  	{"0", 0, 0},
   452  	{"0", 1, 0},
   453  	{"0", 1000, 0},
   454  
   455  	{"0x1", 0, 1},
   456  	{"0x10", 0, 0},
   457  	{"0x10", 3, 0},
   458  	{"0x10", 4, 1},
   459  	{"0x10", 5, 0},
   460  
   461  	{"0x8000000000000000", 62, 0},
   462  	{"0x8000000000000000", 63, 1},
   463  	{"0x8000000000000000", 64, 0},
   464  
   465  	{"0x3" + strings.Repeat("0", 32), 127, 0},
   466  	{"0x3" + strings.Repeat("0", 32), 128, 1},
   467  	{"0x3" + strings.Repeat("0", 32), 129, 1},
   468  	{"0x3" + strings.Repeat("0", 32), 130, 0},
   469  }
   470  
   471  func TestBit(t *testing.T) {
   472  	for i, test := range bitTests {
   473  		x := natFromString(test.x)
   474  		if got := x.bit(test.i); got != test.want {
   475  			t.Errorf("#%d: %s.bit(%d) = %v; want %v", i, test.x, test.i, got, test.want)
   476  		}
   477  	}
   478  }
   479  
   480  var stickyTests = []struct {
   481  	x    string
   482  	i    uint
   483  	want uint
   484  }{
   485  	{"0", 0, 0},
   486  	{"0", 1, 0},
   487  	{"0", 1000, 0},
   488  
   489  	{"0x1", 0, 0},
   490  	{"0x1", 1, 1},
   491  
   492  	{"0x1350", 0, 0},
   493  	{"0x1350", 4, 0},
   494  	{"0x1350", 5, 1},
   495  
   496  	{"0x8000000000000000", 63, 0},
   497  	{"0x8000000000000000", 64, 1},
   498  
   499  	{"0x1" + strings.Repeat("0", 100), 400, 0},
   500  	{"0x1" + strings.Repeat("0", 100), 401, 1},
   501  }
   502  
   503  func TestSticky(t *testing.T) {
   504  	for i, test := range stickyTests {
   505  		x := natFromString(test.x)
   506  		if got := x.sticky(test.i); got != test.want {
   507  			t.Errorf("#%d: %s.sticky(%d) = %v; want %v", i, test.x, test.i, got, test.want)
   508  		}
   509  		if test.want == 1 {
   510  			// all subsequent i's should also return 1
   511  			for d := uint(1); d <= 3; d++ {
   512  				if got := x.sticky(test.i + d); got != 1 {
   513  					t.Errorf("#%d: %s.sticky(%d) = %v; want %v", i, test.x, test.i+d, got, 1)
   514  				}
   515  			}
   516  		}
   517  	}
   518  }