als-statistics
Version:
Modular JS statistics toolkit for Node.js and the browser: descriptive stats, correlations (Pearson/Spearman/Kendall), t-tests & ANOVA (Student/Welch), reliability (Cronbach’s alpha), regression (linear/logistic), clustering (DBSCAN/HDBSCAN), and table/co
94 lines (77 loc) • 4.11 kB
JavaScript
import { describe, it } from 'node:test';
import assert from 'node:assert';
import round from '../../lib/utils/round.js'; // в примере было "range", но это именно round-функция
describe('round utility', () => {
it('returns 0 for falsy inputs (undefined, null, 0, NaN)', () => {
assert.strictEqual(round(), 0);
assert.strictEqual(round(undefined), 0);
assert.strictEqual(round(null), 0);
assert.strictEqual(round(0), 0);
assert.strictEqual(round(NaN), 0);
});
it('returns the same integer when value is already an integer', () => {
assert.strictEqual(round(5), 5);
assert.strictEqual(round(-12), -12);
});
it('rounds typical positive decimals to minimal needed places (returns string)', () => {
assert.strictEqual(round(1.2), '1.2'); // один знак, т.к. d- round(d) = 0.2
assert.strictEqual(round(1.23), '1.2'); // разница 0.23 -> n=1 => toFixed(1)
assert.strictEqual(round(1.234), '1.2'); // n=1
});
it('handles half values according to Math.round behavior (still returns string)', () => {
assert.strictEqual(round(2.5), '2.5'); // Math.round(2.5) = 3 -> n=1
assert.strictEqual(round(-1.5), '-1.5'); // Math.round(-1.5) = -1 -> n=1
});
it('handles values close to next integer (e.g., 0.9999)', () => {
const r = round(0.9999);
// Возвращается строка, и она остаётся < 1, но с минимально нужным числом знаков
assert.strictEqual(typeof r, 'string');
assert.ok(r === '1.0'); // допускаем '0.9999', '0.99990', ...
// assert.ok(r.startsWith('0.9999')); // допускаем '0.9999', '0.99990', ...
// assert.ok(Number(r) < 1); // не перепрыгивает к 1.0000
});
it('handles small positive decimals with leading zeros after decimal', () => {
assert.strictEqual(round(0.00056), '0.0006'); // 0.00056 -> n=4 -> toFixed(4)
});
it('handles small negative decimals', () => {
assert.strictEqual(round(-0.0043), '-0.004'); // n=3
});
it('caps precision by "max" and returns 0 if rounded string becomes "0.000..."', () => {
// Требуется >8 знаков после запятой, но max=5 -> "0.00000" -> Number(...) === 0 -> вернуть число 0
assert.strictEqual(round(0.000000009, 5), '0.00000');
});
it('returns string for non-integer outputs and number for integers', () => {
assert.strictEqual(typeof round(1.1), 'string');
assert.strictEqual(typeof round(2), 'number');
});
it('does not over-round when integer is passed as float representation', () => {
// Если d === Math.round(d), функция возвращает исходное число без строкового преобразования
assert.strictEqual(round(1.0), 1);
assert.strictEqual(round(-3.0), -3);
});
});
describe('Additional tests', () => {
it('round: базовые числа → не всегда number', () => {
assert.equal(typeof round(1), 'number');
assert.equal(typeof round(1.23456789), 'string');
assert.equal(typeof round(-1.20000000001), 'string');
});
it('round: null/undefined/NaN → 0 (по договорённости)', () => {
assert.equal(round(null), 0);
assert.equal(round(undefined), 0);
assert.equal(round(NaN), 0);
});
it('round: возвращает строки', () => {
const r = round(2.5);
assert.equal(typeof r, 'string');
});
it('round: сохранение целых без дрейфа', () => {
assert.equal(round(123), 123);
assert.equal(round(-42), -42);
});
it('round: точность до нужного количества знаков (автоограничение)', () => {
const v = 0.1000000000003;
const r = round(v);
assert.ok(Math.abs(r - 0.1) < 1e-9);
});
})