mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2026-03-13 15:21:15 +08:00
merge: Remove duplicate helper functions in cache tests. (#933)
* refactor: added one implementation of fibonacciCahce * chore: move union function * chore: renamed the cacheTest file
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { LRUCache } from '../LRUCache'
|
||||
import { fibonacciCache } from './cacheTest'
|
||||
|
||||
describe('LRUCache', () => {
|
||||
it('Example 1 (Small Cache, size=2)', () => {
|
||||
@@ -29,26 +30,8 @@ describe('LRUCache', () => {
|
||||
it('Example 2 (Computing Fibonacci Series, size=100)', () => {
|
||||
const cache = new LRUCache(100)
|
||||
for (let i = 1; i <= 100; i++) {
|
||||
fib(i, cache)
|
||||
fibonacciCache(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