OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [math/] [big/] [nat_test.go] - Blame information for rev 833

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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
        "strings"
10
        "testing"
11
)
12
 
13
var cmpTests = []struct {
14
        x, y nat
15
        r    int
16
}{
17
        {nil, nil, 0},
18
        {nil, nat(nil), 0},
19
        {nat(nil), nil, 0},
20
        {nat(nil), nat(nil), 0},
21
        {nat{0}, nat{0}, 0},
22
        {nat{0}, nat{1}, -1},
23
        {nat{1}, nat{0}, 1},
24
        {nat{1}, nat{1}, 0},
25
        {nat{0, _M}, nat{1}, 1},
26
        {nat{1}, nat{0, _M}, -1},
27
        {nat{1, _M}, nat{0, _M}, 1},
28
        {nat{0, _M}, nat{1, _M}, -1},
29
        {nat{16, 571956, 8794, 68}, nat{837, 9146, 1, 754489}, -1},
30
        {nat{34986, 41, 105, 1957}, nat{56, 7458, 104, 1957}, 1},
31
}
32
 
33
func TestCmp(t *testing.T) {
34
        for i, a := range cmpTests {
35
                r := a.x.cmp(a.y)
36
                if r != a.r {
37
                        t.Errorf("#%d got r = %v; want %v", i, r, a.r)
38
                }
39
        }
40
}
41
 
42
type funNN func(z, x, y nat) nat
43
type argNN struct {
44
        z, x, y nat
45
}
46
 
47
var sumNN = []argNN{
48
        {},
49
        {nat{1}, nil, nat{1}},
50
        {nat{1111111110}, nat{123456789}, nat{987654321}},
51
        {nat{0, 0, 0, 1}, nil, nat{0, 0, 0, 1}},
52
        {nat{0, 0, 0, 1111111110}, nat{0, 0, 0, 123456789}, nat{0, 0, 0, 987654321}},
53
        {nat{0, 0, 0, 1}, nat{0, 0, _M}, nat{0, 0, 1}},
54
}
55
 
56
var prodNN = []argNN{
57
        {},
58
        {nil, nil, nil},
59
        {nil, nat{991}, nil},
60
        {nat{991}, nat{991}, nat{1}},
61
        {nat{991 * 991}, nat{991}, nat{991}},
62
        {nat{0, 0, 991 * 991}, nat{0, 991}, nat{0, 991}},
63
        {nat{1 * 991, 2 * 991, 3 * 991, 4 * 991}, nat{1, 2, 3, 4}, nat{991}},
64
        {nat{4, 11, 20, 30, 20, 11, 4}, nat{1, 2, 3, 4}, nat{4, 3, 2, 1}},
65
}
66
 
67
func TestSet(t *testing.T) {
68
        for _, a := range sumNN {
69
                z := nat(nil).set(a.z)
70
                if z.cmp(a.z) != 0 {
71
                        t.Errorf("got z = %v; want %v", z, a.z)
72
                }
73
        }
74
}
75
 
76
func testFunNN(t *testing.T, msg string, f funNN, a argNN) {
77
        z := f(nil, a.x, a.y)
78
        if z.cmp(a.z) != 0 {
79
                t.Errorf("%s%+v\n\tgot z = %v; want %v", msg, a, z, a.z)
80
        }
81
}
82
 
83
func TestFunNN(t *testing.T) {
84
        for _, a := range sumNN {
85
                arg := a
86
                testFunNN(t, "add", nat.add, arg)
87
 
88
                arg = argNN{a.z, a.y, a.x}
89
                testFunNN(t, "add symmetric", nat.add, arg)
90
 
91
                arg = argNN{a.x, a.z, a.y}
92
                testFunNN(t, "sub", nat.sub, arg)
93
 
94
                arg = argNN{a.y, a.z, a.x}
95
                testFunNN(t, "sub symmetric", nat.sub, arg)
96
        }
97
 
98
        for _, a := range prodNN {
99
                arg := a
100
                testFunNN(t, "mul", nat.mul, arg)
101
 
102
                arg = argNN{a.z, a.y, a.x}
103
                testFunNN(t, "mul symmetric", nat.mul, arg)
104
        }
105
}
106
 
