ArrayUtils.spec.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import 'reflect-metadata';
  2. import { assert } from 'chai';
  3. import { ServiceIdentifiers } from '../../../src/container/ServiceIdentifiers';
  4. import { IArrayUtils } from '../../../src/interfaces/utils/IArrayUtils';
  5. import { IInversifyContainerFacade } from '../../../src/interfaces/container/IInversifyContainerFacade';
  6. import { InversifyContainerFacade } from '../../../src/container/InversifyContainerFacade';
  7. describe('ArrayUtils', () => {
  8. let arrayUtils: IArrayUtils;
  9. before(() => {
  10. const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
  11. inversifyContainerFacade.load('', '', {});
  12. arrayUtils = inversifyContainerFacade.get<IArrayUtils>(ServiceIdentifiers.IArrayUtils);
  13. });
  14. describe('createWithRange', () => {
  15. describe('range length more than 0', () => {
  16. const rangeLength: number = 5;
  17. const expectedArray: number[] = [0, 1, 2, 3, 4];
  18. let array: number[];
  19. before(() => {
  20. array = arrayUtils.createWithRange(rangeLength);
  21. });
  22. it('should return array with range of numbers', () => {
  23. assert.deepEqual(array, expectedArray);
  24. });
  25. });
  26. describe('range length is 0', () => {
  27. const rangeLength: number = 0;
  28. const expectedArray: number[] = [];
  29. let array: number[];
  30. before(() => {
  31. array = arrayUtils.createWithRange(rangeLength);
  32. });
  33. it('should return empty array', () => {
  34. assert.deepEqual(array, expectedArray);
  35. });
  36. });
  37. describe('range length less than 0', () => {
  38. const rangeLength: number = -5;
  39. const expectedArray: number[] = [];
  40. let array: number[];
  41. before(() => {
  42. array = arrayUtils.createWithRange(rangeLength);
  43. });
  44. it('should return empty array', () => {
  45. assert.deepEqual(array, expectedArray);
  46. });
  47. });
  48. });
  49. describe('findMostOccurringElement', () => {
  50. describe('empty array', () => {
  51. const array: string[] = [];
  52. const expectedMostOccurringElement: null = null;
  53. let mostOccurringElement: string | null;
  54. before(() => {
  55. mostOccurringElement = arrayUtils.findMostOccurringElement(array);
  56. });
  57. it('should return null if array is empty', () => {
  58. assert.equal(mostOccurringElement, expectedMostOccurringElement);
  59. });
  60. });
  61. describe('one elements is most occurring', () => {
  62. const array: string[] = ['foo', 'bar', 'bar', 'baz', 'bar', 'foo'];
  63. const expectedMostOccurringElement: string = 'bar';
  64. let mostOccurringElement: string | null;
  65. before(() => {
  66. mostOccurringElement = arrayUtils.findMostOccurringElement(array);
  67. });
  68. it('should return most occurring element', () => {
  69. assert.equal(mostOccurringElement, expectedMostOccurringElement);
  70. });
  71. });
  72. describe('few elements are most occurring', () => {
  73. const array: string[] = ['foo', 'bar', 'bar', 'baz', 'bar'];
  74. const expectedMostOccurringElement: string = 'bar';
  75. let mostOccurringElement: string | null;
  76. before(() => {
  77. mostOccurringElement = arrayUtils.findMostOccurringElement(array);
  78. });
  79. it('should return first most occurring element', () => {
  80. assert.equal(mostOccurringElement, expectedMostOccurringElement);
  81. });
  82. });
  83. });
  84. describe('rotate', () => {
  85. let array: number[],
  86. rotatedArray: number[];
  87. beforeEach(() => {
  88. array = [1, 2, 3, 4, 5, 6];
  89. });
  90. describe('value is not 0', () => {
  91. const rotateValue: number = 2;
  92. const expectedArray: number[] = [5, 6, 1, 2, 3, 4];
  93. beforeEach(() => {
  94. rotatedArray = arrayUtils.rotate(array, rotateValue);
  95. });
  96. it('should rotate (shift) array by a given value', () => {
  97. assert.deepEqual(rotatedArray, expectedArray);
  98. });
  99. });
  100. describe('value equals or less 0', () => {
  101. const rotateValue: number = 0;
  102. const expectedArray: number[] = [1, 2, 3, 4, 5, 6];
  103. beforeEach(() => {
  104. rotatedArray = arrayUtils.rotate(array, rotateValue);
  105. });
  106. it('shouldn\'t rotate array', () => {
  107. assert.deepEqual(rotatedArray, expectedArray);
  108. });
  109. });
  110. describe('empty array', () => {
  111. const emptyArray: number[] = [];
  112. const rotateValue: number = 5;
  113. const expectedError: ReferenceErrorConstructor = ReferenceError;
  114. let testFunc: () => void;
  115. beforeEach(() => {
  116. testFunc = () => arrayUtils.rotate(emptyArray, rotateValue);
  117. });
  118. it('should throw exception if array is empty', () => {
  119. assert.throws(testFunc, expectedError);
  120. });
  121. });
  122. });
  123. });