namespace SalmonCacheTest; public class SalmonCacheTests { private readonly List _words = new(); [SetUp] public void Setup() { StreamReader reader = new("/usr/share/dict/words"); Dictionary Words = new(); var line = reader.ReadLine(); while (line != null) { var word = line.Trim().ToLower(); if (!Words.ContainsKey(word)) Words.Add(word, true); } reader.Close(); _words.AddRange(Words.Keys.ToArray()); _words.Sort(); } [Test] public void TestBasicInsertAndLookup() { var cache = new SalmonCache.SalmonCache(_words.Count); Assert.That(cache.Capacity, Is.EqualTo(_words.Count)); foreach (var word in _words) { var inserted = cache.Insert(word, word.ToUpper()); Assert.That(inserted, Is.True); var (stored, found) = cache.Lookup(word); Assert.That(found, Is.True); Assert.That(stored, Is.EqualTo(word.ToUpper())); } Assert.That(cache.Count, Is.EqualTo(_words.Count)); } [Test] public void TestRemove() { var cache = new SalmonCache.SalmonCache(_words.Count); foreach (var word in _words) { Assert.That(cache.Insert(word, word.ToUpper()), Is.True); } Assert.That(cache.Count, Is.EqualTo(_words.Count)); foreach (var word in _words) { var (stored, found) = cache.Remove(word); Assert.That(found, Is.True); Assert.That(stored, Is.EqualTo(word.ToUpper())); } } [Test] public void TestResizeAndClear() { var cache = new SalmonCache.SalmonCache(_words.Count); foreach (var word in _words) { Assert.That(cache.Insert(word, word.ToUpper()), Is.True); } Assert.That(cache.Count, Is.EqualTo(_words.Count)); var capacity = _words.Count / 2; Assert.That(cache.Count, Is.EqualTo(capacity)); // the first (_words.Count - capacity) words will have been removed for (var i = 0; i < _words.Count - capacity; i++) { var word = _words.ElementAt(i); var (stored, found) = cache.Lookup(word); Assert.That(found, Is.False); Assert.That(stored, Is.Empty); } for (var i = _words.Count - capacity; i < _words.Count; i++) { var word = _words.ElementAt(i); var (stored, found) = cache.Lookup(word); Assert.That(found, Is.True); Assert.That(stored, Is.EqualTo(word.ToUpper())); } cache.Clear(); Assert.That(cache.Count, Is.Empty); } }