107
var mulRangesN = []struct {
108
        a, b uint64
109
        prod string
110
}{
111
        {0, 0, "0"},
112
        {1, 1, "1"},
113
        {1, 2, "2"},
114
        {1, 3, "6"},
115
        {10, 10, "10"},
116
        {0, 100, "0"},
117
        {0, 1e9, "0"},
118
        {1, 0, "1"},                    // empty range
119
        {100, 1, "1"},                  // empty range
120
        {1, 10, "3628800"},             // 10!
121
        {1, 20, "2432902008176640000"}, // 20!
122
        {1, 100,
123
                "933262154439441526816992388562667004907159682643816214685929" +
124
                        "638952175999932299156089414639761565182862536979208272237582" +
125
                        "51185210916864000000000000000000000000", // 100!
126
        },
127
}
128
 
129
func TestMulRangeN(t *testing.T) {
130
        for i, r := range mulRangesN {
131
                prod := nat(nil).mulRange(r.a, r.b).decimalString()
132
                if prod != r.prod {
133
                        t.Errorf("#%d: got %s; want %s", i, prod, r.prod)
134
                }
135
        }
136
}
137
 
138
var mulArg, mulTmp nat
139
 
140
func init() {
141
        const n = 1000
142
        mulArg = make(nat, n)
143
        for i := 0; i < n; i++ {
144
                mulArg[i] = _M
145
        }
146
}
147
 
148
func benchmarkMulLoad() {
149
        for j := 1; j <= 10; j++ {
150
                x := mulArg[0 : j*100]
151
                mulTmp.mul(x, x)
152
        }
153
}
154
 
155
func BenchmarkMul(b *testing.B) {
156
        for i := 0; i < b.N; i++ {
157
                benchmarkMulLoad()
158
        }
159
}
160
 
161
func toString(x nat, charset string) string {
162
        base := len(charset)
163
 
164
        // special cases
165
        switch {
166
        case base < 2:
167
                panic("illegal base")
168
        case len(x) == 0:
169
                return string(charset[0])
170
        }
171
 
172
        // allocate buffer for conversion
173
        i := x.bitLen()/log2(Word(base)) + 1 // +1: round up
174
        s := make([]byte, i)
175
 
176
        // don't destroy x
177
        q := nat(nil).set(x)
178
 
179
        // convert
180
        for len(q) > 0 {
181
                i--
182
                var r Word
183
                q, r = q.divW(q, Word(base))
184
                s[i] = charset[r]
185
        }
186
 
187
        return string(s[i:])
188
}
189
 
190
var strTests = []struct {
191
        x nat    // nat value to be converted
192
        c string // conversion charset
193
        s string // expected result
194
}{
195
        {nil, "01", "0"},
196
        {nat{1}, "01", "1"},
197
        {nat{0xc5}, "01", "11000101"},
198
        {nat{03271}, lowercaseDigits[0:8], "3271"},
199
        {nat{10}, lowercaseDigits[0:10], "10"},
200
        {nat{1234567890}, uppercaseDigits[0:10], "1234567890"},
201
        {nat{0xdeadbeef}, lowercaseDigits[0:16], "deadbeef"},
202
        {nat{0xdeadbeef}, uppercaseDigits[0:16], "DEADBEEF"},
203
        {nat{0x229be7}, lowercaseDigits[0:17], "1a2b3c"},
204
        {nat{0x309663e6}, uppercaseDigits[0:32], "O9COV6"},
205
}
206
 
