|
| 1 | +using HintServiceMeow.Core.Utilities; |
| 2 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 3 | +using System; |
| 4 | + |
| 5 | +namespace HintServiceMeow.Tests |
| 6 | +{ |
| 7 | + [TestClass] |
| 8 | + public class CacheTests |
| 9 | + { |
| 10 | + [TestMethod] |
| 11 | + [ExpectedException(typeof(ArgumentOutOfRangeException))] |
| 12 | + public void Constructor_Throws_OnInvalidMaxSize() |
| 13 | + { |
| 14 | + var cache = new Cache<string, int>(0); |
| 15 | + } |
| 16 | + |
| 17 | + [TestMethod] |
| 18 | + [ExpectedException(typeof(ArgumentNullException))] |
| 19 | + public void Add_Throws_OnNullKey() |
| 20 | + { |
| 21 | + var cache = new Cache<string, int>(5); |
| 22 | + cache.Add(null, 1); |
| 23 | + } |
| 24 | + |
| 25 | + [TestMethod] |
| 26 | + public void Add_And_TryGet_ReturnsCorrectValue() |
| 27 | + { |
| 28 | + var cache = new Cache<string, int>(3); |
| 29 | + cache.Add("a", 1); |
| 30 | + cache.Add("b", 2); |
| 31 | + |
| 32 | + Assert.IsTrue(cache.TryGet("a", out int v1)); |
| 33 | + Assert.AreEqual(1, v1); |
| 34 | + Assert.IsTrue(cache.TryGet("b", out int v2)); |
| 35 | + Assert.AreEqual(2, v2); |
| 36 | + Assert.IsFalse(cache.TryGet("c", out _)); |
| 37 | + } |
| 38 | + |
| 39 | + [TestMethod] |
| 40 | + public void TryRemove_RemovesItem_And_ReturnsValue() |
| 41 | + { |
| 42 | + var cache = new Cache<string, int>(3); |
| 43 | + cache.Add("a", 1); |
| 44 | + cache.Add("b", 2); |
| 45 | + |
| 46 | + Assert.IsTrue(cache.TryRemove("a", out int val)); |
| 47 | + Assert.AreEqual(1, val); |
| 48 | + Assert.IsFalse(cache.TryGet("a", out _)); |
| 49 | + |
| 50 | + Assert.IsFalse(cache.TryRemove("c", out _)); |
| 51 | + } |
| 52 | + |
| 53 | + [TestMethod] |
| 54 | + public void Add_Replaces_OldValue() |
| 55 | + { |
| 56 | + var cache = new Cache<string, int>(3); |
| 57 | + cache.Add("a", 1); |
| 58 | + cache.Add("a", 2); |
| 59 | + |
| 60 | + Assert.IsTrue(cache.TryGet("a", out int v)); |
| 61 | + Assert.AreEqual(2, v); |
| 62 | + } |
| 63 | + |
| 64 | + [TestMethod] |
| 65 | + public void Capacity_Is_Respected_And_LRU_Removed() |
| 66 | + { |
| 67 | + var cache = new Cache<string, int>(2); |
| 68 | + cache.Add("a", 1); |
| 69 | + cache.Add("b", 2); |
| 70 | + |
| 71 | + // a, b in cache (a is oldest, b is newest) |
| 72 | + cache.Add("c", 3); // Should remove "a" |
| 73 | + |
| 74 | + Assert.IsFalse(cache.TryGet("a", out _)); |
| 75 | + Assert.IsTrue(cache.TryGet("b", out int v2) && v2 == 2); |
| 76 | + Assert.IsTrue(cache.TryGet("c", out int v3) && v3 == 3); |
| 77 | + } |
| 78 | + |
| 79 | + [TestMethod] |
| 80 | + public void Access_Updates_LRU_Order() |
| 81 | + { |
| 82 | + var cache = new Cache<string, int>(2); |
| 83 | + cache.Add("a", 1); // a |
| 84 | + cache.Add("b", 2); // b,a |
| 85 | + cache.TryGet("a", out _); // a,b |
| 86 | + cache.Add("c", 3); // b should be removed here |
| 87 | + |
| 88 | + Assert.IsTrue(cache.TryGet("a", out int v1) && v1 == 1); |
| 89 | + Assert.IsTrue(cache.TryGet("c", out int v2) && v2 == 3); |
| 90 | + Assert.IsFalse(cache.TryGet("b", out _)); |
| 91 | + } |
| 92 | + |
| 93 | + [TestMethod] |
| 94 | + public void TryRemove_OnNonExistentKey_DoesNothing() |
| 95 | + { |
| 96 | + var cache = new Cache<string, int>(2); |
| 97 | + cache.Add("a", 1); |
| 98 | + Assert.IsFalse(cache.TryRemove("x", out _)); |
| 99 | + } |
| 100 | + |
| 101 | + [TestMethod] |
| 102 | + public void Multiple_Keys_Work() |
| 103 | + { |
| 104 | + var cache = new Cache<int, string>(10); |
| 105 | + for (int i = 0; i < 10; i++) |
| 106 | + cache.Add(i, i.ToString()); |
| 107 | + |
| 108 | + for (int i = 0; i < 10; i++) |
| 109 | + Assert.IsTrue(cache.TryGet(i, out string s) && s == i.ToString()); |
| 110 | + } |
| 111 | + |
| 112 | + [TestMethod] |
| 113 | + public void Cache_Thread_Safety() |
| 114 | + { |
| 115 | + var cache = new Cache<int, int>(1000); |
| 116 | + var random = new Random(); |
| 117 | + int threadCount = 8; |
| 118 | + var threads = new System.Threading.Thread[threadCount]; |
| 119 | + |
| 120 | + for (int t = 0; t < threadCount; t++) |
| 121 | + { |
| 122 | + threads[t] = new System.Threading.Thread(() => |
| 123 | + { |
| 124 | + for (int i = 0; i < 5000; i++) |
| 125 | + { |
| 126 | + int key = random.Next(0, 1500); |
| 127 | + cache.Add(key, key); |
| 128 | + cache.TryGet(key, out _); |
| 129 | + cache.TryRemove(key, out _); |
| 130 | + } |
| 131 | + }); |
| 132 | + } |
| 133 | + |
| 134 | + foreach (var th in threads) th.Start(); |
| 135 | + foreach (var th in threads) th.Join(); |
| 136 | + |
| 137 | + // No exception = passed test |
| 138 | + Assert.IsTrue(true); |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments