Skip to content

Commit

Permalink
[ops] Update tests to use sets over maps
Browse files Browse the repository at this point in the history
  • Loading branch information
peterver committed Apr 9, 2024
1 parent 3f45372 commit e7704bc
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
18 changes: 9 additions & 9 deletions test/lib/array/shuffle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ describe('Array - shuffle', () => {
});

it('Should shuffle an array of primitives (numbers) in a unique way (benchmark 100 shuffles with a 10 number array)', () => {
const map = new Map();
const set = new Set();
for (let i = 0; i < 100; i++) {
const el = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
shuffle(el);
map.set(el, el);
set.add(el);
}
assert.ok(map.size > 90);
assert.ok(set.size > 90);
});

it('Should shuffle an array of primitives (strings) in a unique way (benchmark 100 shuffles with a 10 number array)', () => {
const map = new Map();
const set = new Set();
for (let i = 0; i < 100; i++) {
const el = [
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent molestie nisi interdum dui facilisis vestibulum. Cras et velit sodales, consectetur sem vitae, imperdiet tellus. Vivamus vulputate aliquam nunc non faucibus. Nunc eget enim sollicitudin, semper magna et, congue odio. Nullam sagittis condimentum sollicitudin. Vestibulum venenatis ullamcorper ligula, eu dapibus arcu aliquet a. Fusce eleifend non nulla vitae tempus. Integer varius libero vitae tincidunt volutpat. Praesent vitae velit mollis, tincidunt magna in, maximus ligula. Phasellus feugiat leo dolor, eget mattis justo condimentum luctus. Sed varius massa eget sagittis ultricies. Pellentesque maximus enim ultricies porta pellentesque. Quisque varius magna magna, quis accumsan odio venenatis in. Sed fringilla dapibus metus. Proin non feugiat metus. Nulla non neque tristique, pulvinar orci in, fermentum lacus.',
Expand All @@ -53,13 +53,13 @@ describe('Array - shuffle', () => {
'Cras sit amet nisl non libero pellentesque maximus. Sed sed lacus quam. Maecenas ultricies dui nulla, sed tempor magna viverra at. Morbi a risus egestas, congue mi eu, vestibulum sem. Vestibulum viverra elit libero, eu faucibus lectus hendrerit ac. Praesent auctor ullamcorper massa, sed aliquam metus fermentum non. Morbi lacinia finibus lorem quis suscipit. Aliquam erat volutpat. Morbi tincidunt nec nunc at consequat. Nulla porta et tellus id ornare.',
];
shuffle(el);
map.set(el, el);
set.add(el);
}
assert.ok(map.size > 90);
assert.ok(set.size > 90);
});

it('Should shuffle an array of mixed values in a unique way (benchmark 100 shuffles with a 10 number array)', () => {
const map = new Map();
const set = new Set();
for (let i = 0; i < 100; i++) {
const el = [
{a: 1, b: 2, c: 3, d: 4},
Expand All @@ -74,8 +74,8 @@ describe('Array - shuffle', () => {
[[0, 2, 4], [1, 2, 3]],
];
shuffle(el);
map.set(el, el);
set.add(el);
}
assert.ok(map.size > 90);
assert.ok(set.size > 90);
});
});
18 changes: 9 additions & 9 deletions test/lib/hash/guid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,32 @@ describe('Hash - guid', () => {
});

it('Be unique (50.000 benchmark)', () => {
const map = new Map();
const set = new Set();
let cursor = 0;
while (cursor < 50000) {
map.set(guid(), true);
set.add(guid());
cursor++;
}
assert.ok(map.size === cursor);
assert.ok(set.size === cursor);
});

it('Be unique (100.000 benchmark)', () => {
const map = new Map();
const set = new Set();
let cursor = 0;
while (cursor < 100000) {
map.set(guid(), true);
set.add(guid());
cursor++;
}
assert.ok(map.size === cursor);
assert.ok(set.size === cursor);
});

it('Be unique (200.000 benchmark)', () => {
const map = new Map();
const set = new Set();
let cursor = 0;
while (cursor < 200000) {
map.set(guid(), true);
set.add(guid());
cursor++;
}
assert.ok(map.size === cursor);
assert.ok(set.size === cursor);
});
});
12 changes: 6 additions & 6 deletions test/lib/number/randomBetween.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ describe('Number - randomBetween', () => {

it('Should return a random number between min and max (random min) - (random max)', () => {
for (let i = 0; i < 50; i++) {
const r1 = Math.round(Math.random() * 1000);
const r2 = Math.round(Math.random() * 1000);
const r1 = Math.round(Math.random() * 1000000);
const r2 = Math.round(Math.random() * 10000000);

if (r1 < r2) {
for (let y = 0; y < 10000; y++) {
Expand All @@ -53,12 +53,12 @@ describe('Number - randomBetween', () => {
});

it('should return a unique random number over subsequent calls', () => {
const map = new Map();
for (let i = 0; i < 100000; i++) {
const set = new Set();
for (let i = 0; i < 10000; i++) {
const random = randomBetween(0, 100);
map.set(random, random);
set.add(random);
}
assert.ok(map.size === 100000);
assert.ok(set.size === 10000);
});

it('should throw an error if the min is not numeric', () => {
Expand Down
14 changes: 7 additions & 7 deletions test/lib/number/randomIntBetween.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ describe('Number - randomIntBetween', () => {

it('Should return a random integer between min and max (random min) - (random max)', () => {
for (let i = 0; i < 50; i++) {
const r1 = Math.round(Math.random() * 1000);
const r2 = Math.round(Math.random() * 1000);
const r1 = Math.round(Math.random() * 1000000);
const r2 = Math.round(Math.random() * 10000000);

if (r1 < r2) {
for (let y = 0; y < 10000; y++) {
Expand All @@ -57,13 +57,13 @@ describe('Number - randomIntBetween', () => {
}
});

it('should return a unique random number over subsequent calls', () => {
const map = new Map();
for (let i = 0; i < 10000; i++) {
it('should return a unique random integer over subsequent calls', () => {
const set = new Set();
for (let i = 0; i < 1000; i++) {
const random = randomIntBetween(0, 10000000000);
map.set(random, random);
set.add(random);
}
assert.ok(map.size === 10000);
assert.ok(set.size === 1000);
});

it('should throw an error if the min is not numeric', () => {
Expand Down

0 comments on commit e7704bc

Please sign in to comment.