Skip to content

Commit

Permalink
test(violin): 修复单测问题 & 增加单测覆盖
Browse files Browse the repository at this point in the history
  • Loading branch information
visiky committed May 30, 2021
1 parent ed76a4c commit abde4da
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 32 deletions.
32 changes: 3 additions & 29 deletions __tests__/unit/plots/violin/box-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ describe('violin', () => {
violin.destroy();
});

it("should not render box views when 'box' set to false.", () => {
// 暂时不开放 box 配置
it.skip("should not render box views when 'box' set to false.", () => {
const violin = new Violin(createDiv(), {
width: 400,
height: 500,
data: BASE_VIOLIN_DATA,
xField: 'type',
yField: 'value',
box: false,
// box: false,
});

violin.render();
Expand All @@ -46,31 +47,4 @@ describe('violin', () => {

violin.destroy();
});

it('should not render box with custom textMap.', () => {
const textMap = {
max: '最大值',
min: '最小值',
median: '中位值',
q1: '上四分位点',
q3: '下四分位点',
};
const violin = new Violin(createDiv(), {
width: 400,
height: 500,
data: BASE_VIOLIN_DATA,
xField: 'type',
yField: 'value',
box: {
textMap,
},
});

violin.render();

// @ts-ignore
expect(violin.options.box.textMap).toEqual(textMap);

violin.destroy();
});
});
50 changes: 50 additions & 0 deletions __tests__/unit/utils/transform/quantile-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { quantile, quantileSorted, quickselect, swap } from '../../../../src/utils/transform/quantile';

describe('quantile', () => {
it('quantile-sorted', () => {
expect(quantileSorted([3, 6, 7, 8, 8, 9, 10, 13, 15, 16, 20], 0.5)).toBe(9);
});

it('quick-select', () => {
const arr = [65, 28, 59, 33, 21, 56, 22, 95, 50, 12, 90, 53, 28, 77, 39];
quickselect(arr, 8);
expect(arr).toEqual([39, 28, 28, 33, 21, 12, 22, 50, 53, 56, 59, 65, 90, 77, 95]);
});

it('quantile', () => {
expect(quantile([3, 6, 7, 8, 8, 9, 10, 13, 15, 16, 20], 0.5)).toBe(9); // => 9

// from https://github.com/simple-statistics
const even = [3, 6, 7, 8, 8, 10, 13, 15, 16, 20];
expect(quantile(even, 0.25)).toBe(7);
expect(quantile(even, 0.5)).toBe(9);
expect(quantile(even, 0.75)).toBe(15);

const odd = [3, 6, 7, 8, 8, 9, 10, 13, 15, 16, 20];
expect(quantile(odd, 0.25)).toBe(7);
expect(quantile(odd, 0.5)).toBe(9);
expect(quantile(odd, 0.75)).toBe(15);
});

it('quantile: throw error', () => {
const testEmptyArray = () => quantile([], 0.5);
expect(testEmptyArray).toThrowError();

const testBadBounds = () => quantile([1, 2, 3], 1.1);
expect(testBadBounds).toThrowError();
});

it('quantile: can get an array of quantiles on a small number of elements', () => {
const input = [500, 468, 454, 469];
expect(quantile(input, [0.25, 0.5, 0.75])).toEqual([461, 468.5, 484.5]);
expect(quantile(input, [0.05, 0.25, 0.5, 0.75, 0.95])).toEqual([454, 461, 468.5, 484.5, 500]);
});

it('swap', () => {
const arr = [1, 2, 3];
swap(arr, 0, 2);
expect(arr).toEqual([3, 2, 1]);
swap(arr, 0, 2);
expect(arr).toEqual([1, 2, 3]);
});
});
12 changes: 9 additions & 3 deletions src/utils/transform/quantile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @example
* quantileSorted([3, 6, 7, 8, 8, 9, 10, 13, 15, 16, 20], 0.5); // => 9
*/
function quantileSorted(x, p) {
export function quantileSorted(x: number[], p: number) {
const idx = x.length * p;
if (x.length === 0) {
throw new Error('quantile requires at least one data point.');
Expand All @@ -39,7 +39,13 @@ function quantileSorted(x, p) {
}
}

function swap(arr, i, j) {
/**
* 交换数组位置
* @param arr T[]
* @param i number
* @param j number
*/
export function swap<T = any>(arr: T[], i: number, j: number): void {
const tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
Expand All @@ -61,7 +67,7 @@ function swap(arr, i, j) {
* quickselect(arr, 8);
* // = [39, 28, 28, 33, 21, 12, 22, 50, 53, 56, 59, 65, 90, 77, 95]
*/
function quickselect(arr, k, left, right) {
export function quickselect(arr: number[], k, left?: number, right?: number): void {
left = left || 0;
right = right || arr.length - 1;

Expand Down

0 comments on commit abde4da

Please sign in to comment.