|
| 1 | +package stack |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +// ------------------------------------------------ --------------------------------------------------------------------- |
| 11 | + |
| 12 | +func TestSyncMaxStack_Clear(t *testing.T) { |
| 13 | + stack := NewSyncMaxStack[int](func(a, b int) int { return a - b }) |
| 14 | + stack.Push(1) |
| 15 | + assert.Equal(t, 1, stack.Size()) |
| 16 | + stack.Clear() |
| 17 | + assert.Equal(t, 0, stack.Size()) |
| 18 | +} |
| 19 | + |
| 20 | +func ExampleSyncMaxStack_Clear() { |
| 21 | + stack := NewSyncMaxStack[int](func(a, b int) int { return a - b }) |
| 22 | + stack.Push(1) |
| 23 | + fmt.Println(stack.Size()) |
| 24 | + stack.Clear() |
| 25 | + fmt.Println(stack.Size()) |
| 26 | + // Output: |
| 27 | + // 1 |
| 28 | + // 0 |
| 29 | +} |
| 30 | + |
| 31 | +// ------------------------------------------------ --------------------------------------------------------------------- |
| 32 | + |
| 33 | +func TestSyncMaxStack_IsEmpty(t *testing.T) { |
| 34 | + stack := NewSyncMaxStack[int](func(a, b int) int { return a - b }) |
| 35 | + assert.Equal(t, true, stack.IsEmpty()) |
| 36 | + stack.Push(1) |
| 37 | + assert.Equal(t, false, stack.IsEmpty()) |
| 38 | +} |
| 39 | + |
| 40 | +func ExampleSyncMaxStack_IsEmpty() { |
| 41 | + stack := NewSyncMaxStack[int](func(a, b int) int { return a - b }) |
| 42 | + fmt.Println(stack.IsEmpty()) |
| 43 | + stack.Push(1) |
| 44 | + fmt.Println(stack.IsEmpty()) |
| 45 | + // Output: |
| 46 | + // true |
| 47 | + // false |
| 48 | +} |
| 49 | + |
| 50 | +// ------------------------------------------------ --------------------------------------------------------------------- |
| 51 | + |
| 52 | +func TestSyncMaxStack_IsNotEmpty(t *testing.T) { |
| 53 | + stack := NewSyncMaxStack[int](func(a, b int) int { return a - b }) |
| 54 | + assert.Equal(t, false, stack.IsNotEmpty()) |
| 55 | + stack.Push(1) |
| 56 | + assert.Equal(t, true, stack.IsNotEmpty()) |
| 57 | +} |
| 58 | + |
| 59 | +func ExampleSyncMaxStack_IsNotEmpty() { |
| 60 | + stack := NewSyncMaxStack[int](func(a, b int) int { return a - b }) |
| 61 | + fmt.Println(stack.IsNotEmpty()) |
| 62 | + stack.Push(1) |
| 63 | + fmt.Println(stack.IsNotEmpty()) |
| 64 | + // Output: |
| 65 | + // false |
| 66 | + // true |
| 67 | +} |
| 68 | + |
| 69 | +// ------------------------------------------------ --------------------------------------------------------------------- |
| 70 | + |
| 71 | +func TestSyncMaxStack_Peek(t *testing.T) { |
| 72 | + type User struct { |
| 73 | + } |
| 74 | + stack := NewSyncMaxStack[*User](func(a, b *User) int { return 0 }) |
| 75 | + assert.Nil(t, stack.Peek()) |
| 76 | + u := &User{} |
| 77 | + stack.Push(u) |
| 78 | + assert.Equal(t, u, stack.Peek()) |
| 79 | +} |
| 80 | + |
| 81 | +func ExampleSyncMaxStack_Peek() { |
| 82 | + type User struct { |
| 83 | + } |
| 84 | + stack := NewSyncMaxStack[*User](func(a, b *User) int { return 0 }) |
| 85 | + fmt.Println(stack.Peek()) |
| 86 | + u := &User{} |
| 87 | + stack.Push(u) |
| 88 | + fmt.Println(stack.Peek()) |
| 89 | + // Output: |
| 90 | + // <nil> |
| 91 | + // &{} |
| 92 | +} |
| 93 | + |
| 94 | +// ------------------------------------------------ --------------------------------------------------------------------- |
| 95 | + |
| 96 | +func TestSyncMaxStack_PeekE(t *testing.T) { |
| 97 | + stack := NewSyncMaxStack[int](func(a, b int) int { return a - b }) |
| 98 | + element, err := stack.PeekE() |
| 99 | + assert.Equal(t, 0, element) |
| 100 | + assert.ErrorIs(t, ErrStackEmpty, err) |
| 101 | + |
| 102 | + stack.Push(1) |
| 103 | + element, err = stack.PeekE() |
| 104 | + assert.Nil(t, err) |
| 105 | + assert.Equal(t, 1, element) |
| 106 | +} |
| 107 | + |
| 108 | +func ExampleSyncMaxStack_PeekE() { |
| 109 | + stack := NewSyncMaxStack[int](func(a, b int) int { return a - b }) |
| 110 | + element, err := stack.PeekE() |
| 111 | + if errors.Is(err, ErrStackEmpty) { |
| 112 | + fmt.Println("stack empty!") |
| 113 | + } |
| 114 | + |
| 115 | + stack.Push(1) |
| 116 | + element, err = stack.PeekE() |
| 117 | + if err != nil { |
| 118 | + fmt.Println(err.Error()) |
| 119 | + return |
| 120 | + } |
| 121 | + fmt.Println(element) |
| 122 | + // Output: |
| 123 | + // stack empty! |
| 124 | + // 1 |
| 125 | +} |
| 126 | + |
| 127 | +// ------------------------------------------------ --------------------------------------------------------------------- |
| 128 | + |
| 129 | +func TestSyncMaxStack_Pop(t *testing.T) { |
| 130 | + type User struct { |
| 131 | + } |
| 132 | + stack := NewSyncMaxStack[*User](func(a, b *User) int { return 0 }) |
| 133 | + assert.Nil(t, stack.Pop()) |
| 134 | + u := &User{} |
| 135 | + stack.Push(u) |
| 136 | + assert.Equal(t, u, stack.Pop()) |
| 137 | +} |
| 138 | + |
| 139 | +func ExampleSyncMaxStack_Pop() { |
| 140 | + type User struct { |
| 141 | + } |
| 142 | + stack := NewSyncMaxStack[*User](func(a, b *User) int { return 0 }) |
| 143 | + fmt.Println(stack.Pop()) |
| 144 | + u := &User{} |
| 145 | + stack.Push(u) |
| 146 | + fmt.Println(stack.Pop()) |
| 147 | + // Output: |
| 148 | + // <nil> |
| 149 | + // &{} |
| 150 | +} |
| 151 | + |
| 152 | +// ------------------------------------------------ --------------------------------------------------------------------- |
| 153 | + |
| 154 | +func TestSyncMaxStack_PopE(t *testing.T) { |
| 155 | + stack := NewSyncMaxStack[int](func(a, b int) int { return a - b }) |
| 156 | + element, err := stack.PopE() |
| 157 | + assert.Equal(t, 0, element) |
| 158 | + assert.ErrorIs(t, ErrStackEmpty, err) |
| 159 | + |
| 160 | + stack.Push(1) |
| 161 | + element, err = stack.PopE() |
| 162 | + assert.Nil(t, err) |
| 163 | + assert.Equal(t, 1, element) |
| 164 | +} |
| 165 | + |
| 166 | +func ExampleSyncMaxStack_PopE() { |
| 167 | + stack := NewSyncMaxStack[int](func(a, b int) int { return a - b }) |
| 168 | + element, err := stack.PopE() |
| 169 | + if errors.Is(err, ErrStackEmpty) { |
| 170 | + fmt.Println("stack empty!") |
| 171 | + } |
| 172 | + |
| 173 | + stack.Push(1) |
| 174 | + element, err = stack.PopE() |
| 175 | + if err != nil { |
| 176 | + fmt.Println(err.Error()) |
| 177 | + return |
| 178 | + } |
| 179 | + fmt.Println(element) |
| 180 | + // Output: |
| 181 | + // stack empty! |
| 182 | + // 1 |
| 183 | +} |
| 184 | + |
| 185 | +// ------------------------------------------------ --------------------------------------------------------------------- |
| 186 | + |
| 187 | +func TestSyncMaxStack_Push(t *testing.T) { |
| 188 | + stack := NewSyncMaxStack[int](func(a, b int) int { return a - b }) |
| 189 | + stack.Push(1) |
| 190 | +} |
| 191 | + |
| 192 | +func ExampleSyncMaxStack_Push() { |
| 193 | + stack := NewSyncMaxStack[int](func(a, b int) int { return a - b }) |
| 194 | + stack.Push(1) |
| 195 | + // Output: |
| 196 | + // |
| 197 | +} |
| 198 | + |
| 199 | +// ------------------------------------------------ --------------------------------------------------------------------- |
| 200 | + |
| 201 | +func TestSyncMaxStack_Size(t *testing.T) { |
| 202 | + stack := NewSyncMaxStack[int](func(a, b int) int { return a - b }) |
| 203 | + stack.Push(1) |
| 204 | + assert.Equal(t, 1, stack.Size()) |
| 205 | +} |
| 206 | + |
| 207 | +func ExampleSyncMaxStack_Size() { |
| 208 | + stack := NewSyncMaxStack[int](func(a, b int) int { return a - b }) |
| 209 | + stack.Push(1) |
| 210 | + fmt.Println(stack.Size()) |
| 211 | + // Output: |
| 212 | + // 1 |
| 213 | +} |
| 214 | + |
| 215 | +// ------------------------------------------------ --------------------------------------------------------------------- |
| 216 | + |
| 217 | +func TestSyncMaxStack_String(t *testing.T) { |
| 218 | + stack := NewSyncMaxStack[int](func(a, b int) int { return a - b }) |
| 219 | + stack.Push(1) |
| 220 | + assert.Equal(t, "[&stack.MaxNode[int]{value:1, max:1}]", stack.String()) |
| 221 | +} |
| 222 | + |
| 223 | +func ExampleSyncMaxStack_String() { |
| 224 | + stack := NewSyncMaxStack[int](func(a, b int) int { return a - b }) |
| 225 | + stack.Push(1) |
| 226 | + fmt.Println(stack.String()) |
| 227 | + // Output: |
| 228 | + // [&stack.MaxNode[int]{value:1, max:1}] |
| 229 | +} |
| 230 | + |
| 231 | +// ------------------------------------------------ --------------------------------------------------------------------- |
| 232 | + |
| 233 | +func TestNewSyncMaxStack(t *testing.T) { |
| 234 | + stack := NewSyncMaxStack[int](func(a, b int) int { return a - b }) |
| 235 | + assert.NotNil(t, stack) |
| 236 | +} |
| 237 | + |
| 238 | +func ExampleNewSyncMaxStack() { |
| 239 | + stack := NewSyncMaxStack[int](func(a, b int) int { return a - b }) |
| 240 | + fmt.Println(stack.String()) |
| 241 | + // Output: |
| 242 | + // [] |
| 243 | +} |
| 244 | + |
| 245 | +// ------------------------------------------------ --------------------------------------------------------------------- |
| 246 | + |
| 247 | +func TestSyncMaxStack_GetMax(t *testing.T) { |
| 248 | + stack := NewSyncMaxStack[int](func(a, b int) int { return a - b }) |
| 249 | + stack.Push(10) |
| 250 | + stack.Push(7) |
| 251 | + stack.Push(9) |
| 252 | + assert.Equal(t, 10, stack.GetMax()) |
| 253 | +} |
| 254 | + |
| 255 | +func ExampleSyncMaxStack_GetMax() { |
| 256 | + stack := NewSyncMaxStack[int](func(a, b int) int { return a - b }) |
| 257 | + stack.Push(10) |
| 258 | + stack.Push(7) |
| 259 | + stack.Push(9) |
| 260 | + fmt.Println(stack.GetMax()) |
| 261 | + // Output: |
| 262 | + // 10 |
| 263 | +} |
| 264 | + |
| 265 | +// ------------------------------------------------ --------------------------------------------------------------------- |
| 266 | + |
| 267 | +func TestSyncMaxStack_GetMaxE(t *testing.T) { |
| 268 | + stack := NewSyncMaxStack[int](func(a, b int) int { return a - b }) |
| 269 | + |
| 270 | + _, err := stack.GetMaxE() |
| 271 | + assert.ErrorIs(t, err, ErrStackEmpty) |
| 272 | + |
| 273 | + stack.Push(10) |
| 274 | + stack.Push(7) |
| 275 | + stack.Push(9) |
| 276 | + element, err := stack.GetMaxE() |
| 277 | + assert.Nil(t, err) |
| 278 | + assert.Equal(t, 10, element) |
| 279 | +} |
| 280 | + |
| 281 | +func ExampleSyncMaxStack_GetMaxE() { |
| 282 | + stack := NewSyncMaxStack[int](func(a, b int) int { return a - b }) |
| 283 | + |
| 284 | + _, err := stack.GetMaxE() |
| 285 | + if errors.Is(err, ErrStackEmpty) { |
| 286 | + fmt.Println("stack empty!") |
| 287 | + } |
| 288 | + |
| 289 | + stack.Push(10) |
| 290 | + stack.Push(7) |
| 291 | + stack.Push(9) |
| 292 | + element, err := stack.GetMaxE() |
| 293 | + if err != nil { |
| 294 | + fmt.Println(err.Error()) |
| 295 | + return |
| 296 | + } |
| 297 | + fmt.Println(element) |
| 298 | + // Output: |
| 299 | + // stack empty! |
| 300 | + // 10 |
| 301 | +} |
| 302 | + |
| 303 | +// ------------------------------------------------ --------------------------------------------------------------------- |
0 commit comments