|
| 1 | +package lru |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "github.com/stretchr/testify/assert" |
| 6 | +) |
| 7 | + |
| 8 | +func TestPutAndGetSuccess(t *testing.T){ |
| 9 | + |
| 10 | + cache := New(10); |
| 11 | + cache.Put("key1", "value1"); |
| 12 | + |
| 13 | + assert.Equal(t, "value1", cache.Get("key1").(string)) |
| 14 | + |
| 15 | +} |
| 16 | + |
| 17 | +func TestPutAndGetSuccessSizeLimited(t *testing.T){ |
| 18 | + |
| 19 | + cache := New(2); |
| 20 | + cache.Put("key1", "value1"); |
| 21 | + cache.Put("key2", "value2"); |
| 22 | + cache.Put("key3", "value3"); |
| 23 | + |
| 24 | + assert.Nil(t, cache.Get("key1")) |
| 25 | + assert.Equal(t, "value2", cache.Get("key2").(string)) |
| 26 | + assert.Equal(t, "value3", cache.Get("key3").(string)) |
| 27 | + |
| 28 | +} |
| 29 | + |
| 30 | +func TestPutAndGeRemovingLeastUsed(t *testing.T){ |
| 31 | + |
| 32 | + cache := New(3); |
| 33 | + |
| 34 | + cache.Put("key1", "value1"); |
| 35 | + cache.Put("key2", "value2"); |
| 36 | + cache.Put("key3", "value3"); |
| 37 | + |
| 38 | + |
| 39 | + cache.Get("key2") |
| 40 | + cache.Get("key1") |
| 41 | + |
| 42 | + cache.Put("key4", "value4"); |
| 43 | + |
| 44 | + |
| 45 | + assert.Equal(t, "value1", cache.Get("key1")) |
| 46 | + assert.Equal(t, "value2", cache.Get("key2").(string)) |
| 47 | + assert.Nil(t, cache.Get("key3")) |
| 48 | + assert.Equal(t, "value4", cache.Get("key4").(string)) |
| 49 | + |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | +func TestPurge(t *testing.T){ |
| 54 | + |
| 55 | + cache := New(3); |
| 56 | + |
| 57 | + cache.Put("key1", "value1"); |
| 58 | + cache.Put("key2", "value2"); |
| 59 | + |
| 60 | + assert.Equal(t, "value1", cache.Get("key1")) |
| 61 | + assert.Equal(t, "value2", cache.Get("key2")) |
| 62 | + |
| 63 | + cache.Clear() |
| 64 | + |
| 65 | + assert.False(t, cache.ContainsKey("key1")) |
| 66 | + assert.False(t, cache.ContainsKey("key2")) |
| 67 | + |
| 68 | +} |
| 69 | + |
| 70 | +func TestRemove(t *testing.T){ |
| 71 | + |
| 72 | + cache := New(3); |
| 73 | + |
| 74 | + cache.Put("key1", "value1"); |
| 75 | + cache.Put("key2", "value2"); |
| 76 | + |
| 77 | + assert.Equal(t, "value1", cache.Get("key1")) |
| 78 | + assert.Equal(t, "value2", cache.Get("key2")) |
| 79 | + |
| 80 | + cache.Remove("key2") |
| 81 | + |
| 82 | + assert.True(t, cache.ContainsKey("key1")) |
| 83 | + assert.False(t, cache.ContainsKey("key2")) |
| 84 | + |
| 85 | +} |
0 commit comments