|
| 1 | +import XCTest |
| 2 | +@testable import Cache |
| 3 | + |
| 4 | +final class LRUCacheTests: XCTestCase { |
| 5 | + func testLRUCacheCapacity() { |
| 6 | + let cache = LRUCache<String, Int>(capacity: 3) |
| 7 | + |
| 8 | + // Add some key-value pairs |
| 9 | + cache["one"] = 1 |
| 10 | + cache["two"] = 2 |
| 11 | + cache["three"] = 3 |
| 12 | + |
| 13 | + // Test that the cache has the expected contents |
| 14 | + XCTAssertEqual(cache["one"], 1) |
| 15 | + XCTAssertEqual(cache["two"], 2) |
| 16 | + XCTAssertEqual(cache["three"], 3) |
| 17 | + |
| 18 | + // Add a new key-value pair to exceed the capacity |
| 19 | + cache["four"] = 4 |
| 20 | + |
| 21 | + // Test that the least recently used key was removed |
| 22 | + XCTAssertNil(cache["one"]) |
| 23 | + |
| 24 | + // Test that the cache has the expected contents |
| 25 | + XCTAssertEqual(cache["two"], 2) |
| 26 | + XCTAssertEqual(cache["three"], 3) |
| 27 | + XCTAssertEqual(cache["four"], 4) |
| 28 | + |
| 29 | + // Access an existing key to promote it to the end of the keys array |
| 30 | + XCTAssert(cache.contains("two")) |
| 31 | + |
| 32 | + // Add another key-value pair to exceed the capacity |
| 33 | + cache["five"] = 5 |
| 34 | + |
| 35 | + // Test that the least recently used key was removed |
| 36 | + XCTAssertNil(cache["three"]) |
| 37 | + |
| 38 | + // Test that the cache has the expected contents |
| 39 | + XCTAssertEqual(cache["two"], 2) |
| 40 | + XCTAssertEqual(cache["four"], 4) |
| 41 | + XCTAssertEqual(cache["five"], 5) |
| 42 | + |
| 43 | + // Remove a key and test that it was removed from both the cache and the keys array |
| 44 | + cache["two"] = nil |
| 45 | + XCTAssertNil(cache["two"]) |
| 46 | + XCTAssertFalse(cache.contains("two")) |
| 47 | + } |
| 48 | + |
| 49 | + func testLRUCacheInitialValues() { |
| 50 | + let cache = LRUCache<String, Int>( |
| 51 | + initialValues: [ |
| 52 | + "one": 1, |
| 53 | + "two": 2, |
| 54 | + "three": 3 |
| 55 | + ] |
| 56 | + ) |
| 57 | + |
| 58 | + // Test that the cache has the expected contents |
| 59 | + XCTAssertEqual(cache["one"], 1) |
| 60 | + XCTAssertEqual(cache["two"], 2) |
| 61 | + XCTAssertEqual(cache["three"], 3) |
| 62 | + |
| 63 | + // Add a new key-value pair to exceed the capacity |
| 64 | + cache["four"] = 4 |
| 65 | + |
| 66 | + // Test that the least recently used key was removed |
| 67 | + XCTAssertNil(cache["one"]) |
| 68 | + |
| 69 | + // Test that the cache has the expected contents |
| 70 | + XCTAssertEqual(cache["two"], 2) |
| 71 | + XCTAssertEqual(cache["three"], 3) |
| 72 | + XCTAssertEqual(cache["four"], 4) |
| 73 | + |
| 74 | + // Access an existing key to promote it to the end of the keys array |
| 75 | + XCTAssert(cache.contains("two")) |
| 76 | + |
| 77 | + // Add another key-value pair to exceed the capacity |
| 78 | + cache["five"] = 5 |
| 79 | + |
| 80 | + // Test that the least recently used key was removed |
| 81 | + XCTAssertNil(cache["three"]) |
| 82 | + |
| 83 | + // Test that the cache has the expected contents |
| 84 | + XCTAssertEqual(cache["two"], 2) |
| 85 | + XCTAssertEqual(cache["four"], 4) |
| 86 | + XCTAssertEqual(cache["five"], 5) |
| 87 | + |
| 88 | + // Remove a key and test that it was removed from both the cache and the keys array |
| 89 | + cache["two"] = nil |
| 90 | + XCTAssertNil(cache["two"]) |
| 91 | + XCTAssertFalse(cache.contains("two")) |
| 92 | + } |
| 93 | +} |
0 commit comments