github.com/rsc/tmp@v0.0.0-20240517235954-6deaab19748b/bootstrap/internal/gc/big/int_test.go (about)

     1  // Do not edit. Bootstrap copy of /Users/rsc/g/go/src/cmd/internal/gc/big/int_test.go
     2  
     3  // Copyright 2009 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package big
     8  
     9  import (
    10  	"bytes"
    11  	"encoding/gob"
    12  	"encoding/hex"
    13  	"encoding/json"
    14  	"encoding/xml"
    15  	"fmt"
    16  	"math/rand"
    17  	"testing"
    18  	"testing/quick"
    19  )
    20  
    21  func isNormalized(x *Int) bool {
    22  	if len(x.abs) == 0 {
    23  		return !x.neg
    24  	}
    25  	// len(x.abs) > 0
    26  	return x.abs[len(x.abs)-1] != 0
    27  }
    28  
    29  type funZZ func(z, x, y *Int) *Int
    30  type argZZ struct {
    31  	z, x, y *Int
    32  }
    33  
    34  var sumZZ = []argZZ{
    35  	{NewInt(0), NewInt(0), NewInt(0)},
    36  	{NewInt(1), NewInt(1), NewInt(0)},
    37  	{NewInt(1111111110), NewInt(123456789), NewInt(987654321)},
    38  	{NewInt(-1), NewInt(-1), NewInt(0)},
    39  	{NewInt(864197532), NewInt(-123456789), NewInt(987654321)},
    40  	{NewInt(-1111111110), NewInt(-123456789), NewInt(-987654321)},
    41  }
    42  
    43  var prodZZ = []argZZ{
    44  	{NewInt(0), NewInt(0), NewInt(0)},
    45  	{NewInt(0), NewInt(1), NewInt(0)},
    46  	{NewInt(1), NewInt(1), NewInt(1)},
    47  	{NewInt(-991 * 991), NewInt(991), NewInt(-991)},
    48  	// TODO(gri) add larger products
    49  }
    50  
    51  func TestSignZ(t *testing.T) {
    52  	var zero Int
    53  	for _, a := range sumZZ {
    54  		s := a.z.Sign()
    55  		e := a.z.Cmp(&zero)
    56  		if s != e {
    57  			t.Errorf("got %d; want %d for z = %v", s, e, a.z)
    58  		}
    59  	}
    60  }
    61  
    62  func TestSetZ(t *testing.T) {
    63  	for _, a := range sumZZ {
    64  		var z Int
    65  		z.Set(a.z)
    66  		if !isNormalized(&z) {
    67  			t.Errorf("%v is not normalized", z)
    68  		}
    69  		if (&z).Cmp(a.z) != 0 {
    70  			t.Errorf("got z = %v; want %v", z, a.z)
    71  		}
    72  	}
    73  }
    74  
    75  func TestAbsZ(t *testing.T) {
    76  	var zero Int
    77  	for _, a := range sumZZ {
    78  		var z Int
    79  		z.Abs(a.z)
    80  		var e Int
    81  		e.Set(a.z)
    82  		if e.Cmp(&zero) < 0 {
    83  			e.Sub(&zero, &e)
    84  		}
    85  		if z.Cmp(&e) != 0 {
    86  			t.Errorf("got z = %v; want %v", z, e)
    87  		}
    88  	}
    89  }
    90  
    91  func testFunZZ(t *testing.T, msg string, f funZZ, a argZZ) {
    92  	var z Int
    93  	f(&z, a.x, a.y)
    94  	if !isNormalized(&z) {
    95  		t.Errorf("%s%v is not normalized", msg, z)
    96  	}
    97  	if (&z).Cmp(a.z) != 0 {
    98  		t.Errorf("%s%+v\n\tgot z = %v; want %v", msg, a, &z, a.z)
    99  	}
   100  }
   101  
   102  func TestSumZZ(t *testing.T) {
   103  	AddZZ := func(z, x, y *Int) *Int { return z.Add(x, y) }
   104  	SubZZ := func(z, x, y *Int) *Int { return z.Sub(x, y) }
   105  	for _, a := range sumZZ {
   106  		arg := a
   107  		testFunZZ(t, "AddZZ", AddZZ, arg)
   108  
   109  		arg = argZZ{a.z, a.y, a.x}
   110  		testFunZZ(t, "AddZZ symmetric", AddZZ, arg)
   111  
   112  		arg = argZZ{a.x, a.z, a.y}
   113  		testFunZZ(t, "SubZZ", SubZZ, arg)
   114  
   115  		arg = argZZ{a.y, a.z, a.x}
   116  		testFunZZ(t, "SubZZ symmetric", SubZZ, arg)
   117  	}
   118  }
   119  
   120  func TestProdZZ(t *testing.T) {
   121  	MulZZ := func(z, x, y *Int) *Int { return z.Mul(x, y) }
   122  	for _, a := range prodZZ {
   123  		arg := a
   124  		testFunZZ(t, "MulZZ", MulZZ, arg)
   125  
   126  		arg = argZZ{a.z, a.y, a.x}
   127  		testFunZZ(t, "MulZZ symmetric", MulZZ, arg)
   128  	}
   129  }
   130  
   131  // mulBytes returns x*y via grade school multiplication. Both inputs
   132  // and the result are assumed to be in big-endian representation (to
   133  // match the semantics of Int.Bytes and Int.SetBytes).
   134  func mulBytes(x, y []byte) []byte {
   135  	z := make([]byte, len(x)+len(y))
   136  
   137  	// multiply
   138  	k0 := len(z) - 1
   139  	for j := len(y) - 1; j >= 0; j-- {
   140  		d := int(y[j])
   141  		if d != 0 {
   142  			k := k0
   143  			carry := 0
   144  			for i := len(x) - 1; i >= 0; i-- {
   145  				t := int(z[k]) + int(x[i])*d + carry
   146  				z[k], carry = byte(t), t>>8
   147  				k--
   148  			}
   149  			z[k] = byte(carry)
   150  		}
   151  		k0--
   152  	}
   153  
   154  	// normalize (remove leading 0's)
   155  	i := 0
   156  	for i < len(z) && z[i] == 0 {
   157  		i++
   158  	}
   159  
   160  	return z[i:]
   161  }
   162  
   163  func checkMul(a, b []byte) bool {
   164  	var x, y, z1 Int
   165  	x.SetBytes(a)
   166  	y.SetBytes(b)
   167  	z1.Mul(&x, &y)
   168  
   169  	var z2 Int
   170  	z2.SetBytes(mulBytes(a, b))
   171  
   172  	return z1.Cmp(&z2) == 0
   173  }
   174  
   175  func TestMul(t *testing.T) {
   176  	if err := quick.Check(checkMul, nil); err != nil {
   177  		t.Error(err)
   178  	}
   179  }
   180  
   181  var mulRangesZ = []struct {
   182  	a, b int64
   183  	prod string
   184  }{
   185  	// entirely positive ranges are covered by mulRangesN
   186  	{-1, 1, "0"},
   187  	{-2, -1, "2"},
   188  	{-3, -2, "6"},
   189  	{-3, -1, "-6"},
   190  	{1, 3, "6"},
   191  	{-10, -10, "-10"},
   192  	{0, -1, "1"},                      // empty range
   193  	{-1, -100, "1"},                   // empty range
   194  	{-1, 1, "0"},                      // range includes 0
   195  	{-1e9, 0, "0"},                    // range includes 0
   196  	{-1e9, 1e9, "0"},                  // range includes 0
   197  	{-10, -1, "3628800"},              // 10!
   198  	{-20, -2, "-2432902008176640000"}, // -20!
   199  	{-99, -1,
   200  		"-933262154439441526816992388562667004907159682643816214685929" +
   201  			"638952175999932299156089414639761565182862536979208272237582" +
   202  			"511852109168640000000000000000000000", // -99!
   203  	},
   204  }
   205  
   206  func TestMulRangeZ(t *testing.T) {
   207  	var tmp Int
   208  	// test entirely positive ranges
   209  	for i, r := range mulRangesN {
   210  		prod := tmp.MulRange(int64(r.a), int64(r.b)).String()
   211  		if prod != r.prod {
   212  			t.Errorf("#%da: got %s; want %s", i, prod, r.prod)
   213  		}
   214  	}
   215  	// test other ranges
   216  	for i, r := range mulRangesZ {
   217  		prod := tmp.MulRange(r.a, r.b).String()
   218  		if prod != r.prod {
   219  			t.Errorf("#%db: got %s; want %s", i, prod, r.prod)
   220  		}
   221  	}
   222  }
   223  
   224  func TestBinomial(t *testing.T) {
   225  	var z Int
   226  	for _, test := range []struct {
   227  		n, k int64
   228  		want string
   229  	}{
   230  		{0, 0, "1"},
   231  		{0, 1, "0"},
   232  		{1, 0, "1"},
   233  		{1, 1, "1"},
   234  		{1, 10, "0"},
   235  		{4, 0, "1"},
   236  		{4, 1, "4"},
   237  		{4, 2, "6"},
   238  		{4, 3, "4"},
   239  		{4, 4, "1"},
   240  		{10, 1, "10"},
   241  		{10, 9, "10"},
   242  		{10, 5, "252"},
   243  		{11, 5, "462"},
   244  		{11, 6, "462"},
   245  		{100, 10, "17310309456440"},
   246  		{100, 90, "17310309456440"},
   247  		{1000, 10, "263409560461970212832400"},
   248  		{1000, 990, "263409560461970212832400"},
   249  	} {
   250  		if got := z.Binomial(test.n, test.k).String(); got != test.want {
   251  			t.Errorf("Binomial(%d, %d) = %s; want %s", test.n, test.k, got, test.want)
   252  		}
   253  	}
   254  }
   255  
   256  func BenchmarkBinomial(b *testing.B) {
   257  	var z Int
   258  	for i := b.N - 1; i >= 0; i-- {
   259  		z.Binomial(1000, 990)
   260  	}
   261  }
   262  
   263  // Examples from the Go Language Spec, section "Arithmetic operators"
   264  var divisionSignsTests = []struct {
   265  	x, y int64
   266  	q, r int64 // T-division
   267  	d, m int64 // Euclidian division
   268  }{
   269  	{5, 3, 1, 2, 1, 2},
   270  	{-5, 3, -1, -2, -2, 1},
   271  	{5, -3, -1, 2, -1, 2},
   272  	{-5, -3, 1, -2, 2, 1},
   273  	{1, 2, 0, 1, 0, 1},
   274  	{8, 4, 2, 0, 2, 0},
   275  }
   276  
   277  func TestDivisionSigns(t *testing.T) {
   278  	for i, test := range divisionSignsTests {
   279  		x := NewInt(test.x)
   280  		y := NewInt(test.y)
   281  		q := NewInt(test.q)
   282  		r := NewInt(test.r)
   283  		d := NewInt(test.d)
   284  		m := NewInt(test.m)
   285  
   286  		q1 := new(Int).Quo(x, y)
   287  		r1 := new(Int).Rem(x, y)
   288  		if !isNormalized(q1) {
   289  			t.Errorf("#%d Quo: %v is not normalized", i, *q1)
   290  		}
   291  		if !isNormalized(r1) {
   292  			t.Errorf("#%d Rem: %v is not normalized", i, *r1)
   293  		}
   294  		if q1.Cmp(q) != 0 || r1.Cmp(r) != 0 {
   295  			t.Errorf("#%d QuoRem: got (%s, %s), want (%s, %s)", i, q1, r1, q, r)
   296  		}
   297  
   298  		q2, r2 := new(Int).QuoRem(x, y, new(Int))
   299  		if !isNormalized(q2) {
   300  			t.Errorf("#%d Quo: %v is not normalized", i, *q2)
   301  		}
   302  		if !isNormalized(r2) {
   303  			t.Errorf("#%d Rem: %v is not normalized", i, *r2)
   304  		}
   305  		if q2.Cmp(q) != 0 || r2.Cmp(r) != 0 {
   306  			t.Errorf("#%d QuoRem: got (%s, %s), want (%s, %s)", i, q2, r2, q, r)
   307  		}
   308  
   309  		d1 := new(Int).Div(x, y)
   310  		m1 := new(Int).Mod(x, y)
   311  		if !isNormalized(d1) {
   312  			t.Errorf("#%d Div: %v is not normalized", i, *d1)
   313  		}
   314  		if !isNormalized(m1) {
   315  			t.Errorf("#%d Mod: %v is not normalized", i, *m1)
   316  		}
   317  		if d1.Cmp(d) != 0 || m1.Cmp(m) != 0 {
   318  			t.Errorf("#%d DivMod: got (%s, %s), want (%s, %s)", i, d1, m1, d, m)
   319  		}
   320  
   321  		d2, m2 := new(Int).DivMod(x, y, new(Int))
   322  		if !isNormalized(d2) {
   323  			t.Errorf("#%d Div: %v is not normalized", i, *d2)
   324  		}
   325  		if !isNormalized(m2) {
   326  			t.Errorf("#%d Mod: %v is not normalized", i, *m2)
   327  		}
   328  		if d2.Cmp(d) != 0 || m2.Cmp(m) != 0 {
   329  			t.Errorf("#%d DivMod: got (%s, %s), want (%s, %s)", i, d2, m2, d, m)
   330  		}
   331  	}
   332  }
   333  
   334  func norm(x nat) nat {
   335  	i := len(x)
   336  	for i > 0 && x[i-1] == 0 {
   337  		i--
   338  	}
   339  	return x[:i]
   340  }
   341  
   342  func TestBits(t *testing.T) {
   343  	for _, test := range []nat{
   344  		nil,
   345  		{0},
   346  		{1},
   347  		{0, 1, 2, 3, 4},
   348  		{4, 3, 2, 1, 0},
   349  		{4, 3, 2, 1, 0, 0, 0, 0},
   350  	} {
   351  		var z Int
   352  		z.neg = true
   353  		got := z.SetBits(test)
   354  		want := norm(test)
   355  		if got.abs.cmp(want) != 0 {
   356  			t.Errorf("SetBits(%v) = %v; want %v", test, got.abs, want)
   357  		}
   358  
   359  		if got.neg {
   360  			t.Errorf("SetBits(%v): got negative result", test)
   361  		}
   362  
   363  		bits := nat(z.Bits())
   364  		if bits.cmp(want) != 0 {
   365  			t.Errorf("%v.Bits() = %v; want %v", z.abs, bits, want)
   366  		}
   367  	}
   368  }
   369  
   370  func checkSetBytes(b []byte) bool {
   371  	hex1 := hex.EncodeToString(new(Int).SetBytes(b).Bytes())
   372  	hex2 := hex.EncodeToString(b)
   373  
   374  	for len(hex1) < len(hex2) {
   375  		hex1 = "0" + hex1
   376  	}
   377  
   378  	for len(hex1) > len(hex2) {
   379  		hex2 = "0" + hex2
   380  	}
   381  
   382  	return hex1 == hex2
   383  }
   384  
   385  func TestSetBytes(t *testing.T) {
   386  	if err := quick.Check(checkSetBytes, nil); err != nil {
   387  		t.Error(err)
   388  	}
   389  }
   390  
   391  func checkBytes(b []byte) bool {
   392  	b2 := new(Int).SetBytes(b).Bytes()
   393  	return bytes.Equal(b, b2)
   394  }
   395  
   396  func TestBytes(t *testing.T) {
   397  	if err := quick.Check(checkBytes, nil); err != nil {
   398  		t.Error(err)
   399  	}
   400  }
   401  
   402  func checkQuo(x, y []byte) bool {
   403  	u := new(Int).SetBytes(x)
   404  	v := new(Int).SetBytes(y)
   405  
   406  	if len(v.abs) == 0 {
   407  		return true
   408  	}
   409  
   410  	r := new(Int)
   411  	q, r := new(Int).QuoRem(u, v, r)
   412  
   413  	if r.Cmp(v) >= 0 {
   414  		return false
   415  	}
   416  
   417  	uprime := new(Int).Set(q)
   418  	uprime.Mul(uprime, v)
   419  	uprime.Add(uprime, r)
   420  
   421  	return uprime.Cmp(u) == 0
   422  }
   423  
   424  var quoTests = []struct {
   425  	x, y string
   426  	q, r string
   427  }{
   428  	{
   429  		"476217953993950760840509444250624797097991362735329973741718102894495832294430498335824897858659711275234906400899559094370964723884706254265559534144986498357",
   430  		"9353930466774385905609975137998169297361893554149986716853295022578535724979483772383667534691121982974895531435241089241440253066816724367338287092081996",
   431  		"50911",
   432  		"1",
   433  	},
   434  	{
   435  		"11510768301994997771168",
   436  		"1328165573307167369775",
   437  		"8",
   438  		"885443715537658812968",
   439  	},
   440  }
   441  
   442  func TestQuo(t *testing.T) {
   443  	if err := quick.Check(checkQuo, nil); err != nil {
   444  		t.Error(err)
   445  	}
   446  
   447  	for i, test := range quoTests {
   448  		x, _ := new(Int).SetString(test.x, 10)
   449  		y, _ := new(Int).SetString(test.y, 10)
   450  		expectedQ, _ := new(Int).SetString(test.q, 10)
   451  		expectedR, _ := new(Int).SetString(test.r, 10)
   452  
   453  		r := new(Int)
   454  		q, r := new(Int).QuoRem(x, y, r)
   455  
   456  		if q.Cmp(expectedQ) != 0 || r.Cmp(expectedR) != 0 {
   457  			t.Errorf("#%d got (%s, %s) want (%s, %s)", i, q, r, expectedQ, expectedR)
   458  		}
   459  	}
   460  }
   461  
   462  func TestQuoStepD6(t *testing.T) {
   463  	// See Knuth, Volume 2, section 4.3.1, exercise 21. This code exercises
   464  	// a code path which only triggers 1 in 10^{-19} cases.
   465  
   466  	u := &Int{false, nat{0, 0, 1 + 1<<(_W-1), _M ^ (1 << (_W - 1))}}
   467  	v := &Int{false, nat{5, 2 + 1<<(_W-1), 1 << (_W - 1)}}
   468  
   469  	r := new(Int)
   470  	q, r := new(Int).QuoRem(u, v, r)
   471  	const expectedQ64 = "18446744073709551613"
   472  	const expectedR64 = "3138550867693340382088035895064302439801311770021610913807"
   473  	const expectedQ32 = "4294967293"
   474  	const expectedR32 = "39614081266355540837921718287"
   475  	if q.String() != expectedQ64 && q.String() != expectedQ32 ||
   476  		r.String() != expectedR64 && r.String() != expectedR32 {
   477  		t.Errorf("got (%s, %s) want (%s, %s) or (%s, %s)", q, r, expectedQ64, expectedR64, expectedQ32, expectedR32)
   478  	}
   479  }
   480  
   481  var bitLenTests = []struct {
   482  	in  string
   483  	out int
   484  }{
   485  	{"-1", 1},
   486  	{"0", 0},
   487  	{"1", 1},
   488  	{"2", 2},
   489  	{"4", 3},
   490  	{"0xabc", 12},
   491  	{"0x8000", 16},
   492  	{"0x80000000", 32},
   493  	{"0x800000000000", 48},
   494  	{"0x8000000000000000", 64},
   495  	{"0x80000000000000000000", 80},
   496  	{"-0x4000000000000000000000", 87},
   497  }
   498  
   499  func TestBitLen(t *testing.T) {
   500  	for i, test := range bitLenTests {
   501  		x, ok := new(Int).SetString(test.in, 0)
   502  		if !ok {
   503  			t.Errorf("#%d test input invalid: %s", i, test.in)
   504  			continue
   505  		}
   506  
   507  		if n := x.BitLen(); n != test.out {
   508  			t.Errorf("#%d got %d want %d", i, n, test.out)
   509  		}
   510  	}
   511  }
   512  
   513  var expTests = []struct {
   514  	x, y, m string
   515  	out     string
   516  }{
   517  	// y <= 0
   518  	{"0", "0", "", "1"},
   519  	{"1", "0", "", "1"},
   520  	{"-10", "0", "", "1"},
   521  	{"1234", "-1", "", "1"},
   522  
   523  	// m == 1
   524  	{"0", "0", "1", "0"},
   525  	{"1", "0", "1", "0"},
   526  	{"-10", "0", "1", "0"},
   527  	{"1234", "-1", "1", "0"},
   528  
   529  	// misc
   530  	{"5", "-7", "", "1"},
   531  	{"-5", "-7", "", "1"},
   532  	{"5", "0", "", "1"},
   533  	{"-5", "0", "", "1"},
   534  	{"5", "1", "", "5"},
   535  	{"-5", "1", "", "-5"},
   536  	{"-5", "1", "7", "2"},
   537  	{"-2", "3", "2", "0"},
   538  	{"5", "2", "", "25"},
   539  	{"1", "65537", "2", "1"},
   540  	{"0x8000000000000000", "2", "", "0x40000000000000000000000000000000"},
   541  	{"0x8000000000000000", "2", "6719", "4944"},
   542  	{"0x8000000000000000", "3", "6719", "5447"},
   543  	{"0x8000000000000000", "1000", "6719", "1603"},
   544  	{"0x8000000000000000", "1000000", "6719", "3199"},
   545  	{"0x8000000000000000", "-1000000", "6719", "1"},
   546  	{
   547  		"2938462938472983472983659726349017249287491026512746239764525612965293865296239471239874193284792387498274256129746192347",
   548  		"298472983472983471903246121093472394872319615612417471234712061",
   549  		"29834729834729834729347290846729561262544958723956495615629569234729836259263598127342374289365912465901365498236492183464",
   550  		"23537740700184054162508175125554701713153216681790245129157191391322321508055833908509185839069455749219131480588829346291",
   551  	},
   552  	// test case for issue 8822
   553  	{
   554  		"-0x1BCE04427D8032319A89E5C4136456671AC620883F2C4139E57F91307C485AD2D6204F4F87A58262652DB5DBBAC72B0613E51B835E7153BEC6068F5C8D696B74DBD18FEC316AEF73985CF0475663208EB46B4F17DD9DA55367B03323E5491A70997B90C059FB34809E6EE55BCFBD5F2F52233BFE62E6AA9E4E26A1D4C2439883D14F2633D55D8AA66A1ACD5595E778AC3A280517F1157989E70C1A437B849F1877B779CC3CDDEDE2DAA6594A6C66D181A00A5F777EE60596D8773998F6E988DEAE4CCA60E4DDCF9590543C89F74F603259FCAD71660D30294FBBE6490300F78A9D63FA660DC9417B8B9DDA28BEB3977B621B988E23D4D954F322C3540541BC649ABD504C50FADFD9F0987D58A2BF689313A285E773FF02899A6EF887D1D4A0D2",
   555  		"0xB08FFB20760FFED58FADA86DFEF71AD72AA0FA763219618FE022C197E54708BB1191C66470250FCE8879487507CEE41381CA4D932F81C2B3F1AB20B539D50DCD",
   556  		"0xAC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF73",
   557  		"21484252197776302499639938883777710321993113097987201050501182909581359357618579566746556372589385361683610524730509041328855066514963385522570894839035884713051640171474186548713546686476761306436434146475140156284389181808675016576845833340494848283681088886584219750554408060556769486628029028720727393293111678826356480455433909233520504112074401376133077150471237549474149190242010469539006449596611576612573955754349042329130631128234637924786466585703488460540228477440853493392086251021228087076124706778899179648655221663765993962724699135217212118535057766739392069738618682722216712319320435674779146070442",
   558  	},
   559  }
   560  
   561  func TestExp(t *testing.T) {
   562  	for i, test := range expTests {
   563  		x, ok1 := new(Int).SetString(test.x, 0)
   564  		y, ok2 := new(Int).SetString(test.y, 0)
   565  		out, ok3 := new(Int).SetString(test.out, 0)
   566  
   567  		var ok4 bool
   568  		var m *Int
   569  
   570  		if len(test.m) == 0 {
   571  			m, ok4 = nil, true
   572  		} else {
   573  			m, ok4 = new(Int).SetString(test.m, 0)
   574  		}
   575  
   576  		if !ok1 || !ok2 || !ok3 || !ok4 {
   577  			t.Errorf("#%d: error in input", i)
   578  			continue
   579  		}
   580  
   581  		z1 := new(Int).Exp(x, y, m)
   582  		if !isNormalized(z1) {
   583  			t.Errorf("#%d: %v is not normalized", i, *z1)
   584  		}
   585  		if z1.Cmp(out) != 0 {
   586  			t.Errorf("#%d: got %s want %s", i, z1, out)
   587  		}
   588  
   589  		if m == nil {
   590  			// The result should be the same as for m == 0;
   591  			// specifically, there should be no div-zero panic.
   592  			m = &Int{abs: nat{}} // m != nil && len(m.abs) == 0
   593  			z2 := new(Int).Exp(x, y, m)
   594  			if z2.Cmp(z1) != 0 {
   595  				t.Errorf("#%d: got %s want %s", i, z2, z1)
   596  			}
   597  		}
   598  	}
   599  }
   600  
   601  func checkGcd(aBytes, bBytes []byte) bool {
   602  	x := new(Int)
   603  	y := new(Int)
   604  	a := new(Int).SetBytes(aBytes)
   605  	b := new(Int).SetBytes(bBytes)
   606  
   607  	d := new(Int).GCD(x, y, a, b)
   608  	x.Mul(x, a)
   609  	y.Mul(y, b)
   610  	x.Add(x, y)
   611  
   612  	return x.Cmp(d) == 0
   613  }
   614  
   615  var gcdTests = []struct {
   616  	d, x, y, a, b string
   617  }{
   618  	// a <= 0 || b <= 0
   619  	{"0", "0", "0", "0", "0"},
   620  	{"0", "0", "0", "0", "7"},
   621  	{"0", "0", "0", "11", "0"},
   622  	{"0", "0", "0", "-77", "35"},
   623  	{"0", "0", "0", "64515", "-24310"},
   624  	{"0", "0", "0", "-64515", "-24310"},
   625  
   626  	{"1", "-9", "47", "120", "23"},
   627  	{"7", "1", "-2", "77", "35"},
   628  	{"935", "-3", "8", "64515", "24310"},
   629  	{"935000000000000000", "-3", "8", "64515000000000000000", "24310000000000000000"},
   630  	{"1", "-221", "22059940471369027483332068679400581064239780177629666810348940098015901108344", "98920366548084643601728869055592650835572950932266967461790948584315647051443", "991"},
   631  
   632  	// test early exit (after one Euclidean iteration) in binaryGCD
   633  	{"1", "", "", "1", "98920366548084643601728869055592650835572950932266967461790948584315647051443"},
   634  }
   635  
   636  func testGcd(t *testing.T, d, x, y, a, b *Int) {
   637  	var X *Int
   638  	if x != nil {
   639  		X = new(Int)
   640  	}
   641  	var Y *Int
   642  	if y != nil {
   643  		Y = new(Int)
   644  	}
   645  
   646  	D := new(Int).GCD(X, Y, a, b)
   647  	if D.Cmp(d) != 0 {
   648  		t.Errorf("GCD(%s, %s): got d = %s, want %s", a, b, D, d)
   649  	}
   650  	if x != nil && X.Cmp(x) != 0 {
   651  		t.Errorf("GCD(%s, %s): got x = %s, want %s", a, b, X, x)
   652  	}
   653  	if y != nil && Y.Cmp(y) != 0 {
   654  		t.Errorf("GCD(%s, %s): got y = %s, want %s", a, b, Y, y)
   655  	}
   656  
   657  	// binaryGCD requires a > 0 && b > 0
   658  	if a.Sign() <= 0 || b.Sign() <= 0 {
   659  		return
   660  	}
   661  
   662  	D.binaryGCD(a, b)
   663  	if D.Cmp(d) != 0 {
   664  		t.Errorf("binaryGcd(%s, %s): got d = %s, want %s", a, b, D, d)
   665  	}
   666  }
   667  
   668  func TestGcd(t *testing.T) {
   669  	for _, test := range gcdTests {
   670  		d, _ := new(Int).SetString(test.d, 0)
   671  		x, _ := new(Int).SetString(test.x, 0)
   672  		y, _ := new(Int).SetString(test.y, 0)
   673  		a, _ := new(Int).SetString(test.a, 0)
   674  		b, _ := new(Int).SetString(test.b, 0)
   675  
   676  		testGcd(t, d, nil, nil, a, b)
   677  		testGcd(t, d, x, nil, a, b)
   678  		testGcd(t, d, nil, y, a, b)
   679  		testGcd(t, d, x, y, a, b)
   680  	}
   681  
   682  	quick.Check(checkGcd, nil)
   683  }
   684  
   685  var primes = []string{
   686  	"2",
   687  	"3",
   688  	"5",
   689  	"7",
   690  	"11",
   691  
   692  	"13756265695458089029",
   693  	"13496181268022124907",
   694  	"10953742525620032441",
   695  	"17908251027575790097",
   696  
   697  	// http://golang.org/issue/638
   698  	"18699199384836356663",
   699  
   700  	"98920366548084643601728869055592650835572950932266967461790948584315647051443",
   701  	"94560208308847015747498523884063394671606671904944666360068158221458669711639",
   702  
   703  	// http://primes.utm.edu/lists/small/small3.html
   704  	"449417999055441493994709297093108513015373787049558499205492347871729927573118262811508386655998299074566974373711472560655026288668094291699357843464363003144674940345912431129144354948751003607115263071543163",
   705  	"230975859993204150666423538988557839555560243929065415434980904258310530753006723857139742334640122533598517597674807096648905501653461687601339782814316124971547968912893214002992086353183070342498989426570593",
   706  	"5521712099665906221540423207019333379125265462121169655563495403888449493493629943498064604536961775110765377745550377067893607246020694972959780839151452457728855382113555867743022746090187341871655890805971735385789993",
   707  	"203956878356401977405765866929034577280193993314348263094772646453283062722701277632936616063144088173312372882677123879538709400158306567338328279154499698366071906766440037074217117805690872792848149112022286332144876183376326512083574821647933992961249917319836219304274280243803104015000563790123",
   708  }
   709  
   710  var composites = []string{
   711  	"0",
   712  	"1",
   713  	"21284175091214687912771199898307297748211672914763848041968395774954376176754",
   714  	"6084766654921918907427900243509372380954290099172559290432744450051395395951",
   715  	"84594350493221918389213352992032324280367711247940675652888030554255915464401",
   716  	"82793403787388584738507275144194252681",
   717  }
   718  
   719  func TestProbablyPrime(t *testing.T) {
   720  	nreps := 20
   721  	if testing.Short() {
   722  		nreps = 1
   723  	}
   724  	for i, s := range primes {
   725  		p, _ := new(Int).SetString(s, 10)
   726  		if !p.ProbablyPrime(nreps) {
   727  			t.Errorf("#%d prime found to be non-prime (%s)", i, s)
   728  		}
   729  	}
   730  
   731  	for i, s := range composites {
   732  		c, _ := new(Int).SetString(s, 10)
   733  		if c.ProbablyPrime(nreps) {
   734  			t.Errorf("#%d composite found to be prime (%s)", i, s)
   735  		}
   736  		if testing.Short() {
   737  			break
   738  		}
   739  	}
   740  
   741  	// check that ProbablyPrime panics if n <= 0
   742  	c := NewInt(11) // a prime
   743  	for _, n := range []int{-1, 0, 1} {
   744  		func() {
   745  			defer func() {
   746  				if n <= 0 && recover() == nil {
   747  					t.Fatalf("expected panic from ProbablyPrime(%d)", n)
   748  				}
   749  			}()
   750  			if !c.ProbablyPrime(n) {
   751  				t.Fatalf("%v should be a prime", c)
   752  			}
   753  		}()
   754  	}
   755  }
   756  
   757  type intShiftTest struct {
   758  	in    string
   759  	shift uint
   760  	out   string
   761  }
   762  
   763  var rshTests = []intShiftTest{
   764  	{"0", 0, "0"},
   765  	{"-0", 0, "0"},
   766  	{"0", 1, "0"},
   767  	{"0", 2, "0"},
   768  	{"1", 0, "1"},
   769  	{"1", 1, "0"},
   770  	{"1", 2, "0"},
   771  	{"2", 0, "2"},
   772  	{"2", 1, "1"},
   773  	{"-1", 0, "-1"},
   774  	{"-1", 1, "-1"},
   775  	{"-1", 10, "-1"},
   776  	{"-100", 2, "-25"},
   777  	{"-100", 3, "-13"},
   778  	{"-100", 100, "-1"},
   779  	{"4294967296", 0, "4294967296"},
   780  	{"4294967296", 1, "2147483648"},
   781  	{"4294967296", 2, "1073741824"},
   782  	{"18446744073709551616", 0, "18446744073709551616"},
   783  	{"18446744073709551616", 1, "9223372036854775808"},
   784  	{"18446744073709551616", 2, "4611686018427387904"},
   785  	{"18446744073709551616", 64, "1"},
   786  	{"340282366920938463463374607431768211456", 64, "18446744073709551616"},
   787  	{"340282366920938463463374607431768211456", 128, "1"},
   788  }
   789  
   790  func TestRsh(t *testing.T) {
   791  	for i, test := range rshTests {
   792  		in, _ := new(Int).SetString(test.in, 10)
   793  		expected, _ := new(Int).SetString(test.out, 10)
   794  		out := new(Int).Rsh(in, test.shift)
   795  
   796  		if !isNormalized(out) {
   797  			t.Errorf("#%d: %v is not normalized", i, *out)
   798  		}
   799  		if out.Cmp(expected) != 0 {
   800  			t.Errorf("#%d: got %s want %s", i, out, expected)
   801  		}
   802  	}
   803  }
   804  
   805  func TestRshSelf(t *testing.T) {
   806  	for i, test := range rshTests {
   807  		z, _ := new(Int).SetString(test.in, 10)
   808  		expected, _ := new(Int).SetString(test.out, 10)
   809  		z.Rsh(z, test.shift)
   810  
   811  		if !isNormalized(z) {
   812  			t.Errorf("#%d: %v is not normalized", i, *z)
   813  		}
   814  		if z.Cmp(expected) != 0 {
   815  			t.Errorf("#%d: got %s want %s", i, z, expected)
   816  		}
   817  	}
   818  }
   819  
   820  var lshTests = []intShiftTest{
   821  	{"0", 0, "0"},
   822  	{"0", 1, "0"},
   823  	{"0", 2, "0"},
   824  	{"1", 0, "1"},
   825  	{"1", 1, "2"},
   826  	{"1", 2, "4"},
   827  	{"2", 0, "2"},
   828  	{"2", 1, "4"},
   829  	{"2", 2, "8"},
   830  	{"-87", 1, "-174"},
   831  	{"4294967296", 0, "4294967296"},
   832  	{"4294967296", 1, "8589934592"},
   833  	{"4294967296", 2, "17179869184"},
   834  	{"18446744073709551616", 0, "18446744073709551616"},
   835  	{"9223372036854775808", 1, "18446744073709551616"},
   836  	{"4611686018427387904", 2, "18446744073709551616"},
   837  	{"1", 64, "18446744073709551616"},
   838  	{"18446744073709551616", 64, "340282366920938463463374607431768211456"},
   839  	{"1", 128, "340282366920938463463374607431768211456"},
   840  }
   841  
   842  func TestLsh(t *testing.T) {
   843  	for i, test := range lshTests {
   844  		in, _ := new(Int).SetString(test.in, 10)
   845  		expected, _ := new(Int).SetString(test.out, 10)
   846  		out := new(Int).Lsh(in, test.shift)
   847  
   848  		if !isNormalized(out) {
   849  			t.Errorf("#%d: %v is not normalized", i, *out)
   850  		}
   851  		if out.Cmp(expected) != 0 {
   852  			t.Errorf("#%d: got %s want %s", i, out, expected)
   853  		}
   854  	}
   855  }
   856  
   857  func TestLshSelf(t *testing.T) {
   858  	for i, test := range lshTests {
   859  		z, _ := new(Int).SetString(test.in, 10)
   860  		expected, _ := new(Int).SetString(test.out, 10)
   861  		z.Lsh(z, test.shift)
   862  
   863  		if !isNormalized(z) {
   864  			t.Errorf("#%d: %v is not normalized", i, *z)
   865  		}
   866  		if z.Cmp(expected) != 0 {
   867  			t.Errorf("#%d: got %s want %s", i, z, expected)
   868  		}
   869  	}
   870  }
   871  
   872  func TestLshRsh(t *testing.T) {
   873  	for i, test := range rshTests {
   874  		in, _ := new(Int).SetString(test.in, 10)
   875  		out := new(Int).Lsh(in, test.shift)
   876  		out = out.Rsh(out, test.shift)
   877  
   878  		if !isNormalized(out) {
   879  			t.Errorf("#%d: %v is not normalized", i, *out)
   880  		}
   881  		if in.Cmp(out) != 0 {
   882  			t.Errorf("#%d: got %s want %s", i, out, in)
   883  		}
   884  	}
   885  	for i, test := range lshTests {
   886  		in, _ := new(Int).SetString(test.in, 10)
   887  		out := new(Int).Lsh(in, test.shift)
   888  		out.Rsh(out, test.shift)
   889  
   890  		if !isNormalized(out) {
   891  			t.Errorf("#%d: %v is not normalized", i, *out)
   892  		}
   893  		if in.Cmp(out) != 0 {
   894  			t.Errorf("#%d: got %s want %s", i, out, in)
   895  		}
   896  	}
   897  }
   898  
   899  var int64Tests = []int64{
   900  	0,
   901  	1,
   902  	-1,
   903  	4294967295,
   904  	-4294967295,
   905  	4294967296,
   906  	-4294967296,
   907  	9223372036854775807,
   908  	-9223372036854775807,
   909  	-9223372036854775808,
   910  }
   911  
   912  func TestInt64(t *testing.T) {
   913  	for i, testVal := range int64Tests {
   914  		in := NewInt(testVal)
   915  		out := in.Int64()
   916  
   917  		if out != testVal {
   918  			t.Errorf("#%d got %d want %d", i, out, testVal)
   919  		}
   920  	}
   921  }
   922  
   923  var uint64Tests = []uint64{
   924  	0,
   925  	1,
   926  	4294967295,
   927  	4294967296,
   928  	8589934591,
   929  	8589934592,
   930  	9223372036854775807,
   931  	9223372036854775808,
   932  	18446744073709551615, // 1<<64 - 1
   933  }
   934  
   935  func TestUint64(t *testing.T) {
   936  	in := new(Int)
   937  	for i, testVal := range uint64Tests {
   938  		in.SetUint64(testVal)
   939  		out := in.Uint64()
   940  
   941  		if out != testVal {
   942  			t.Errorf("#%d got %d want %d", i, out, testVal)
   943  		}
   944  
   945  		str := fmt.Sprint(testVal)
   946  		strOut := in.String()
   947  		if strOut != str {
   948  			t.Errorf("#%d.String got %s want %s", i, strOut, str)
   949  		}
   950  	}
   951  }
   952  
   953  var bitwiseTests = []struct {
   954  	x, y                 string
   955  	and, or, xor, andNot string
   956  }{
   957  	{"0x00", "0x00", "0x00", "0x00", "0x00", "0x00"},
   958  	{"0x00", "0x01", "0x00", "0x01", "0x01", "0x00"},
   959  	{"0x01", "0x00", "0x00", "0x01", "0x01", "0x01"},
   960  	{"-0x01", "0x00", "0x00", "-0x01", "-0x01", "-0x01"},
   961  	{"-0xaf", "-0x50", "-0xf0", "-0x0f", "0xe1", "0x41"},
   962  	{"0x00", "-0x01", "0x00", "-0x01", "-0x01", "0x00"},
   963  	{"0x01", "0x01", "0x01", "0x01", "0x00", "0x00"},
   964  	{"-0x01", "-0x01", "-0x01", "-0x01", "0x00", "0x00"},
   965  	{"0x07", "0x08", "0x00", "0x0f", "0x0f", "0x07"},
   966  	{"0x05", "0x0f", "0x05", "0x0f", "0x0a", "0x00"},
   967  	{"0xff", "-0x0a", "0xf6", "-0x01", "-0xf7", "0x09"},
   968  	{"0x013ff6", "0x9a4e", "0x1a46", "0x01bffe", "0x01a5b8", "0x0125b0"},
   969  	{"-0x013ff6", "0x9a4e", "0x800a", "-0x0125b2", "-0x01a5bc", "-0x01c000"},
   970  	{"-0x013ff6", "-0x9a4e", "-0x01bffe", "-0x1a46", "0x01a5b8", "0x8008"},
   971  	{
   972  		"0x1000009dc6e3d9822cba04129bcbe3401",
   973  		"0xb9bd7d543685789d57cb918e833af352559021483cdb05cc21fd",
   974  		"0x1000001186210100001000009048c2001",
   975  		"0xb9bd7d543685789d57cb918e8bfeff7fddb2ebe87dfbbdfe35fd",
   976  		"0xb9bd7d543685789d57ca918e8ae69d6fcdb2eae87df2b97215fc",
   977  		"0x8c40c2d8822caa04120b8321400",
   978  	},
   979  	{
   980  		"0x1000009dc6e3d9822cba04129bcbe3401",
   981  		"-0xb9bd7d543685789d57cb918e833af352559021483cdb05cc21fd",
   982  		"0x8c40c2d8822caa04120b8321401",
   983  		"-0xb9bd7d543685789d57ca918e82229142459020483cd2014001fd",
   984  		"-0xb9bd7d543685789d57ca918e8ae69d6fcdb2eae87df2b97215fe",
   985  		"0x1000001186210100001000009048c2000",
   986  	},
   987  	{
   988  		"-0x1000009dc6e3d9822cba04129bcbe3401",
   989  		"-0xb9bd7d543685789d57cb918e833af352559021483cdb05cc21fd",
   990  		"-0xb9bd7d543685789d57cb918e8bfeff7fddb2ebe87dfbbdfe35fd",
   991  		"-0x1000001186210100001000009048c2001",
   992  		"0xb9bd7d543685789d57ca918e8ae69d6fcdb2eae87df2b97215fc",
   993  		"0xb9bd7d543685789d57ca918e82229142459020483cd2014001fc",
   994  	},
   995  }
   996  
   997  type bitFun func(z, x, y *Int) *Int
   998  
   999  func testBitFun(t *testing.T, msg string, f bitFun, x, y *Int, exp string) {
  1000  	expected := new(Int)
  1001  	expected.SetString(exp, 0)
  1002  
  1003  	out := f(new(Int), x, y)
  1004  	if out.Cmp(expected) != 0 {
  1005  		t.Errorf("%s: got %s want %s", msg, out, expected)
  1006  	}
  1007  }
  1008  
  1009  func testBitFunSelf(t *testing.T, msg string, f bitFun, x, y *Int, exp string) {
  1010  	self := new(Int)
  1011  	self.Set(x)
  1012  	expected := new(Int)
  1013  	expected.SetString(exp, 0)
  1014  
  1015  	self = f(self, self, y)
  1016  	if self.Cmp(expected) != 0 {
  1017  		t.Errorf("%s: got %s want %s", msg, self, expected)
  1018  	}
  1019  }
  1020  
  1021  func altBit(x *Int, i int) uint {
  1022  	z := new(Int).Rsh(x, uint(i))
  1023  	z = z.And(z, NewInt(1))
  1024  	if z.Cmp(new(Int)) != 0 {
  1025  		return 1
  1026  	}
  1027  	return 0
  1028  }
  1029  
  1030  func altSetBit(z *Int, x *Int, i int, b uint) *Int {
  1031  	one := NewInt(1)
  1032  	m := one.Lsh(one, uint(i))
  1033  	switch b {
  1034  	case 1:
  1035  		return z.Or(x, m)
  1036  	case 0:
  1037  		return z.AndNot(x, m)
  1038  	}
  1039  	panic("set bit is not 0 or 1")
  1040  }
  1041  
  1042  func testBitset(t *testing.T, x *Int) {
  1043  	n := x.BitLen()
  1044  	z := new(Int).Set(x)
  1045  	z1 := new(Int).Set(x)
  1046  	for i := 0; i < n+10; i++ {
  1047  		old := z.Bit(i)
  1048  		old1 := altBit(z1, i)
  1049  		if old != old1 {
  1050  			t.Errorf("bitset: inconsistent value for Bit(%s, %d), got %v want %v", z1, i, old, old1)
  1051  		}
  1052  		z := new(Int).SetBit(z, i, 1)
  1053  		z1 := altSetBit(new(Int), z1, i, 1)
  1054  		if z.Bit(i) == 0 {
  1055  			t.Errorf("bitset: bit %d of %s got 0 want 1", i, x)
  1056  		}
  1057  		if z.Cmp(z1) != 0 {
  1058  			t.Errorf("bitset: inconsistent value after SetBit 1, got %s want %s", z, z1)
  1059  		}
  1060  		z.SetBit(z, i, 0)
  1061  		altSetBit(z1, z1, i, 0)
  1062  		if z.Bit(i) != 0 {
  1063  			t.Errorf("bitset: bit %d of %s got 1 want 0", i, x)
  1064  		}
  1065  		if z.Cmp(z1) != 0 {
  1066  			t.Errorf("bitset: inconsistent value after SetBit 0, got %s want %s", z, z1)
  1067  		}
  1068  		altSetBit(z1, z1, i, old)
  1069  		z.SetBit(z, i, old)
  1070  		if z.Cmp(z1) != 0 {
  1071  			t.Errorf("bitset: inconsistent value after SetBit old, got %s want %s", z, z1)
  1072  		}
  1073  	}
  1074  	if z.Cmp(x) != 0 {
  1075  		t.Errorf("bitset: got %s want %s", z, x)
  1076  	}
  1077  }
  1078  
  1079  var bitsetTests = []struct {
  1080  	x string
  1081  	i int
  1082  	b uint
  1083  }{
  1084  	{"0", 0, 0},
  1085  	{"0", 200, 0},
  1086  	{"1", 0, 1},
  1087  	{"1", 1, 0},
  1088  	{"-1", 0, 1},
  1089  	{"-1", 200, 1},
  1090  	{"0x2000000000000000000000000000", 108, 0},
  1091  	{"0x2000000000000000000000000000", 109, 1},
  1092  	{"0x2000000000000000000000000000", 110, 0},
  1093  	{"-0x2000000000000000000000000001", 108, 1},
  1094  	{"-0x2000000000000000000000000001", 109, 0},
  1095  	{"-0x2000000000000000000000000001", 110, 1},
  1096  }
  1097  
  1098  func TestBitSet(t *testing.T) {
  1099  	for _, test := range bitwiseTests {
  1100  		x := new(Int)
  1101  		x.SetString(test.x, 0)
  1102  		testBitset(t, x)
  1103  		x = new(Int)
  1104  		x.SetString(test.y, 0)
  1105  		testBitset(t, x)
  1106  	}
  1107  	for i, test := range bitsetTests {
  1108  		x := new(Int)
  1109  		x.SetString(test.x, 0)
  1110  		b := x.Bit(test.i)
  1111  		if b != test.b {
  1112  			t.Errorf("#%d got %v want %v", i, b, test.b)
  1113  		}
  1114  	}
  1115  	z := NewInt(1)
  1116  	z.SetBit(NewInt(0), 2, 1)
  1117  	if z.Cmp(NewInt(4)) != 0 {
  1118  		t.Errorf("destination leaked into result; got %s want 4", z)
  1119  	}
  1120  }
  1121  
  1122  func BenchmarkBitset(b *testing.B) {
  1123  	z := new(Int)
  1124  	z.SetBit(z, 512, 1)
  1125  	b.ResetTimer()
  1126  	b.StartTimer()
  1127  	for i := b.N - 1; i >= 0; i-- {
  1128  		z.SetBit(z, i&512, 1)
  1129  	}
  1130  }
  1131  
  1132  func BenchmarkBitsetNeg(b *testing.B) {
  1133  	z := NewInt(-1)
  1134  	z.SetBit(z, 512, 0)
  1135  	b.ResetTimer()
  1136  	b.StartTimer()
  1137  	for i := b.N - 1; i >= 0; i-- {
  1138  		z.SetBit(z, i&512, 0)
  1139  	}
  1140  }
  1141  
  1142  func BenchmarkBitsetOrig(b *testing.B) {
  1143  	z := new(Int)
  1144  	altSetBit(z, z, 512, 1)
  1145  	b.ResetTimer()
  1146  	b.StartTimer()
  1147  	for i := b.N - 1; i >= 0; i-- {
  1148  		altSetBit(z, z, i&512, 1)
  1149  	}
  1150  }
  1151  
  1152  func BenchmarkBitsetNegOrig(b *testing.B) {
  1153  	z := NewInt(-1)
  1154  	altSetBit(z, z, 512, 0)
  1155  	b.ResetTimer()
  1156  	b.StartTimer()
  1157  	for i := b.N - 1; i >= 0; i-- {
  1158  		altSetBit(z, z, i&512, 0)
  1159  	}
  1160  }
  1161  
  1162  func TestBitwise(t *testing.T) {
  1163  	x := new(Int)
  1164  	y := new(Int)
  1165  	for _, test := range bitwiseTests {
  1166  		x.SetString(test.x, 0)
  1167  		y.SetString(test.y, 0)
  1168  
  1169  		testBitFun(t, "and", (*Int).And, x, y, test.and)
  1170  		testBitFunSelf(t, "and", (*Int).And, x, y, test.and)
  1171  		testBitFun(t, "andNot", (*Int).AndNot, x, y, test.andNot)
  1172  		testBitFunSelf(t, "andNot", (*Int).AndNot, x, y, test.andNot)
  1173  		testBitFun(t, "or", (*Int).Or, x, y, test.or)
  1174  		testBitFunSelf(t, "or", (*Int).Or, x, y, test.or)
  1175  		testBitFun(t, "xor", (*Int).Xor, x, y, test.xor)
  1176  		testBitFunSelf(t, "xor", (*Int).Xor, x, y, test.xor)
  1177  	}
  1178  }
  1179  
  1180  var notTests = []struct {
  1181  	in  string
  1182  	out string
  1183  }{
  1184  	{"0", "-1"},
  1185  	{"1", "-2"},
  1186  	{"7", "-8"},
  1187  	{"0", "-1"},
  1188  	{"-81910", "81909"},
  1189  	{
  1190  		"298472983472983471903246121093472394872319615612417471234712061",
  1191  		"-298472983472983471903246121093472394872319615612417471234712062",
  1192  	},
  1193  }
  1194  
  1195  func TestNot(t *testing.T) {
  1196  	in := new(Int)
  1197  	out := new(Int)
  1198  	expected := new(Int)
  1199  	for i, test := range notTests {
  1200  		in.SetString(test.in, 10)
  1201  		expected.SetString(test.out, 10)
  1202  		out = out.Not(in)
  1203  		if out.Cmp(expected) != 0 {
  1204  			t.Errorf("#%d: got %s want %s", i, out, expected)
  1205  		}
  1206  		out = out.Not(out)
  1207  		if out.Cmp(in) != 0 {
  1208  			t.Errorf("#%d: got %s want %s", i, out, in)
  1209  		}
  1210  	}
  1211  }
  1212  
  1213  var modInverseTests = []struct {
  1214  	element string
  1215  	modulus string
  1216  }{
  1217  	{"1234567", "458948883992"},
  1218  	{"239487239847", "2410312426921032588552076022197566074856950548502459942654116941958108831682612228890093858261341614673227141477904012196503648957050582631942730706805009223062734745341073406696246014589361659774041027169249453200378729434170325843778659198143763193776859869524088940195577346119843545301547043747207749969763750084308926339295559968882457872412993810129130294592999947926365264059284647209730384947211681434464714438488520940127459844288859336526896320919633919"},
  1219  }
  1220  
  1221  func TestModInverse(t *testing.T) {
  1222  	var element, modulus, gcd, inverse Int
  1223  	one := NewInt(1)
  1224  	for i, test := range modInverseTests {
  1225  		(&element).SetString(test.element, 10)
  1226  		(&modulus).SetString(test.modulus, 10)
  1227  		(&inverse).ModInverse(&element, &modulus)
  1228  		(&inverse).Mul(&inverse, &element)
  1229  		(&inverse).Mod(&inverse, &modulus)
  1230  		if (&inverse).Cmp(one) != 0 {
  1231  			t.Errorf("#%d: failed (e·e^(-1)=%s)", i, &inverse)
  1232  		}
  1233  	}
  1234  	// exhaustive test for small values
  1235  	for n := 2; n < 100; n++ {
  1236  		(&modulus).SetInt64(int64(n))
  1237  		for x := 1; x < n; x++ {
  1238  			(&element).SetInt64(int64(x))
  1239  			(&gcd).GCD(nil, nil, &element, &modulus)
  1240  			if (&gcd).Cmp(one) != 0 {
  1241  				continue
  1242  			}
  1243  			(&inverse).ModInverse(&element, &modulus)
  1244  			(&inverse).Mul(&inverse, &element)
  1245  			(&inverse).Mod(&inverse, &modulus)
  1246  			if (&inverse).Cmp(one) != 0 {
  1247  				t.Errorf("ModInverse(%d,%d)*%d%%%d=%d, not 1", &element, &modulus, &element, &modulus, &inverse)
  1248  			}
  1249  		}
  1250  	}
  1251  }
  1252  
  1253  var encodingTests = []string{
  1254  	"-539345864568634858364538753846587364875430589374589",
  1255  	"-678645873",
  1256  	"-100",
  1257  	"-2",
  1258  	"-1",
  1259  	"0",
  1260  	"1",
  1261  	"2",
  1262  	"10",
  1263  	"42",
  1264  	"1234567890",
  1265  	"298472983472983471903246121093472394872319615612417471234712061",
  1266  }
  1267  
  1268  func TestIntGobEncoding(t *testing.T) {
  1269  	var medium bytes.Buffer
  1270  	enc := gob.NewEncoder(&medium)
  1271  	dec := gob.NewDecoder(&medium)
  1272  	for _, test := range encodingTests {
  1273  		medium.Reset() // empty buffer for each test case (in case of failures)
  1274  		var tx Int
  1275  		tx.SetString(test, 10)
  1276  		if err := enc.Encode(&tx); err != nil {
  1277  			t.Errorf("encoding of %s failed: %s", &tx, err)
  1278  		}
  1279  		var rx Int
  1280  		if err := dec.Decode(&rx); err != nil {
  1281  			t.Errorf("decoding of %s failed: %s", &tx, err)
  1282  		}
  1283  		if rx.Cmp(&tx) != 0 {
  1284  			t.Errorf("transmission of %s failed: got %s want %s", &tx, &rx, &tx)
  1285  		}
  1286  	}
  1287  }
  1288  
  1289  // Sending a nil Int pointer (inside a slice) on a round trip through gob should yield a zero.
  1290  // TODO: top-level nils.
  1291  func TestGobEncodingNilIntInSlice(t *testing.T) {
  1292  	buf := new(bytes.Buffer)
  1293  	enc := gob.NewEncoder(buf)
  1294  	dec := gob.NewDecoder(buf)
  1295  
  1296  	var in = make([]*Int, 1)
  1297  	err := enc.Encode(&in)
  1298  	if err != nil {
  1299  		t.Errorf("gob encode failed: %q", err)
  1300  	}
  1301  	var out []*Int
  1302  	err = dec.Decode(&out)
  1303  	if err != nil {
  1304  		t.Fatalf("gob decode failed: %q", err)
  1305  	}
  1306  	if len(out) != 1 {
  1307  		t.Fatalf("wrong len; want 1 got %d", len(out))
  1308  	}
  1309  	var zero Int
  1310  	if out[0].Cmp(&zero) != 0 {
  1311  		t.Errorf("transmission of (*Int)(nill) failed: got %s want 0", out)
  1312  	}
  1313  }
  1314  
  1315  func TestIntJSONEncoding(t *testing.T) {
  1316  	for _, test := range encodingTests {
  1317  		var tx Int
  1318  		tx.SetString(test, 10)
  1319  		b, err := json.Marshal(&tx)
  1320  		if err != nil {
  1321  			t.Errorf("marshaling of %s failed: %s", &tx, err)
  1322  		}
  1323  		var rx Int
  1324  		if err := json.Unmarshal(b, &rx); err != nil {
  1325  			t.Errorf("unmarshaling of %s failed: %s", &tx, err)
  1326  		}
  1327  		if rx.Cmp(&tx) != 0 {
  1328  			t.Errorf("JSON encoding of %s failed: got %s want %s", &tx, &rx, &tx)
  1329  		}
  1330  	}
  1331  }
  1332  
  1333  var intVals = []string{
  1334  	"-141592653589793238462643383279502884197169399375105820974944592307816406286",
  1335  	"-1415926535897932384626433832795028841971",
  1336  	"-141592653589793",
  1337  	"-1",
  1338  	"0",
  1339  	"1",
  1340  	"141592653589793",
  1341  	"1415926535897932384626433832795028841971",
  1342  	"141592653589793238462643383279502884197169399375105820974944592307816406286",
  1343  }
  1344  
  1345  func TestIntJSONEncodingTextMarshaller(t *testing.T) {
  1346  	for _, num := range intVals {
  1347  		var tx Int
  1348  		tx.SetString(num, 0)
  1349  		b, err := json.Marshal(&tx)
  1350  		if err != nil {
  1351  			t.Errorf("marshaling of %s failed: %s", &tx, err)
  1352  			continue
  1353  		}
  1354  		var rx Int
  1355  		if err := json.Unmarshal(b, &rx); err != nil {
  1356  			t.Errorf("unmarshaling of %s failed: %s", &tx, err)
  1357  			continue
  1358  		}
  1359  		if rx.Cmp(&tx) != 0 {
  1360  			t.Errorf("JSON encoding of %s failed: got %s want %s", &tx, &rx, &tx)
  1361  		}
  1362  	}
  1363  }
  1364  
  1365  func TestIntXMLEncodingTextMarshaller(t *testing.T) {
  1366  	for _, num := range intVals {
  1367  		var tx Int
  1368  		tx.SetString(num, 0)
  1369  		b, err := xml.Marshal(&tx)
  1370  		if err != nil {
  1371  			t.Errorf("marshaling of %s failed: %s", &tx, err)
  1372  			continue
  1373  		}
  1374  		var rx Int
  1375  		if err := xml.Unmarshal(b, &rx); err != nil {
  1376  			t.Errorf("unmarshaling of %s failed: %s", &tx, err)
  1377  			continue
  1378  		}
  1379  		if rx.Cmp(&tx) != 0 {
  1380  			t.Errorf("XML encoding of %s failed: got %s want %s", &tx, &rx, &tx)
  1381  		}
  1382  	}
  1383  }
  1384  
  1385  func TestIssue2607(t *testing.T) {
  1386  	// This code sequence used to hang.
  1387  	n := NewInt(10)
  1388  	n.Rand(rand.New(rand.NewSource(9)), n)
  1389  }