microsoft/TypeAgent
Publicmirrored fromhttps://github.com/microsoft/TypeAgentAvailable
dotnet/typeagent/common.test/LRUCacheTests.cs
80lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System; |
| 5 | using System.Collections.Generic; |
| 6 | using System.Linq; |
| 7 | using System.Text; |
| 8 | using System.Threading.Tasks; |
| 9 | using NuGet.Frameworks; |
| 10 | using TypeAgent.Common; |
| 11 | |
| 12 | namespace common.test; |
| 13 | |
| 14 | public class LRUCacheTests |
| 15 | { |
| 16 | [Fact] |
| 17 | public void TestEviction() |
| 18 | { |
| 19 | LRUCache<int, int> cache = new(3); |
| 20 | |
| 21 | for(int i = 0; i < 5; i++) |
| 22 | { |
| 23 | cache.Add(i, i * 10); |
| 24 | } |
| 25 | |
| 26 | Assert.Equal(3, cache.Count); |
| 27 | Assert.False(cache.Contains(0)); |
| 28 | Assert.False(cache.TryGet(0, out _)); |
| 29 | Assert.True(cache.Contains(4)); |
| 30 | Assert.True(cache.TryGet(4, out _)); |
| 31 | } |
| 32 | |
| 33 | [Fact] |
| 34 | public void TestUpdate() |
| 35 | { |
| 36 | LRUCache<int, int> cache = new(3); |
| 37 | |
| 38 | cache.Put(0, 0); |
| 39 | cache.Put(0, 1); |
| 40 | |
| 41 | Assert.Equal(1, cache.Get(0)); |
| 42 | |
| 43 | cache.Remove(0); |
| 44 | Assert.Equal(0, cache.Count); |
| 45 | } |
| 46 | |
| 47 | [Fact] |
| 48 | public void TestClear() |
| 49 | { |
| 50 | LRUCache<int, int> cache = new(3); |
| 51 | |
| 52 | for (int i = 0; i < 5; i++) |
| 53 | { |
| 54 | cache.Add(i, i * 10); |
| 55 | } |
| 56 | |
| 57 | cache.Clear(); |
| 58 | Assert.Equal(0, cache.Count); |
| 59 | } |
| 60 | |
| 61 | [Fact] |
| 62 | public void TestTrim() |
| 63 | { |
| 64 | LRUCache<int, int> cache = new(5); |
| 65 | int purged = 0; |
| 66 | cache.Purged += (kv) => |
| 67 | { |
| 68 | purged++; |
| 69 | }; |
| 70 | |
| 71 | for (int i = 0; i < 5; i++) |
| 72 | { |
| 73 | cache.Add(i, i * 10); |
| 74 | } |
| 75 | |
| 76 | cache.SetCount(3); |
| 77 | Assert.Equal(2, cache.Count); |
| 78 | Assert.Equal(3, purged); |
| 79 | } |
| 80 | } |
| 81 | |