Skip to content

Captain's Log: Fix random override cleanup #2734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions exercises/concept/captains-log/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ randomStardate();
// => 41458.15721310934
```

<!-- prettier-ignore -->
~~~exercism/caution
It is expected that the smallest random number (0) results in the smallest random stardate (41000.0) and
the largest random number (just under 1) results in the largest random stardate (41999.999...).
~~~

## 3. Generate a random planet

The Starship Enterprise encounters many planets in its travels.
Expand Down
21 changes: 15 additions & 6 deletions exercises/concept/captains-log/captains-log.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, test } from '@jest/globals';
import { describe, expect, test, beforeEach, afterEach } from '@jest/globals';
import {
randomShipRegistryNumber,
randomStardate,
Expand All @@ -12,7 +12,7 @@ describe('randomShipRegistryNumber', () => {
}
});

test('returns a random registry number', () => {
test('is a random registry number', () => {
expect(randomShipRegistryNumber()).not.toEqual(randomShipRegistryNumber());
});
});
Expand All @@ -33,24 +33,33 @@ function loadDie(...values) {
}

describe('randomStardate', () => {
test('stardate is between 41000 and 42000', () => {
let restore;

beforeEach(() => {
const min = 0;
const max = 1 - Number.EPSILON * 32;

// prettier-ignore
const restore = loadDie(
restore = loadDie(
min, min, min, min, min, min,
max, max, max, max, max, max,
0.5, 0.5, 0.5, 0.5, 0.5, 0.5
);
});

afterEach(() => {
if (restore) {
restore();
restore = undefined;
}
});

test('stardate is between 41000 and 42000', () => {
for (let i = 0; i < 10_000; i++) {
const starDate = randomStardate();
expect(starDate).toBeGreaterThanOrEqual(41_000);
expect(starDate).toBeLessThan(42_000);
}

restore();
});
});

Expand Down