-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex1.test.js
More file actions
52 lines (42 loc) · 1.4 KB
/
ex1.test.js
File metadata and controls
52 lines (42 loc) · 1.4 KB
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
'use strict';
const { incrementConcurrently } = require('./ex1');
function assert(condition, message) {
if (!condition) throw new Error(message || 'Assertion failed');
}
async function assertThrows(fn, msg) {
let ok = false;
try { await fn(); } catch { ok = true; }
assert(ok, msg);
}
(async () => {
await assertThrows(
() => incrementConcurrently({ value: 'x' }, 1),
'counter must validate numeric value'
);
await assertThrows(
() => incrementConcurrently({ value: 0 }, -1),
'times must validate >= 0'
);
{
const counter = { value: 0 };
const result = await incrementConcurrently(counter, 1);
assert(result === 1, 'Expected returned value to equal total increments');
assert(counter.value === 1, 'Expected counter to reflect all increments');
}
{
const counter = { value: 0 };
const result = await incrementConcurrently(counter, 25);
assert(result === 25, 'Expected returned value to equal total increments');
assert(counter.value === 25, 'Expected counter to reflect all increments');
}
{
const counter = { value: 10 };
const result = await incrementConcurrently(counter, 0);
assert(result === 10, 'Expected zero increments to keep value unchanged');
assert(counter.value === 10, 'Expected counter to remain unchanged');
}
console.log('ex1 tests passed');
})().catch((err) => {
console.error(err);
process.exit(1);
});