Skip to content

Commit 89c7e71

Browse files
authored
Fix Duplicate ID, Add Per-Problem Time Limit and Custom Answer Comparison (#545)
* Fix occasional duplicate testcase ids. * Do an explicit null check.
1 parent 8a0d898 commit 89c7e71

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

src/companion.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,10 @@ const handleNewProblem = async (problem: Problem) => {
213213

214214
// Add fields absent in competitive companion.
215215
problem.srcPath = srcPath;
216-
problem.tests = problem.tests.map((testcase) => ({
216+
problem.tests = problem.tests.map((testcase, index) => ({
217217
...testcase,
218-
id: randomId(),
218+
// Pass in index to avoid generating duplicate id
219+
id: randomId(index),
219220
}));
220221
if (!existsSync(srcPath)) {
221222
writeFileSync(srcPath, '');

src/runTestCases.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const createLocalProblem = async (editor: vscode.TextEditor) => {
6464
url: srcPath,
6565
tests: [
6666
{
67-
id: randomId(),
67+
id: randomId(null),
6868
input: '',
6969
output: '',
7070
},

src/utils.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,13 @@ export const ocHide = () => {
164164
oc.hide();
165165
};
166166

167-
export const randomId = () => Math.floor(Date.now() + Math.random() * 100);
167+
export const randomId = (index: number | null) => {
168+
if (index !== null) {
169+
return Math.floor(Date.now() + index);
170+
} else {
171+
return Math.floor(Date.now() + Math.random() * 100);
172+
}
173+
};
168174

169175
/**
170176
* Check if file is supported. If not, shows an error dialog. Returns true if

0 commit comments

Comments
 (0)