|
| 1 | +package types |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | +) |
| 8 | + |
| 9 | +func TestStack_Push(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + name string |
| 12 | + values []string |
| 13 | + expected []string |
| 14 | + }{ |
| 15 | + {name: "single item", values: []string{"a"}, expected: []string{"a"}}, |
| 16 | + {name: "multiple items", values: []string{"a", "b", "c"}, expected: []string{"a", "b", "c"}}, |
| 17 | + } |
| 18 | + |
| 19 | + for _, tt := range tests { |
| 20 | + t.Run(tt.name, func(t *testing.T) { |
| 21 | + stack := NewStack() |
| 22 | + for _, v := range tt.values { |
| 23 | + stack.Push(v) |
| 24 | + } |
| 25 | + |
| 26 | + require.Equal(t, tt.expected, stack.stack) |
| 27 | + }) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func TestStack_Pop(t *testing.T) { |
| 32 | + tests := []struct { |
| 33 | + name string |
| 34 | + initialStack []string |
| 35 | + popResult string |
| 36 | + finalStack []string |
| 37 | + }{ |
| 38 | + {name: "pop from empty stack", initialStack: []string{}, popResult: "", finalStack: []string{}}, |
| 39 | + {name: "pop last item", initialStack: []string{"a"}, popResult: "a", finalStack: []string{}}, |
| 40 | + {name: "pop multiple items", initialStack: []string{"a", "b", "c"}, popResult: "c", finalStack: []string{"a", "b"}}, |
| 41 | + } |
| 42 | + |
| 43 | + for _, tt := range tests { |
| 44 | + t.Run(tt.name, func(t *testing.T) { |
| 45 | + stack := &Stack{stack: tt.initialStack} |
| 46 | + result := stack.Pop() |
| 47 | + |
| 48 | + require.Equal(t, tt.popResult, result) |
| 49 | + require.Equal(t, tt.finalStack, stack.stack) |
| 50 | + }) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestStack_PopMany(t *testing.T) { |
| 55 | + tests := []struct { |
| 56 | + name string |
| 57 | + initialStack []string |
| 58 | + count int |
| 59 | + popResult []string |
| 60 | + finalStack []string |
| 61 | + }{ |
| 62 | + {name: "pop many on empty stack", initialStack: []string{}, count: 3, popResult: []string{}, finalStack: []string{}}, |
| 63 | + {name: "pop 0 items", initialStack: []string{"a", "b", "c"}, count: 0, popResult: nil, finalStack: []string{"a", "b", "c"}}, |
| 64 | + {name: "pop less than stack size", initialStack: []string{"a", "b", "c"}, count: 2, popResult: []string{"b", "c"}, finalStack: []string{"a"}}, |
| 65 | + {name: "pop all items", initialStack: []string{"a", "b", "c"}, count: 3, popResult: []string{"a", "b", "c"}, finalStack: []string{}}, |
| 66 | + {name: "pop more than available", initialStack: []string{"a", "b"}, count: 5, popResult: []string{"a", "b"}, finalStack: []string{}}, |
| 67 | + } |
| 68 | + |
| 69 | + for _, tt := range tests { |
| 70 | + t.Run(tt.name, func(t *testing.T) { |
| 71 | + stack := &Stack{stack: tt.initialStack} |
| 72 | + result := stack.PopMany(tt.count) |
| 73 | + |
| 74 | + if tt.popResult == nil { |
| 75 | + require.Nil(t, result) |
| 76 | + } else { |
| 77 | + require.Equal(t, tt.popResult, result) |
| 78 | + } |
| 79 | + require.Equal(t, tt.finalStack, stack.stack) |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func TestStack_Peek(t *testing.T) { |
| 85 | + tests := []struct { |
| 86 | + name string |
| 87 | + initialStack []string |
| 88 | + expected string |
| 89 | + finalStack []string |
| 90 | + }{ |
| 91 | + {name: "peek on empty stack", initialStack: []string{}, expected: "", finalStack: []string{}}, |
| 92 | + {name: "peek single item", initialStack: []string{"a"}, expected: "a", finalStack: []string{"a"}}, |
| 93 | + {name: "peek multiple items", initialStack: []string{"a", "b", "c"}, expected: "c", finalStack: []string{"a", "b", "c"}}, |
| 94 | + } |
| 95 | + |
| 96 | + for _, tt := range tests { |
| 97 | + t.Run(tt.name, func(t *testing.T) { |
| 98 | + stack := &Stack{stack: tt.initialStack} |
| 99 | + result := stack.Peek() |
| 100 | + |
| 101 | + require.Equal(t, tt.expected, result) |
| 102 | + require.Equal(t, tt.finalStack, stack.stack) |
| 103 | + }) |
| 104 | + } |
| 105 | +} |
0 commit comments