ArrayUtils.spec.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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('getLastElement', () => {
  85. describe('empty array', () => {
  86. const array: string[] = [];
  87. const expectedLastElement: null = null;
  88. let lastElement: string | null;
  89. before(() => {
  90. lastElement = arrayUtils.getLastElement(array);
  91. });
  92. it('should return null if array is empty', () => {
  93. assert.equal(lastElement, expectedLastElement);
  94. });
  95. });
  96. describe('array length: `1`', () => {
  97. const array: string[] = ['foo'];
  98. const expectedLastElement: string = 'foo';
  99. let lastElement: string | null;
  100. before(() => {
  101. lastElement = arrayUtils.getLastElement(array);
  102. });
  103. it('should return first element for array with length: `1`', () => {
  104. assert.equal(lastElement, expectedLastElement);
  105. });
  106. });
  107. describe('array length: `3`', () => {
  108. const array: string[] = ['foo', 'bar', 'baz'];
  109. const expectedLastElement: string = 'baz';
  110. let lastElement: string | null;
  111. before(() => {
  112. lastElement = arrayUtils.getLastElement(array);
  113. });
  114. it('should return last element for array with length: `3`', () => {
  115. assert.equal(lastElement, expectedLastElement);
  116. });
  117. });
  118. });
  119. describe('rotate', () => {
  120. let array: number[],
  121. rotatedArray: number[];
  122. beforeEach(() => {
  123. array = [1, 2, 3, 4, 5, 6];
  124. });
  125. describe('value is not 0', () => {
  126. const rotateValue: number = 2;
  127. const expectedArray: number[] = [5, 6, 1, 2, 3, 4];
  128. beforeEach(() => {
  129. rotatedArray = arrayUtils.rotate(array, rotateValue);
  130. });
  131. it('should rotate (shift) array by a given value', () => {
  132. assert.deepEqual(rotatedArray, expectedArray);
  133. });
  134. });
  135. describe('value equals or less 0', () => {
  136. const rotateValue: number = 0;
  137. const expectedArray: number[] = [1, 2, 3, 4, 5, 6];
  138. beforeEach(() => {
  139. rotatedArray = arrayUtils.rotate(array, rotateValue);
  140. });
  141. it('shouldn\'t rotate array', () => {
  142. assert.deepEqual(rotatedArray, expectedArray);
  143. });
  144. });
  145. describe('empty array', () => {
  146. const emptyArray: number[] = [];
  147. const rotateValue: number = 5;
  148. const expectedError: ReferenceErrorConstructor = ReferenceError;
  149. let testFunc: () => void;
  150. beforeEach(() => {
  151. testFunc = () => arrayUtils.rotate(emptyArray, rotateValue);
  152. });
  153. it('should throw exception if array is empty', () => {
  154. assert.throws(testFunc, expectedError);
  155. });
  156. });
  157. });
  158. });