|
| 1 | +// FindIntersectionPoint.test.js |
1 | 2 |
|
2 |
| -import { findIntersection } from '../FindIntersectionPoint'; |
| 3 | +import { ListNode, findIntersection } from '../FindIntersectionPoint.js'; // Ensure the path is correct |
| 4 | +import { createLinkedList } from '../SlowFast.js'; // Ensure the path is correct |
3 | 5 |
|
4 |
| -function runTests() { |
5 |
| - const tests = [ |
6 |
| - { |
7 |
| - desc: 'Intersecting lines', |
8 |
| - input: [0, 0, 2, 2, 0, 2, 2, 0], |
9 |
| - expected: { x: 1, y: 1 } |
10 |
| - }, |
11 |
| - { |
12 |
| - desc: 'Parallel lines (no intersection)', |
13 |
| - input: [0, 0, 2, 2, 0, 1, 2, 3], |
14 |
| - expected: null |
15 |
| - }, |
16 |
| - { |
17 |
| - desc: 'Overlap lines (fully overlapping)', |
18 |
| - input: [0, 0, 2, 2, 1, 1, 3, 3], |
19 |
| - expected: { x: 1, y: 1 } |
20 |
| - }, |
21 |
| - { |
22 |
| - desc: 'Non-intersecting lines (far apart)', |
23 |
| - input: [0, 0, 1, 1, 2, 2, 3, 3], |
24 |
| - expected: null |
25 |
| - }, |
26 |
| - { |
27 |
| - desc: 'Intersecting at an endpoint', |
28 |
| - input: [0, 0, 2, 2, 2, 0, 0, 2], |
29 |
| - expected: { x: 2, y: 2 } |
30 |
| - } |
31 |
| - ]; |
| 6 | +describe('Find Intersection Point', () => { |
| 7 | + it('should find the intersection point in two linked lists', () => { |
| 8 | + // Create linked list 1: 1 -> 2 -> 3 -> 6 -> 7 |
| 9 | + const list1 = createLinkedList([1, 2, 3]); |
| 10 | + const intersection = new ListNode(6); // ListNode should be correctly imported |
| 11 | + intersection.next = new ListNode(7); |
| 12 | + list1.next.next.next = intersection; // Connect 3 -> 6 |
32 | 13 |
|
33 |
| - tests.forEach(({ desc, input, expected }, index) => { |
34 |
| - const result = findIntersection(...input); |
35 |
| - const isPassed = JSON.stringify(result) === JSON.stringify(expected); |
36 |
| - console.log(`Test ${index + 1}: ${desc} - ${isPassed ? 'Passed' : 'Failed'}`); |
37 |
| - if (!isPassed) { |
38 |
| - console.log(` Expected: ${JSON.stringify(expected)}, Got: ${JSON.stringify(result)}`); |
39 |
| - } |
40 |
| - }); |
41 |
| -} |
| 14 | + // Create linked list 2: 4 -> 5 -> 6 -> 7 |
| 15 | + const list2 = createLinkedList([4, 5]); |
| 16 | + list2.next.next = intersection; // Connect 5 -> 6 |
42 | 17 |
|
43 |
| -// Run the tests |
44 |
| -runTests(); |
| 18 | + const expected = intersection; // We expect the intersection node |
| 19 | + |
| 20 | + const result = findIntersection(list1, list2); |
| 21 | + expect(result).toBe(expected); // Check if the result matches the expected output |
| 22 | + }); |
| 23 | + |
| 24 | + // Additional test cases can be added here |
| 25 | +}); |
0 commit comments