github.com/bgentry/go@v0.0.0-20150121062915-6cf5a733d54d/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  	"io"
     9  	"runtime"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  var cmpTests = []struct {
    15  	x, y nat
    16  	r    int
    17  }{
    18  	{nil, nil, 0},
    19  	{nil, nat(nil), 0},
    20  	{nat(nil), nil, 0},
    21  	{nat(nil), nat(nil), 0},
    22  	{nat{0}, nat{0}, 0},
    23  	{nat{0}, nat{1}, -1},
    24  	{nat{1}, nat{0}, 1},
    25  	{nat{1}, nat{1}, 0},
    26  	{nat{0, _M}, nat{1}, 1},
    27  	{nat{1}, nat{0, _M}, -1},
    28  	{nat{1, _M}, nat{0, _M}, 1},
    29  	{nat{0, _M}, nat{1, _M}, -1},
    30  	{nat{16, 571956, 8794, 68}, nat{837, 9146, 1, 754489}, -1},
    31  	{nat{34986, 41, 105, 1957}, nat{56, 7458, 104, 1957}, 1},
    32  }
    33  
    34  func TestCmp(t *testing.T) {
    35  	for i, a := range cmpTests {
    36  		r := a.x.cmp(a.y)
    37  		if r != a.r {
    38  			t.Errorf("#%d got r = %v; want %v", i, r, a.r)
    39  		}
    40  	}
    41  }
    42  
    43  type funNN func(z, x, y nat) nat
    44  type argNN struct {
    45  	z, x, y nat
    46  }
    47  
    48  var sumNN = []argNN{
    49  	{},
    50  	{nat{1}, nil, nat{1}},
    51  	{nat{1111111110}, nat{123456789}, nat{987654321}},
    52  	{nat{0, 0, 0, 1}, nil, nat{0, 0, 0, 1}},
    53  	{nat{0, 0, 0, 1111111110}, nat{0, 0, 0, 123456789}, nat{0, 0, 0, 987654321}},
    54  	{nat{0, 0, 0, 1}, nat{0, 0, _M}, nat{0, 0, 1}},
    55  }
    56  
    57  var prodNN = []argNN{
    58  	{},
    59  	{nil, nil, nil},
    60  	{nil, nat{991}, nil},
    61  	{nat{991}, nat{991}, nat{1}},
    62  	{nat{991 * 991}, nat{991}, nat{991}},
    63  	{nat{0, 0, 991 * 991}, nat{0, 991}, nat{0, 991}},
    64  	{nat{1 * 991, 2 * 991, 3 * 991, 4 * 991}, nat{1, 2, 3, 4}, nat{991}},
    65  	{nat{4, 11, 20, 30, 20, 11, 4}, nat{1, 2, 3, 4}, nat{4, 3, 2, 1}},
    66  	// 3^100 * 3^28 = 3^128
    67  	{
    68  		natFromString("11790184577738583171520872861412518665678211592275841109096961"),
    69  		natFromString("515377520732011331036461129765621272702107522001"),
    70  		natFromString("22876792454961"),
    71  	},
    72  	// z = 111....1 (70000 digits)
    73  	// x = 10^(99*700) + ... + 10^1400 + 10^700 + 1
    74  	// y = 111....1 (700 digits, larger than Karatsuba threshold on 32-bit and 64-bit)
    75  	{
    76  		natFromString(strings.Repeat("1", 70000)),
    77  		natFromString("1" + strings.Repeat(strings.Repeat("0", 699)+"1", 99)),
    78  		natFromString(strings.Repeat("1", 700)),
    79  	},
    80  	// z = 111....1 (20000 digits)
    81  	// x = 10^10000 + 1
    82  	// y = 111....1 (10000 digits)
    83  	{
    84  		natFromString(strings.Repeat("1", 20000)),
    85  		natFromString("1" + strings.Repeat("0", 9999) + "1"),
    86  		natFromString(strings.Repeat("1", 10000)),
    87  	},
    88  }
    89  
    90  func natFromString(s string) nat {
    91  	x, _, _, err := nat(nil).scan(strings.NewReader(s), 0)
    92  	if err != nil {
    93  		panic(err)
    94  	}
    95  	return x
    96  }
    97  
    98  func TestSet(t *testing.T) {
    99  	for _, a := range sumNN {
   100  		z := nat(nil).set(a.z)
   101  		if z.cmp(a.z) != 0 {
   102  			t.Errorf("got z = %v; want %v", z, a.z)
   103  		}
   104  	}
   105  }
   106  
   107  func testFunNN(t *testing.T, msg string, f funNN, a argNN) {
   108  	z := f(nil, a.x, a.y)
   109  	if z.cmp(a.z) != 0 {
   110  		t.Errorf("%s%+v\n\tgot z = %v; want %v", msg, a, z, a.z)
   111  	}
   112  }
   113  
   114  func TestFunNN(t *testing.T) {
   115  	for _, a := range sumNN {
   116  		arg := a
   117  		testFunNN(t, "add", nat.add, arg)
   118  
   119  		arg = argNN{a.z, a.y, a.x}
   120  		testFunNN(t, "add symmetric", nat.add, arg)
   121  
   122  		arg = argNN{a.x, a.z, a.y}
   123  		testFunNN(t, "sub", nat.sub, arg)
   124  
   125  		arg = argNN{a.y, a.z, a.x}
   126  		testFunNN(t, "sub symmetric", nat.sub, arg)
   127  	}
   128  
   129  	for _, a := range prodNN {
   130  		arg := a
   131  		testFunNN(t, "mul", nat.mul, arg)
   132  
   133  		arg = argNN{a.z, a.y, a.x}
   134  		testFunNN(t, "mul symmetric", nat.mul, arg)
   135  	}
   136  }
   137  
   138  var mulRangesN = []struct {
   139  	a, b uint64
   140  	prod string
   141  }{
   142  	{0, 0, "0"},
   143  	{1, 1, "1"},
   144  	{1, 2, "2"},
   145  	{1, 3, "6"},
   146  	{10, 10, "10"},
   147  	{0, 100, "0"},
   148  	{0, 1e9, "0"},
   149  	{1, 0, "1"},                    // empty range
   150  	{100, 1, "1"},                  // empty range
   151  	{1, 10, "3628800"},             // 10!
   152  	{1, 20, "2432902008176640000"}, // 20!
   153  	{1, 100,
   154  		"933262154439441526816992388562667004907159682643816214685929" +
   155  			"638952175999932299156089414639761565182862536979208272237582" +
   156  			"51185210916864000000000000000000000000", // 100!
   157  	},
   158  }
   159  
   160  func TestMulRangeN(t *testing.T) {
   161  	for i, r := range mulRangesN {
   162  		prod := nat(nil).mulRange(r.a, r.b).decimalString()
   163  		if prod != r.prod {
   164  			t.Errorf("#%d: got %s; want %s", i, prod, r.prod)
   165  		}
   166  	}
   167  }
   168  
   169  // allocBytes returns the number of bytes allocated by invoking f.
   170  func allocBytes(f func()) uint64 {
   171  	var stats runtime.MemStats
   172  	runtime.ReadMemStats(&stats)
   173  	t := stats.TotalAlloc
   174  	f()
   175  	runtime.ReadMemStats(&stats)
   176  	return stats.TotalAlloc - t
   177  }
   178  
   179  // TestMulUnbalanced tests that multiplying numbers of different lengths
   180  // does not cause deep recursion and in turn allocate too much memory.
   181  // Test case for issue 3807.
   182  func TestMulUnbalanced(t *testing.T) {
   183  	defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
   184  	x := rndNat(50000)
   185  	y := rndNat(40)
   186  	allocSize := allocBytes(func() {
   187  		nat(nil).mul(x, y)
   188  	})
   189  	inputSize := uint64(len(x)+len(y)) * _S
   190  	if ratio := allocSize / uint64(inputSize); ratio > 10 {
   191  		t.Errorf("multiplication uses too much memory (%d > %d times the size of inputs)", allocSize, ratio)
   192  	}
   193  }
   194  
   195  func rndNat(n int) nat {
   196  	return nat(rndV(n)).norm()
   197  }
   198  
   199  func BenchmarkMul(b *testing.B) {
   200  	mulx := rndNat(1e4)
   201  	muly := rndNat(1e4)
   202  	b.ResetTimer()
   203  	for i := 0; i < b.N; i++ {
   204  		var z nat
   205  		z.mul(mulx, muly)
   206  	}
   207  }
   208  
   209  func toString(x nat, charset string) string {
   210  	base := len(charset)
   211  
   212  	// special cases
   213  	switch {
   214  	case base < 2:
   215  		panic("illegal base")
   216  	case len(x) == 0:
   217  		return string(charset[0])
   218  	}
   219  
   220  	// allocate buffer for conversion
   221  	i := x.bitLen()/log2(Word(base)) + 1 // +1: round up
   222  	s := make([]byte, i)
   223  
   224  	// don't destroy x
   225  	q := nat(nil).set(x)
   226  
   227  	// convert
   228  	for len(q) > 0 {
   229  		i--
   230  		var r Word
   231  		q, r = q.divW(q, Word(base))
   232  		s[i] = charset[r]
   233  	}
   234  
   235  	return string(s[i:])
   236  }
   237  
   238  var strTests = []struct {
   239  	x nat    // nat value to be converted
   240  	c string // conversion charset
   241  	s string // expected result
   242  }{
   243  	{nil, "01", "0"},
   244  	{nat{1}, "01", "1"},
   245  	{nat{0xc5}, "01", "11000101"},
   246  	{nat{03271}, lowercaseDigits[:8], "3271"},
   247  	{nat{10}, lowercaseDigits[:10], "10"},
   248  	{nat{1234567890}, uppercaseDigits[:10], "1234567890"},
   249  	{nat{0xdeadbeef}, lowercaseDigits[:16], "deadbeef"},
   250  	{nat{0xdeadbeef}, uppercaseDigits[:16], "DEADBEEF"},
   251  	{nat{0x229be7}, lowercaseDigits[:17], "1a2b3c"},
   252  	{nat{0x309663e6}, uppercaseDigits[:32], "O9COV6"},
   253  }
   254  
   255  func TestString(t *testing.T) {
   256  	// test invalid character set explicitly
   257  	var panicStr string
   258  	func() {
   259  		defer func() {
   260  			panicStr = recover().(string)
   261  		}()
   262  		natOne.string("0")
   263  	}()
   264  	if panicStr != "invalid character set length" {
   265  		t.Errorf("expected panic for invalid character set")
   266  	}
   267  
   268  	for _, a := range strTests {
   269  		s := a.x.string(a.c)
   270  		if s != a.s {
   271  			t.Errorf("string%+v\n\tgot s = %s; want %s", a, s, a.s)
   272  		}
   273  
   274  		x, b, _, err := nat(nil).scan(strings.NewReader(a.s), len(a.c))
   275  		if x.cmp(a.x) != 0 {
   276  			t.Errorf("scan%+v\n\tgot z = %v; want %v", a, x, a.x)
   277  		}
   278  		if b != len(a.c) {
   279  			t.Errorf("scan%+v\n\tgot b = %d; want %d", a, b, len(a.c))
   280  		}
   281  		if err != nil {
   282  			t.Errorf("scan%+v\n\tgot error = %s", a, err)
   283  		}
   284  	}
   285  }
   286  
   287  var natScanTests = []struct {
   288  	s     string // string to be scanned
   289  	base  int    // input base
   290  	x     nat    // expected nat
   291  	b     int    // expected base
   292  	count int    // expected digit count
   293  	ok    bool   // expected success
   294  	next  rune   // next character (or 0, if at EOF)
   295  }{
   296  	// error: illegal base
   297  	{base: -1},
   298  	{base: 37},
   299  
   300  	// error: no mantissa
   301  	{},
   302  	{s: "?"},
   303  	{base: 10},
   304  	{base: 36},
   305  	{s: "?", base: 10},
   306  	{s: "0x"},
   307  	{s: "345", base: 2},
   308  
   309  	// error: incorrect use of decimal point
   310  	{s: ".0"},
   311  	{s: ".0", base: 10},
   312  	{s: ".", base: 1},
   313  	{s: "0x.0"},
   314  
   315  	// no errors
   316  	{"0", 0, nil, 10, 1, true, 0},
   317  	{"0", 10, nil, 10, 1, true, 0},
   318  	{"0", 36, nil, 36, 1, true, 0},
   319  	{"1", 0, nat{1}, 10, 1, true, 0},
   320  	{"1", 10, nat{1}, 10, 1, true, 0},
   321  	{"0 ", 0, nil, 10, 1, true, ' '},
   322  	{"08", 0, nil, 10, 1, true, '8'},
   323  	{"08", 10, nat{8}, 10, 2, true, 0},
   324  	{"018", 0, nat{1}, 8, 1, true, '8'},
   325  	{"0b1", 0, nat{1}, 2, 1, true, 0},
   326  	{"0b11000101", 0, nat{0xc5}, 2, 8, true, 0},
   327  	{"03271", 0, nat{03271}, 8, 4, true, 0},
   328  	{"10ab", 0, nat{10}, 10, 2, true, 'a'},
   329  	{"1234567890", 0, nat{1234567890}, 10, 10, true, 0},
   330  	{"xyz", 36, nat{(33*36+34)*36 + 35}, 36, 3, true, 0},
   331  	{"xyz?", 36, nat{(33*36+34)*36 + 35}, 36, 3, true, '?'},
   332  	{"0x", 16, nil, 16, 1, true, 'x'},
   333  	{"0xdeadbeef", 0, nat{0xdeadbeef}, 16, 8, true, 0},
   334  	{"0XDEADBEEF", 0, nat{0xdeadbeef}, 16, 8, true, 0},
   335  
   336  	// no errors, decimal point
   337  	{"0.", 0, nil, 10, 1, true, '.'},
   338  	{"0.", 1, nil, 10, 0, true, 0},
   339  	{"0.1.2", 1, nat{1}, 10, -1, true, '.'},
   340  	{".000", 1, nil, 10, -3, true, 0},
   341  	{"12.3", 1, nat{123}, 10, -1, true, 0},
   342  	{"012.345", 1, nat{12345}, 10, -3, true, 0},
   343  }
   344  
   345  func TestScanBase(t *testing.T) {
   346  	for _, a := range natScanTests {
   347  		r := strings.NewReader(a.s)
   348  		x, b, count, err := nat(nil).scan(r, a.base)
   349  		if err == nil && !a.ok {
   350  			t.Errorf("scan%+v\n\texpected error", a)
   351  		}
   352  		if err != nil {
   353  			if a.ok {
   354  				t.Errorf("scan%+v\n\tgot error = %s", a, err)
   355  			}
   356  			continue
   357  		}
   358  		if x.cmp(a.x) != 0 {
   359  			t.Errorf("scan%+v\n\tgot z = %v; want %v", a, x, a.x)
   360  		}
   361  		if b != a.b {
   362  			t.Errorf("scan%+v\n\tgot b = %d; want %d", a, b, a.base)
   363  		}
   364  		if count != a.count {
   365  			t.Errorf("scan%+v\n\tgot count = %d; want %d", a, count, a.count)
   366  		}
   367  		next, _, err := r.ReadRune()
   368  		if err == io.EOF {
   369  			next = 0
   370  			err = nil
   371  		}
   372  		if err == nil && next != a.next {
   373  			t.Errorf("scan%+v\n\tgot next = %q; want %q", a, next, a.next)
   374  		}
   375  	}
   376  }
   377  
   378  var pi = "3" +
   379  	"14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651" +
   380  	"32823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461" +
   381  	"28475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920" +
   382  	"96282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179" +
   383  	"31051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798" +
   384  	"60943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901" +
   385  	"22495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999837" +
   386  	"29780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083" +
   387  	"81420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909" +
   388  	"21642019893809525720106548586327886593615338182796823030195203530185296899577362259941389124972177528347913151" +
   389  	"55748572424541506959508295331168617278558890750983817546374649393192550604009277016711390098488240128583616035" +
   390  	"63707660104710181942955596198946767837449448255379774726847104047534646208046684259069491293313677028989152104" +
   391  	"75216205696602405803815019351125338243003558764024749647326391419927260426992279678235478163600934172164121992" +
   392  	"45863150302861829745557067498385054945885869269956909272107975093029553211653449872027559602364806654991198818" +
   393  	"34797753566369807426542527862551818417574672890977772793800081647060016145249192173217214772350141441973568548" +
   394  	"16136115735255213347574184946843852332390739414333454776241686251898356948556209921922218427255025425688767179" +
   395  	"04946016534668049886272327917860857843838279679766814541009538837863609506800642251252051173929848960841284886" +
   396  	"26945604241965285022210661186306744278622039194945047123713786960956364371917287467764657573962413890865832645" +
   397  	"99581339047802759009946576407895126946839835259570982582262052248940772671947826848260147699090264013639443745" +
   398  	"53050682034962524517493996514314298091906592509372216964615157098583874105978859597729754989301617539284681382" +
   399  	"68683868942774155991855925245953959431049972524680845987273644695848653836736222626099124608051243884390451244" +
   400  	"13654976278079771569143599770012961608944169486855584840635342207222582848864815845602850601684273945226746767" +
   401  	"88952521385225499546667278239864565961163548862305774564980355936345681743241125150760694794510965960940252288" +
   402  	"79710893145669136867228748940560101503308617928680920874760917824938589009714909675985261365549781893129784821" +
   403  	"68299894872265880485756401427047755513237964145152374623436454285844479526586782105114135473573952311342716610" +
   404  	"21359695362314429524849371871101457654035902799344037420073105785390621983874478084784896833214457138687519435" +
   405  	"06430218453191048481005370614680674919278191197939952061419663428754440643745123718192179998391015919561814675" +
   406  	"14269123974894090718649423196156794520809514655022523160388193014209376213785595663893778708303906979207734672" +
   407  	"21825625996615014215030680384477345492026054146659252014974428507325186660021324340881907104863317346496514539" +
   408  	"05796268561005508106658796998163574736384052571459102897064140110971206280439039759515677157700420337869936007" +
   409  	"23055876317635942187312514712053292819182618612586732157919841484882916447060957527069572209175671167229109816" +
   410  	"90915280173506712748583222871835209353965725121083579151369882091444210067510334671103141267111369908658516398" +
   411  	"31501970165151168517143765761835155650884909989859982387345528331635507647918535893226185489632132933089857064" +
   412  	"20467525907091548141654985946163718027098199430992448895757128289059232332609729971208443357326548938239119325" +
   413  	"97463667305836041428138830320382490375898524374417029132765618093773444030707469211201913020330380197621101100" +
   414  	"44929321516084244485963766983895228684783123552658213144957685726243344189303968642624341077322697802807318915" +
   415  	"44110104468232527162010526522721116603966655730925471105578537634668206531098965269186205647693125705863566201" +
   416  	"85581007293606598764861179104533488503461136576867532494416680396265797877185560845529654126654085306143444318" +
   417  	"58676975145661406800700237877659134401712749470420562230538994561314071127000407854733269939081454664645880797" +
   418  	"27082668306343285878569830523580893306575740679545716377525420211495576158140025012622859413021647155097925923" +
   419  	"09907965473761255176567513575178296664547791745011299614890304639947132962107340437518957359614589019389713111" +
   420  	"79042978285647503203198691514028708085990480109412147221317947647772622414254854540332157185306142288137585043" +
   421  	"06332175182979866223717215916077166925474873898665494945011465406284336639379003976926567214638530673609657120" +
   422  	"91807638327166416274888800786925602902284721040317211860820419000422966171196377921337575114959501566049631862" +
   423  	"94726547364252308177036751590673502350728354056704038674351362222477158915049530984448933309634087807693259939" +
   424  	"78054193414473774418426312986080998886874132604721569516239658645730216315981931951673538129741677294786724229" +
   425  	"24654366800980676928238280689964004824354037014163149658979409243237896907069779422362508221688957383798623001" +
   426  	"59377647165122893578601588161755782973523344604281512627203734314653197777416031990665541876397929334419521541" +
   427  	"34189948544473456738316249934191318148092777710386387734317720754565453220777092120190516609628049092636019759" +
   428  	"88281613323166636528619326686336062735676303544776280350450777235547105859548702790814356240145171806246436267" +
   429  	"94561275318134078330336254232783944975382437205835311477119926063813346776879695970309833913077109870408591337"
   430  
   431  // Test case for BenchmarkScanPi.
   432  func TestScanPi(t *testing.T) {
   433  	var x nat
   434  	z, _, _, err := x.scan(strings.NewReader(pi), 10)
   435  	if err != nil {
   436  		t.Errorf("scanning pi: %s", err)
   437  	}
   438  	if s := z.decimalString(); s != pi {
   439  		t.Errorf("scanning pi: got %s", s)
   440  	}
   441  }
   442  
   443  func TestScanPiParallel(t *testing.T) {
   444  	const n = 2
   445  	c := make(chan int)
   446  	for i := 0; i < n; i++ {
   447  		go func() {
   448  			TestScanPi(t)
   449  			c <- 0
   450  		}()
   451  	}
   452  	for i := 0; i < n; i++ {
   453  		<-c
   454  	}
   455  }
   456  
   457  func BenchmarkScanPi(b *testing.B) {
   458  	for i := 0; i < b.N; i++ {
   459  		var x nat
   460  		x.scan(strings.NewReader(pi), 10)
   461  	}
   462  }
   463  
   464  func BenchmarkStringPiParallel(b *testing.B) {
   465  	var x nat
   466  	x, _, _, _ = x.scan(strings.NewReader(pi), 0)
   467  	if x.decimalString() != pi {
   468  		panic("benchmark incorrect: conversion failed")
   469  	}
   470  	b.RunParallel(func(pb *testing.PB) {
   471  		for pb.Next() {
   472  			x.decimalString()
   473  		}
   474  	})
   475  }
   476  
   477  func BenchmarkScan10Base2(b *testing.B)     { ScanHelper(b, 2, 10, 10) }
   478  func BenchmarkScan100Base2(b *testing.B)    { ScanHelper(b, 2, 10, 100) }
   479  func BenchmarkScan1000Base2(b *testing.B)   { ScanHelper(b, 2, 10, 1000) }
   480  func BenchmarkScan10000Base2(b *testing.B)  { ScanHelper(b, 2, 10, 10000) }
   481  func BenchmarkScan100000Base2(b *testing.B) { ScanHelper(b, 2, 10, 100000) }
   482  
   483  func BenchmarkScan10Base8(b *testing.B)     { ScanHelper(b, 8, 10, 10) }
   484  func BenchmarkScan100Base8(b *testing.B)    { ScanHelper(b, 8, 10, 100) }
   485  func BenchmarkScan1000Base8(b *testing.B)   { ScanHelper(b, 8, 10, 1000) }
   486  func BenchmarkScan10000Base8(b *testing.B)  { ScanHelper(b, 8, 10, 10000) }
   487  func BenchmarkScan100000Base8(b *testing.B) { ScanHelper(b, 8, 10, 100000) }
   488  
   489  func BenchmarkScan10Base10(b *testing.B)     { ScanHelper(b, 10, 10, 10) }
   490  func BenchmarkScan100Base10(b *testing.B)    { ScanHelper(b, 10, 10, 100) }
   491  func BenchmarkScan1000Base10(b *testing.B)   { ScanHelper(b, 10, 10, 1000) }
   492  func BenchmarkScan10000Base10(b *testing.B)  { ScanHelper(b, 10, 10, 10000) }
   493  func BenchmarkScan100000Base10(b *testing.B) { ScanHelper(b, 10, 10, 100000) }
   494  
   495  func BenchmarkScan10Base16(b *testing.B)     { ScanHelper(b, 16, 10, 10) }
   496  func BenchmarkScan100Base16(b *testing.B)    { ScanHelper(b, 16, 10, 100) }
   497  func BenchmarkScan1000Base16(b *testing.B)   { ScanHelper(b, 16, 10, 1000) }
   498  func BenchmarkScan10000Base16(b *testing.B)  { ScanHelper(b, 16, 10, 10000) }
   499  func BenchmarkScan100000Base16(b *testing.B) { ScanHelper(b, 16, 10, 100000) }
   500  
   501  func ScanHelper(b *testing.B, base int, x, y Word) {
   502  	b.StopTimer()
   503  	var z nat
   504  	z = z.expWW(x, y)
   505  
   506  	var s string
   507  	s = z.string(lowercaseDigits[:base])
   508  	if t := toString(z, lowercaseDigits[:base]); t != s {
   509  		b.Fatalf("scanning: got %s; want %s", s, t)
   510  	}
   511  	b.StartTimer()
   512  
   513  	for i := 0; i < b.N; i++ {
   514  		z.scan(strings.NewReader(s), base)
   515  	}
   516  }
   517  
   518  func BenchmarkString10Base2(b *testing.B)     { StringHelper(b, 2, 10, 10) }
   519  func BenchmarkString100Base2(b *testing.B)    { StringHelper(b, 2, 10, 100) }
   520  func BenchmarkString1000Base2(b *testing.B)   { StringHelper(b, 2, 10, 1000) }
   521  func BenchmarkString10000Base2(b *testing.B)  { StringHelper(b, 2, 10, 10000) }
   522  func BenchmarkString100000Base2(b *testing.B) { StringHelper(b, 2, 10, 100000) }
   523  
   524  func BenchmarkString10Base8(b *testing.B)     { StringHelper(b, 8, 10, 10) }
   525  func BenchmarkString100Base8(b *testing.B)    { StringHelper(b, 8, 10, 100) }
   526  func BenchmarkString1000Base8(b *testing.B)   { StringHelper(b, 8, 10, 1000) }
   527  func BenchmarkString10000Base8(b *testing.B)  { StringHelper(b, 8, 10, 10000) }
   528  func BenchmarkString100000Base8(b *testing.B) { StringHelper(b, 8, 10, 100000) }
   529  
   530  func BenchmarkString10Base10(b *testing.B)     { StringHelper(b, 10, 10, 10) }
   531  func BenchmarkString100Base10(b *testing.B)    { StringHelper(b, 10, 10, 100) }
   532  func BenchmarkString1000Base10(b *testing.B)   { StringHelper(b, 10, 10, 1000) }
   533  func BenchmarkString10000Base10(b *testing.B)  { StringHelper(b, 10, 10, 10000) }
   534  func BenchmarkString100000Base10(b *testing.B) { StringHelper(b, 10, 10, 100000) }
   535  
   536  func BenchmarkString10Base16(b *testing.B)     { StringHelper(b, 16, 10, 10) }
   537  func BenchmarkString100Base16(b *testing.B)    { StringHelper(b, 16, 10, 100) }
   538  func BenchmarkString1000Base16(b *testing.B)   { StringHelper(b, 16, 10, 1000) }
   539  func BenchmarkString10000Base16(b *testing.B)  { StringHelper(b, 16, 10, 10000) }
   540  func BenchmarkString100000Base16(b *testing.B) { StringHelper(b, 16, 10, 100000) }
   541  
   542  func StringHelper(b *testing.B, base int, x, y Word) {
   543  	b.StopTimer()
   544  	var z nat
   545  	z = z.expWW(x, y)
   546  	z.string(lowercaseDigits[:base]) // warm divisor cache
   547  	b.StartTimer()
   548  
   549  	for i := 0; i < b.N; i++ {
   550  		_ = z.string(lowercaseDigits[:base])
   551  	}
   552  }
   553  
   554  func BenchmarkLeafSize0(b *testing.B)  { LeafSizeHelper(b, 10, 0) } // test without splitting
   555  func BenchmarkLeafSize1(b *testing.B)  { LeafSizeHelper(b, 10, 1) }
   556  func BenchmarkLeafSize2(b *testing.B)  { LeafSizeHelper(b, 10, 2) }
   557  func BenchmarkLeafSize3(b *testing.B)  { LeafSizeHelper(b, 10, 3) }
   558  func BenchmarkLeafSize4(b *testing.B)  { LeafSizeHelper(b, 10, 4) }
   559  func BenchmarkLeafSize5(b *testing.B)  { LeafSizeHelper(b, 10, 5) }
   560  func BenchmarkLeafSize6(b *testing.B)  { LeafSizeHelper(b, 10, 6) }
   561  func BenchmarkLeafSize7(b *testing.B)  { LeafSizeHelper(b, 10, 7) }
   562  func BenchmarkLeafSize8(b *testing.B)  { LeafSizeHelper(b, 10, 8) }
   563  func BenchmarkLeafSize9(b *testing.B)  { LeafSizeHelper(b, 10, 9) }
   564  func BenchmarkLeafSize10(b *testing.B) { LeafSizeHelper(b, 10, 10) }
   565  func BenchmarkLeafSize11(b *testing.B) { LeafSizeHelper(b, 10, 11) }
   566  func BenchmarkLeafSize12(b *testing.B) { LeafSizeHelper(b, 10, 12) }
   567  func BenchmarkLeafSize13(b *testing.B) { LeafSizeHelper(b, 10, 13) }
   568  func BenchmarkLeafSize14(b *testing.B) { LeafSizeHelper(b, 10, 14) }
   569  func BenchmarkLeafSize15(b *testing.B) { LeafSizeHelper(b, 10, 15) }
   570  func BenchmarkLeafSize16(b *testing.B) { LeafSizeHelper(b, 10, 16) }
   571  func BenchmarkLeafSize32(b *testing.B) { LeafSizeHelper(b, 10, 32) } // try some large lengths
   572  func BenchmarkLeafSize64(b *testing.B) { LeafSizeHelper(b, 10, 64) }
   573  
   574  func LeafSizeHelper(b *testing.B, base Word, size int) {
   575  	b.StopTimer()
   576  	originalLeafSize := leafSize
   577  	resetTable(cacheBase10.table[:])
   578  	leafSize = size
   579  	b.StartTimer()
   580  
   581  	for d := 1; d <= 10000; d *= 10 {
   582  		b.StopTimer()
   583  		var z nat
   584  		z = z.expWW(base, Word(d))           // build target number
   585  		_ = z.string(lowercaseDigits[:base]) // warm divisor cache
   586  		b.StartTimer()
   587  
   588  		for i := 0; i < b.N; i++ {
   589  			_ = z.string(lowercaseDigits[:base])
   590  		}
   591  	}
   592  
   593  	b.StopTimer()
   594  	resetTable(cacheBase10.table[:])
   595  	leafSize = originalLeafSize
   596  	b.StartTimer()
   597  }
   598  
   599  func resetTable(table []divisor) {
   600  	if table != nil && table[0].bbb != nil {
   601  		for i := 0; i < len(table); i++ {
   602  			table[i].bbb = nil
   603  			table[i].nbits = 0
   604  			table[i].ndigits = 0
   605  		}
   606  	}
   607  }
   608  
   609  func TestStringPowers(t *testing.T) {
   610  	var b, p Word
   611  	for b = 2; b <= 16; b++ {
   612  		for p = 0; p <= 512; p++ {
   613  			x := nat(nil).expWW(b, p)
   614  			xs := x.string(lowercaseDigits[:b])
   615  			xs2 := toString(x, lowercaseDigits[:b])
   616  			if xs != xs2 {
   617  				t.Errorf("failed at %d ** %d in base %d: %s != %s", b, p, b, xs, xs2)
   618  			}
   619  		}
   620  		if b >= 3 && testing.Short() {
   621  			break
   622  		}
   623  	}
   624  }
   625  
   626  func TestLeadingZeros(t *testing.T) {
   627  	var x Word = _B >> 1
   628  	for i := 0; i <= _W; i++ {
   629  		if int(leadingZeros(x)) != i {
   630  			t.Errorf("failed at %x: got %d want %d", x, leadingZeros(x), i)
   631  		}
   632  		x >>= 1
   633  	}
   634  }
   635  
   636  type shiftTest struct {
   637  	in    nat
   638  	shift uint
   639  	out   nat
   640  }
   641  
   642  var leftShiftTests = []shiftTest{
   643  	{nil, 0, nil},
   644  	{nil, 1, nil},
   645  	{natOne, 0, natOne},
   646  	{natOne, 1, natTwo},
   647  	{nat{1 << (_W - 1)}, 1, nat{0}},
   648  	{nat{1 << (_W - 1), 0}, 1, nat{0, 1}},
   649  }
   650  
   651  func TestShiftLeft(t *testing.T) {
   652  	for i, test := range leftShiftTests {
   653  		var z nat
   654  		z = z.shl(test.in, test.shift)
   655  		for j, d := range test.out {
   656  			if j >= len(z) || z[j] != d {
   657  				t.Errorf("#%d: got: %v want: %v", i, z, test.out)
   658  				break
   659  			}
   660  		}
   661  	}
   662  }
   663  
   664  var rightShiftTests = []shiftTest{
   665  	{nil, 0, nil},
   666  	{nil, 1, nil},
   667  	{natOne, 0, natOne},
   668  	{natOne, 1, nil},
   669  	{natTwo, 1, natOne},
   670  	{nat{0, 1}, 1, nat{1 << (_W - 1)}},
   671  	{nat{2, 1, 1}, 1, nat{1<<(_W-1) + 1, 1 << (_W - 1)}},
   672  }
   673  
   674  func TestShiftRight(t *testing.T) {
   675  	for i, test := range rightShiftTests {
   676  		var z nat
   677  		z = z.shr(test.in, test.shift)
   678  		for j, d := range test.out {
   679  			if j >= len(z) || z[j] != d {
   680  				t.Errorf("#%d: got: %v want: %v", i, z, test.out)
   681  				break
   682  			}
   683  		}
   684  	}
   685  }
   686  
   687  type modWTest struct {
   688  	in       string
   689  	dividend string
   690  	out      string
   691  }
   692  
   693  var modWTests32 = []modWTest{
   694  	{"23492635982634928349238759823742", "252341", "220170"},
   695  }
   696  
   697  var modWTests64 = []modWTest{
   698  	{"6527895462947293856291561095690465243862946", "524326975699234", "375066989628668"},
   699  }
   700  
   701  func runModWTests(t *testing.T, tests []modWTest) {
   702  	for i, test := range tests {
   703  		in, _ := new(Int).SetString(test.in, 10)
   704  		d, _ := new(Int).SetString(test.dividend, 10)
   705  		out, _ := new(Int).SetString(test.out, 10)
   706  
   707  		r := in.abs.modW(d.abs[0])
   708  		if r != out.abs[0] {
   709  			t.Errorf("#%d failed: got %d want %s", i, r, out)
   710  		}
   711  	}
   712  }
   713  
   714  func TestModW(t *testing.T) {
   715  	if _W >= 32 {
   716  		runModWTests(t, modWTests32)
   717  	}
   718  	if _W >= 64 {
   719  		runModWTests(t, modWTests64)
   720  	}
   721  }
   722  
   723  func TestTrailingZeroBits(t *testing.T) {
   724  	// test 0 case explicitly
   725  	if n := trailingZeroBits(0); n != 0 {
   726  		t.Errorf("got trailingZeroBits(0) = %d; want 0", n)
   727  	}
   728  
   729  	x := Word(1)
   730  	for i := uint(0); i < _W; i++ {
   731  		n := trailingZeroBits(x)
   732  		if n != i {
   733  			t.Errorf("got trailingZeroBits(%#x) = %d; want %d", x, n, i%_W)
   734  		}
   735  		x <<= 1
   736  	}
   737  
   738  	// test 0 case explicitly
   739  	if n := nat(nil).trailingZeroBits(); n != 0 {
   740  		t.Errorf("got nat(nil).trailingZeroBits() = %d; want 0", n)
   741  	}
   742  
   743  	y := nat(nil).set(natOne)
   744  	for i := uint(0); i <= 3*_W; i++ {
   745  		n := y.trailingZeroBits()
   746  		if n != i {
   747  			t.Errorf("got 0x%s.trailingZeroBits() = %d; want %d", y.hexString(), n, i)
   748  		}
   749  		y = y.shl(y, 1)
   750  	}
   751  }
   752  
   753  var expNNTests = []struct {
   754  	x, y, m string
   755  	out     string
   756  }{
   757  	{"0", "0", "0", "1"},
   758  	{"0", "0", "1", "0"},
   759  	{"1", "1", "1", "0"},
   760  	{"2", "1", "1", "0"},
   761  	{"2", "2", "1", "0"},
   762  	{"10", "100000000000", "1", "0"},
   763  	{"0x8000000000000000", "2", "", "0x40000000000000000000000000000000"},
   764  	{"0x8000000000000000", "2", "6719", "4944"},
   765  	{"0x8000000000000000", "3", "6719", "5447"},
   766  	{"0x8000000000000000", "1000", "6719", "1603"},
   767  	{"0x8000000000000000", "1000000", "6719", "3199"},
   768  	{
   769  		"2938462938472983472983659726349017249287491026512746239764525612965293865296239471239874193284792387498274256129746192347",
   770  		"298472983472983471903246121093472394872319615612417471234712061",
   771  		"29834729834729834729347290846729561262544958723956495615629569234729836259263598127342374289365912465901365498236492183464",
   772  		"23537740700184054162508175125554701713153216681790245129157191391322321508055833908509185839069455749219131480588829346291",
   773  	},
   774  }
   775  
   776  func TestExpNN(t *testing.T) {
   777  	for i, test := range expNNTests {
   778  		x := natFromString(test.x)
   779  		y := natFromString(test.y)
   780  		out := natFromString(test.out)
   781  
   782  		var m nat
   783  		if len(test.m) > 0 {
   784  			m = natFromString(test.m)
   785  		}
   786  
   787  		z := nat(nil).expNN(x, y, m)
   788  		if z.cmp(out) != 0 {
   789  			t.Errorf("#%d got %s want %s", i, z.decimalString(), out.decimalString())
   790  		}
   791  	}
   792  }
   793  
   794  func ExpHelper(b *testing.B, x, y Word) {
   795  	var z nat
   796  	for i := 0; i < b.N; i++ {
   797  		z.expWW(x, y)
   798  	}
   799  }
   800  
   801  func BenchmarkExp3Power0x10(b *testing.B)     { ExpHelper(b, 3, 0x10) }
   802  func BenchmarkExp3Power0x40(b *testing.B)     { ExpHelper(b, 3, 0x40) }
   803  func BenchmarkExp3Power0x100(b *testing.B)    { ExpHelper(b, 3, 0x100) }
   804  func BenchmarkExp3Power0x400(b *testing.B)    { ExpHelper(b, 3, 0x400) }
   805  func BenchmarkExp3Power0x1000(b *testing.B)   { ExpHelper(b, 3, 0x1000) }
   806  func BenchmarkExp3Power0x4000(b *testing.B)   { ExpHelper(b, 3, 0x4000) }
   807  func BenchmarkExp3Power0x10000(b *testing.B)  { ExpHelper(b, 3, 0x10000) }
   808  func BenchmarkExp3Power0x40000(b *testing.B)  { ExpHelper(b, 3, 0x40000) }
   809  func BenchmarkExp3Power0x100000(b *testing.B) { ExpHelper(b, 3, 0x100000) }
   810  func BenchmarkExp3Power0x400000(b *testing.B) { ExpHelper(b, 3, 0x400000) }
   811  
   812  func fibo(n int) nat {
   813  	switch n {
   814  	case 0:
   815  		return nil
   816  	case 1:
   817  		return nat{1}
   818  	}
   819  	f0 := fibo(0)
   820  	f1 := fibo(1)
   821  	var f2 nat
   822  	for i := 1; i < n; i++ {
   823  		f2 = f2.add(f0, f1)
   824  		f0, f1, f2 = f1, f2, f0
   825  	}
   826  	return f1
   827  }
   828  
   829  var fiboNums = []string{
   830  	"0",
   831  	"55",
   832  	"6765",
   833  	"832040",
   834  	"102334155",
   835  	"12586269025",
   836  	"1548008755920",
   837  	"190392490709135",
   838  	"23416728348467685",
   839  	"2880067194370816120",
   840  	"354224848179261915075",
   841  }
   842  
   843  func TestFibo(t *testing.T) {
   844  	for i, want := range fiboNums {
   845  		n := i * 10
   846  		got := fibo(n).decimalString()
   847  		if got != want {
   848  			t.Errorf("fibo(%d) failed: got %s want %s", n, got, want)
   849  		}
   850  	}
   851  }
   852  
   853  func BenchmarkFibo(b *testing.B) {
   854  	for i := 0; i < b.N; i++ {
   855  		fibo(1e0)
   856  		fibo(1e1)
   857  		fibo(1e2)
   858  		fibo(1e3)
   859  		fibo(1e4)
   860  		fibo(1e5)
   861  	}
   862  }
   863  
   864  var bitTests = []struct {
   865  	x    string
   866  	i    uint
   867  	want uint
   868  }{
   869  	{"0", 0, 0},
   870  	{"0", 1, 0},
   871  	{"0", 1000, 0},
   872  
   873  	{"0x1", 0, 1},
   874  	{"0x10", 0, 0},
   875  	{"0x10", 3, 0},
   876  	{"0x10", 4, 1},
   877  	{"0x10", 5, 0},
   878  
   879  	{"0x8000000000000000", 62, 0},
   880  	{"0x8000000000000000", 63, 1},
   881  	{"0x8000000000000000", 64, 0},
   882  
   883  	{"0x3" + strings.Repeat("0", 32), 127, 0},
   884  	{"0x3" + strings.Repeat("0", 32), 128, 1},
   885  	{"0x3" + strings.Repeat("0", 32), 129, 1},
   886  	{"0x3" + strings.Repeat("0", 32), 130, 0},
   887  }
   888  
   889  func TestBit(t *testing.T) {
   890  	for i, test := range bitTests {
   891  		x := natFromString(test.x)
   892  		if got := x.bit(test.i); got != test.want {
   893  			t.Errorf("#%d: %s.bit(%d) = %v; want %v", i, test.x, test.i, got, test.want)
   894  		}
   895  	}
   896  }