207
func TestString(t *testing.T) {
208
        for _, a := range strTests {
209
                s := a.x.string(a.c)
210
                if s != a.s {
211
                        t.Errorf("string%+v\n\tgot s = %s; want %s", a, s, a.s)
212
                }
213
 
214
                x, b, err := nat(nil).scan(strings.NewReader(a.s), len(a.c))
215
                if x.cmp(a.x) != 0 {
216
                        t.Errorf("scan%+v\n\tgot z = %v; want %v", a, x, a.x)
217
                }
218
                if b != len(a.c) {
219
                        t.Errorf("scan%+v\n\tgot b = %d; want %d", a, b, len(a.c))
220
                }
221
                if err != nil {
222
                        t.Errorf("scan%+v\n\tgot error = %s", a, err)
223
                }
224
        }
225
}
226
 
227
var natScanTests = []struct {
228
        s    string // string to be scanned
229
        base int    // input base
230
        x    nat    // expected nat
231
        b    int    // expected base
232
        ok   bool   // expected success
233
        next rune   // next character (or 0, if at EOF)
234
}{
235
        // error: illegal base
236
        {base: -1},
237
        {base: 1},
238
        {base: 37},
239
 
240
        // error: no mantissa
241
        {},
242
        {s: "?"},
243
        {base: 10},
244
        {base: 36},
245
        {s: "?", base: 10},
246
        {s: "0x"},
247
        {s: "345", base: 2},
248
 
249
        // no errors
250
        {"0", 0, nil, 10, true, 0},
251
        {"0", 10, nil, 10, true, 0},
252
        {"0", 36, nil, 36, true, 0},
253
        {"1", 0, nat{1}, 10, true, 0},
254
        {"1", 10, nat{1}, 10, true, 0},
255
        {"0 ", 0, nil, 10, true, ' '},
256
        {"08", 0, nil, 10, true, '8'},
257
        {"018", 0, nat{1}, 8, true, '8'},
258
        {"0b1", 0, nat{1}, 2, true, 0},
259
        {"0b11000101", 0, nat{0xc5}, 2, true, 0},
260
        {"03271", 0, nat{03271}, 8, true, 0},
261
        {"10ab", 0, nat{10}, 10, true, 'a'},
262
        {"1234567890", 0, nat{1234567890}, 10, true, 0},
263
        {"xyz", 36, nat{(33*36+34)*36 + 35}, 36, true, 0},
264
        {"xyz?", 36, nat{(33*36+34)*36 + 35}, 36, true, '?'},
265
        {"0x", 16, nil, 16, true, 'x'},
266
        {"0xdeadbeef", 0, nat{0xdeadbeef}, 16, true, 0},
267
        {"0XDEADBEEF", 0, nat{0xdeadbeef}, 16, true, 0},
268
}
269
 
270
func TestScanBase(t *testing.T) {
271
        for _, a := range natScanTests {
272
                r := strings.NewReader(a.s)
273
                x, b, err := nat(nil).scan(r, a.base)
274
                if err == nil && !a.ok {
275
                        t.Errorf("scan%+v\n\texpected error", a)
276
                }
277
                if err != nil {
278
                        if a.ok {
279
                                t.Errorf("scan%+v\n\tgot error = %s", a, err)
280
                        }
281
                        continue
282
                }
283
                if x.cmp(a.x) != 0 {
284
                        t.Errorf("scan%+v\n\tgot z = %v; want %v", a, x, a.x)
285
                }
286
                if b != a.b {
287
                        t.Errorf("scan%+v\n\tgot b = %d; want %d", a, b, a.base)
288
                }
289
                next, _, err := r.ReadRune()
290
                if err == io.EOF {
291
                        next = 0
292
                        err = nil
293
                }
294
                if err == nil && next != a.next {
295
                        t.Errorf("scan%+v\n\tgot next = %q; want %q", a, next, a.next)
296
                }
297
        }
298
}
299
 
300
var pi = "3" +
301
        "14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651" +
302
        "32823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461" +
303
        "28475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920" +
304
        "96282925409171536436789259036001133053054882046652138414695194151160943305727036575959195309218611738193261179" +
305
        "31051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798" +
306
        "60943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901" +
307
        "22495343014654958537105079227968925892354201995611212902196086403441815981362977477130996051870721134999999837" +
308
        "29780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083" +
309
        "81420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909" +
310
        "21642019893809525720106548586327886593615338182796823030195203530185296899577362259941389124972177528347913151" +
