array-utils-test.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { range } from 'lodash';
  2. function testNotMutatingArray(f, array, ...otherArgs) {
  3. const original = array.slice();
  4. f(array, ...otherArgs);
  5. expect(array).toEqual(original);
  6. }
  7. describe('ArrayUtils', () => {
  8. const ArrayUtils = require('../array-utils');
  9. describe('uniformSelect', () => {
  10. const f = ArrayUtils.uniformSelect;
  11. it('it should select the array elements uniformly, including the endpoints', () => {
  12. testNotMutatingArray(f, ['A', 'B', 'C', 'D', 'E'], 3);
  13. {
  14. const arr = ['x', 'y'];
  15. expect(f(arr, 3)).toEqual(['x', 'y']);
  16. expect(f(arr, 2)).toEqual(['x', 'y']);
  17. }
  18. {
  19. const arr = ['A', 'B', 'C', 'D', 'E'];
  20. expect(f(arr, 6)).toEqual(['A', 'B', 'C', 'D', 'E']);
  21. expect(f(arr, 5)).toEqual(['A', 'B', 'C', 'D', 'E']);
  22. expect(f(arr, 4)).toEqual(['A', 'B', 'D', 'E']);
  23. expect(f(arr, 3)).toEqual(['A', 'C', 'E']);
  24. expect(f(arr, 2)).toEqual(['A', 'E']);
  25. }
  26. {
  27. const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
  28. expect(f(arr, 12)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
  29. expect(f(arr, 11)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
  30. expect(f(arr, 10)).toEqual([1, 2, 3, 4, 5, 7, 8, 9, 10, 11]);
  31. expect(f(arr, 9)).toEqual([1, 2, 3, 5, 6, 7, 9, 10, 11]);
  32. expect(f(arr, 8)).toEqual([1, 2, 4, 5, 7, 8, 10, 11]);
  33. expect(f(arr, 7)).toEqual([1, 2, 4, 6, 8, 10, 11]);
  34. expect(f(arr, 6)).toEqual([1, 3, 5, 7, 9, 11]);
  35. expect(f(arr, 5)).toEqual([1, 3, 6, 9, 11]);
  36. expect(f(arr, 4)).toEqual([1, 4, 8, 11]);
  37. expect(f(arr, 3)).toEqual([1, 6, 11]);
  38. expect(f(arr, 2)).toEqual([1, 11]);
  39. }
  40. {
  41. const arr = range(1, 10001);
  42. expect(f(arr, 4)).toEqual([1, 3334, 6667, 10000]);
  43. expect(f(arr, 3)).toEqual([1, 5000, 10000]);
  44. expect(f(arr, 2)).toEqual([1, 10000]);
  45. }
  46. });
  47. });
  48. describe('insertElement', () => {
  49. const f = ArrayUtils.insertElement;
  50. it('it should insert an element into the array at the specified index', () => {
  51. testNotMutatingArray(f, ['x', 'y', 'z'], 0, 'a');
  52. expect(f(['x', 'y', 'z'], 0, 'a')).toEqual(['a', 'x', 'y', 'z']);
  53. expect(f(['x', 'y', 'z'], 1, 'a')).toEqual(['x', 'a', 'y', 'z']);
  54. expect(f(['x', 'y', 'z'], 2, 'a')).toEqual(['x', 'y', 'a', 'z']);
  55. expect(f(['x', 'y', 'z'], 3, 'a')).toEqual(['x', 'y', 'z', 'a']);
  56. });
  57. });
  58. describe('removeElement', () => {
  59. const f = ArrayUtils.removeElement;
  60. it('it should remove the element at the specified index from the array', () => {
  61. testNotMutatingArray(f, ['x', 'y', 'z'], 0);
  62. expect(f(['x', 'y', 'z'], 0)).toEqual(['y', 'z']);
  63. expect(f(['x', 'y', 'z'], 1)).toEqual(['x', 'z']);
  64. expect(f(['x', 'y', 'z'], 2)).toEqual(['x', 'y']);
  65. });
  66. });
  67. describe('moveElement', () => {
  68. const f = ArrayUtils.moveElement;
  69. it('it should move an array element, modifying the array', () => {
  70. testNotMutatingArray(f, ['x', 'y', 'z'], 0, 1);
  71. expect(f(['x', 'y', 'z'], 0, 1)).toEqual(['y', 'x', 'z']);
  72. expect(f(['x', 'y', 'z'], 1, 0)).toEqual(['y', 'x', 'z']);
  73. expect(f(['x', 'y', 'z'], 0, 2)).toEqual(['y', 'z', 'x']);
  74. expect(f(['x', 'y', 'z'], 2, 0)).toEqual(['z', 'x', 'y']);
  75. expect(f(['x', 'y', 'z'], 1, 2)).toEqual(['x', 'z', 'y']);
  76. expect(f(['x', 'y', 'z'], 2, 1)).toEqual(['x', 'z', 'y']);
  77. expect(f(['x', 'y', 'z'], 0, 0)).toEqual(['x', 'y', 'z']);
  78. expect(f(['x', 'y', 'z'], 1, 1)).toEqual(['x', 'y', 'z']);
  79. expect(f(['x', 'y', 'z'], 2, 2)).toEqual(['x', 'y', 'z']);
  80. expect(f(['a', 'b', 'c', 'd', 'e'], 4, 1)).toEqual(['a', 'e', 'b', 'c', 'd']);
  81. expect(f(['a', 'b', 'c', 'd', 'e'], 1, 4)).toEqual(['a', 'c', 'd', 'e', 'b']);
  82. expect(f(['a', 'b', 'c', 'd', 'e'], 1, 3)).toEqual(['a', 'c', 'd', 'b', 'e']);
  83. });
  84. });
  85. });