-
Notifications
You must be signed in to change notification settings - Fork 0
/
prime.spec.js
71 lines (67 loc) · 2.03 KB
/
prime.spec.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const {
isPrime,
generatePrimes,
generateTable,
} = require('./prime');
describe('isPrime', () => {
it('returns false if n is less than or equal to 1', () => {
expect(isPrime(1)).toBe(false);
expect(isPrime(-11)).toBe(false);
});
it('returns true if n is equal to 2', () => {
expect(isPrime(2)).toBe(true);
});
it('returns true if n is equal to 3', () => {
expect(isPrime(3)).toBe(true);
});
it('returns false if n is a multiple of 2', () => {
expect(isPrime(8)).toBe(false);
expect(isPrime(22)).toBe(false);
});
it('returns false if n is a multiple of 3', () => {
expect(isPrime(9)).toBe(false);
expect(isPrime(27)).toBe(false);
});
it('returns true if n is equal to 5', () => {
expect(isPrime(5)).toBe(true);
});
it('returns true if n ie equal to 7', () => {
expect(isPrime(7)).toBe(true);
});
it('return false for multiples of 5', () => {
expect(isPrime(25)).toBe(false);
});
it('return false for multiples of 7', () => {
expect(isPrime(49)).toBe(false);
});
it('returns false if n is the square or cube of a prime', () => {
let n = 11 * 11;
expect(isPrime(n)).toBe(false);
n = 13 * 13 * 13;
expect(isPrime(n)).toBe(false);
});
});
describe('generatePrimes', () => {
it('returns an array with 2 and 3 when given 2', () => {
expect(generatePrimes(2)).toEqual([2, 3]);
});
it('returns an empty array when given 0', () => {
expect(generatePrimes(0)).toEqual([]);
});
it('returns an empty array when given a negative number', () => {
expect(generatePrimes(-1)).toEqual([]);
});
});
describe('generateTable', () => {
it('returns Invalid input when given 0', () => {
expect(generateTable(0)).toBe('Invalid input');
});
it('returns Invalid input when given a negative number', () => {
expect(generateTable(-5)).toBe('Invalid input');
});
it('returns the table when input is valid', () => {
expect(generateTable(2)).toBe(
`| | 2\t | 3\t | \n| 2\t | 4\t | 6\t | \n| 3\t | 6\t | 9\t | \n`
);
});
});