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