Skip to content

Commit 96a0996

Browse files
authored
test: add more tests for xStandardDeviation (#298)
1 parent 2efb656 commit 96a0996

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/x/__tests__/xStandardDeviation.test.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,30 @@ import { expect, test } from 'vitest';
22

33
import { xStandardDeviation } from '../xStandardDeviation';
44

5-
const data = [15, 13, 17, 7];
6-
7-
const typedArray = new Uint16Array(4);
8-
typedArray[0] = 15;
9-
typedArray[1] = 13;
10-
typedArray[2] = 17;
11-
typedArray[3] = 7;
12-
135
test('standard deviation', () => {
6+
const data = [15, 13, 17, 7];
147
const s = xStandardDeviation(data);
158
expect(s).toBeCloseTo(Math.sqrt(18.667), 3);
169
expect(xStandardDeviation(data, { unbiased: true })).toBe(s);
1710
expect(xStandardDeviation(data, { unbiased: false })).toBe(Math.sqrt(14));
1811
});
1912

2013
test('standard deviation of typed array', () => {
14+
const data = [15, 13, 17, 7];
15+
const typedArray = Uint16Array.from(data);
2116
const s = xStandardDeviation(typedArray);
2217
expect(s).toBeCloseTo(Math.sqrt(18.667), 3);
2318
expect(xStandardDeviation(data, { unbiased: true })).toBe(s);
2419
expect(xStandardDeviation(data, { unbiased: false })).toBe(Math.sqrt(14));
2520
});
21+
22+
test('one element', () => {
23+
const data = [15];
24+
expect(xStandardDeviation(data)).toBe(Number.NaN);
25+
expect(xStandardDeviation(data, { unbiased: false })).toBe(0);
26+
});
27+
28+
test('empty array', () => {
29+
const data: number[] = [];
30+
expect(() => xStandardDeviation(data)).toThrow('input must not be empty');
31+
});

src/x/xVariance.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export interface XVarianceOptions {
1111
unbiased?: boolean;
1212

1313
/**
14-
* Mean of the data
14+
* Precalculated mean of the data. If undefined it will be recalculated internally
1515
* @default mean calculated
1616
*/
1717
mean?: number;

0 commit comments

Comments
 (0)