|
1 | 1 | package graph |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "reflect" |
4 | 5 | "testing" |
5 | 6 | ) |
6 | 7 |
|
@@ -222,3 +223,167 @@ func TestPriorityQueue_Len(t *testing.T) { |
222 | 223 | } |
223 | 224 | } |
224 | 225 | } |
| 226 | + |
| 227 | +func Test_stackImpl_push(t *testing.T) { |
| 228 | + type args[T any] struct { |
| 229 | + t T |
| 230 | + } |
| 231 | + type testCase[T any] struct { |
| 232 | + name string |
| 233 | + s stackImpl[T] |
| 234 | + args args[T] |
| 235 | + } |
| 236 | + tests := []testCase[int]{ |
| 237 | + { |
| 238 | + "push 1", |
| 239 | + stackImpl[int]{ |
| 240 | + elements: make([]int, 0), |
| 241 | + }, |
| 242 | + args[int]{ |
| 243 | + t: 1, |
| 244 | + }, |
| 245 | + }, |
| 246 | + } |
| 247 | + for _, tt := range tests { |
| 248 | + t.Run(tt.name, func(t *testing.T) { |
| 249 | + tt.s.push(tt.args.t) |
| 250 | + }) |
| 251 | + } |
| 252 | +} |
| 253 | + |
| 254 | +func Test_stackImpl_pop(t *testing.T) { |
| 255 | + type testCase[T any] struct { |
| 256 | + name string |
| 257 | + s stackImpl[T] |
| 258 | + want T |
| 259 | + wantErr bool |
| 260 | + } |
| 261 | + tests := []testCase[int]{ |
| 262 | + { |
| 263 | + "pop element", |
| 264 | + stackImpl[int]{ |
| 265 | + elements: []int{1}, |
| 266 | + }, |
| 267 | + 1, |
| 268 | + false, |
| 269 | + }, |
| 270 | + { |
| 271 | + "pop element from empty stack", |
| 272 | + stackImpl[int]{ |
| 273 | + elements: []int{}, |
| 274 | + }, |
| 275 | + 0, |
| 276 | + true, |
| 277 | + }, |
| 278 | + } |
| 279 | + for _, tt := range tests { |
| 280 | + t.Run(tt.name, func(t *testing.T) { |
| 281 | + got, err := tt.s.pop() |
| 282 | + if (err != nil) != tt.wantErr { |
| 283 | + t.Errorf("pop() error = %v, wantErr %v", err, tt.wantErr) |
| 284 | + return |
| 285 | + } |
| 286 | + if !reflect.DeepEqual(got, tt.want) { |
| 287 | + t.Errorf("pop() got = %v, want %v", got, tt.want) |
| 288 | + } |
| 289 | + }) |
| 290 | + } |
| 291 | +} |
| 292 | + |
| 293 | +func Test_stackImpl_top(t *testing.T) { |
| 294 | + type testCase[T any] struct { |
| 295 | + name string |
| 296 | + s stackImpl[T] |
| 297 | + want T |
| 298 | + wantErr bool |
| 299 | + } |
| 300 | + tests := []testCase[int]{ |
| 301 | + { |
| 302 | + "top element", |
| 303 | + stackImpl[int]{ |
| 304 | + elements: []int{1}, |
| 305 | + }, |
| 306 | + 1, |
| 307 | + false, |
| 308 | + }, |
| 309 | + { |
| 310 | + "top element of empty stack", |
| 311 | + stackImpl[int]{ |
| 312 | + elements: []int{}, |
| 313 | + }, |
| 314 | + 0, |
| 315 | + true, |
| 316 | + }, |
| 317 | + } |
| 318 | + for _, tt := range tests { |
| 319 | + t.Run(tt.name, func(t *testing.T) { |
| 320 | + got, err := tt.s.top() |
| 321 | + if (err != nil) != tt.wantErr { |
| 322 | + t.Errorf("top() error = %v, wantErr %v", err, tt.wantErr) |
| 323 | + return |
| 324 | + } |
| 325 | + if !reflect.DeepEqual(got, tt.want) { |
| 326 | + t.Errorf("top() got = %v, want %v", got, tt.want) |
| 327 | + } |
| 328 | + }) |
| 329 | + } |
| 330 | +} |
| 331 | + |
| 332 | +func Test_stackImpl_isEmpty(t *testing.T) { |
| 333 | + type testCase[T any] struct { |
| 334 | + name string |
| 335 | + s stackImpl[T] |
| 336 | + want bool |
| 337 | + } |
| 338 | + tests := []testCase[int]{ |
| 339 | + { |
| 340 | + "empty", |
| 341 | + stackImpl[int]{ |
| 342 | + elements: []int{}, |
| 343 | + }, |
| 344 | + true, |
| 345 | + }, |
| 346 | + { |
| 347 | + "not empty", |
| 348 | + stackImpl[int]{ |
| 349 | + elements: []int{1}, |
| 350 | + }, |
| 351 | + false, |
| 352 | + }, |
| 353 | + } |
| 354 | + for _, tt := range tests { |
| 355 | + t.Run(tt.name, func(t *testing.T) { |
| 356 | + if got := tt.s.isEmpty(); got != tt.want { |
| 357 | + t.Errorf("isEmpty() = %v, want %v", got, tt.want) |
| 358 | + } |
| 359 | + }) |
| 360 | + } |
| 361 | +} |
| 362 | + |
| 363 | +func Test_stackImpl_forEach(t *testing.T) { |
| 364 | + type args[T any] struct { |
| 365 | + f func(T) |
| 366 | + } |
| 367 | + type testCase[T any] struct { |
| 368 | + name string |
| 369 | + s stackImpl[T] |
| 370 | + args args[T] |
| 371 | + } |
| 372 | + tests := []testCase[int]{ |
| 373 | + { |
| 374 | + name: "forEach", |
| 375 | + s: stackImpl[int]{ |
| 376 | + elements: []int{1, 2, 3, 4, 5, 6}, |
| 377 | + }, |
| 378 | + args: args[int]{ |
| 379 | + f: func(i int) { |
| 380 | + }, |
| 381 | + }, |
| 382 | + }, |
| 383 | + } |
| 384 | + for _, tt := range tests { |
| 385 | + t.Run(tt.name, func(t *testing.T) { |
| 386 | + tt.s.forEach(tt.args.f) |
| 387 | + }) |
| 388 | + } |
| 389 | +} |
0 commit comments