tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

fibonacci-matrix-generator.js (1272B)


      1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 //-----------------------------------------------------------------------------
      7 var summary = "Fibonacci generator by matrix multiplication";
      8 var actual, expect;
      9 
     10 printStatus(summary);
     11 
     12 /**************
     13 * BEGIN TEST *
     14 **************/
     15 
     16 function* fib()
     17 {
     18  var init = [1, 0];
     19  var mx = [[1, 1], [1, 0]];
     20  while (true)
     21  {
     22    yield init[1];
     23    var tmp = [,];
     24    tmp[0] =
     25      mx[0][0]*init[0] + mx[0][1]*init[1];
     26    tmp[1] =
     27      mx[1][0]*init[0] + mx[1][1]*init[1];
     28    init = tmp;
     29  }
     30 }
     31 
     32 var failed = false;
     33 var it = fib();
     34 
     35 try
     36 {
     37  if (it.next().value != 0)
     38    throw "F_0 failed";
     39  if (it.next().value != 1)
     40    throw "F_1 failed";
     41  if (it.next().value != 1)
     42    throw "F_2 failed";
     43  if (it.next().value != 2)
     44    throw "F_3 failed";
     45  if (it.next().value != 3)
     46    throw "F_4 failed";
     47  if (it.next().value != 5)
     48    throw "F_5 failed";
     49  if (it.next().value != 8)
     50    throw "F_6 failed";
     51 }
     52 catch (e)
     53 {
     54  failed = e;
     55 }
     56 
     57 
     58 
     59 expect = false;
     60 actual = failed;
     61 
     62 reportCompare(expect, actual, summary);