xen-dev-utils
Version:
Utility functions used by the Scale Workshop ecosystem
128 lines • 5.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const primes_1 = require("../primes");
(0, vitest_1.describe)('Array of prime numbers', () => {
(0, vitest_1.it)('has no gaps', () => {
(0, vitest_1.expect)(primes_1.PRIMES[0]).toBe(2);
let index = 1;
for (let n = 3; n <= 7919; n += 2) {
let isPrime_ = true;
for (let i = 3; i * i <= n; ++i) {
if (n % i === 0) {
isPrime_ = false;
break;
}
}
if (isPrime_) {
(0, vitest_1.expect)(primes_1.PRIMES[index]).toBe(n);
index++;
}
}
});
});
const LARGER_PRIMES = [
7927, 7933, 7937, 7949, 7951, 7963, 7993, 8009, 8011, 8017, 8039, 8053, 8059,
8069, 8081, 8087, 8089, 8093,
];
(0, vitest_1.describe)('Primeness detector', () => {
(0, vitest_1.it)('works for small primes', () => {
for (let n = -1.5; n < 7920.5; n += 0.5) {
if ((0, primes_1.isPrime)(n)) {
(0, vitest_1.expect)(primes_1.PRIMES).includes(n);
}
else {
(0, vitest_1.expect)(primes_1.PRIMES).not.includes(n);
}
}
});
vitest_1.it.each(LARGER_PRIMES)('works for %s', (prime) => {
(0, vitest_1.expect)((0, primes_1.isPrime)(prime)).toBe(true);
});
(0, vitest_1.it)('works for larger composites', () => {
for (let n = 7919.25; n < 8095; n += 0.25) {
if (LARGER_PRIMES.includes(n)) {
continue;
}
(0, vitest_1.expect)((0, primes_1.isPrime)(n)).toBe(false);
}
});
(0, vitest_1.it)('works for 62837327', () => {
(0, vitest_1.expect)((0, primes_1.isPrime)(62837327)).toBe(false);
});
(0, vitest_1.it)('works for 62837303', () => {
(0, vitest_1.expect)((0, primes_1.isPrime)(62837303)).toBe(true);
});
});
(0, vitest_1.describe)('Lists of primes', () => {
(0, vitest_1.it)('works from implicit 2 to 7', () => {
(0, vitest_1.expect)((0, primes_1.primes)(7)).toEqual([2, 3, 5, 7]);
});
(0, vitest_1.it)('works from implicit 2 to below 16', () => {
(0, vitest_1.expect)((0, primes_1.primes)(16)).toEqual([2, 3, 5, 7, 11, 13]);
});
(0, vitest_1.it)('produces an empty array with negative bounds', () => {
(0, vitest_1.expect)((0, primes_1.primes)(-100, -50)).toHaveLength(0);
});
(0, vitest_1.it)('produces an empty array when end < start', () => {
(0, vitest_1.expect)((0, primes_1.primes)(10, 1)).toHaveLength(0);
});
(0, vitest_1.it)('supports start and end parameters', () => {
(0, vitest_1.expect)((0, primes_1.primes)(12, 31)).toEqual([13, 17, 19, 23, 29, 31]);
});
(0, vitest_1.it)('works with medium-sized values', () => {
(0, vitest_1.expect)((0, primes_1.primes)(7907, 7933)).toEqual([7907, 7919, 7927, 7933]);
});
(0, vitest_1.it)('works with larger values', () => {
(0, vitest_1.expect)((0, primes_1.primes)(7920, 8094)).toEqual(LARGER_PRIMES);
});
});
(0, vitest_1.describe)('Nth odd prime generator', () => {
(0, vitest_1.it)('knowns 3 is the 1st odd prime', () => {
(0, vitest_1.expect)((0, primes_1.nthPrime)(1)).toBe(3);
});
(0, vitest_1.it)('knowns 2 is the prime at index 0', () => {
(0, vitest_1.expect)((0, primes_1.nthPrime)(0)).toBe(2);
});
(0, vitest_1.it)('returns undefined for a negative index', () => {
(0, vitest_1.expect)((0, primes_1.nthPrime)(-1)).toBe(undefined);
});
(0, vitest_1.it)('returns undefined for a fractional index', () => {
(0, vitest_1.expect)((0, primes_1.nthPrime)(0.5)).toBe(undefined);
});
(0, vitest_1.it)('knows that 7919 is the 999th odd prime', () => {
(0, vitest_1.expect)((0, primes_1.nthPrime)(999)).toBe(7919);
});
(0, vitest_1.it)('knows that 7927 is the 1000th odd prime', () => {
(0, vitest_1.expect)((0, primes_1.nthPrime)(1000)).toBe(7927);
});
(0, vitest_1.it)('knows the about larger primes', () => {
for (let i = 0; i < LARGER_PRIMES.length; ++i) {
(0, vitest_1.expect)((0, primes_1.nthPrime)(1000 + i)).toBe(LARGER_PRIMES[i]);
}
});
});
(0, vitest_1.describe)('Prime range generator', () => {
(0, vitest_1.it)('produces an empty range for start > end', () => {
(0, vitest_1.expect)((0, primes_1.primeRange)(99999, 9999)).toEqual([]);
});
(0, vitest_1.it)('produces an empty range for start === end', () => {
(0, vitest_1.expect)((0, primes_1.primeRange)(9999, 9999)).toEqual([]);
});
(0, vitest_1.it)('produces the first 4 primes', () => {
(0, vitest_1.expect)((0, primes_1.primeRange)(0, 4)).toEqual([2, 3, 5, 7]);
});
(0, vitest_1.it)('produces the first 4 primes (start is optional)', () => {
(0, vitest_1.expect)((0, primes_1.primeRange)(4)).toEqual([2, 3, 5, 7]);
});
(0, vitest_1.it)('produces 5 primes starting from the 999th odd prime', () => {
(0, vitest_1.expect)((0, primes_1.primeRange)(999, 999 + 5)).toEqual([7919, 7927, 7933, 7937, 7949]);
});
(0, vitest_1.it)('produces the larger primes', () => {
(0, vitest_1.expect)((0, primes_1.primeRange)(1000, 1000 + LARGER_PRIMES.length)).toEqual(LARGER_PRIMES);
});
(0, vitest_1.it)('produces the larger primes but one', () => {
(0, vitest_1.expect)((0, primes_1.primeRange)(1001, 1000 + LARGER_PRIMES.length)).toEqual(LARGER_PRIMES.slice(1));
});
});
//# sourceMappingURL=primes.spec.js.map