Skip to content

Commit 9f7ed56

Browse files
authored
merge: Remove duplicate helper functions in cache tests. (TheAlgorithms#933)
* refactor: added one implementation of fibonacciCahce * chore: move union function * chore: renamed the cacheTest file
1 parent bb23382 commit 9f7ed56

File tree

4 files changed

+44
-49
lines changed

4 files changed

+44
-49
lines changed

Cache/test/LFUCache.test.js

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { LFUCache } from '../LFUCache'
2+
import { fibonacciCache } from './cacheTest'
23

34
describe('LFUCache', () => {
45
it('Example 1 (Small Cache, size=2)', () => {
@@ -28,27 +29,11 @@ describe('LFUCache', () => {
2829

2930
it('Example 2 (Computing Fibonacci Series, size=100)', () => {
3031
const cache = new LFUCache(100)
32+
3133
for (let i = 1; i <= 100; i++) {
32-
fib(i, cache)
34+
fibonacciCache(i, cache)
3335
}
36+
3437
expect(cache.cacheInfo()).toBe('CacheInfo(hits=193, misses=103, capacity=100, current size=98)')
3538
})
3639
})
37-
38-
// Helper for building and caching Fibonacci series
39-
function fib (num, cache = null) {
40-
if (cache) {
41-
const value = cache.get(num)
42-
if (value) {
43-
return value
44-
}
45-
}
46-
if (num === 1 || num === 2) {
47-
return 1
48-
}
49-
const result = fib(num - 1, cache) + fib(num - 2, cache)
50-
if (cache) {
51-
cache.set(num, result)
52-
}
53-
return result
54-
}

Cache/test/LRUCache.test.js

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { LRUCache } from '../LRUCache'
2+
import { fibonacciCache } from './cacheTest'
23

34
describe('LRUCache', () => {
45
it('Example 1 (Small Cache, size=2)', () => {
@@ -29,26 +30,8 @@ describe('LRUCache', () => {
2930
it('Example 2 (Computing Fibonacci Series, size=100)', () => {
3031
const cache = new LRUCache(100)
3132
for (let i = 1; i <= 100; i++) {
32-
fib(i, cache)
33+
fibonacciCache(i, cache)
3334
}
3435
expect(cache.cacheInfo()).toBe('CacheInfo(hits=193, misses=103, capacity=100, current size=98)')
3536
})
3637
})
37-
38-
// Helper for building and caching Fibonacci series
39-
function fib (num, cache = null) {
40-
if (cache) {
41-
const value = cache.get(num)
42-
if (value) {
43-
return value
44-
}
45-
}
46-
if (num === 1 || num === 2) {
47-
return 1
48-
}
49-
const result = fib(num - 1, cache) + fib(num - 2, cache)
50-
if (cache) {
51-
cache.set(num, result)
52-
}
53-
return result
54-
}

Cache/test/Memoize.test.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
11
import { memoize } from '../Memoize'
2+
import { union } from './cacheTest'
23
import { fibonacci } from '../../Dynamic-Programming/FibonacciNumber'
34
import { factorial } from '../../Recursive/Factorial'
45

56
const multipleFactorials = (arr) => arr.map(factorial)
67

7-
/**
8-
* @title implementation of union function
9-
* @param {Set} sets
10-
* @return {new Set}
11-
*/
12-
function union (...sets) {
13-
return new Set(
14-
sets.reduce((flatArray, set) => [...flatArray, ...set], [])
15-
)
16-
}
17-
188
describe('Testing Memoize', () => {
199
it('expects the fibonacci function to use the cache on the second call', () => {
2010
const memoFibonacci = memoize(fibonacci)

Cache/test/cacheTest.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @function fibonacciCache
3+
* @description - this is a cached variant of fib number
4+
* @param {number} n - Real number (n > -1)
5+
* @param {Object} cache
6+
* @returns {number}
7+
*/
8+
export const fibonacciCache = (n, cache = null) => {
9+
if (cache) {
10+
const value = cache.get(n)
11+
12+
if (value !== null) {
13+
return value
14+
}
15+
}
16+
17+
if (n === 1 || n === 2) {
18+
return 1
19+
}
20+
21+
const result = fibonacciCache(n - 1, cache) + fibonacciCache(n - 2, cache)
22+
23+
cache && cache.set(n, result)
24+
25+
return result
26+
}
27+
28+
/**
29+
* @title implementation of union function
30+
* @param {Set} sets
31+
* @return {new Set}
32+
*/
33+
export const union = (...sets) => {
34+
return new Set(
35+
sets.reduce((flatArray, set) => [...flatArray, ...set], [])
36+
)
37+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy