From ab7e51909aa09569cdc1755f0454c2609308a531 Mon Sep 17 00:00:00 2001 From: Eric Lavault <39483232+lvlte@users.noreply.github.com> Date: Sat, 9 Oct 2021 18:01:42 +0200 Subject: [PATCH] LFUCache/LRUCache : convert live code example to Jest test. --- Cache/LFUCache.js | 39 +-------------------------- Cache/LRUCache.js | 39 +-------------------------- Cache/test/LFUCache.test.js | 54 +++++++++++++++++++++++++++++++++++++ Cache/test/LRUCache.test.js | 54 +++++++++++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+), 76 deletions(-) create mode 100644 Cache/test/LFUCache.test.js create mode 100644 Cache/test/LRUCache.test.js diff --git a/Cache/LFUCache.js b/Cache/LFUCache.js index 7238b9cb7..67974f0ff 100644 --- a/Cache/LFUCache.js +++ b/Cache/LFUCache.js @@ -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 } diff --git a/Cache/LRUCache.js b/Cache/LRUCache.js index 5106331df..1bee80089 100644 --- a/Cache/LRUCache.js +++ b/Cache/LRUCache.js @@ -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 } diff --git a/Cache/test/LFUCache.test.js b/Cache/test/LFUCache.test.js new file mode 100644 index 000000000..5e4bd67ac --- /dev/null +++ b/Cache/test/LFUCache.test.js @@ -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 +} diff --git a/Cache/test/LRUCache.test.js b/Cache/test/LRUCache.test.js new file mode 100644 index 000000000..6ebeb1a01 --- /dev/null +++ b/Cache/test/LRUCache.test.js @@ -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 +}