ArrayUtils.spec.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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('fillWithRange', () => {
  50. const valueFunction: (index: number) => string = (index: number) => `foo${index}`;
  51. describe('range length more than 0', () => {
  52. const rangeLength: number = 5;
  53. const expectedArray: string[] = [
  54. 'foo0',
  55. 'foo1',
  56. 'foo2',
  57. 'foo3',
  58. 'foo4',
  59. ];
  60. let array: string[];
  61. before(() => {
  62. array = arrayUtils.fillWithRange(rangeLength, valueFunction);
  63. });
  64. it('should return array with range of strings', () => {
  65. assert.deepEqual(array, expectedArray);
  66. });
  67. });
  68. describe('range length is 0', () => {
  69. const rangeLength: number = 0;
  70. const expectedArray: string[] = [];
  71. let array: string[];
  72. before(() => {
  73. array = arrayUtils.fillWithRange(rangeLength, valueFunction);
  74. });
  75. it('should return empty array', () => {
  76. assert.deepEqual(array, expectedArray);
  77. });
  78. });
  79. describe('range length less than 0', () => {
  80. const rangeLength: number = -5;
  81. const expectedArray: string[] = [];
  82. let array: string[];
  83. before(() => {
  84. array = arrayUtils.fillWithRange(rangeLength, valueFunction);
  85. });
  86. it('should return empty array', () => {
  87. assert.deepEqual(array, expectedArray);
  88. });
  89. });
  90. });
  91. describe('findMostOccurringElement', () => {
  92. describe('empty array', () => {
  93. const array: string[] = [];
  94. const expectedMostOccurringElement: null = null;
  95. let mostOccurringElement: string | null;
  96. before(() => {
  97. mostOccurringElement = arrayUtils.findMostOccurringElement(array);
  98. });
  99. it('should return null if array is empty', () => {
  100. assert.equal(mostOccurringElement, expectedMostOccurringElement);
  101. });
  102. });
  103. describe('one elements is most occurring', () => {
  104. const array: string[] = ['foo', 'bar', 'bar', 'baz', 'bar', 'foo'];
  105. const expectedMostOccurringElement: string = 'bar';
  106. let mostOccurringElement: string | null;
  107. before(() => {
  108. mostOccurringElement = arrayUtils.findMostOccurringElement(array);
  109. });
  110. it('should return most occurring element', () => {
  111. assert.equal(mostOccurringElement, expectedMostOccurringElement);
  112. });
  113. });
  114. describe('few elements are most occurring', () => {
  115. const array: string[] = ['foo', 'bar', 'bar', 'baz', 'bar'];
  116. const expectedMostOccurringElement: string = 'bar';
  117. let mostOccurringElement: string | null;
  118. before(() => {
  119. mostOccurringElement = arrayUtils.findMostOccurringElement(array);
  120. });
  121. it('should return first most occurring element', () => {
  122. assert.equal(mostOccurringElement, expectedMostOccurringElement);
  123. });
  124. });
  125. });
  126. describe('getLastElement', () => {
  127. describe('empty array', () => {
  128. const array: string[] = [];
  129. const expectedLastElement: undefined = undefined;
  130. let lastElement: string | undefined;
  131. before(() => {
  132. lastElement = arrayUtils.getLastElement(array);
  133. });
  134. it('should return undefined if array is empty', () => {
  135. assert.equal(lastElement, expectedLastElement);
  136. });
  137. });
  138. describe('array length: `1`', () => {
  139. const array: string[] = ['foo'];
  140. const expectedLastElement: string = 'foo';
  141. let lastElement: string | undefined;
  142. before(() => {
  143. lastElement = arrayUtils.getLastElement(array);
  144. });
  145. it('should return first element for array with length: `1`', () => {
  146. assert.equal(lastElement, expectedLastElement);
  147. });
  148. });
  149. describe('array length: `3`', () => {
  150. const array: string[] = ['foo', 'bar', 'baz'];
  151. const expectedLastElement: string = 'baz';
  152. let lastElement: string | undefined;
  153. before(() => {
  154. lastElement = arrayUtils.getLastElement(array);
  155. });
  156. it('should return last element for array with length: `3`', () => {
  157. assert.equal(lastElement, expectedLastElement);
  158. });
  159. });
  160. });
  161. describe('rotate', () => {
  162. let array: number[],
  163. rotatedArray: number[];
  164. beforeEach(() => {
  165. array = [1, 2, 3, 4, 5, 6];
  166. });
  167. describe('value is not 0', () => {
  168. const rotateValue: number = 2;
  169. const expectedArray: number[] = [5, 6, 1, 2, 3, 4];
  170. beforeEach(() => {
  171. rotatedArray = arrayUtils.rotate(array, rotateValue);
  172. });
  173. it('should rotate (shift) array by a given value', () => {
  174. assert.deepEqual(rotatedArray, expectedArray);
  175. });
  176. });
  177. describe('value equals or less 0', () => {
  178. const rotateValue: number = 0;
  179. const expectedArray: number[] = [1, 2, 3, 4, 5, 6];
  180. beforeEach(() => {
  181. rotatedArray = arrayUtils.rotate(array, rotateValue);
  182. });
  183. it('shouldn\'t rotate array', () => {
  184. assert.deepEqual(rotatedArray, expectedArray);
  185. });
  186. });
  187. describe('empty array', () => {
  188. const emptyArray: number[] = [];
  189. const rotateValue: number = 5;
  190. const expectedError: ReferenceErrorConstructor = ReferenceError;
  191. let testFunc: () => void;
  192. beforeEach(() => {
  193. testFunc = () => arrayUtils.rotate(emptyArray, rotateValue);
  194. });
  195. it('should throw exception if array is empty', () => {
  196. assert.throws(testFunc, expectedError);
  197. });
  198. });
  199. });
  200. });