frequi_origin/tests/unit/bincount.spec.ts

50 lines
1.5 KiB
TypeScript
Raw Normal View History

2023-04-11 04:38:26 +00:00
import { describe, it, expect } from 'vitest';
2024-07-05 12:31:06 +00:00
import { binData } from '@/utils/charts/binCount';
2022-06-21 17:51:47 +00:00
2022-09-14 18:20:15 +00:00
describe('binCount.ts', () => {
2022-06-21 17:51:47 +00:00
it('Bins data as expected', () => {
const testData = [1, 1, 2, 3, 5, 6, 8, 10];
const res = binData(testData, 3);
expect(res.length).toEqual(3);
expect(res).toEqual([
[1, 4],
[4.03, 2],
[7.06, 2],
]);
expect(res.map((v) => v[1]).reduce((a, b) => a + b)).toEqual(testData.length);
const res1 = binData(testData, 5);
// expect(res1.length).toEqual(5);
expect(res1).toEqual([
[1, 3],
[2.818, 1],
[4.636, 2],
[6.454, 1],
[8.272, 1],
]);
expect(res1.map((v) => v[1]).reduce((a, b) => a + b)).toEqual(testData.length);
});
it('Bins data with negatives', () => {
const testData = [1, 1, 2, 3, 5, 6, 8, -1, -3, -5, -4];
const res = binData(testData, 3);
expect(res.length).toEqual(3);
expect(res.map((v) => v[1]).reduce((a, b) => a + b)).toEqual(testData.length);
expect(res).toEqual([
[-5, 4],
[-0.623, 4],
[3.753, 3],
]);
});
it('Bins data performant', () => {
const randomSize = 20000;
const randomData = Array.from({ length: randomSize }, () => Math.floor(Math.random() * 10));
2022-09-14 18:20:15 +00:00
const startTime = Date.now();
2022-06-21 17:51:47 +00:00
const res = binData(randomData, 5);
2022-09-14 18:20:15 +00:00
const endTime = Date.now();
2022-12-05 13:08:25 +00:00
expect(endTime - startTime).toBeLessThan(30);
2022-06-21 17:51:47 +00:00
expect(res.map((v) => v[1]).reduce((a, b) => a + b)).toEqual(randomData.length);
});
});