311
        "55748572424541506959508295331168617278558890750983817546374649393192550604009277016711390098488240128583616035" +
312
        "63707660104710181942955596198946767837449448255379774726847104047534646208046684259069491293313677028989152104" +
313
        "75216205696602405803815019351125338243003558764024749647326391419927260426992279678235478163600934172164121992" +
314
        "45863150302861829745557067498385054945885869269956909272107975093029553211653449872027559602364806654991198818" +
315
        "34797753566369807426542527862551818417574672890977772793800081647060016145249192173217214772350141441973568548" +
316
        "16136115735255213347574184946843852332390739414333454776241686251898356948556209921922218427255025425688767179" +
317
        "04946016534668049886272327917860857843838279679766814541009538837863609506800642251252051173929848960841284886" +
318
        "26945604241965285022210661186306744278622039194945047123713786960956364371917287467764657573962413890865832645" +
319
        "99581339047802759009946576407895126946839835259570982582262052248940772671947826848260147699090264013639443745" +
320
        "53050682034962524517493996514314298091906592509372216964615157098583874105978859597729754989301617539284681382" +
321
        "68683868942774155991855925245953959431049972524680845987273644695848653836736222626099124608051243884390451244" +
322
        "13654976278079771569143599770012961608944169486855584840635342207222582848864815845602850601684273945226746767" +
323
        "88952521385225499546667278239864565961163548862305774564980355936345681743241125150760694794510965960940252288" +
324
        "79710893145669136867228748940560101503308617928680920874760917824938589009714909675985261365549781893129784821" +
325
        "68299894872265880485756401427047755513237964145152374623436454285844479526586782105114135473573952311342716610" +
326
        "21359695362314429524849371871101457654035902799344037420073105785390621983874478084784896833214457138687519435" +
327
        "06430218453191048481005370614680674919278191197939952061419663428754440643745123718192179998391015919561814675" +
328
        "14269123974894090718649423196156794520809514655022523160388193014209376213785595663893778708303906979207734672" +
329
        "21825625996615014215030680384477345492026054146659252014974428507325186660021324340881907104863317346496514539" +
330
        "05796268561005508106658796998163574736384052571459102897064140110971206280439039759515677157700420337869936007" +
331
        "23055876317635942187312514712053292819182618612586732157919841484882916447060957527069572209175671167229109816" +
332
        "90915280173506712748583222871835209353965725121083579151369882091444210067510334671103141267111369908658516398" +
333
        "31501970165151168517143765761835155650884909989859982387345528331635507647918535893226185489632132933089857064" +
334
        "20467525907091548141654985946163718027098199430992448895757128289059232332609729971208443357326548938239119325" +
335
        "97463667305836041428138830320382490375898524374417029132765618093773444030707469211201913020330380197621101100" +
336
        "44929321516084244485963766983895228684783123552658213144957685726243344189303968642624341077322697802807318915" +
337
        "44110104468232527162010526522721116603966655730925471105578537634668206531098965269186205647693125705863566201" +
338
        "85581007293606598764861179104533488503461136576867532494416680396265797877185560845529654126654085306143444318" +
339
        "58676975145661406800700237877659134401712749470420562230538994561314071127000407854733269939081454664645880797" +
340
        "27082668306343285878569830523580893306575740679545716377525420211495576158140025012622859413021647155097925923" +
341
        "09907965473761255176567513575178296664547791745011299614890304639947132962107340437518957359614589019389713111" +
342
        "79042978285647503203198691514028708085990480109412147221317947647772622414254854540332157185306142288137585043" +
343
        "06332175182979866223717215916077166925474873898665494945011465406284336639379003976926567214638530673609657120" +
344
        "91807638327166416274888800786925602902284721040317211860820419000422966171196377921337575114959501566049631862" +
345
        "94726547364252308177036751590673502350728354056704038674351362222477158915049530984448933309634087807693259939" +
346
        "78054193414473774418426312986080998886874132604721569516239658645730216315981931951673538129741677294786724229" +
