| 7 | ) |
| 8 | |
| 9 | func TestLFU(t *testing.T) { |
| 10 | lfuCache := cache.NewLFU(3) |
| 11 | t.Run("Test 1: Put number and Get is correct", func(t *testing.T) { |
| 12 | key, value := "1", 1 |
| 13 | lfuCache.Put(key, value) |
| 14 | got := lfuCache.Get(key) |
| 15 | |
| 16 | if got != value { |
| 17 | t.Errorf("expected: %v, got: %v", value, got) |
| 18 | } |
| 19 | }) |
| 20 | |
| 21 | t.Run("Test 2: Get data not stored in cache should return nil", func(t *testing.T) { |
| 22 | got := lfuCache.Get("2") |
| 23 | if got != nil { |
| 24 | t.Errorf("expected: nil, got: %v", got) |
| 25 | } |
| 26 | }) |
| 27 | |
| 28 | t.Run("Test 3: Put data over capacity and Get should return nil", func(t *testing.T) { |
| 29 | lfuCache.Put("2", 2) |
| 30 | lfuCache.Put("3", 3) |
| 31 | lfuCache.Put("4", 4) |
| 32 | |
| 33 | got := lfuCache.Get("1") |
| 34 | if got != nil { |
| 35 | t.Errorf("expected: nil, got: %v", got) |
| 36 | } |
| 37 | }) |
| 38 | |
| 39 | t.Run("test 4: Put key over capacity but recent key exists", func(t *testing.T) { |
| 40 | lfuCache.Put("4", 4) |
| 41 | lfuCache.Put("3", 3) |
| 42 | lfuCache.Put("2", 2) |
| 43 | lfuCache.Put("1", 1) |
| 44 | |
| 45 | got := lfuCache.Get("4") |
| 46 | if got != nil { |
| 47 | t.Errorf("expected: nil, got: %v", got) |
| 48 | } |
| 49 | |
| 50 | expected := 3 |
| 51 | got = lfuCache.Get("3") |
| 52 | |
| 53 | if got != expected { |
| 54 | t.Errorf("expected: %v, got: %v", expected, got) |
| 55 | } |
| 56 | |
| 57 | }) |
| 58 | } |