|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { MapLru } from "./memory.advanced.js"; |
| 3 | + |
| 4 | +describe("MapLru", () => { |
| 5 | + it("should throw an error for invalid capacity", () => { |
| 6 | + expect(() => new MapLru(0)).toThrow("Capacity must be a positive integer"); |
| 7 | + expect(() => new MapLru(-1)).toThrow("Capacity must be a positive integer"); |
| 8 | + expect(() => new MapLru(1.5)).toThrow( |
| 9 | + "Capacity must be a positive integer", |
| 10 | + ); |
| 11 | + }); |
| 12 | + |
| 13 | + it("should set and get values correctly", () => { |
| 14 | + const cache = new MapLru<string, number>(2); |
| 15 | + cache.set("a", 1); |
| 16 | + cache.set("b", 2); |
| 17 | + |
| 18 | + expect(cache.get("a")).toBe(1); |
| 19 | + expect(cache.get("b")).toBe(2); |
| 20 | + expect(cache.size).toBe(2); |
| 21 | + }); |
| 22 | + |
| 23 | + it("should evict the least recently used item when capacity is exceeded", () => { |
| 24 | + const cache = new MapLru<string, number>(2); |
| 25 | + cache.set("a", 1); |
| 26 | + cache.set("b", 2); |
| 27 | + cache.set("c", 3); // This should evict "a" |
| 28 | + |
| 29 | + expect(cache.has("a")).toBe(false); |
| 30 | + expect(cache.get("b")).toBe(2); |
| 31 | + expect(cache.get("c")).toBe(3); |
| 32 | + expect(cache.size).toBe(2); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should update the recently used status on get, affecting eviction order", () => { |
| 36 | + const cache = new MapLru<string, number>(2); |
| 37 | + cache.set("a", 1); |
| 38 | + cache.set("b", 2); |
| 39 | + cache.get("a"); // "a" is now the most recently used |
| 40 | + cache.set("c", 3); // This should evict "b" |
| 41 | + |
| 42 | + expect(cache.has("b")).toBe(false); |
| 43 | + expect(cache.get("a")).toBe(1); |
| 44 | + expect(cache.get("c")).toBe(3); |
| 45 | + expect(cache.size).toBe(2); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should handle deletion correctly", () => { |
| 49 | + const cache = new MapLru<string, number>(2); |
| 50 | + cache.set("a", 1); |
| 51 | + cache.set("b", 2); |
| 52 | + cache.delete("a"); |
| 53 | + |
| 54 | + expect(cache.has("a")).toBe(false); |
| 55 | + expect(cache.get("b")).toBe(2); |
| 56 | + expect(cache.size).toBe(1); |
| 57 | + |
| 58 | + // @ts-expect-error - accessing private property for testing |
| 59 | + expect(cache.lru.has("a")).toBe(false); |
| 60 | + }); |
| 61 | + |
| 62 | + describe("iteration behavior", () => { |
| 63 | + it("should handle LRU updates when an item is accessed during iteration", () => { |
| 64 | + const cache = new MapLru<string, number>(3); |
| 65 | + cache.set("a", 1).set("b", 2).set("c", 3); |
| 66 | + |
| 67 | + // Initial LRU order: a, b, c (c is MRU) |
| 68 | + // @ts-expect-error - accessing private property for testing |
| 69 | + expect(Array.from(cache.lru.keys())).toEqual(["a", "b", "c"]); |
| 70 | + |
| 71 | + for (const [key] of cache.entries()) { |
| 72 | + if (key === "b") { |
| 73 | + cache.get("a"); // Access 'a', making it the new MRU |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // Final LRU order should be: b, c, a |
| 78 | + // @ts-expect-error - accessing private property for testing |
| 79 | + expect(Array.from(cache.lru.keys())).toEqual(["b", "c", "a"]); |
| 80 | + |
| 81 | + // Adding a new item should evict the new LRU item, which is 'b' |
| 82 | + cache.set("d", 4); |
| 83 | + expect(cache.has("b")).toBe(false); |
| 84 | + expect(cache.has("c")).toBe(true); |
| 85 | + expect(cache.has("a")).toBe(true); |
| 86 | + expect(cache.has("d")).toBe(true); |
| 87 | + }); |
| 88 | + |
| 89 | + it("should handle modifications and evictions during iteration", () => { |
| 90 | + const cache = new MapLru<string, number>(3); |
| 91 | + cache.set("a", 1).set("b", 2).set("c", 3); |
| 92 | + |
| 93 | + // Initial state: keys are [a, b, c], LRU order is [a, b, c] |
| 94 | + const visited: string[] = []; |
| 95 | + // The standard Map iterator will visit newly added items. |
| 96 | + // When we add "d", "a" gets evicted. The iterator, having already passed "a", |
| 97 | + // will continue to "c" and then visit the new item "d". |
| 98 | + for (const [key] of cache.entries()) { |
| 99 | + visited.push(key); |
| 100 | + if (key === "b") { |
| 101 | + cache.set("d", 4); // This will evict 'a' |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + expect(visited).toEqual(["a", "b", "c", "d"]); |
| 106 | + |
| 107 | + // Final state of the cache |
| 108 | + expect(cache.has("a")).toBe(false); |
| 109 | + expect(cache.size).toBe(3); |
| 110 | + expect(Array.from(cache.keys())).toEqual(["b", "c", "d"]); |
| 111 | + }); |
| 112 | + }); |
| 113 | +}); |
0 commit comments