347
        "24654366800980676928238280689964004824354037014163149658979409243237896907069779422362508221688957383798623001" +
348
        "59377647165122893578601588161755782973523344604281512627203734314653197777416031990665541876397929334419521541" +
349
        "34189948544473456738316249934191318148092777710386387734317720754565453220777092120190516609628049092636019759" +
350
        "88281613323166636528619326686336062735676303544776280350450777235547105859548702790814356240145171806246436267" +
351
        "94561275318134078330336254232783944975382437205835311477119926063813346776879695970309833913077109870408591337"
352
 
353
// Test case for BenchmarkScanPi.
354
func TestScanPi(t *testing.T) {
355
        var x nat
356
        z, _, err := x.scan(strings.NewReader(pi), 10)
357
        if err != nil {
358
                t.Errorf("scanning pi: %s", err)
359
        }
360
        if s := z.decimalString(); s != pi {
361
                t.Errorf("scanning pi: got %s", s)
362
        }
363
}
364
 
365
func BenchmarkScanPi(b *testing.B) {
366
        for i := 0; i < b.N; i++ {
367
                var x nat
368
                x.scan(strings.NewReader(pi), 10)
369
        }
370
}
371
 
372
func BenchmarkScan10Base2(b *testing.B)     { ScanHelper(b, 2, 10, 10) }
373
func BenchmarkScan100Base2(b *testing.B)    { ScanHelper(b, 2, 10, 100) }
374
func BenchmarkScan1000Base2(b *testing.B)   { ScanHelper(b, 2, 10, 1000) }
375
func BenchmarkScan10000Base2(b *testing.B)  { ScanHelper(b, 2, 10, 10000) }
376
func BenchmarkScan100000Base2(b *testing.B) { ScanHelper(b, 2, 10, 100000) }
377
 
378
func BenchmarkScan10Base8(b *testing.B)     { ScanHelper(b, 8, 10, 10) }
379
func BenchmarkScan100Base8(b *testing.B)    { ScanHelper(b, 8, 10, 100) }
380
func BenchmarkScan1000Base8(b *testing.B)   { ScanHelper(b, 8, 10, 1000) }
381
func BenchmarkScan10000Base8(b *testing.B)  { ScanHelper(b, 8, 10, 10000) }
382
func BenchmarkScan100000Base8(b *testing.B) { ScanHelper(b, 8, 10, 100000) }
383
 
384
func BenchmarkScan10Base10(b *testing.B)     { ScanHelper(b, 10, 10, 10) }
385
func BenchmarkScan100Base10(b *testing.B)    { ScanHelper(b, 10, 10, 100) }
386
func BenchmarkScan1000Base10(b *testing.B)   { ScanHelper(b, 10, 10, 1000) }
387
func BenchmarkScan10000Base10(b *testing.B)  { ScanHelper(b, 10, 10, 10000) }
388
func BenchmarkScan100000Base10(b *testing.B) { ScanHelper(b, 10, 10, 100000) }
389
 
390
func BenchmarkScan10Base16(b *testing.B)     { ScanHelper(b, 16, 10, 10) }
391
func BenchmarkScan100Base16(b *testing.B)    { ScanHelper(b, 16, 10, 100) }
392
func BenchmarkScan1000Base16(b *testing.B)   { ScanHelper(b, 16, 10, 1000) }
393
func BenchmarkScan10000Base16(b *testing.B)  { ScanHelper(b, 16, 10, 10000) }
394
func BenchmarkScan100000Base16(b *testing.B) { ScanHelper(b, 16, 10, 100000) }
395
 
396
func ScanHelper(b *testing.B, base int, x, y Word) {
397
        b.StopTimer()
398
        var z nat
399
        z = z.expWW(x, y)
400
 
401
        var s string
402
        s = z.string(lowercaseDigits[0:base])
403
        if t := toString(z, lowercaseDigits[0:base]); t != s {
404
                b.Fatalf("scanning: got %s; want %s", s, t)
405
        }
406
        b.StartTimer()
407
 
408
        for i := 0; i < b.N; i++ {
409
                z.scan(strings.NewReader(s), base)
410
        }
411
}
412
 
