public virtual void TestSize() { NUnit.Framework.Assert.AreEqual(0, cache.SizeInMemory()); NUnit.Framework.Assert.AreEqual(0, cache.Count); cache["key"] = "value"; cache["key2"] = "value2"; NUnit.Framework.Assert.AreEqual(2, cache.SizeInMemory()); // assume no GC NUnit.Framework.Assert.AreEqual(2, cache.Count); cache.Clear(); NUnit.Framework.Assert.AreEqual(0, cache.SizeInMemory()); NUnit.Framework.Assert.AreEqual(2, cache.cacheDir.ListFiles().Length); NUnit.Framework.Assert.AreEqual(2, cache.Count); NUnit.Framework.Assert.AreEqual(2, cache.SizeInMemory()); NUnit.Framework.Assert.IsTrue(FileBackedCache.LocksHeld().IsEmpty()); }
public virtual void TestCollision() { // Custom Setup File cacheDir = File.CreateTempFile("cache", ".dir"); cacheDir.Delete(); FileBackedCache <FileBackedCacheTest.CustomHash, string> myCache = new FileBackedCache <FileBackedCacheTest.CustomHash, string>(cacheDir); NUnit.Framework.Assert.AreEqual(0, cacheDir.ListFiles().Length); // Test myCache[new FileBackedCacheTest.CustomHash(0, 0)] = "zero"; myCache[new FileBackedCacheTest.CustomHash(1, 0)] = "one"; myCache[new FileBackedCacheTest.CustomHash(1, 1)] = "one'"; NUnit.Framework.Assert.AreEqual("zero", myCache[new FileBackedCacheTest.CustomHash(0, 0)]); NUnit.Framework.Assert.AreEqual("one", myCache[new FileBackedCacheTest.CustomHash(1, 0)]); NUnit.Framework.Assert.AreEqual("one'", myCache[new FileBackedCacheTest.CustomHash(1, 1)]); myCache.Clear(); NUnit.Framework.Assert.AreEqual(0, myCache.SizeInMemory()); NUnit.Framework.Assert.AreEqual("zero", myCache[new FileBackedCacheTest.CustomHash(0, 0)]); NUnit.Framework.Assert.AreEqual("one", myCache[new FileBackedCacheTest.CustomHash(1, 0)]); NUnit.Framework.Assert.AreEqual("one'", myCache[new FileBackedCacheTest.CustomHash(1, 1)]); // Retest FileBackedCache <FileBackedCacheTest.CustomHash, string> reload = new FileBackedCache <FileBackedCacheTest.CustomHash, string>(cacheDir); NUnit.Framework.Assert.AreEqual("zero", reload[new FileBackedCacheTest.CustomHash(0, 0)]); NUnit.Framework.Assert.AreEqual("one", reload[new FileBackedCacheTest.CustomHash(1, 0)]); NUnit.Framework.Assert.AreEqual("one'", reload[new FileBackedCacheTest.CustomHash(1, 1)]); reload[new FileBackedCacheTest.CustomHash(2, 0)] = "two"; NUnit.Framework.Assert.AreEqual("two", reload[new FileBackedCacheTest.CustomHash(2, 0)]); // Custom Teardown foreach (File c in cache.cacheDir.ListFiles()) { NUnit.Framework.Assert.IsTrue(c.Delete()); } NUnit.Framework.Assert.IsTrue(cache.cacheDir.Delete()); NUnit.Framework.Assert.IsTrue(FileBackedCache.LocksHeld().IsEmpty()); }