-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursion.test.js
More file actions
82 lines (78 loc) · 1.88 KB
/
recursion.test.js
File metadata and controls
82 lines (78 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const recursiveMethods = require('../src/recursion');
/* eslint-disable no-undef */
describe('recursion', () => {
describe('nFibonacci', () => {
it('should return the nth fibonacci number', () => {
const fib1 = recursiveMethods.nFibonacci(5);
const fib2 = recursiveMethods.nFibonacci(3);
const fib3 = recursiveMethods.nFibonacci(1);
expect(fib1).toBe(5);
expect(fib2).toBe(2);
expect(fib3).toBe(1);
});
});
describe('nFactorial', () => {
it('should return the factorial of a given number', () => {
const factorial1 = recursiveMethods.nFactorial(5);
const factorial2 = recursiveMethods.nFactorial(3);
expect(factorial1).toBe(120);
expect(factorial2).toBe(6);
});
});
describe('checkMatchingLeaves', () => {
it('should check if all leaves match', () => {
const tree1 = {
x: 1,
y: 1,
z: 1,
};
const tree2 = {
x: 1,
y: 1,
z: 2,
};
expect(recursiveMethods.checkMatchingLeaves(tree1)).toBe(true);
expect(recursiveMethods.checkMatchingLeaves(tree2)).toBe(false);
});
it('should check nested objects', () => {
const tree1 = {
x: 1,
y: 1,
z: 1,
xa: {
xx: 1,
xy: 1,
xz: 1,
zz: {
a: {
b: {
z: 1,
},
},
},
},
};
const tree2 = {
x: 1,
y: 1,
z: 1,
xa: {
xx: 1,
xy: 1,
xz: 1,
zz: {
a: {
b: {
z: 2,
},
},
},
},
r: 1,
};
expect(recursiveMethods.checkMatchingLeaves(tree1)).toBe(true);
expect(recursiveMethods.checkMatchingLeaves(tree2)).toBe(false);
});
});
});
/* eslint-enable no-undef */