413
func BenchmarkString10Base2(b *testing.B)     { StringHelper(b, 2, 10, 10) }
414
func BenchmarkString100Base2(b *testing.B)    { StringHelper(b, 2, 10, 100) }
415
func BenchmarkString1000Base2(b *testing.B)   { StringHelper(b, 2, 10, 1000) }
416
func BenchmarkString10000Base2(b *testing.B)  { StringHelper(b, 2, 10, 10000) }
417
func BenchmarkString100000Base2(b *testing.B) { StringHelper(b, 2, 10, 100000) }
418
 
419
func BenchmarkString10Base8(b *testing.B)     { StringHelper(b, 8, 10, 10) }
420
func BenchmarkString100Base8(b *testing.B)    { StringHelper(b, 8, 10, 100) }
421
func BenchmarkString1000Base8(b *testing.B)   { StringHelper(b, 8, 10, 1000) }
422
func BenchmarkString10000Base8(b *testing.B)  { StringHelper(b, 8, 10, 10000) }
423
func BenchmarkString100000Base8(b *testing.B) { StringHelper(b, 8, 10, 100000) }
424
 
425
func BenchmarkString10Base10(b *testing.B)     { StringHelper(b, 10, 10, 10) }
426
func BenchmarkString100Base10(b *testing.B)    { StringHelper(b, 10, 10, 100) }
427
func BenchmarkString1000Base10(b *testing.B)   { StringHelper(b, 10, 10, 1000) }
428
func BenchmarkString10000Base10(b *testing.B)  { StringHelper(b, 10, 10, 10000) }
429
func BenchmarkString100000Base10(b *testing.B) { StringHelper(b, 10, 10, 100000) }
430
 
431
func BenchmarkString10Base16(b *testing.B)     { StringHelper(b, 16, 10, 10) }
432
func BenchmarkString100Base16(b *testing.B)    { StringHelper(b, 16, 10, 100) }
433
func BenchmarkString1000Base16(b *testing.B)   { StringHelper(b, 16, 10, 1000) }
434
func BenchmarkString10000Base16(b *testing.B)  { StringHelper(b, 16, 10, 10000) }
435
func BenchmarkString100000Base16(b *testing.B) { StringHelper(b, 16, 10, 100000) }
436
 
437
func StringHelper(b *testing.B, base int, x, y Word) {
438
        b.StopTimer()
439
        var z nat
440
        z = z.expWW(x, y)
441
        z.string(lowercaseDigits[0:base]) // warm divisor cache
442
        b.StartTimer()
443
 
444
        for i := 0; i < b.N; i++ {
445
                _ = z.string(lowercaseDigits[0:base])
446
        }
447
}
448
 
449
func BenchmarkLeafSize0(b *testing.B)  { LeafSizeHelper(b, 10, 0) } // test without splitting
450
func BenchmarkLeafSize1(b *testing.B)  { LeafSizeHelper(b, 10, 1) }
451
func BenchmarkLeafSize2(b *testing.B)  { LeafSizeHelper(b, 10, 2) }
452
func BenchmarkLeafSize3(b *testing.B)  { LeafSizeHelper(b, 10, 3) }
453
func BenchmarkLeafSize4(b *testing.B)  { LeafSizeHelper(b, 10, 4) }
454
func BenchmarkLeafSize5(b *testing.B)  { LeafSizeHelper(b, 10, 5) }
455
func BenchmarkLeafSize6(b *testing.B)  { LeafSizeHelper(b, 10, 6) }
456
func BenchmarkLeafSize7(b *testing.B)  { LeafSizeHelper(b, 10, 7) }
457
func BenchmarkLeafSize8(b *testing.B)  { LeafSizeHelper(b, 10, 8) }
458
func BenchmarkLeafSize9(b *testing.B)  { LeafSizeHelper(b, 10, 9) }
459
func BenchmarkLeafSize10(b *testing.B) { LeafSizeHelper(b, 10, 10) }
460
func BenchmarkLeafSize11(b *testing.B) { LeafSizeHelper(b, 10, 11) }
461
func BenchmarkLeafSize12(b *testing.B) { LeafSizeHelper(b, 10, 12) }
462
func BenchmarkLeafSize13(b *testing.B) { LeafSizeHelper(b, 10, 13) }
463
func BenchmarkLeafSize14(b *testing.B) { LeafSizeHelper(b, 10, 14) }
464
func BenchmarkLeafSize15(b *testing.B) { LeafSizeHelper(b, 10, 15) }
465
func BenchmarkLeafSize16(b *testing.B) { LeafSizeHelper(b, 10, 16) }
466
func BenchmarkLeafSize32(b *testing.B) { LeafSizeHelper(b, 10, 32) } // try some large lengths
467
func BenchmarkLeafSize64(b *testing.B) { LeafSizeHelper(b, 10, 64) }
468
 
