mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 01:18:23 +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 { LFUCache } from '../LFUCache'
|
import { LFUCache } from '../LFUCache'
|
||||||
|
import { fibonacciCache } from './cacheTest'
|
||||||
|
|
||||||
describe('LFUCache', () => {
|
describe('LFUCache', () => {
|
||||||
it('Example 1 (Small Cache, size=2)', () => {
|
it('Example 1 (Small Cache, size=2)', () => {
|
||||||
@ -28,27 +29,11 @@ describe('LFUCache', () => {
|
|||||||
|
|
||||||
it('Example 2 (Computing Fibonacci Series, size=100)', () => {
|
it('Example 2 (Computing Fibonacci Series, size=100)', () => {
|
||||||
const cache = new LFUCache(100)
|
const cache = new LFUCache(100)
|
||||||
|
|
||||||
for (let i = 1; i <= 100; i++) {
|
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)')
|
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
|
|
||||||
}
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { LRUCache } from '../LRUCache'
|
import { LRUCache } from '../LRUCache'
|
||||||
|
import { fibonacciCache } from './cacheTest'
|
||||||
|
|
||||||
describe('LRUCache', () => {
|
describe('LRUCache', () => {
|
||||||
it('Example 1 (Small Cache, size=2)', () => {
|
it('Example 1 (Small Cache, size=2)', () => {
|
||||||
@ -29,26 +30,8 @@ describe('LRUCache', () => {
|
|||||||
it('Example 2 (Computing Fibonacci Series, size=100)', () => {
|
it('Example 2 (Computing Fibonacci Series, size=100)', () => {
|
||||||
const cache = new LRUCache(100)
|
const cache = new LRUCache(100)
|
||||||
for (let i = 1; i <= 100; i++) {
|
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)')
|
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
|
|
||||||
}
|
|
||||||
|
@ -1,20 +1,10 @@
|
|||||||
import { memoize } from '../Memoize'
|
import { memoize } from '../Memoize'
|
||||||
|
import { union } from './cacheTest'
|
||||||
import { fibonacci } from '../../Dynamic-Programming/FibonacciNumber'
|
import { fibonacci } from '../../Dynamic-Programming/FibonacciNumber'
|
||||||
import { factorial } from '../../Recursive/Factorial'
|
import { factorial } from '../../Recursive/Factorial'
|
||||||
|
|
||||||
const multipleFactorials = (arr) => arr.map(factorial)
|
const multipleFactorials = (arr) => arr.map(factorial)
|
||||||
|
|
||||||
/**
|
|
||||||
* @title implementation of union function
|
|
||||||
* @param {Set} sets
|
|
||||||
* @return {new Set}
|
|
||||||
*/
|
|
||||||
function union (...sets) {
|
|
||||||
return new Set(
|
|
||||||
sets.reduce((flatArray, set) => [...flatArray, ...set], [])
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
describe('Testing Memoize', () => {
|
describe('Testing Memoize', () => {
|
||||||
it('expects the fibonacci function to use the cache on the second call', () => {
|
it('expects the fibonacci function to use the cache on the second call', () => {
|
||||||
const memoFibonacci = memoize(fibonacci)
|
const memoFibonacci = memoize(fibonacci)
|
||||||
|
37
Cache/test/cacheTest.js
Normal file
37
Cache/test/cacheTest.js
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/**
|
||||||
|
* @function fibonacciCache
|
||||||
|
* @description - this is a cached variant of fib number
|
||||||
|
* @param {number} n - Real number (n > -1)
|
||||||
|
* @param {Object} cache
|
||||||
|
* @returns {number}
|
||||||
|
*/
|
||||||
|
export const fibonacciCache = (n, cache = null) => {
|
||||||
|
if (cache) {
|
||||||
|
const value = cache.get(n)
|
||||||
|
|
||||||
|
if (value !== null) {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (n === 1 || n === 2) {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = fibonacciCache(n - 1, cache) + fibonacciCache(n - 2, cache)
|
||||||
|
|
||||||
|
cache && cache.set(n, result)
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @title implementation of union function
|
||||||
|
* @param {Set} sets
|
||||||
|
* @return {new Set}
|
||||||
|
*/
|
||||||
|
export const union = (...sets) => {
|
||||||
|
return new Set(
|
||||||
|
sets.reduce((flatArray, set) => [...flatArray, ...set], [])
|
||||||
|
)
|
||||||
|
}
|
Reference in New Issue
Block a user