LCOV - code coverage report
Current view: top level - src/core/prng - index.js (source / functions) Hit Total Coverage
Test: cov_profile.lcov Lines: 55 55 100.0 %
Date: 2021-12-16 00:47:58 Functions: 4 4 100.0 %
Branches: 8 8 100.0 %

           Branch data     Line data    Source code
       1                 :         44 : // TODO: add a Mersenne Twister for comparison, and benchmark
       2            [ + ]:         44 : import xor from "./xor.js";
       3                 :         44 : import processSeed from "./process-seed/index.js";
       4                 :         44 : import Distribution from "./distribution/index.js";
       5                 :         44 : 
       6                 :         44 : // TODO: add specific tests for prng wrapper
       7                 :         44 : export default (seeded, initseed) => {
       8                 :       1484 :   // initialise prng functions
       9                 :       1484 :   const { reseed, getState, setState, random: prngRandom, reverse } = xor(0);
      10                 :       1484 : 
      11                 :       1484 :   // seed prng with initial seed
      12                 :       1484 :   reseed(processSeed(initseed));
      13                 :       1484 : 
      14                 :       1484 :   // capture initial prng state
      15                 :       1484 :   const initialState = getState();
      16                 :       1484 : 
      17                 :       1484 :   // TODO: should be normalized to serialised string for portability?
      18                 :       1484 :   // define prng state getter/setter method
      19                 :       1484 :   const state = (newVal) => {
      20                 :       1794 :     // if called with no arguments, return state
      21                 :       1794 :     // else if called with null, reset state and return seeded instance
      22                 :       1794 :     // else if called with value, set state to value and return seeded instance
      23                 :       1794 :     if (newVal === undefined) {
      24         [ +  + ]:       1950 :       return getState();
      25                 :       1934 :     } else {
      26            [ + ]:       1948 :       if (newVal === null) {
      27            [ + ]:       1950 :         setState(initialState);
      28                 :       1948 :       } else {
      29            [ + ]:       1960 :         setState(newVal);
      30                 :       1948 :       }
      31                 :       1948 :       return seeded;
      32                 :       1794 :     }
      33                 :       1484 :   };
      34                 :       1484 : 
      35                 :       1484 :   const distribution = Distribution(seeded);
      36                 :       1484 : 
      37                 :       1484 :   // define prng reset method
      38                 :       1484 :   const reset = (seed) => {
      39                 :       1532 :     reseed(
      40                 :       1532 :       processSeed(seed !== undefined ? JSON.stringify([seed, []]) : initseed),
      41         [ +  + ]:       1532 :     );
      42                 :       1532 :     return seeded;
      43                 :       1484 :   };
      44                 :       1484 : 
      45                 :       1484 :   // define random method based on distribution weighted prng random function
      46                 :       1484 :   const random = () => distribution(prngRandom());
      47                 :       1484 : 
      48                 :       1484 :   return {
      49                 :       1484 :     state,
      50                 :       1484 :     reset,
      51                 :       1484 :     random,
      52                 :       1484 :     reverse,
      53                 :       1484 :     distribution,
      54                 :       1484 :   };
      55                 :         22 : };

Generated by: LCOV version 1.15