File tree Expand file tree Collapse file tree 1 file changed +25
-1
lines changed
src/test/java/g2601_2700/s2623_memoize Expand file tree Collapse file tree 1 file changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -3,5 +3,29 @@ import { memoize } from 'src/main/java/g2601_2700/s2623_memoize/solution'
33import { expect , test } from 'vitest'
44
55test ( 'memoize' , ( ) => {
6- expect ( 1 ) . toEqual ( 1 )
6+ const sum = ( a , b ) => a + b
7+ const memoizedSum = memoize ( sum )
8+ expect ( memoizedSum ( 2 , 2 ) ) . toEqual ( 4 )
9+ // Returns 4. sum() was called as (2, 2) was not seen before.
10+ expect ( memoizedSum ( 2 , 2 ) ) . toEqual ( 4 )
11+ // Returns 4. However sum() was not called because the same inputs were seen before.
12+ // Total call count: 1
13+ expect ( memoizedSum ( 1 , 2 ) ) . toEqual ( 3 )
14+ // Returns 3. sum() was called as (1, 2) was not seen before.
15+ // Total call count: 2
16+ } )
17+
18+ test ( 'memoize2' , ( ) => {
19+ const factorial = ( n ) => ( n <= 1 ? 1 : n * factorial ( n - 1 ) )
20+ const memoFactorial = memoize ( factorial )
21+ expect ( memoFactorial ( 2 ) ) . toEqual ( 2 )
22+ // Returns 2.
23+ expect ( memoFactorial ( 3 ) ) . toEqual ( 6 )
24+ // Returns 6.
25+ expect ( memoFactorial ( 2 ) ) . toEqual ( 2 )
26+ // Returns 2. However factorial was not called because 2 was seen before.
27+ // Total call count: 2
28+ expect ( memoFactorial ( 3 ) ) . toEqual ( 6 )
29+ // Returns 6. However factorial was not called because 3 was seen before.
30+ // Total call count: 2
731} )
You can’t perform that action at this time.
0 commit comments