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.")
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))
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))
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