123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- 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([43,20,15]);
- });
- 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]);
- });
|