als-statistics
Version:
A powerful and lightweight JavaScript library for descriptive statistics, regression, clustering, outlier detection, and noise analysis using a flexible table/column architecture.
138 lines (113 loc) • 5.2 kB
JavaScript
const { describe, it, beforeEach } = require('node:test');
const assert = require('node:assert');
const Column = require('../../lib/column/index');
describe('Column class', () => {
let column;
beforeEach(() => {
column = new Column([1, 2, 3, 4, 5]);
});
it('should initialize correctly', () => {
assert.deepStrictEqual(column._values, [1, 2, 3, 4, 5]);
});
it('should return correct values', () => {
assert.deepStrictEqual(column.values, [1, 2, 3, 4, 5]);
});
it('should compute correct n (length of values)', () => {
assert.strictEqual(column.n, 5);
});
it('should cache computed values correctly', () => {
let computeCount = 0;
const result = column.cached('testMetric', () => {
computeCount++;
return 42;
});
assert.strictEqual(result, 42);
assert.strictEqual(column.cached('testMetric', () => 100), 42); // Закэшированное значение
assert.strictEqual(computeCount, 1); // Вычисление только раз
});
it('should update cache when filterState changes', () => {
let computeCount = 0;
const computeFn = () => {
computeCount++;
return 42;
};
const result1 = column.cached('testMetric', computeFn);
assert.strictEqual(result1, 42);
column.columnFilter.filterState++; // Изменение состояния фильтрации
const result2 = column.cached('testMetric', computeFn);
assert.strictEqual(result2, 42);
assert.strictEqual(computeCount, 2); // Пересчитано после изменения filterState
});
it('should apply filtering correctly', () => {
column.filterRowsBy(v => v % 2 === 0); // Фильтруем четные
assert.deepStrictEqual(column.values, [1, 3, 5]); // Нечетные остались
});
it('should not modify original _values array', () => {
column.filterRowsBy(v => v > 2);
assert.deepStrictEqual(column._values, [1, 2, 3, 4, 5]); // Исходный массив неизменен
});
it('should return unfiltered values if isFiltered is false', () => {
const column = new Column([1, 2, 3, 4, 5]);
assert.deepStrictEqual(column.values, [1, 2, 3, 4, 5]);
});
it('should clone column with filtered values', () => {
const column = new Column([1, 2, 3, 4, 5]);
column.filterRows([1, 2, 3]);
const cloned = column.clone(true);
assert.deepStrictEqual(cloned.values, [1, 5]);
assert.deepStrictEqual(cloned._values, [1, 5]); // Клонируются текущие значения
});
it('should clone column with original values', () => {
const column = new Column([1, 2, 3, 4, 5]);
column.filterRows([1, 2, 3]);
const cloned = column.clone(false);
assert.deepStrictEqual(cloned.values, [1, 2, 3, 4, 5]);
assert.deepStrictEqual(cloned._values, [1, 2, 3, 4, 5]);
});
it('should filter rows using filterRowsBy', () => {
const column = new Column([1, 2, 3, 4, 5]);
column.filterRowsBy(v => v > 3);
assert.deepStrictEqual(column.values, [1, 2, 3]);
});
it('should clear specific filters using clearRowsFilters', () => {
const column = new Column([1, 2, 3, 4, 5]);
column.filterRows([0, 1, 2, 3]); // Фильтруем 0, 1, 2, 3
column.clearRowsFilters([1, 2]); // Очищаем фильтры 1 и 2
assert.deepStrictEqual(column.values, [2, 3, 5]);
});
it('should clear all filters using clearAllRowsFilters', () => {
const column = new Column([1, 2, 3, 4, 5]);
column.filterRows([0, 1, 2]);
column.clearAllRowsFilters();
assert.deepStrictEqual(column.values, [1, 2, 3, 4, 5]);
});
it('should filter rows using filterRowsBy', () => {
const column = new Column([1, 2, 3, 4, 5]);
column.filterRowsBy(v => v > 3); // Исключаем элементы > 3
assert.deepStrictEqual(column.values, [1, 2, 3]);
});
it('should handle empty values in sorted', () => {
const column = new Column([1, 2, 3]);
column.filterRows([0, 1, 2]); // Фильтруем все
assert.deepStrictEqual(column.sorted, []);
});
it('should return unfiltered values after clearing filter', () => {
const column = new Column([1, 2, 3, 4, 5]);
column.filterRows([1, 2]);
column.clearAllRowsFilters();
assert.deepStrictEqual(column.values, [1, 2, 3, 4, 5]);
});
it('should return empty sorted array when all values filtered', () => {
const column = new Column([1, 2, 3]);
column.filterRows([0, 1, 2]);
assert.deepStrictEqual(column.sorted, []);
});
it('should return min for percentile 0', () => {
const column = new Column([5, 2, 8, 1, 9]);
assert.strictEqual(column.percentile(0), 1);
});
it('should return max for percentile 100', () => {
const column = new Column([5, 2, 8, 1, 9]);
assert.strictEqual(column.percentile(100), 9);
});
});