mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-11-15 02:49:33 +01:00
test(single-line): make the linearity check fail on time, not on the result
The input ended on a whitespace run, so the old regex returned a different
string and the test failed on the result before it reached the time bound.
Keep the run inside a line ('a<run>b\nc'): the old regex and the split now
return the same 'a<run>b c', and only the elapsed time tells them apart
(about 1.8 s against well under a millisecond).
Co-Authored-By: Christian Haberl <116439789+christianhaberl@users.noreply.github.com>
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
74dcf9a59d
commit
00d89ff828
1 file changed
+5
-4
@@ -43,12 +43,13 @@ describe('toSingleLine', () => {
|
||||
});
|
||||
|
||||
it('stays linear on a long whitespace run', () => {
|
||||
// A regex that folds the whitespace around each break backtracks over every
|
||||
// start position in the run: 50,000 spaces took well over a second that way.
|
||||
// The split takes well under a millisecond, so the bound leaves room either side.
|
||||
// The run sits inside a line, so the result is the same either way; only the time
|
||||
// differs. A regex that folds the whitespace around each break retries the run from
|
||||
// every start position: 50,000 spaces took over a second that way. The split takes
|
||||
// well under a millisecond, so the bound leaves room either side.
|
||||
const run = ' '.repeat(50_000);
|
||||
const started = performance.now();
|
||||
const joined = toSingleLine(`a${run}b\nc${run}`);
|
||||
const joined = toSingleLine(`a${run}b\nc`);
|
||||
const elapsed = performance.now() - started;
|
||||
expect(joined).toBe(`a${run}b c`);
|
||||
expect(elapsed).toBeLessThan(250);
|
||||
|
||||
Reference in new issue
Block a user