Beispiel #1
0
 def setUp(self):
     self.lru = LeastRecentlyUsedCache(8)
     self.cache = self.lru.cache
     self.size = self.lru.size
     self.entries = [Entry(n, n + 1) for n in range(self.lru.size * 4)]
     for i in range(0, self.size):
         self.lru.store(self.entries[i])
Beispiel #2
0
class TestLRU:
    def setUp(self):
        self.lru = LeastRecentlyUsedCache(8)
        self.cache = self.lru.cache
        self.size = self.lru.size
        self.entries = [Entry(n, n + 1) for n in range(self.lru.size * 4)]
        for i in range(0, self.size):
            self.lru.store(self.entries[i])

    def tearDown(self):
         self.lru = LeastRecentlyUsedCache(8)

    def test_ordering(self):
        # test first/last elements on initial insertion
        assert self.cache[self.size - 1] == self.size
        assert self.cache[self.cache.iterkeys().next()] == 1
        assert self.cache[next(reversed(self.cache))] == self.size

        # test first/last elements in overflow case
        self.lru.store(self.entries[self.size])
        assert self.lru.is_full()
        assert self.cache[self.cache.iterkeys().next()] == 2
        assert self.cache[next(reversed(self.cache))] == self.size + 1

    def test_removal_insertion_cases(self):
        # test removal of one element
        self.lru.remove(self.entries[3])
        assert len(self.cache) == self.size - 1

        # test removal and re-insertion of first element
        self.lru.remove(self.entries[0])
        assert self.cache.iterkeys().next() == self.entries[1].id

    def test_getters(self):
        assert self.lru.get_item_by_id(0).data == 1
        assert self.lru.get_item_by_value(1).id == 0
Beispiel #3
0
 def tearDown(self):
      self.lru = LeastRecentlyUsedCache(8)