瀏覽代碼

Add unit tests for the Stack

ApisNecros 1 年之前
父節點
當前提交
b7d50d839c
共有 1 個文件被更改,包括 106 次插入0 次删除
  1. 106 0
      tests/stack.test.js

+ 106 - 0
tests/stack.test.js

@@ -0,0 +1,106 @@
+import { Stack } from "../dist/main.js";
+
+test("Initialize an empty stack", () => {
+    const stack = new Stack();
+    expect(stack.values()).toEqual([]);
+});
+
+test("Initialize a stack with array of numbers", () => {
+    const stack = new Stack([1,2,3]);
+    expect(stack.values()).toEqual([1,2,3]);
+});
+
+test("Initialize a stack with string of comma-separated numbers", () => {
+    const stack = new Stack("1,2,3");
+    expect(stack.values()).toEqual([1,2,3]);
+});
+
+test("Initialize a stack with string of space-separated numbers", () => {
+    const stack = new Stack("1 2 3");
+    expect(stack.values()).toEqual([1,2,3]);
+});
+
+test("Initialize a stack with 1 string", () => {
+    const stack = new Stack('"whodey"');
+    expect(stack.values()).toEqual([119,104,111,100,101,121]);
+});
+
+test("Initialize a stack with 2 strings", () => {
+    const stack = new Stack('"Hello, World!" "whodey"');
+    expect(stack.values()).toEqual([72,101,108,108,111,44,32,87,111,114,108,100,33,119,104,111,100,101,121]);
+});
+
+test("Initialize a stack with string, number, string", () => {
+    const stack = new Stack('"Hello, World!" 32 "whodey"');
+    expect(stack.values()).toEqual([72,101,108,108,111,44,32,87,111,114,108,100,33,32,119,104,111,100,101,121]);
+});
+
+test("Push a value onto the stack", () => {
+    const stack = new Stack([]);
+    stack.push(32);
+    expect(stack.values()).toEqual([32]);
+});
+
+test("Push many values onto the stack", () => {
+    const stack = new Stack([12]);
+    stack.push([13,14]);
+    expect(stack.values()).toEqual([12,13,14]);
+});
+
+test("Pop a value off the stack", () => {
+    const stack = new Stack([42]);
+    expect(stack.pop()).toBe(42);
+});
+
+test("Pop multiple values off the stack", () => {
+    const stack = new Stack([42, 15, 20, 43]);
+    expect(stack.popMany(3)).toEqual([15,20,43]);
+});
+
+test("Shift stack left", () => {
+    const stack = new Stack([42, 15, 20, 43]);
+    stack.shiftLeft();
+    expect(stack.values()).toEqual([15,20,43,42]);
+});
+
+test("Shift stack Right", () => {
+    const stack = new Stack([42, 15, 20, 43]);
+    stack.shiftRight();
+    expect(stack.values()).toEqual([43,42,15,20]);
+});
+
+test("Swap top 2 values on the stack", () => {
+    const stack = new Stack([42, 15, 20, 43]);
+    stack.swapTwo();
+    expect(stack.values()).toEqual([42,15,43,20]);
+});
+
+test("Swap top 3 values on the stack", () => {
+    const stack = new Stack([42, 15, 20, 43]);
+    stack.swapThree();
+    expect(stack.values()).toEqual([42,43,15,20]);
+});
+
+test("Duplicate top value on stack", () => {
+    const stack = new Stack([42, 15, 20, 43]);
+    stack.duplicate();
+    expect(stack.values()).toEqual([42,15,20,43,43]);
+});
+
+test("Remove top value on stack", () => {
+    const stack = new Stack([42, 15, 20, 43]);
+    stack.remove();
+    expect(stack.values()).toEqual([42,15,20]);
+});
+
+test("Reverse the stack", () => {
+    const stack = new Stack([42, 15, 20, 43]);
+    stack.reverse();
+    expect(stack.values()).toEqual([43,20,15,42]);
+});
+
+test("Push the length of the stack onto the top", () => {
+    const stack = new Stack([42, 15, 20, 43]);
+    stack.pushLength();
+    expect(stack.values()).toEqual([42,15,20,43,4]);
+});