| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
var gTestfile = 'fibonacci-matrix-generator.js'; |
| 7 |
|
| 8 |
var BUGNUMBER = "(none)"; |
| 9 |
var summary = "Fibonacci generator by matrix multiplication"; |
| 10 |
var actual, expect; |
| 11 |
|
| 12 |
printBugNumber(BUGNUMBER); |
| 13 |
printStatus(summary); |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
function fib() |
| 20 |
{ |
| 21 |
var init = [1, 0]; |
| 22 |
var mx = [[1, 1], [1, 0]]; |
| 23 |
while (true) |
| 24 |
{ |
| 25 |
yield init[1]; |
| 26 |
var tmp = [,]; |
| 27 |
tmp[0] = |
| 28 |
mx[0][0]*init[0] + mx[0][1]*init[1]; |
| 29 |
tmp[1] = |
| 30 |
mx[1][0]*init[0] + mx[1][1]*init[1]; |
| 31 |
init = tmp; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
var failed = false; |
| 36 |
var it = fib(); |
| 37 |
|
| 38 |
try |
| 39 |
{ |
| 40 |
if (it.next() != 0) |
| 41 |
throw "F_0 failed"; |
| 42 |
if (it.next() != 1) |
| 43 |
throw "F_1 failed"; |
| 44 |
if (it.next() != 1) |
| 45 |
throw "F_2 failed"; |
| 46 |
if (it.next() != 2) |
| 47 |
throw "F_3 failed"; |
| 48 |
if (it.next() != 3) |
| 49 |
throw "F_4 failed"; |
| 50 |
if (it.next() != 5) |
| 51 |
throw "F_5 failed"; |
| 52 |
if (it.next() != 8) |
| 53 |
throw "F_6 failed"; |
| 54 |
} |
| 55 |
catch (e) |
| 56 |
{ |
| 57 |
failed = e; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
expect = false; |
| 63 |
actual = failed; |
| 64 |
|
| 65 |
reportCompare(expect, actual, summary); |