Skip to content

Remove duplicate helper functions in cache tests. #933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 4 additions & 19 deletions Cache/test/LFUCache.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { LFUCache } from '../LFUCache'
import { fibonacciCache } from './cacheTest'

describe('LFUCache', () => {
it('Example 1 (Small Cache, size=2)', () => {
Expand Down Expand Up @@ -28,27 +29,11 @@ describe('LFUCache', () => {

it('Example 2 (Computing Fibonacci Series, size=100)', () => {
const cache = new LFUCache(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
}
21 changes: 2 additions & 19 deletions Cache/test/LRUCache.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { LRUCache } from '../LRUCache'
import { fibonacciCache } from './cacheTest'

describe('LRUCache', () => {
it('Example 1 (Small Cache, size=2)', () => {
Expand Down Expand Up @@ -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
}
12 changes: 1 addition & 11 deletions Cache/test/Memoize.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
import { memoize } from '../Memoize'
import { union } from './cacheTest'
import { fibonacci } from '../../Dynamic-Programming/FibonacciNumber'
import { factorial } from '../../Recursive/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', () => {
it('expects the fibonacci function to use the cache on the second call', () => {
const memoFibonacci = memoize(fibonacci)
Expand Down
37 changes: 37 additions & 0 deletions Cache/test/cacheTest.js
Original file line number Diff line number Diff line change
@@ -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], [])
)
}
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