Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

lru.test.ts

24 lines751 Bytes

1import { describe, expect, it } from 'vitest'
2import { createLRUCache } from '../src/lru-cache'
3​
4describe('LRU Cache', () => {
5 it('evicts oldest set', () => {
6 const cache = createLRUCache<string, number>(3)
7 cache.set('a', 1)
8 cache.set('b', 2)
9 cache.set('c', 3)
10 cache.set('d', 4) // 'a' should be evicted
11 expect(cache.get('a')).toBeUndefined()
12 expect(cache.get('b')).toBe(2)
13 })
14 it('evicts oldest used', () => {
15 const cache = createLRUCache<string, number>(3)
16 cache.set('a', 1)
17 cache.set('b', 2)
18 cache.set('c', 3)
19 cache.get('a') // 'a' is now the most recently used
20 cache.set('d', 4) // 'b' should be evicted
21 expect(cache.get('b')).toBeUndefined()
22 expect(cache.get('a')).toBe(1)
23 })
24})