zindex.test.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { moveOneLeft, moveOneRight, moveAllLeft, moveAllRight } from "./zindex";
  2. function expectMove<T>(
  3. fn: (elements: T[], indicesToMove: number[]) => void,
  4. elems: T[],
  5. indices: number[],
  6. equal: T[]
  7. ) {
  8. fn(elems, indices);
  9. expect(elems).toEqual(equal);
  10. }
  11. it("should moveOneLeft", () => {
  12. expectMove(moveOneLeft, ["a", "b", "c", "d"], [1, 2], ["b", "c", "a", "d"]);
  13. expectMove(moveOneLeft, ["a", "b", "c", "d"], [0], ["a", "b", "c", "d"]);
  14. expectMove(
  15. moveOneLeft,
  16. ["a", "b", "c", "d"],
  17. [0, 1, 2, 3],
  18. ["a", "b", "c", "d"]
  19. );
  20. expectMove(moveOneLeft, ["a", "b", "c", "d"], [1, 3], ["b", "a", "d", "c"]);
  21. });
  22. it("should moveOneRight", () => {
  23. expectMove(moveOneRight, ["a", "b", "c", "d"], [1, 2], ["a", "d", "b", "c"]);
  24. expectMove(moveOneRight, ["a", "b", "c", "d"], [3], ["a", "b", "c", "d"]);
  25. expectMove(
  26. moveOneRight,
  27. ["a", "b", "c", "d"],
  28. [0, 1, 2, 3],
  29. ["a", "b", "c", "d"]
  30. );
  31. expectMove(moveOneRight, ["a", "b", "c", "d"], [0, 2], ["b", "a", "d", "c"]);
  32. });
  33. it("should moveAllLeft", () => {
  34. expectMove(
  35. moveAllLeft,
  36. ["a", "b", "c", "d", "e", "f", "g"],
  37. [2, 5],
  38. ["c", "f", "a", "b", "d", "e", "g"]
  39. );
  40. expectMove(
  41. moveAllLeft,
  42. ["a", "b", "c", "d", "e", "f", "g"],
  43. [5],
  44. ["f", "a", "b", "c", "d", "e", "g"]
  45. );
  46. expectMove(
  47. moveAllLeft,
  48. ["a", "b", "c", "d", "e", "f", "g"],
  49. [0, 1, 2, 3, 4, 5, 6],
  50. ["a", "b", "c", "d", "e", "f", "g"]
  51. );
  52. expectMove(
  53. moveAllLeft,
  54. ["a", "b", "c", "d", "e", "f", "g"],
  55. [0, 1, 2],
  56. ["a", "b", "c", "d", "e", "f", "g"]
  57. );
  58. expectMove(
  59. moveAllLeft,
  60. ["a", "b", "c", "d", "e", "f", "g"],
  61. [4, 5, 6],
  62. ["e", "f", "g", "a", "b", "c", "d"]
  63. );
  64. });
  65. it("should moveAllRight", () => {
  66. expectMove(
  67. moveAllRight,
  68. ["a", "b", "c", "d", "e", "f", "g"],
  69. [2, 5],
  70. ["a", "b", "d", "e", "g", "c", "f"]
  71. );
  72. expectMove(
  73. moveAllRight,
  74. ["a", "b", "c", "d", "e", "f", "g"],
  75. [5],
  76. ["a", "b", "c", "d", "e", "g", "f"]
  77. );
  78. expectMove(
  79. moveAllRight,
  80. ["a", "b", "c", "d", "e", "f", "g"],
  81. [0, 1, 2, 3, 4, 5, 6],
  82. ["a", "b", "c", "d", "e", "f", "g"]
  83. );
  84. expectMove(
  85. moveAllRight,
  86. ["a", "b", "c", "d", "e", "f", "g"],
  87. [0, 1, 2],
  88. ["d", "e", "f", "g", "a", "b", "c"]
  89. );
  90. expectMove(
  91. moveAllRight,
  92. ["a", "b", "c", "d", "e", "f", "g"],
  93. [4, 5, 6],
  94. ["a", "b", "c", "d", "e", "f", "g"]
  95. );
  96. });