December-software-project/sort-algo

View on GitHub
src/__tests__/visualizer/algorithm/sortingalgorithms/countingSort.test.js

Summary

Maintainability
A
0 mins
Test Coverage
import countingSort from '../../../../visualizer/algorithm/sortingalgorithms/countingSort';
import { assertSortSuccess } from '../../../../_testutil/TestUtil';
import {
  ARRAY_DUPLICATES,
  ARRAY_EXPECTED_RANDOM_POSITIVE,
  ARRAY_EXPECTED_STRICTLY_DESCENDING,
  ARRAY_RANDOM_POSITIVE,
  ARRAY_STRICTLY_ASCENDING,
  ARRAY_STRICTLY_DESCENDING,
} from '../../../../_testutil/ArraysUtil';

test('Already Sorted with duplicates', () => {
  assertCountSortSuccess(ARRAY_DUPLICATES, ARRAY_DUPLICATES);
});

test('Already Sorted ascending', () => {
  assertCountSortSuccess(ARRAY_STRICTLY_ASCENDING, ARRAY_STRICTLY_ASCENDING);
});

test('Descending array', () => {
  assertCountSortSuccess(ARRAY_STRICTLY_DESCENDING, ARRAY_EXPECTED_STRICTLY_DESCENDING);
});

test('Random array positive numbers', () => {
  assertCountSortSuccess(ARRAY_RANDOM_POSITIVE, ARRAY_EXPECTED_RANDOM_POSITIVE);
});

const assertCountSortSuccess = (initialArray, sortedArray) => {
  const appendedArray = generateAppendedArray(sortedArray);
  const expected = initialArray.concat(appendedArray);
  assertSortSuccess(initialArray, initialArray, expected, countingSort);
};

const generateAppendedArray = (arr) => {
  let id = -1;
  return arr.map((obj) => ({
    id: (id += 1),
    height: obj.height,
    isShown: true,
  }));
};