tests/generators/uniform.test.js   A
last analyzed

Complexity

Total Complexity 7
Complexity/F 1.4

Size

Lines of Code 44
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 28
mnd 2
bc 2
fnc 5
dl 0
loc 44
rs 10
bpm 0.4
cpm 1.4
noi 2
c 0
b 0
f 0
1
import { assert } from 'chai';
2
import entry from '../entry';
3
4
suite('Generators: uniform');
5
6
const sse = arr => arr.reduce((a, num) => a + (num ** 2), 0) / arr.length;
0 ignored issues
show
Unused Code introduced by
The parameter num is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
7
8
test('Distribution', function () {
9
    const min = -10;
10
    const max = 20;
11
    const expectedPerSegment = 10_000;
0 ignored issues
show
Bug introduced by
The variable _000 seems to be never declared. If this is a global, consider adding a /** global: _000 */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
12
    const segmentWidth = 1;
13
    const allowedMaxDiff = 0.05;
14
15
    const segments = Array.from({ length: (max - min) / segmentWidth }).map((i, index) => {
16
        const from = min + index * segmentWidth;
17
18
        return {
19
            from,
20
            to    : from + segmentWidth,
21
            count : 0
22
        };
23
    });
24
25
    for (let i = 0; i < segments.length * expectedPerSegment; i++) {
26
        const num = entry.uniform(min, max);
27
        const index = Math.ceil((num - segmentWidth) / segmentWidth) - min;
28
29
        segments[index].count += 1;
30
    }
31
32
    for (const segment of segments) {
33
        segment.error =  Math.abs(segment.count - expectedPerSegment) / expectedPerSegment;
34
        assert.isAtMost(
35
            segment.error,
36
            allowedMaxDiff,
37
            JSON.stringify(segment)
38
        );
39
    }
40
41
    const errors = segments.map(s => s.error);
42
43
    assert.isAtMost(sse(errors), allowedMaxDiff / 2, errors);
44
});
45