469
func LeafSizeHelper(b *testing.B, base Word, size int) {
470
        b.StopTimer()
471
        originalLeafSize := leafSize
472
        resetTable(cacheBase10[:])
473
        leafSize = size
474
        b.StartTimer()
475
 
476
        for d := 1; d <= 10000; d *= 10 {
477
                b.StopTimer()
478
                var z nat
479
                z = z.expWW(base, Word(d))            // build target number
480
                _ = z.string(lowercaseDigits[0:base]) // warm divisor cache
481
                b.StartTimer()
482
 
483
                for i := 0; i < b.N; i++ {
484
                        _ = z.string(lowercaseDigits[0:base])
485
                }
486
        }
487
 
488
        b.StopTimer()
489
        resetTable(cacheBase10[:])
490
        leafSize = originalLeafSize
491
        b.StartTimer()
492
}
493
 
494
func resetTable(table []divisor) {
495
        if table != nil && table[0].bbb != nil {
496
                for i := 0; i < len(table); i++ {
497
                        table[i].bbb = nil
498
                        table[i].nbits = 0
499
                        table[i].ndigits = 0
500
                }
501
        }
502
}
503
 
504
func TestStringPowers(t *testing.T) {
505
        var b, p Word
506
        for b = 2; b <= 16; b++ {
507
                for p = 0; p <= 512; p++ {
508
                        x := nat(nil).expWW(b, p)
509
                        xs := x.string(lowercaseDigits[0:b])
510
                        xs2 := toString(x, lowercaseDigits[0:b])
511
                        if xs != xs2 {
512
                                t.Errorf("failed at %d ** %d in base %d: %s != %s", b, p, b, xs, xs2)
513
                        }
514
                }
515
        }
516
}
517
 
518
func TestLeadingZeros(t *testing.T) {
519
        var x Word = _B >> 1
520
        for i := 0; i <= _W; i++ {
521
                if int(leadingZeros(x)) != i {
522
                        t.Errorf("failed at %x: got %d want %d", x, leadingZeros(x), i)
523
                }
524
                x >>= 1
525
        }
526
}
527
 
528
type shiftTest struct {
529
        in    nat
530
        shift uint
531
        out   nat
532
}
533
 
534
var leftShiftTests = []shiftTest{
535
        {nil, 0, nil},
536
        {nil, 1, nil},
537
        {natOne, 0, natOne},
538
        {natOne, 1, natTwo},
539
        {nat{1 << (_W - 1)}, 1, nat{0}},
540
        {nat{1 << (_W - 1), 0}, 1, nat{0, 1}},
541
}
542
 
543
func TestShiftLeft(t *testing.T) {
544
        for i, test := range leftShiftTests {
545
                var z nat
546
                z = z.shl(test.in, test.shift)
547
                for j, d := range test.out {
548
                        if j >= len(z) || z[j] != d {
549
                                t.Errorf("#%d: got: %v want: %v", i, z, test.out)
550
                                break
551
                        }
552
                }
553
        }
554
}
555
 
