import { beforeEach, describe, expect, it, } from "vitest"; import { LRUCache, } from "./cache.js"; describe("LRUCache", () => { describe("constructor", () => { it("should create cache with valid size", () => { const cache = new LRUCache(10); expect(cache.capacity()).toBe(10); expect(cache.size()).toBe(0); }); it("should throw error for non-positive size", () => { expect(() => new LRUCache(0)).toThrow("Cache size must be positive"); expect(() => new LRUCache(-1)).toThrow("Cache size must be positive"); }); }); describe("add and get", () => { let cache: LRUCache; beforeEach(() => { cache = new LRUCache(3); }); it("should add and retrieve values", () => { cache.add("a", 1); cache.add("b", 2); cache.add("c", 3); expect(cache.get("a")).toBe(1); expect(cache.get("b")).toBe(2); expect(cache.get("c")).toBe(3); }); it("should return undefined for non-existent keys", () => { expect(cache.get("nonexistent")).toBeUndefined(); }); it("should update existing values", () => { cache.add("a", 1); const oldValue = cache.add("a", 10); expect(oldValue).toBe(1); expect(cache.get("a")).toBe(10); }); it("should increase size when adding new items", () => { expect(cache.size()).toBe(0); cache.add("a", 1); expect(cache.size()).toBe(1); cache.add("b", 2); expect(cache.size()).toBe(2); cache.add("c", 3); expect(cache.size()).toBe(3); }); it("should not increase size when updating existing items", () => { cache.add("a", 1); cache.add("b", 2); expect(cache.size()).toBe(2); cache.add("a", 10); expect(cache.size()).toBe(2); }); }); describe("LRU eviction", () => { let cache: LRUCache; beforeEach(() => { cache = new LRUCache(3); }); it("should evict least recently used item when capacity exceeded", () => { cache.add("a", 1); cache.add("b", 2); cache.add("c", 3); // Cache is now full: [c, b, a] (c is most recent) const evicted = cache.add("d", 4); // "a" should be evicted as it's the least recently used expect(evicted).toBe(1); expect(cache.size()).toBe(3); expect(cache.get("a")).toBeUndefined(); expect(cache.get("b")).toBe(2); expect(cache.get("c")).toBe(3); expect(cache.get("d")).toBe(4); }); it("should update LRU order on get", () => { cache.add("a", 1); cache.add("b", 2); cache.add("c", 3); // Cache: [c, b, a] // Access "a" to make it most recent cache.get("a"); // Cache: [a, c, b] // Add "d" - "b" should be evicted const evicted = cache.add("d", 4); expect(evicted).toBe(2); expect(cache.get("a")).toBe(1); expect(cache.get("b")).toBeUndefined(); expect(cache.get("c")).toBe(3); expect(cache.get("d")).toBe(4); }); it("should update LRU order on add (update)", () => { cache.add("a", 1); cache.add("b", 2); cache.add("c", 3); // Cache: [c, b, a] // Update "a" to make it most recent cache.add("a", 10); // Cache: [a, c, b] // Add "d" - "b" should be evicted const evicted = cache.add("d", 4); expect(evicted).toBe(2); expect(cache.get("a")).toBe(10); expect(cache.get("b")).toBeUndefined(); expect(cache.get("c")).toBe(3); expect(cache.get("d")).toBe(4); }); it("should handle multiple evictions correctly", () => { cache.add("a", 1); cache.add("b", 2); cache.add("c", 3); // Evict "a" let evicted = cache.add("d", 4); expect(evicted).toBe(1); // Evict "b" evicted = cache.add("e", 5); expect(evicted).toBe(2); // Evict "c" evicted = cache.add("f", 6); expect(evicted).toBe(3); expect(cache.get("d")).toBe(4); expect(cache.get("e")).toBe(5); expect(cache.get("f")).toBe(6); expect(cache.size()).toBe(3); }); }); describe("has", () => { let cache: LRUCache; beforeEach(() => { cache = new LRUCache(3); }); it("should return true for existing keys", () => { cache.add("a", 1); expect(cache.has("a")).toBe(true); }); it("should return false for non-existent keys", () => { expect(cache.has("nonexistent")).toBe(false); }); it("should not affect LRU order", () => { cache.add("a", 1); cache.add("b", 2); cache.add("c", 3); // Check "a" exists (should not move it to front) expect(cache.has("a")).toBe(true); // Add "d" - "a" should still be evicted const evicted = cache.add("d", 4); expect(evicted).toBe(1); }); }); describe("remove", () => { let cache: LRUCache; beforeEach(() => { cache = new LRUCache(3); }); it("should remove existing items", () => { cache.add("a", 1); cache.add("b", 2); const removed = cache.remove("a"); expect(removed).toBe(1); expect(cache.size()).toBe(1); expect(cache.get("a")).toBeUndefined(); expect(cache.get("b")).toBe(2); }); it("should return undefined for non-existent keys", () => { const removed = cache.remove("nonexistent"); expect(removed).toBeUndefined(); }); it("should handle removing all items", () => { cache.add("a", 1); cache.add("b", 2); cache.add("c", 3); cache.remove("a"); cache.remove("b"); cache.remove("c"); expect(cache.size()).toBe(0); }); it("should allow adding after remove", () => { cache.add("a", 1); cache.add("b", 2); cache.add("c", 3); cache.remove("b"); cache.add("d", 4); expect(cache.size()).toBe(3); expect(cache.get("a")).toBe(1); expect(cache.get("b")).toBeUndefined(); expect(cache.get("c")).toBe(3); expect(cache.get("d")).toBe(4); }); }); describe("edge cases", () => { it("should work with capacity of 1", () => { const cache = new LRUCache(1); cache.add("a", 1); expect(cache.get("a")).toBe(1); const evicted = cache.add("b", 2); expect(evicted).toBe(1); expect(cache.get("a")).toBeUndefined(); expect(cache.get("b")).toBe(2); }); it("should handle complex types as keys", () => { const cache = new LRUCache(2); const key1 = { id: 1, }; const key2 = { id: 2, }; cache.add(key1, "value1"); cache.add(key2, "value2"); expect(cache.get(key1)).toBe("value1"); expect(cache.get(key2)).toBe("value2"); }); it("should handle null and undefined values", () => { const cache = new LRUCache(2); cache.add("null", null); cache.add("undefined", undefined); expect(cache.get("null")).toBeNull(); expect(cache.get("undefined")).toBeUndefined(); expect(cache.has("null")).toBe(true); expect(cache.has("undefined")).toBe(true); }); it("should maintain correct size during mixed operations", () => { const cache = new LRUCache(3); expect(cache.size()).toBe(0); cache.add("a", 1); expect(cache.size()).toBe(1); cache.add("b", 2); expect(cache.size()).toBe(2); cache.remove("a"); expect(cache.size()).toBe(1); cache.add("c", 3); expect(cache.size()).toBe(2); cache.add("d", 4); expect(cache.size()).toBe(3); cache.add("e", 5); // Evicts "b" expect(cache.size()).toBe(3); }); }); describe("stress test", () => { it("should handle large number of operations", () => { const cache = new LRUCache(100); // Add 200 items (should evict first 100) for (let i = 0; i < 200; i++) { cache.add(i, i * 10); } expect(cache.size()).toBe(100); // First 100 should be evicted for (let i = 0; i < 100; i++) { expect(cache.get(i)).toBeUndefined(); } // Last 100 should exist for (let i = 100; i < 200; i++) { expect(cache.get(i)).toBe(i * 10); } }); }); });