std/cache/lru_cache_test.ts
lionel-rowe 0c64f32cc3
feat(cache/unstable): add memoize() and LruCache (#4725)
Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
2024-08-09 01:12:24 +09:00

25 lines
690 B
TypeScript

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals } from "@std/assert";
import { LruCache } from "./lru_cache.ts";
Deno.test("LruCache deletes least-recently-used", () => {
const cache = new LruCache(3);
cache.set(1, "!");
cache.set(2, "!");
cache.set(1, "updated");
cache.set(3, "!");
cache.set(4, "!");
assertEquals(cache.size, 3);
assert(!cache.has(2));
assertEquals(cache.get(2), undefined);
assertEquals([...cache.keys()], [1, 3, 4]);
assertEquals(cache.get(3), "!");
assertEquals(cache.get(1), "updated");
cache.delete(3);
assertEquals(cache.size, 2);
assertEquals(cache.get(3), undefined);
});