|
4 | 4 | package lru
|
5 | 5 |
|
6 | 6 | import (
|
| 7 | + "reflect" |
7 | 8 | "testing"
|
8 | 9 | )
|
9 | 10 |
|
@@ -293,3 +294,150 @@ func TestLRUResize(t *testing.T) {
|
293 | 294 | t.Errorf("Cache should have contained 2 elements")
|
294 | 295 | }
|
295 | 296 | }
|
| 297 | + |
| 298 | +func (c *Cache[K, V]) wantKeys(t *testing.T, want []K) { |
| 299 | + t.Helper() |
| 300 | + got := c.Keys() |
| 301 | + if !reflect.DeepEqual(got, want) { |
| 302 | + t.Errorf("wrong keys got: %v, want: %v ", got, want) |
| 303 | + } |
| 304 | +} |
| 305 | + |
| 306 | +func TestCache_EvictionSameKey(t *testing.T) { |
| 307 | + t.Run("Add", func(t *testing.T) { |
| 308 | + var evictedKeys []int |
| 309 | + |
| 310 | + cache, _ := NewWithEvict( |
| 311 | + 2, |
| 312 | + func(key int, _ struct{}) { |
| 313 | + evictedKeys = append(evictedKeys, key) |
| 314 | + }) |
| 315 | + |
| 316 | + if evicted := cache.Add(1, struct{}{}); evicted { |
| 317 | + t.Error("First 1: got unexpected eviction") |
| 318 | + } |
| 319 | + cache.wantKeys(t, []int{1}) |
| 320 | + |
| 321 | + if evicted := cache.Add(2, struct{}{}); evicted { |
| 322 | + t.Error("2: got unexpected eviction") |
| 323 | + } |
| 324 | + cache.wantKeys(t, []int{1, 2}) |
| 325 | + |
| 326 | + if evicted := cache.Add(1, struct{}{}); evicted { |
| 327 | + t.Error("Second 1: got unexpected eviction") |
| 328 | + } |
| 329 | + cache.wantKeys(t, []int{2, 1}) |
| 330 | + |
| 331 | + if evicted := cache.Add(3, struct{}{}); !evicted { |
| 332 | + t.Error("3: did not get expected eviction") |
| 333 | + } |
| 334 | + cache.wantKeys(t, []int{1, 3}) |
| 335 | + |
| 336 | + want := []int{2} |
| 337 | + if !reflect.DeepEqual(evictedKeys, want) { |
| 338 | + t.Errorf("evictedKeys got: %v want: %v", evictedKeys, want) |
| 339 | + } |
| 340 | + }) |
| 341 | + |
| 342 | + t.Run("ContainsOrAdd", func(t *testing.T) { |
| 343 | + var evictedKeys []int |
| 344 | + |
| 345 | + cache, _ := NewWithEvict( |
| 346 | + 2, |
| 347 | + func(key int, _ struct{}) { |
| 348 | + evictedKeys = append(evictedKeys, key) |
| 349 | + }) |
| 350 | + |
| 351 | + contained, evicted := cache.ContainsOrAdd(1, struct{}{}) |
| 352 | + if contained { |
| 353 | + t.Error("First 1: got unexpected contained") |
| 354 | + } |
| 355 | + if evicted { |
| 356 | + t.Error("First 1: got unexpected eviction") |
| 357 | + } |
| 358 | + cache.wantKeys(t, []int{1}) |
| 359 | + |
| 360 | + contained, evicted = cache.ContainsOrAdd(2, struct{}{}) |
| 361 | + if contained { |
| 362 | + t.Error("2: got unexpected contained") |
| 363 | + } |
| 364 | + if evicted { |
| 365 | + t.Error("2: got unexpected eviction") |
| 366 | + } |
| 367 | + cache.wantKeys(t, []int{1, 2}) |
| 368 | + |
| 369 | + contained, evicted = cache.ContainsOrAdd(1, struct{}{}) |
| 370 | + if !contained { |
| 371 | + t.Error("Second 1: did not get expected contained") |
| 372 | + } |
| 373 | + if evicted { |
| 374 | + t.Error("Second 1: got unexpected eviction") |
| 375 | + } |
| 376 | + cache.wantKeys(t, []int{1, 2}) |
| 377 | + |
| 378 | + contained, evicted = cache.ContainsOrAdd(3, struct{}{}) |
| 379 | + if contained { |
| 380 | + t.Error("3: got unexpected contained") |
| 381 | + } |
| 382 | + if !evicted { |
| 383 | + t.Error("3: did not get expected eviction") |
| 384 | + } |
| 385 | + cache.wantKeys(t, []int{2, 3}) |
| 386 | + |
| 387 | + want := []int{1} |
| 388 | + if !reflect.DeepEqual(evictedKeys, want) { |
| 389 | + t.Errorf("evictedKeys got: %v want: %v", evictedKeys, want) |
| 390 | + } |
| 391 | + }) |
| 392 | + |
| 393 | + t.Run("PeekOrAdd", func(t *testing.T) { |
| 394 | + var evictedKeys []int |
| 395 | + |
| 396 | + cache, _ := NewWithEvict( |
| 397 | + 2, |
| 398 | + func(key int, _ struct{}) { |
| 399 | + evictedKeys = append(evictedKeys, key) |
| 400 | + }) |
| 401 | + |
| 402 | + _, contained, evicted := cache.PeekOrAdd(1, struct{}{}) |
| 403 | + if contained { |
| 404 | + t.Error("First 1: got unexpected contained") |
| 405 | + } |
| 406 | + if evicted { |
| 407 | + t.Error("First 1: got unexpected eviction") |
| 408 | + } |
| 409 | + cache.wantKeys(t, []int{1}) |
| 410 | + |
| 411 | + _, contained, evicted = cache.PeekOrAdd(2, struct{}{}) |
| 412 | + if contained { |
| 413 | + t.Error("2: got unexpected contained") |
| 414 | + } |
| 415 | + if evicted { |
| 416 | + t.Error("2: got unexpected eviction") |
| 417 | + } |
| 418 | + cache.wantKeys(t, []int{1, 2}) |
| 419 | + |
| 420 | + _, contained, evicted = cache.PeekOrAdd(1, struct{}{}) |
| 421 | + if !contained { |
| 422 | + t.Error("Second 1: did not get expected contained") |
| 423 | + } |
| 424 | + if evicted { |
| 425 | + t.Error("Second 1: got unexpected eviction") |
| 426 | + } |
| 427 | + cache.wantKeys(t, []int{1, 2}) |
| 428 | + |
| 429 | + _, contained, evicted = cache.PeekOrAdd(3, struct{}{}) |
| 430 | + if contained { |
| 431 | + t.Error("3: got unexpected contained") |
| 432 | + } |
| 433 | + if !evicted { |
| 434 | + t.Error("3: did not get expected eviction") |
| 435 | + } |
| 436 | + cache.wantKeys(t, []int{2, 3}) |
| 437 | + |
| 438 | + want := []int{1} |
| 439 | + if !reflect.DeepEqual(evictedKeys, want) { |
| 440 | + t.Errorf("evictedKeys got: %v want: %v", evictedKeys, want) |
| 441 | + } |
| 442 | + }) |
| 443 | +} |
0 commit comments