Total Complexity | 7 |
Complexity/F | 1.4 |
Lines of Code | 44 |
Function Count | 5 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 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; |
||
|
|||
7 | |||
8 | test('Distribution', function () { |
||
9 | const min = -10; |
||
10 | const max = 20; |
||
11 | const expectedPerSegment = 10_000; |
||
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 |
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.