Home | History | Annotate | Download | only in big
      1 // Copyright 2015 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 	"strings"
     10 	"testing"
     11 )
     12 
     13 func toString(x nat, charset string) string {
     14 	base := len(charset)
     15 
     16 	// special cases
     17 	switch {
     18 	case base < 2:
     19 		panic("illegal base")
     20 	case len(x) == 0:
     21 		return string(charset[0])
     22 	}
     23 
     24 	// allocate buffer for conversion
     25 	i := x.bitLen()/log2(Word(base)) + 1 // +1: round up
     26 	s := make([]byte, i)
     27 
     28 	// don't destroy x
     29 	q := nat(nil).set(x)
     30 
     31 	// convert
     32 	for len(q) > 0 {
     33 		i--
     34 		var r Word
     35 		q, r = q.divW(q, Word(base))
     36 		s[i] = charset[r]
     37 	}
     38 
     39 	return string(s[i:])
     40 }
     41 
     42 var strTests = []struct {
     43 	x nat    // nat value to be converted
     44 	c string // conversion charset
     45 	s string // expected result
     46 }{
     47 	{nil, "01", "0"},
     48 	{nat{1}, "01", "1"},
     49 	{nat{0xc5}, "01", "11000101"},
     50 	{nat{03271}, lowercaseDigits[:8], "3271"},
     51 	{nat{10}, lowercaseDigits[:10], "10"},
     52 	{nat{1234567890}, uppercaseDigits[:10], "1234567890"},
     53 	{nat{0xdeadbeef}, lowercaseDigits[:16], "deadbeef"},
     54 	{nat{0xdeadbeef}, uppercaseDigits[:16], "DEADBEEF"},
     55 	{nat{0x229be7}, lowercaseDigits[:17], "1a2b3c"},
     56 	{nat{0x309663e6}, uppercaseDigits[:32], "O9COV6"},
     57 }
     58 
     59 func TestString(t *testing.T) {
     60 	// test invalid character set explicitly
     61 	var panicStr string
     62 	func() {
     63 		defer func() {
     64 			panicStr = recover().(string)
     65 		}()
     66 		natOne.string("0")
     67 	}()
     68 	if panicStr != "invalid character set length" {
     69 		t.Errorf("expected panic for invalid character set")
     70 	}
     71 
     72 	for _, a := range strTests {
     73 		s := a.x.string(a.c)
     74 		if s != a.s {
     75 			t.Errorf("string%+v\n\tgot s = %s; want %s", a, s, a.s)
     76 		}
     77 
     78 		x, b, _, err := nat(nil).scan(strings.NewReader(a.s), len(a.c), false)
     79 		if x.cmp(a.x) != 0 {
     80 			t.Errorf("scan%+v\n\tgot z = %v; want %v", a, x, a.x)
     81 		}
     82 		if b != len(a.c) {
     83 			t.Errorf("scan%+v\n\tgot b = %d; want %d", a, b, len(a.c))
     84 		}
     85 		if err != nil {
     86 			t.Errorf("scan%+v\n\tgot error = %s", a, err)
     87 		}
     88 	}
     89 }
     90 
     91 var natScanTests = []struct {
     92 	s     string // string to be scanned
     93 	base  int    // input base
     94 	frac  bool   // fraction ok
     95 	x     nat    // expected nat
     96 	b     int    // expected base
     97 	count int    // expected digit count
     98 	ok    bool   // expected success
     99 	next  rune   // next character (or 0, if at EOF)
    100 }{
    101 	// error: no mantissa
    102 	{},
    103 	{s: "?"},
    104 	{base: 10},
    105 	{base: 36},
    106 	{s: "?", base: 10},
    107 	{s: "0x"},
    108 	{s: "345", base: 2},
    109 
    110 	// error: incorrect use of decimal point
    111 	{s: ".0"},
    112 	{s: ".0", base: 10},
    113 	{s: ".", base: 0},
    114 	{s: "0x.0"},
    115 
    116 	// no errors
    117 	{"0", 0, false, nil, 10, 1, true, 0},
    118 	{"0", 10, false, nil, 10, 1, true, 0},
    119 	{"0", 36, false, nil, 36, 1, true, 0},
    120 	{"1", 0, false, nat{1}, 10, 1, true, 0},
    121 	{"1", 10, false, nat{1}, 10, 1, true, 0},
    122 	{"0 ", 0, false, nil, 10, 1, true, ' '},
    123 	{"08", 0, false, nil, 10, 1, true, '8'},
    124 	{"08", 10, false, nat{8}, 10, 2, true, 0},
    125 	{"018", 0, false, nat{1}, 8, 1, true, '8'},
    126 	{"0b1", 0, false, nat{1}, 2, 1, true, 0},
    127 	{"0b11000101", 0, false, nat{0xc5}, 2, 8, true, 0},
    128 	{"03271", 0, false, nat{03271}, 8, 4, true, 0},
    129 	{"10ab", 0, false, nat{10}, 10, 2, true, 'a'},
    130 	{"1234567890", 0, false, nat{1234567890}, 10, 10, true, 0},
    131 	{"xyz", 36, false, nat{(33*36+34)*36 + 35}, 36, 3, true, 0},
    132 	{"xyz?", 36, false, nat{(33*36+34)*36 + 35}, 36, 3, true, '?'},
    133 	{"0x", 16, false, nil, 16, 1, true, 'x'},
    134 	{"0xdeadbeef", 0, false, nat{0xdeadbeef}, 16, 8, true, 0},
    135 	{"0XDEADBEEF", 0, false, nat{0xdeadbeef}, 16, 8, true, 0},
    136 
    137 	// no errors, decimal point
    138 	{"0.", 0, false, nil, 10, 1, true, '.'},
    139 	{"0.", 10, true, nil, 10, 0, true, 0},
    140 	{"0.1.2", 10, true, nat{1}, 10, -1, true, '.'},
    141 	{".000", 10, true, nil, 10, -3, true, 0},
    142 	{"12.3", 10, true, nat{123}, 10, -1, true, 0},
    143 	{"012.345", 10, true, nat{12345}, 10, -3, true, 0},
    144 }
    145 
    146 func TestScanBase(t *testing.T) {
    147 	for _, a := range natScanTests {
    148 		r := strings.NewReader(a.s)
    149 		x, b, count, err := nat(nil).scan(r, a.base, a.frac)
    150 		if err == nil && !a.ok {
    151 			t.Errorf("scan%+v\n\texpected error", a)
    152 		}
    153 		if err != nil {
    154 			if a.ok {
    155 				t.Errorf("scan%+v\n\tgot error = %s", a, err)
    156 			}
    157 			continue
    158 		}
    159 		if x.cmp(a.x) != 0 {
    160 			t.Errorf("scan%+v\n\tgot z = %v; want %v", a, x, a.x)
    161 		}
    162 		if b != a.b {
    163 			t.Errorf("scan%+v\n\tgot b = %d; want %d", a, b, a.base)
    164 		}
    165 		if count != a.count {
    166 			t.Errorf("scan%+v\n\tgot count = %d; want %d", a, count, a.count)
    167 		}
    168 		next, _, err := r.ReadRune()
    169 		if err == io.EOF {
    170 			next = 0
    171 			err = nil
    172 		}
    173 		if err == nil && next != a.next {
    174 			t.Errorf("scan%+v\n\tgot next = %q; want %q", a, next, a.next)
    175 		}
    176 	}
    177 }
    178 
    179 var pi = "3" +
    180 	"14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651" +
    181 	"32823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461" +
    182 	"28475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920" +
    183 	"96282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179" +
    184 	"31051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798" +
    185 	"60943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901" +
    186 	"22495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999837" +
    187 	"29780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083" +
    188 	"81420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909" +
    189 	"21642019893809525720106548586327886593615338182796823030195203530185296899577362259941389124972177528347913151" +
    190 	"55748572424541506959508295331168617278558890750983817546374649393192550604009277016711390098488240128583616035" +
    191 	"63707660104710181942955596198946767837449448255379774726847104047534646208046684259069491293313677028989152104" +
    192 	"75216205696602405803815019351125338243003558764024749647326391419927260426992279678235478163600934172164121992" +
    193 	"45863150302861829745557067498385054945885869269956909272107975093029553211653449872027559602364806654991198818" +
    194 	"34797753566369807426542527862551818417574672890977772793800081647060016145249192173217214772350141441973568548" +
    195 	"16136115735255213347574184946843852332390739414333454776241686251898356948556209921922218427255025425688767179" +
    196 	"04946016534668049886272327917860857843838279679766814541009538837863609506800642251252051173929848960841284886" +
    197 	"26945604241965285022210661186306744278622039194945047123713786960956364371917287467764657573962413890865832645" +
    198 	"99581339047802759009946576407895126946839835259570982582262052248940772671947826848260147699090264013639443745" +
    199 	"53050682034962524517493996514314298091906592509372216964615157098583874105978859597729754989301617539284681382" +
    200 	"68683868942774155991855925245953959431049972524680845987273644695848653836736222626099124608051243884390451244" +
    201 	"13654976278079771569143599770012961608944169486855584840635342207222582848864815845602850601684273945226746767" +
    202 	"88952521385225499546667278239864565961163548862305774564980355936345681743241125150760694794510965960940252288" +
    203 	"79710893145669136867228748940560101503308617928680920874760917824938589009714909675985261365549781893129784821" +
    204 	"68299894872265880485756401427047755513237964145152374623436454285844479526586782105114135473573952311342716610" +
    205 	"21359695362314429524849371871101457654035902799344037420073105785390621983874478084784896833214457138687519435" +
    206 	"06430218453191048481005370614680674919278191197939952061419663428754440643745123718192179998391015919561814675" +
    207 	"14269123974894090718649423196156794520809514655022523160388193014209376213785595663893778708303906979207734672" +
    208 	"21825625996615014215030680384477345492026054146659252014974428507325186660021324340881907104863317346496514539" +
    209 	"05796268561005508106658796998163574736384052571459102897064140110971206280439039759515677157700420337869936007" +
    210 	"23055876317635942187312514712053292819182618612586732157919841484882916447060957527069572209175671167229109816" +
    211 	"90915280173506712748583222871835209353965725121083579151369882091444210067510334671103141267111369908658516398" +
    212 	"31501970165151168517143765761835155650884909989859982387345528331635507647918535893226185489632132933089857064" +
    213 	"20467525907091548141654985946163718027098199430992448895757128289059232332609729971208443357326548938239119325" +
    214 	"97463667305836041428138830320382490375898524374417029132765618093773444030707469211201913020330380197621101100" +
    215 	"44929321516084244485963766983895228684783123552658213144957685726243344189303968642624341077322697802807318915" +
    216 	"44110104468232527162010526522721116603966655730925471105578537634668206531098965269186205647693125705863566201" +
    217 	"85581007293606598764861179104533488503461136576867532494416680396265797877185560845529654126654085306143444318" +
    218 	"58676975145661406800700237877659134401712749470420562230538994561314071127000407854733269939081454664645880797" +
    219 	"27082668306343285878569830523580893306575740679545716377525420211495576158140025012622859413021647155097925923" +
    220 	"09907965473761255176567513575178296664547791745011299614890304639947132962107340437518957359614589019389713111" +
    221 	"79042978285647503203198691514028708085990480109412147221317947647772622414254854540332157185306142288137585043" +
    222 	"06332175182979866223717215916077166925474873898665494945011465406284336639379003976926567214638530673609657120" +
    223 	"91807638327166416274888800786925602902284721040317211860820419000422966171196377921337575114959501566049631862" +
    224 	"94726547364252308177036751590673502350728354056704038674351362222477158915049530984448933309634087807693259939" +
    225 	"78054193414473774418426312986080998886874132604721569516239658645730216315981931951673538129741677294786724229" +
    226 	"24654366800980676928238280689964004824354037014163149658979409243237896907069779422362508221688957383798623001" +
    227 	"59377647165122893578601588161755782973523344604281512627203734314653197777416031990665541876397929334419521541" +
    228 	"34189948544473456738316249934191318148092777710386387734317720754565453220777092120190516609628049092636019759" +
    229 	"88281613323166636528619326686336062735676303544776280350450777235547105859548702790814356240145171806246436267" +
    230 	"94561275318134078330336254232783944975382437205835311477119926063813346776879695970309833913077109870408591337"
    231 
    232 // Test case for BenchmarkScanPi.
    233 func TestScanPi(t *testing.T) {
    234 	var x nat
    235 	z, _, _, err := x.scan(strings.NewReader(pi), 10, false)
    236 	if err != nil {
    237 		t.Errorf("scanning pi: %s", err)
    238 	}
    239 	if s := z.decimalString(); s != pi {
    240 		t.Errorf("scanning pi: got %s", s)
    241 	}
    242 }
    243 
    244 func TestScanPiParallel(t *testing.T) {
    245 	const n = 2
    246 	c := make(chan int)
    247 	for i := 0; i < n; i++ {
    248 		go func() {
    249 			TestScanPi(t)
    250 			c <- 0
    251 		}()
    252 	}
    253 	for i := 0; i < n; i++ {
    254 		<-c
    255 	}
    256 }
    257 
    258 func BenchmarkScanPi(b *testing.B) {
    259 	for i := 0; i < b.N; i++ {
    260 		var x nat
    261 		x.scan(strings.NewReader(pi), 10, false)
    262 	}
    263 }
    264 
    265 func BenchmarkStringPiParallel(b *testing.B) {
    266 	var x nat
    267 	x, _, _, _ = x.scan(strings.NewReader(pi), 0, false)
    268 	if x.decimalString() != pi {
    269 		panic("benchmark incorrect: conversion failed")
    270 	}
    271 	b.RunParallel(func(pb *testing.PB) {
    272 		for pb.Next() {
    273 			x.decimalString()
    274 		}
    275 	})
    276 }
    277 
    278 func BenchmarkScan10Base2(b *testing.B)     { ScanHelper(b, 2, 10, 10) }
    279 func BenchmarkScan100Base2(b *testing.B)    { ScanHelper(b, 2, 10, 100) }
    280 func BenchmarkScan1000Base2(b *testing.B)   { ScanHelper(b, 2, 10, 1000) }
    281 func BenchmarkScan10000Base2(b *testing.B)  { ScanHelper(b, 2, 10, 10000) }
    282 func BenchmarkScan100000Base2(b *testing.B) { ScanHelper(b, 2, 10, 100000) }
    283 
    284 func BenchmarkScan10Base8(b *testing.B)     { ScanHelper(b, 8, 10, 10) }
    285 func BenchmarkScan100Base8(b *testing.B)    { ScanHelper(b, 8, 10, 100) }
    286 func BenchmarkScan1000Base8(b *testing.B)   { ScanHelper(b, 8, 10, 1000) }
    287 func BenchmarkScan10000Base8(b *testing.B)  { ScanHelper(b, 8, 10, 10000) }
    288 func BenchmarkScan100000Base8(b *testing.B) { ScanHelper(b, 8, 10, 100000) }
    289 
    290 func BenchmarkScan10Base10(b *testing.B)     { ScanHelper(b, 10, 10, 10) }
    291 func BenchmarkScan100Base10(b *testing.B)    { ScanHelper(b, 10, 10, 100) }
    292 func BenchmarkScan1000Base10(b *testing.B)   { ScanHelper(b, 10, 10, 1000) }
    293 func BenchmarkScan10000Base10(b *testing.B)  { ScanHelper(b, 10, 10, 10000) }
    294 func BenchmarkScan100000Base10(b *testing.B) { ScanHelper(b, 10, 10, 100000) }
    295 
    296 func BenchmarkScan10Base16(b *testing.B)     { ScanHelper(b, 16, 10, 10) }
    297 func BenchmarkScan100Base16(b *testing.B)    { ScanHelper(b, 16, 10, 100) }
    298 func BenchmarkScan1000Base16(b *testing.B)   { ScanHelper(b, 16, 10, 1000) }
    299 func BenchmarkScan10000Base16(b *testing.B)  { ScanHelper(b, 16, 10, 10000) }
    300 func BenchmarkScan100000Base16(b *testing.B) { ScanHelper(b, 16, 10, 100000) }
    301 
    302 func ScanHelper(b *testing.B, base int, x, y Word) {
    303 	b.StopTimer()
    304 	var z nat
    305 	z = z.expWW(x, y)
    306 
    307 	var s string
    308 	s = z.string(lowercaseDigits[:base])
    309 	if t := toString(z, lowercaseDigits[:base]); t != s {
    310 		b.Fatalf("scanning: got %s; want %s", s, t)
    311 	}
    312 	b.StartTimer()
    313 
    314 	for i := 0; i < b.N; i++ {
    315 		z.scan(strings.NewReader(s), base, false)
    316 	}
    317 }
    318 
    319 func BenchmarkString10Base2(b *testing.B)     { StringHelper(b, 2, 10, 10) }
    320 func BenchmarkString100Base2(b *testing.B)    { StringHelper(b, 2, 10, 100) }
    321 func BenchmarkString1000Base2(b *testing.B)   { StringHelper(b, 2, 10, 1000) }
    322 func BenchmarkString10000Base2(b *testing.B)  { StringHelper(b, 2, 10, 10000) }
    323 func BenchmarkString100000Base2(b *testing.B) { StringHelper(b, 2, 10, 100000) }
    324 
    325 func BenchmarkString10Base8(b *testing.B)     { StringHelper(b, 8, 10, 10) }
    326 func BenchmarkString100Base8(b *testing.B)    { StringHelper(b, 8, 10, 100) }
    327 func BenchmarkString1000Base8(b *testing.B)   { StringHelper(b, 8, 10, 1000) }
    328 func BenchmarkString10000Base8(b *testing.B)  { StringHelper(b, 8, 10, 10000) }
    329 func BenchmarkString100000Base8(b *testing.B) { StringHelper(b, 8, 10, 100000) }
    330 
    331 func BenchmarkString10Base10(b *testing.B)     { StringHelper(b, 10, 10, 10) }
    332 func BenchmarkString100Base10(b *testing.B)    { StringHelper(b, 10, 10, 100) }
    333 func BenchmarkString1000Base10(b *testing.B)   { StringHelper(b, 10, 10, 1000) }
    334 func BenchmarkString10000Base10(b *testing.B)  { StringHelper(b, 10, 10, 10000) }
    335 func BenchmarkString100000Base10(b *testing.B) { StringHelper(b, 10, 10, 100000) }
    336 
    337 func BenchmarkString10Base16(b *testing.B)     { StringHelper(b, 16, 10, 10) }
    338 func BenchmarkString100Base16(b *testing.B)    { StringHelper(b, 16, 10, 100) }
    339 func BenchmarkString1000Base16(b *testing.B)   { StringHelper(b, 16, 10, 1000) }
    340 func BenchmarkString10000Base16(b *testing.B)  { StringHelper(b, 16, 10, 10000) }
    341 func BenchmarkString100000Base16(b *testing.B) { StringHelper(b, 16, 10, 100000) }
    342 
    343 func StringHelper(b *testing.B, base int, x, y Word) {
    344 	b.StopTimer()
    345 	var z nat
    346 	z = z.expWW(x, y)
    347 	z.string(lowercaseDigits[:base]) // warm divisor cache
    348 	b.StartTimer()
    349 
    350 	for i := 0; i < b.N; i++ {
    351 		_ = z.string(lowercaseDigits[:base])
    352 	}
    353 }
    354 
    355 func BenchmarkLeafSize0(b *testing.B)  { LeafSizeHelper(b, 10, 0) } // test without splitting
    356 func BenchmarkLeafSize1(b *testing.B)  { LeafSizeHelper(b, 10, 1) }
    357 func BenchmarkLeafSize2(b *testing.B)  { LeafSizeHelper(b, 10, 2) }
    358 func BenchmarkLeafSize3(b *testing.B)  { LeafSizeHelper(b, 10, 3) }
    359 func BenchmarkLeafSize4(b *testing.B)  { LeafSizeHelper(b, 10, 4) }
    360 func BenchmarkLeafSize5(b *testing.B)  { LeafSizeHelper(b, 10, 5) }
    361 func BenchmarkLeafSize6(b *testing.B)  { LeafSizeHelper(b, 10, 6) }
    362 func BenchmarkLeafSize7(b *testing.B)  { LeafSizeHelper(b, 10, 7) }
    363 func BenchmarkLeafSize8(b *testing.B)  { LeafSizeHelper(b, 10, 8) }
    364 func BenchmarkLeafSize9(b *testing.B)  { LeafSizeHelper(b, 10, 9) }
    365 func BenchmarkLeafSize10(b *testing.B) { LeafSizeHelper(b, 10, 10) }
    366 func BenchmarkLeafSize11(b *testing.B) { LeafSizeHelper(b, 10, 11) }
    367 func BenchmarkLeafSize12(b *testing.B) { LeafSizeHelper(b, 10, 12) }
    368 func BenchmarkLeafSize13(b *testing.B) { LeafSizeHelper(b, 10, 13) }
    369 func BenchmarkLeafSize14(b *testing.B) { LeafSizeHelper(b, 10, 14) }
    370 func BenchmarkLeafSize15(b *testing.B) { LeafSizeHelper(b, 10, 15) }
    371 func BenchmarkLeafSize16(b *testing.B) { LeafSizeHelper(b, 10, 16) }
    372 func BenchmarkLeafSize32(b *testing.B) { LeafSizeHelper(b, 10, 32) } // try some large lengths
    373 func BenchmarkLeafSize64(b *testing.B) { LeafSizeHelper(b, 10, 64) }
    374 
    375 func LeafSizeHelper(b *testing.B, base Word, size int) {
    376 	b.StopTimer()
    377 	originalLeafSize := leafSize
    378 	resetTable(cacheBase10.table[:])
    379 	leafSize = size
    380 	b.StartTimer()
    381 
    382 	for d := 1; d <= 10000; d *= 10 {
    383 		b.StopTimer()
    384 		var z nat
    385 		z = z.expWW(base, Word(d))           // build target number
    386 		_ = z.string(lowercaseDigits[:base]) // warm divisor cache
    387 		b.StartTimer()
    388 
    389 		for i := 0; i < b.N; i++ {
    390 			_ = z.string(lowercaseDigits[:base])
    391 		}
    392 	}
    393 
    394 	b.StopTimer()
    395 	resetTable(cacheBase10.table[:])
    396 	leafSize = originalLeafSize
    397 	b.StartTimer()
    398 }
    399 
    400 func resetTable(table []divisor) {
    401 	if table != nil && table[0].bbb != nil {
    402 		for i := 0; i < len(table); i++ {
    403 			table[i].bbb = nil
    404 			table[i].nbits = 0
    405 			table[i].ndigits = 0
    406 		}
    407 	}
    408 }
    409 
    410 func TestStringPowers(t *testing.T) {
    411 	var b, p Word
    412 	for b = 2; b <= 16; b++ {
    413 		for p = 0; p <= 512; p++ {
    414 			x := nat(nil).expWW(b, p)
    415 			xs := x.string(lowercaseDigits[:b])
    416 			xs2 := toString(x, lowercaseDigits[:b])
    417 			if xs != xs2 {
    418 				t.Errorf("failed at %d ** %d in base %d: %s != %s", b, p, b, xs, xs2)
    419 			}
    420 		}
    421 		if b >= 3 && testing.Short() {
    422 			break
    423 		}
    424 	}
    425 }
    426