556
var rightShiftTests = []shiftTest{
557
        {nil, 0, nil},
558
        {nil, 1, nil},
559
        {natOne, 0, natOne},
560
        {natOne, 1, nil},
561
        {natTwo, 1, natOne},
562
        {nat{0, 1}, 1, nat{1 << (_W - 1)}},
563
        {nat{2, 1, 1}, 1, nat{1<<(_W-1) + 1, 1 << (_W - 1)}},
564
}
565
 
566
func TestShiftRight(t *testing.T) {
567
        for i, test := range rightShiftTests {
568
                var z nat
569
                z = z.shr(test.in, test.shift)
570
                for j, d := range test.out {
571
                        if j >= len(z) || z[j] != d {
572
                                t.Errorf("#%d: got: %v want: %v", i, z, test.out)
573
                                break
574
                        }
575
                }
576
        }
577
}
578
 
579
type modWTest struct {
580
        in       string
581
        dividend string
582
        out      string
583
}
584
 
585
var modWTests32 = []modWTest{
586
        {"23492635982634928349238759823742", "252341", "220170"},
587
}
588
 
589
var modWTests64 = []modWTest{
590
        {"6527895462947293856291561095690465243862946", "524326975699234", "375066989628668"},
591
}
592
 
593
func runModWTests(t *testing.T, tests []modWTest) {
594
        for i, test := range tests {
595
                in, _ := new(Int).SetString(test.in, 10)
596
                d, _ := new(Int).SetString(test.dividend, 10)
597
                out, _ := new(Int).SetString(test.out, 10)
598
 
599
                r := in.abs.modW(d.abs[0])
600
                if r != out.abs[0] {
601
                        t.Errorf("#%d failed: got %s want %s", i, r, out)
602
                }
603
        }
604
}
605
 
606
func TestModW(t *testing.T) {
607
        if _W >= 32 {
608
                runModWTests(t, modWTests32)
609
        }
610
        if _W >= 64 {
611
                runModWTests(t, modWTests64)
612
        }
613
}
614
 
615
func TestTrailingZeroBits(t *testing.T) {
616
        var x Word
617
        x--
618
        for i := 0; i < _W; i++ {
619
                if trailingZeroBits(x) != i {
620
                        t.Errorf("Failed at step %d: x: %x got: %d", i, x, trailingZeroBits(x))
621
                }
622
                x <<= 1
623
        }
624
}
625
 
626
var expNNTests = []struct {
627
        x, y, m string
628
        out     string
629
}{
630
        {"0x8000000000000000", "2", "", "0x40000000000000000000000000000000"},
631
        {"0x8000000000000000", "2", "6719", "4944"},
632
        {"0x8000000000000000", "3", "6719", "5447"},
633
        {"0x8000000000000000", "1000", "6719", "1603"},
634
        {"0x8000000000000000", "1000000", "6719", "3199"},
635
        {
636
                "2938462938472983472983659726349017249287491026512746239764525612965293865296239471239874193284792387498274256129746192347",
637
                "298472983472983471903246121093472394872319615612417471234712061",
638
                "29834729834729834729347290846729561262544958723956495615629569234729836259263598127342374289365912465901365498236492183464",
639
                "23537740700184054162508175125554701713153216681790245129157191391322321508055833908509185839069455749219131480588829346291",
640
        },
641
}
642
 
643
func TestExpNN(t *testing.T) {
644
        for i, test := range expNNTests {
645
                x, _, _ := nat(nil).scan(strings.NewReader(test.x), 0)
646
                y, _, _ := nat(nil).scan(strings.NewReader(test.y), 0)
647
                out, _, _ := nat(nil).scan(strings.NewReader(test.out), 0)
648
 
649
                var m nat
650
 
651
                if len(test.m) > 0 {
652
                        m, _, _ = nat(nil).scan(strings.NewReader(test.m), 0)
653
                }
654
 
655
                z := nat(nil).expNN(x, y, m)
656
                if z.cmp(out) != 0 {
657
                        t.Errorf("#%d got %v want %v", i, z, out)
658
                }
659
        }
660
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.