-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestutil.js
97 lines (86 loc) · 2.62 KB
/
testutil.js
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Assertion function
function assertEqual(actual, expected, testName) {
if (Array.isArray(expected)) {
if (!arraysEqual(actual, expected)) {
throw new Error(testName + " failed: expected " + JSON.stringify(expected) + ", but got " + JSON.stringify(actual) + ". diff: " + JSON.stringify(diff(JSON.stringify(expected), JSON.stringify(actual))));
}
} else if (actual !== expected) {
throw new Error(testName + " failed: expected " + expected + ", but got " + actual);
}
}
// Helper function for array comparison
function arraysEqual(arr1, arr2) {
if (arr1.length !== arr2.length) return false;
for (var i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) return false;
}
return true;
}
function lcs(str1, str2) {
const dp = Array.from({ length: str1.length + 1 }, () => Array(str2.length + 1).fill(0));
for (let i = 1; i <= str1.length; i++) {
for (let j = 1; j <= str2.length; j++) {
if (str1[i - 1] === str2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp;
}
function getContext(str, index, range = 5) {
const start = Math.max(0, index - range);
const end = Math.min(str.length, index + range + 1);
return str.substring(start, end);
}
function diff(str1, str2) {
const dp = lcs(str1, str2);
let i = str1.length, j = str2.length;
const diffResult = [];
while (i > 0 && j > 0) {
if (str1[i - 1] === str2[j - 1]) {
i--;
j--;
} else if (dp[i][j - 1] > dp[i - 1][j]) {
diffResult.unshift({
type: 'add',
value: str2[j - 1],
indexBefore: i,
indexAfter: j - 1,
context: getContext(str2, j - 1)
});
j--;
} else {
diffResult.unshift({
type: 'delete',
value: str1[i - 1],
indexBefore: i - 1,
indexAfter: j,
context: getContext(str2, j)
});
i--;
}
}
while (i > 0) {
diffResult.unshift({
type: 'delete',
value: str1[i - 1],
indexBefore: i - 1,
indexAfter: j,
context: getContext(str2, j)
});
i--;
}
while (j > 0) {
diffResult.unshift({
type: 'add',
value: str2[j - 1],
indexBefore: i,
indexAfter: j - 1,
context: getContext(str2, j - 1)
});
j--;
}
return diffResult;
}