const expect = require('chai').expect; const sinon = require('sinon'); const Clock = require('../src/clock.js'); describe("Clock Tests", function(){ var count = 0; var count2 = 0; var tick = ()=>{count += 1;}; var tick2 = () =>{count2 += 1;}; var clk = (new Clock()).clk(tick); var clk2 = (new Clock(2)).clk(tick).clk(tick2, 1); function speedTest(hz, c){ c = (c) ? c : 1; let stime = Date.now(); let ltime = stime; let dt = 0; let _clk = (c === 1) ? clk : clk2; _clk.hz = hz; while (count < hz && dt < 1000){ let ctime = Date.now(); let _dt = ctime - ltime; if (_dt > 0){ ltime = ctime; _clk.tick(_dt); } dt = Date.now() - stime; } return dt; } it("4hz for approx. 1 second", function(){ let dt = speedTest(4); expect(count).to.equal(4); expect(dt).to.be.gt(750); }); it("4Khz for approx. 1 second", function(){ count = 0; let ts = 4000; let d = ts * 0.01; let dt = speedTest(ts); expect(count).to.be.gt(ts - d); expect(count).to.be.lt(ts + d); expect(dt).to.be.gt(990); expect(dt).to.be.lt(1010); }); it("4Mhz for approx. 1 second", function(){ count = 0; let ts = 4000000; let d = ts * 0.01; let dt = speedTest(ts); expect(count).to.be.gt(ts - d); expect(count).to.be.lt(ts + d); expect(dt).to.be.gt(990); expect(dt).to.be.lt(1010); }); it("4khz w/two layers for approx. 1 second", function(){ count = 0; count2 = 0; let ts = 4000; let d = ts * 0.01; let dt = speedTest(ts, 2); expect(count).to.be.gt((ts*0.5) - dt); expect(count).to.be.lt((ts*0.5) + dt); expect(count2).to.be.gt((ts*0.5) - dt); expect(count2).to.be.lt((ts*0.5) + dt); expect(dt).to.be.gt(990); expect(dt).to.be.lt(1010); }); });