Skip to content

Commit ff62f2a

Browse files
committed
Update tests mergeSort,binarySearch
- More logging and descriptions of concepts are being added to DSA related tests with styles for mo' bitchen console loggin'
1 parent 9b88ef1 commit ff62f2a

File tree

4 files changed

+62
-5
lines changed

4 files changed

+62
-5
lines changed

data-structures/sorting-algos/binarySearch.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const binarySearchIterative = (arr, targetVal) => {
2222
rightPointer = midIdx;
2323
}
2424
}
25+
console.log('targetVal not found');
2526
return null;
2627
};
2728

@@ -49,6 +50,7 @@ export const binarySearchRecursive = (arr, targetVal, leftPointer = 0, rightPoin
4950
return binarySearchRecursive(arr, targetVal, leftPointer, rightPointer);
5051
}
5152
}
53+
console.log('targetVal not found');
5254
return null;
5355
}
5456

data-structures/sorting-algos/binarySearch.test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ const jestConsole = console;
1616
beforeEach(() => {
1717
global.console = console;
1818
console.log(style.color(255,0,255),'▷',style.reset,style.color(39),expect.getState().currentTestName,style.reset);
19-
// console.log(expect.getState());
2019
});
2120

2221
afterEach(() => {
@@ -190,7 +189,7 @@ ${style.h2('## Steps')}
190189
});
191190

192191
// BINARY SEARCH (RECURSIVE)
193-
describe(`${style.underline}${style.italic}binarySearch (Recursive)\n${style.reset}`, () => {
192+
describe(`${style.underline}${style.italic}(Recursive)\n${style.reset}`, () => {
194193

195194
logItDescription('should find index of target AT END of a 4 element searchable', () => {
196195
const searchable = [2, 4, 6, 8];

data-structures/sorting-algos/mergeSort.test.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,58 @@ afterEach(() => {
2626
console.log(style.color(99), style.hr.double, style.reset);
2727
});
2828

29+
const logItDescription = (description, callback) => {
30+
it(description, async () => {
31+
// Array to store captured log messages for this specific test
32+
const capturedLogs = [];
33+
34+
// Spy on console.log and push messages to our array
35+
const logSpy = jest.spyOn(console, 'log').mockImplementation((...args) => {
36+
capturedLogs.push(args);
37+
});
38+
39+
// Capture console.log output during the test's execution
40+
try {
41+
await callback(); // Assuming the callback might be async
42+
} finally {
43+
// Restore the original console.log behavior
44+
logSpy.mockRestore();
45+
46+
// Print the custom description and then the captured logs
47+
// console.log(style.green, ' ☞ ', style.reset, description);
48+
capturedLogs.forEach(logArgs => {
49+
console.log(' ↳ ', ...logArgs);
50+
});
51+
}
52+
});
53+
};
54+
2955
describe('mergeSort', () => {
30-
it('should sort incrementally', () => {
56+
57+
console.log(`
58+
${style.h1('# Merge Sort')}
59+
Breaks a list into smaller parts to simplify sorting
60+
61+
${style.h2('## Time Complexity')}
62+
Ο(N * log N) Ω(N * log N) Θ(N * log N)
63+
64+
${style.h2('## Notes')}
65+
- created in 1945 by John von Neumann
66+
- average, best, and worst case complexity are equal due to
67+
the same steps being taken regardless of the input (order and length).
68+
- _divide-and-conquer alorithm_
69+
- does not mutate original list
70+
- base case of recursive splitting function is when \`arr.length = 1\`
71+
72+
${style.h2('## Steps')}
73+
The \`log N\` part:
74+
- recursively split input list down to single-element lists
75+
76+
The \`N\` part:
77+
- merge and sort the single-element lists until the originial input size is acheived
78+
`);
79+
80+
logItDescription('should sort incrementally', () => {
3181
console.log(mergeSort([5, 10, -3, -3, 7, 9]));
3282
expect(mergeSort([5, 10, -3, -3, 7, 9]))
3383
.toStrictEqual([-3, -3, 5, 7, 9, 10])

styles.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ const style = {
4646
h4: (text) => {
4747
return `${style.bold}${style.blue}${text}${style.reset}`;
4848
},
49+
h5: (text) => {
50+
return `${style.underline}${style.green}${text}${style.reset}`;
51+
},
52+
h6: (text) => {
53+
return `${style.underline}${style.magenta}${text}${style.reset}`;
54+
},
4955
chain: (...styles) => {
5056
let chainedStyles = '';
5157
for (const styleName of styles) {
@@ -126,7 +132,7 @@ const style = {
126132
style.hr.double,
127133
style.wrap('red', style.hr.single),
128134
style.wrap(style.color(255,0,255), style.hr.single),
129-
style.wrap('\x1b[33m', style.hr.single),
135+
style.wrap('\x1b[33m', style.hr.single)+'\n',
130136
style.wrap('italic', 'Make me ITALIC (reset included for `style.wrap()`)')+'\n'+
131137
style.wrap('black', 'redbg', 'bold', 'strike', 'Make me RED BACKGROUND STRIKED')+
132138
style.chain('red', 'bold')+ 'Make me RED BOLD'+ style.reset+'\n'+
@@ -162,7 +168,7 @@ SAMPLES (run \`styles.test()\` to view styled in console)
162168
style.hr.double,
163169
style.wrap('red', style.hr.single),
164170
style.wrap(style.color(255,0,255), style.hr.single),
165-
style.wrap('\\x1b[33m', style.hr.single),
171+
style.wrap('\\x1b[33m', style.hr.single)+'\\n',
166172
167173
style.wrap('italic', 'Make me ITALIC (reset included for \`style.wrap()\`)')+'\\n'+
168174
style.wrap('black', 'redbg', 'bold', 'strike', 'Make me RED BACKGROUND STRIKED')+

0 commit comments

Comments
 (0)