stack.test.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { Stack } from "../dist/main.js";
  2. test("Initialize an empty stack", () => {
  3. const stack = new Stack();
  4. expect(stack.values()).toEqual([]);
  5. });
  6. test("Initialize a stack with array of numbers", () => {
  7. const stack = new Stack([1,2,3]);
  8. expect(stack.values()).toEqual([1,2,3]);
  9. });
  10. test("Initialize a stack with string of comma-separated numbers", () => {
  11. const stack = new Stack("1,2,3");
  12. expect(stack.values()).toEqual([1,2,3]);
  13. });
  14. test("Initialize a stack with string of space-separated numbers", () => {
  15. const stack = new Stack("1 2 3");
  16. expect(stack.values()).toEqual([1,2,3]);
  17. });
  18. test("Initialize a stack with 1 string", () => {
  19. const stack = new Stack('"whodey"');
  20. expect(stack.values()).toEqual([119,104,111,100,101,121]);
  21. });
  22. test("Initialize a stack with 2 strings", () => {
  23. const stack = new Stack('"Hello, World!" "whodey"');
  24. expect(stack.values()).toEqual([72,101,108,108,111,44,32,87,111,114,108,100,33,119,104,111,100,101,121]);
  25. });
  26. test("Initialize a stack with string, number, string", () => {
  27. const stack = new Stack('"Hello, World!" 32 "whodey"');
  28. expect(stack.values()).toEqual([72,101,108,108,111,44,32,87,111,114,108,100,33,32,119,104,111,100,101,121]);
  29. });
  30. test("Push a value onto the stack", () => {
  31. const stack = new Stack([]);
  32. stack.push(32);
  33. expect(stack.values()).toEqual([32]);
  34. });
  35. test("Push many values onto the stack", () => {
  36. const stack = new Stack([12]);
  37. stack.push([13,14]);
  38. expect(stack.values()).toEqual([12,13,14]);
  39. });
  40. test("Pop a value off the stack", () => {
  41. const stack = new Stack([42]);
  42. expect(stack.pop()).toBe(42);
  43. });
  44. test("Pop multiple values off the stack", () => {
  45. const stack = new Stack([42, 15, 20, 43]);
  46. expect(stack.popMany(3)).toEqual([43,20,15]);
  47. });
  48. test("Shift stack left", () => {
  49. const stack = new Stack([42, 15, 20, 43]);
  50. stack.shiftLeft();
  51. expect(stack.values()).toEqual([15,20,43,42]);
  52. });
  53. test("Shift stack Right", () => {
  54. const stack = new Stack([42, 15, 20, 43]);
  55. stack.shiftRight();
  56. expect(stack.values()).toEqual([43,42,15,20]);
  57. });
  58. test("Swap top 2 values on the stack", () => {
  59. const stack = new Stack([42, 15, 20, 43]);
  60. stack.swapTwo();
  61. expect(stack.values()).toEqual([42,15,43,20]);
  62. });
  63. test("Swap top 3 values on the stack", () => {
  64. const stack = new Stack([42, 15, 20, 43]);
  65. stack.swapThree();
  66. expect(stack.values()).toEqual([42,43,15,20]);
  67. });
  68. test("Duplicate top value on stack", () => {
  69. const stack = new Stack([42, 15, 20, 43]);
  70. stack.duplicate();
  71. expect(stack.values()).toEqual([42,15,20,43,43]);
  72. });
  73. test("Remove top value on stack", () => {
  74. const stack = new Stack([42, 15, 20, 43]);
  75. stack.remove();
  76. expect(stack.values()).toEqual([42,15,20]);
  77. });
  78. test("Reverse the stack", () => {
  79. const stack = new Stack([42, 15, 20, 43]);
  80. stack.reverse();
  81. expect(stack.values()).toEqual([43,20,15,42]);
  82. });
  83. test("Push the length of the stack onto the top", () => {
  84. const stack = new Stack([42, 15, 20, 43]);
  85. stack.pushLength();
  86. expect(stack.values()).toEqual([42,15,20,43,4]);
  87. });