1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/js1_7/geniter/fibonacci-matrix-generator.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,64 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +//----------------------------------------------------------------------------- 1.10 +var BUGNUMBER = "(none)"; 1.11 +var summary = "Fibonacci generator by matrix multiplication"; 1.12 +var actual, expect; 1.13 + 1.14 +printBugNumber(BUGNUMBER); 1.15 +printStatus(summary); 1.16 + 1.17 +/************** 1.18 + * BEGIN TEST * 1.19 + **************/ 1.20 + 1.21 +function fib() 1.22 +{ 1.23 + var init = [1, 0]; 1.24 + var mx = [[1, 1], [1, 0]]; 1.25 + while (true) 1.26 + { 1.27 + yield init[1]; 1.28 + var tmp = [,]; 1.29 + tmp[0] = 1.30 + mx[0][0]*init[0] + mx[0][1]*init[1]; 1.31 + tmp[1] = 1.32 + mx[1][0]*init[0] + mx[1][1]*init[1]; 1.33 + init = tmp; 1.34 + } 1.35 +} 1.36 + 1.37 +var failed = false; 1.38 +var it = fib(); 1.39 + 1.40 +try 1.41 +{ 1.42 + if (it.next() != 0) 1.43 + throw "F_0 failed"; 1.44 + if (it.next() != 1) 1.45 + throw "F_1 failed"; 1.46 + if (it.next() != 1) 1.47 + throw "F_2 failed"; 1.48 + if (it.next() != 2) 1.49 + throw "F_3 failed"; 1.50 + if (it.next() != 3) 1.51 + throw "F_4 failed"; 1.52 + if (it.next() != 5) 1.53 + throw "F_5 failed"; 1.54 + if (it.next() != 8) 1.55 + throw "F_6 failed"; 1.56 +} 1.57 +catch (e) 1.58 +{ 1.59 + failed = e; 1.60 +} 1.61 + 1.62 + 1.63 + 1.64 +expect = false; 1.65 +actual = failed; 1.66 + 1.67 +reportCompare(expect, actual, summary);