Example #1
0
 def test2(self):
     lru = LRUCache(1)
     lru.set(2, 1)
     lru.set(3, 2)
     res = lru.get(2)
     self.assertEqual(res, -1)
     print res
Example #2
0
def test_1():
    lru = LRUCache(1)
    k_prev = np.random.randint(-100, 100)
    for _ in xrange(10):
        k, v = np.random.randint(-100, 100, 2)
        lru.set(k, v)
        assert lru.get(k) == v
        assert lru.get(k_prev) == -1
        k_prev = k
Example #3
0
def test_5():
    lru = LRUCache(2)
    lru.set(1, 1)
    lru.set(2, 2)
    assert lru.get(2) == 2
    lru.set(3, 3)
    assert lru.get(1) == -1
    assert lru.get(2) == 2
    assert lru.get(3) == 3
Example #4
0
def test_4():
    lru = LRUCache(2)
    lru.set(2, 1)
    lru.set(2, 2)
    assert lru.get(2) == 2
    lru.set(1, 1)
    lru.set(4, 1)
    assert lru.get(2) == -1
Example #5
0
    def test001(self):
        
        print 'test 001\n'
        
        startTime = time.clock()
        c = LRUCache(1)
        c.set(2, 1)
        print c.get(2)
        c.set(3, 2)
        print c.get(2)
        print c.get(3)
        timeUsed = time.clock() - startTime
        
#         print '  input:\t{0}'.format(n)
#         print '  expect:\t{0}'.format(?)
        print '  output:\t{0}'.format(c)
        print '  time used:\t{0:.6f}'.format(timeUsed)
        print
Example #6
0
class importTests(unittest.TestCase):
    def setUp(self):
        self.cache = LRUCache(2)

    def test_all(self):
        self.assertEqual(None, self.cache.put(1, 1))
        self.assertEqual(None, self.cache.put(2, 2))
        self.assertEqual(1, self.cache.get(1))
        self.assertEqual(None, self.cache.put(3, 3))
        self.assertEqual(-1, self.cache.get(2))
        self.assertEqual(None, self.cache.put(4, 4))
        self.assertEqual(-1, self.cache.get(1))
        self.assertEqual(3, self.cache.get(3))
        self.assertEqual(4, self.cache.get(4))
Example #7
0
class LRUCacheTests(unittest.TestCase):
    """Tests LRUCache."""
    def setUp(self):
        """Set up our SUT for testing."""
        self.sut = LRUCache(capacity=2)

    def test_basic(self):
        """Test using as basic as possible, with valid empty input."""
        self.assertEqual(None, self.sut.put(1, 1))
        self.assertEqual(None, self.sut.put(2, 2))
        self.assertEqual(1, self.sut.get(1))
        self.assertEqual(None, self.sut.put(3, 3))
        self.assertEqual(-1, self.sut.get(2))
        self.assertEqual(None, self.sut.put(4, 4))
        self.assertEqual(-1, self.sut.get(1))
        self.assertEqual(3, self.sut.get(3))
        self.assertEqual(4, self.sut.get(4))
Example #8
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from solution import LRUCache
from solution import DLinkNode

lru = LRUCache(3)
lru.set(1, 1)
lru.set(2, 2)
lru.set(3, 3)
lru.set(4, 4)
print(lru.get(4))
print(lru.get(3))
print(lru.get(2))
print(lru.get(1))
lru.set(5, 5)
print(lru.get(1))
print(lru.get(2))
print(lru.get(3))
print(lru.get(4))
print(lru.get(5))
Example #9
0
    def test000(self):
        
        print 'test 000\n'
        
        startTime = time.clock()
        c = LRUCache(3)
        c.set(0, 'a')
        print c.get(0), c.get(1), c.get(2), c.get(3)
        c.set(1, 'b')
        print c.get(0), c.get(1), c.get(2), c.get(3)
        c.set(0, 'c')
        print c.get(0), c.get(1), c.get(2), c.get(3)
        c.set(2, 'd')
        print c.get(0), c.get(1), c.get(2), c.get(3)
        c.set(1, 'e')
        print c.get(0), c.get(1), c.get(2), c.get(3)
        timeUsed = time.clock() - startTime
        
#         print '  input:\t{0}'.format(n)
#         print '  expect:\t{0}'.format(?)
        print '  output:\t{0}'.format(c)
        print '  time used:\t{0:.6f}'.format(timeUsed)
        print
Example #10
0
 def setUp(self):
     self.cache = LRUCache(2)
Example #11
0
def test_3():
    lru = LRUCache(3)
    assert lru.get(3) == -1
    lru.set(1, 1)
    lru.set(2, 2)
    lru.set(3, 3)
    lru.set(4, 4)
    assert lru.get(1) == -1
    assert lru.get(2) == 2
    assert lru.get(3) == 3
    assert lru.get(4) == 4
    lru.set(5, 5)
    assert lru.get(2) == -1
    assert lru.get(3) == 3
    assert lru.get(4) == 4
    assert lru.get(5) == 5
Example #12
0
def test_2():
    lru = LRUCache(2)
    assert lru.get(2) == -1
    lru.set(1, 1)
    assert lru.get(1) == 1
    lru.set(2, 2)
    lru.set(3, 3)
    assert lru.get(1) == -1
    assert lru.get(2) == 2
    lru.set(4, 4)
    assert lru.get(3) == -1
    assert lru.get(2) == 2
    assert lru.get(4) == 4
Example #13
0
def test_0():
    lru = LRUCache(0)
    for _ in xrange(10):
        k, v = np.random.randint(-100, 100, 2)
        lru.set(k, v)
        assert lru.get(k) == -1
Example #14
0
 def setUp(self):
     """Set up our SUT for testing."""
     self.sut = LRUCache(capacity=2)
Example #15
0
def main():
    test = LRUCache(3)
    test.put(1, "one")
    test.put(2, "two")
    test.put(3, "three")
    assert test.cache == {1: "one", 2: "two", 3: "three"}
    test.put(3, "four")
    assert test.cache == {1: "one", 2: "two", 3: "four"}
    test.put(4, "five")
    test.put(5, "six")
    assert test.cache == {3: "four", 4: "five", 5: "six"}
    print("Yayy! All the assert statements are passed.")
Example #16
0
def test_solution():
    cache = LRUCache(2)
    cache.put(1, 1)
    cache.put(2, 2)
    assert cache.get(1) == 1
    cache.put(3, 3)
    assert cache.get(2) == -1
    cache.put(4, 4)
    assert cache.get(1) == -1
    assert cache.get(3) == 3
    assert cache.get(4) == 4