mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-04 15:39:42 +08:00
LFUCache/LRUCache : convert live code example to Jest test.
This commit is contained in:
@ -103,41 +103,4 @@ class LFUCache {
|
||||
}
|
||||
}
|
||||
|
||||
function main () {
|
||||
// Example 1 (Small Cache)
|
||||
const cache = new LFUCache(2)
|
||||
cache.set(1, 1)
|
||||
cache.set(2, 2)
|
||||
|
||||
console.log(cache.get(1))
|
||||
|
||||
cache.set(3, 3)
|
||||
|
||||
console.log(cache.get(2)) // cache miss
|
||||
|
||||
cache.set(4, 4)
|
||||
|
||||
console.log(cache.get(1)) // cache miss
|
||||
console.log(cache.get(3))
|
||||
console.log(cache.get(4))
|
||||
|
||||
console.log('Example Cache: ', cache.cacheInfo(), '\n')
|
||||
|
||||
// Example 2 (Computing Fibonacci Series - 100 terms)
|
||||
function fib (num, cache = null) {
|
||||
if (cache) {
|
||||
const value = cache.get(num)
|
||||
if (value) { return value }
|
||||
}
|
||||
if (num === 1 || num === 2) { return 1 }
|
||||
const result = fib(num - 1, cache) + fib(num - 2, cache)
|
||||
if (cache) { cache.set(num, result) }
|
||||
return result
|
||||
}
|
||||
|
||||
const fibCache = new LFUCache(100)
|
||||
for (let i = 1; i <= 100; i++) { fib(i, fibCache) }
|
||||
console.log('Fibonacci Series Cache: ', fibCache.cacheInfo(), '\n')
|
||||
}
|
||||
|
||||
main()
|
||||
export { LFUCache }
|
||||
|
@ -86,41 +86,4 @@ class LRUCache {
|
||||
}
|
||||
}
|
||||
|
||||
function main () {
|
||||
// Example 1 (Small Cache)
|
||||
const cache = new LRUCache(2)
|
||||
cache.set(1, 1)
|
||||
cache.set(2, 2)
|
||||
|
||||
console.log(cache.get(1))
|
||||
|
||||
cache.set(3, 3)
|
||||
|
||||
console.log(cache.get(2)) // cache miss
|
||||
|
||||
cache.set(4, 4)
|
||||
|
||||
console.log(cache.get(1)) // cache miss
|
||||
console.log(cache.get(3))
|
||||
console.log(cache.get(4))
|
||||
|
||||
console.log('Example Cache: ', cache.cacheInfo(), '\n')
|
||||
|
||||
// Example 2 (Computing Fibonacci Series - 100 terms)
|
||||
function fib (num, cache = null) {
|
||||
if (cache) {
|
||||
const value = cache.get(num)
|
||||
if (value) { return value }
|
||||
}
|
||||
if (num === 1 || num === 2) { return 1 }
|
||||
const result = fib(num - 1, cache) + fib(num - 2, cache)
|
||||
if (cache) { cache.set(num, result) }
|
||||
return result
|
||||
}
|
||||
|
||||
const fibCache = new LRUCache(100)
|
||||
for (let i = 1; i <= 100; i++) { fib(i, fibCache) }
|
||||
console.log('Fibonacci Series Cache: ', fibCache.cacheInfo(), '\n')
|
||||
}
|
||||
|
||||
main()
|
||||
export { LRUCache }
|
||||
|
54
Cache/test/LFUCache.test.js
Normal file
54
Cache/test/LFUCache.test.js
Normal file
@ -0,0 +1,54 @@
|
||||
import { LFUCache } from '../LFUCache'
|
||||
|
||||
describe('LFUCache', () => {
|
||||
it('Example 1 (Small Cache, size=2)', () => {
|
||||
const cache = new LFUCache(2)
|
||||
cache.set(1, 1)
|
||||
cache.set(2, 2)
|
||||
|
||||
expect(cache.get(1)).toBe(1)
|
||||
expect(cache.get(2)).toBe(2)
|
||||
|
||||
// Additional entries triggers cache rotate
|
||||
cache.set(3, 3)
|
||||
|
||||
// Then we should have a cache miss for the first entry added
|
||||
expect(cache.get(1)).toBe(null)
|
||||
expect(cache.get(2)).toBe(2)
|
||||
expect(cache.get(3)).toBe(3)
|
||||
|
||||
cache.set(4, 4)
|
||||
expect(cache.get(1)).toBe(null) // cache miss
|
||||
expect(cache.get(2)).toBe(null) // cache miss
|
||||
expect(cache.get(3)).toBe(3)
|
||||
expect(cache.get(4)).toBe(4)
|
||||
|
||||
expect(cache.cacheInfo()).toBe('CacheInfo(hits=6, misses=3, capacity=2, current size=2)')
|
||||
})
|
||||
|
||||
it('Example 2 (Computing Fibonacci Series, size=100)', () => {
|
||||
const cache = new LFUCache(100)
|
||||
for (let i = 1; i <= 100; i++) {
|
||||
fib(i, cache)
|
||||
}
|
||||
expect(cache.cacheInfo()).toBe('CacheInfo(hits=193, misses=103, capacity=100, current size=98)')
|
||||
})
|
||||
})
|
||||
|
||||
// Helper for building and caching Fibonacci series
|
||||
function fib (num, cache = null) {
|
||||
if (cache) {
|
||||
const value = cache.get(num)
|
||||
if (value) {
|
||||
return value
|
||||
}
|
||||
}
|
||||
if (num === 1 || num === 2) {
|
||||
return 1
|
||||
}
|
||||
const result = fib(num - 1, cache) + fib(num - 2, cache)
|
||||
if (cache) {
|
||||
cache.set(num, result)
|
||||
}
|
||||
return result
|
||||
}
|
54
Cache/test/LRUCache.test.js
Normal file
54
Cache/test/LRUCache.test.js
Normal file
@ -0,0 +1,54 @@
|
||||
import { LRUCache } from '../LRUCache'
|
||||
|
||||
describe('LRUCache', () => {
|
||||
it('Example 1 (Small Cache, size=2)', () => {
|
||||
const cache = new LRUCache(2)
|
||||
cache.set(1, 1)
|
||||
cache.set(2, 2)
|
||||
|
||||
expect(cache.get(1)).toBe(1)
|
||||
expect(cache.get(2)).toBe(2)
|
||||
|
||||
// Additional entries triggers cache rotate
|
||||
cache.set(3, 3)
|
||||
|
||||
// Then we should have a cache miss for the first entry added
|
||||
expect(cache.get(1)).toBe(null)
|
||||
expect(cache.get(2)).toBe(2)
|
||||
expect(cache.get(3)).toBe(3)
|
||||
|
||||
cache.set(4, 4)
|
||||
expect(cache.get(1)).toBe(null) // cache miss
|
||||
expect(cache.get(2)).toBe(null) // cache miss
|
||||
expect(cache.get(3)).toBe(3)
|
||||
expect(cache.get(4)).toBe(4)
|
||||
|
||||
expect(cache.cacheInfo()).toBe('CacheInfo(hits=6, misses=3, capacity=2, current size=2)')
|
||||
})
|
||||
|
||||
it('Example 2 (Computing Fibonacci Series, size=100)', () => {
|
||||
const cache = new LRUCache(100)
|
||||
for (let i = 1; i <= 100; i++) {
|
||||
fib(i, cache)
|
||||
}
|
||||
expect(cache.cacheInfo()).toBe('CacheInfo(hits=193, misses=103, capacity=100, current size=98)')
|
||||
})
|
||||
})
|
||||
|
||||
// Helper for building and caching Fibonacci series
|
||||
function fib (num, cache = null) {
|
||||
if (cache) {
|
||||
const value = cache.get(num)
|
||||
if (value) {
|
||||
return value
|
||||
}
|
||||
}
|
||||
if (num === 1 || num === 2) {
|
||||
return 1
|
||||
}
|
||||
const result = fib(num - 1, cache) + fib(num - 2, cache)
|
||||
if (cache) {
|
||||
cache.set(num, result)
|
||||
}
|
||||
return result
|
||||
}
|
Reference in New Issue
Block a user