public void GetTest() { LRUElementCache cache = new LRUElementCache(9); int index = 0; Element ele = new Element(new byte[0]); GetAddTestAssert(index, ele, cache); int index1 = 1; Element ele1 = new Element(new byte[0]); GetAddTestAssert(index1, ele1, cache); int index2 = 2; Element ele2 = new Element(new byte[0]); GetAddTestAssert(index2, ele2, cache); int index3 = 2; Element ele3 = new Element(new byte[0]); GetAddTestAssert(index3, ele3, cache); Element ele4 = cache.Get(4); Assert.IsNull(ele4); LRUElementCache emptyCache = new LRUElementCache(0); emptyCache.AddToCache(new Element(new byte[0]), 0); Assert.IsNull(emptyCache.Get(0)); LRUElementCache smallCache = new LRUElementCache(1); smallCache.AddToCache(new Element(new byte[0]), 0); smallCache.AddToCache(new Element(new byte[0]), 7); Assert.IsNotNull(smallCache.Get(7)); Assert.IsNull(smallCache.Get(0)); }
public Node(Node child, Node parent, Element element, int eleIdx) { Child = child; Parent = parent; Element = element; ElementIndex = eleIdx; }
public void NodeCtorTest() { Element ele = new Element(new byte[0]); int elementIdx = 0; Node parent = new Node(null, null, ele, elementIdx); Node child = new Node(null, null, ele, elementIdx); NodeCtorTestAssert(elementIdx, ele, parent, child); }
private static void GetAddTestAssert(int index, Element ele, LRUElementCache cache) { cache.AddToCache(ele, index); Element actual = cache.Get(index); Assert.AreEqual(ele, actual); }
private void WriteReadStorageTestAssert(int elementSize, byte[] data, IStorage storage) { Element expected = new Element(data); byte[] expectedBytes = expected.Serialize().ExtendTo(elementSize); storage.WriteByteArray(expectedBytes); storage.Seek(0); Element actual = Element.ReadFromStorage(storage, elementSize); storage.Seek(0); AssertElementsAreSame(expected, actual); }
public void AddToCache(Element element, int elementIdx) { Node cachedNode; if (_index.TryGetValue(elementIdx, out cachedNode)) { cachedNode.SetElement(element); _cacheList.MoveToHead(cachedNode); } else { Node node = new Node(null, null, element, elementIdx); _cacheList.Insert(node); _index.Add(elementIdx, node); } if (_cacheList.Size > _cacheSize) RemoveLast(); }
private void AssertElementsAreSame(Element expected, Element actual) { TestHelper.AssertByteArraysAreSame(expected.Data, actual.Data); }
private static void ElementCtorTestAssert(byte[] data) { Element element = new Element(data); Assert.IsNotNull(element); TestHelper.AssertByteArraysAreSame(element.Data, data); }
public void SetElement(Element element) { Element = element; }
private static void NodeCtorTestAssert(int elementIdx, Element ele, Node parent, Node child) { Node n = new Node(child, parent, ele, elementIdx); Assert.AreEqual(n.Element, ele); Assert.AreEqual(n.ElementIndex, elementIdx); Assert.AreEqual(n.Child, child); Assert.AreEqual(n.Parent, parent); }