Skip to content

added tests #82

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

Merged
merged 2 commits into from
May 1, 2018
Merged
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
4 changes: 4 additions & 0 deletions src/ts/algorithms/math/find-divisors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export const findDivisors = (num: number) => {
}
}

if (num >= 2 && !divisors.includes(num)) {
divisors.push(num);
}

divisors.sort((a, b) => a - b);

return divisors;
Expand Down
5 changes: 3 additions & 2 deletions src/ts/algorithms/math/primality-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const isPrime = (n: number) => {
}

const sqrt = Math.floor(Math.sqrt(n));
for (let i = 2; i < sqrt; i++) {
for (let i = 2; i <= sqrt; i++) {
if (n % i === 0) {
return false;
}
Expand Down Expand Up @@ -33,4 +33,5 @@ export const testPrime = (n: number) => {
return true;
};

export const isPrime2 = (n: number) => ![...Array(n).keys()].slice(2).map(i => !(n % i)).includes(true) && ![0, 1].includes(n);
// tslint:disable-next-line:max-line-length
export const isPrime2 = (n: number) => (n >= 2) ? (![...Array(n).keys()].slice(2).map(i => !(n % i)).includes(true) && ![0, 1].includes(n)) : false;
21 changes: 15 additions & 6 deletions src/ts/algorithms/math/sieve-eratosthenes.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
export const sieveOfEratosthenes = (n: number) => {

const prime: boolean[] = [];
const sieve: boolean[] = [];
const primes: number[] = [];

for (let i = 0; i < n; i++) {
prime[i] = true;
sieve[1] = false;

for (let i = 2; i <= n; i++) {
sieve[i] = true;
}

for (let p = 2; p * p <= n; p++) {
if (prime[p]) {
if (sieve[p]) {
for (let i = p * 2; i <= n; i += p) {
prime[i] = false;
sieve[i] = false;
}
}
}

return prime.filter(num => num === true);
sieve.forEach((value, index) => {
if (value) {
primes.push(index);
}
}, primes);

return primes;
};
9 changes: 9 additions & 0 deletions src/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ export { matrixChainOrder as matrixChainOrderGreedy } from './algorithms/greedy/
export { ratInAMaze } from './algorithms/backtracking/rat-in-maze';
export { sudokuSolver } from './algorithms/backtracking/sudoku-solver';

// others
export { findDivisors } from './algorithms/math/find-divisors';
export { gcd } from './algorithms/math/gcd';
export { lcm } from './algorithms/math/lcm';
export { greatestDifference } from './algorithms/math/greatest-difference';
export { isPrime } from './algorithms/math/primality-test';
export { testPrime } from './algorithms/math/primality-test';
export { isPrime2 } from './algorithms/math/primality-test';
export { sieveOfEratosthenes } from './algorithms/math/sieve-eratosthenes';


/* import { hotPotato } from './others/hot-potato';
Expand Down
8 changes: 8 additions & 0 deletions test/js/algorithms/search/search-algorithms-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ export function testSearchAlgorithm(
expect(searchAlgorithm(array, SIZE)).to.equal(SIZE - 1);
});

it('finds value at different positions', () => {
const array = createSortedArray();

for (let value = 1; value <= SIZE; value++) {
expect(searchAlgorithm(array, value)).to.equal(value - 1);
}
});

if (config.customEquals) {
it('finds value with custom equals function', () => {
const array = [{ key: 1 }, { key: 2 }, { key: 3 }];
Expand Down
17 changes: 17 additions & 0 deletions test/ts/algorithms/math/find-divisors.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'mocha';
import { expect } from 'chai';
import { findDivisors } from '../../../../src/ts/index';

describe('Find Divisors', () => {

it('returns the divisors of the number', () => {

expect(findDivisors(-1)).to.deep.equal([]);
expect(findDivisors(0)).to.deep.equal([]);
expect(findDivisors(1)).to.deep.equal([1]);
expect(findDivisors(2)).to.deep.equal([1, 2]);
expect(findDivisors(3)).to.deep.equal([1, 3]);
expect(findDivisors(4)).to.deep.equal([1, 2, 4]);
expect(findDivisors(100)).to.deep.equal([1, 2, 4, 5, 10, 20, 25, 50, 100 ]);
});
});
16 changes: 16 additions & 0 deletions test/ts/algorithms/math/gcd.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'mocha';
import { expect } from 'chai';
import { gcd } from '../../../../src/ts/index';

describe('GCD', () => {

it('returns the gcd between two numbers', () => {

expect(gcd(1, 0)).to.equal(0);
expect(gcd(1, 1)).to.equal(1);
expect(gcd(2, 2)).to.equal(2);
expect(gcd(2, 4)).to.equal(2);
expect(gcd(2, 3)).to.equal(1);
expect(gcd(10, 1000)).to.equal(10);
});
});
14 changes: 14 additions & 0 deletions test/ts/algorithms/math/greatest-difference.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'mocha';
import { expect } from 'chai';
import { greatestDifference } from '../../../../src/ts/index';

describe('Greatest Difference', () => {

it('returns the gcd between two numbers', () => {

expect(greatestDifference([5, 6, 7, 2, 10])).to.equal(8);
expect(greatestDifference([1, 2, 4])).to.equal(3);
expect(greatestDifference([1, 3])).to.equal(2);

});
});
16 changes: 16 additions & 0 deletions test/ts/algorithms/math/lcm.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'mocha';
import { expect } from 'chai';
import { lcm } from '../../../../src/ts/index';

describe('LCM', () => {

it('returns the lcm between two numbers', () => {

expect(lcm(0, 0)).to.equal(0);
expect(lcm(1, 1)).to.equal(1);
expect(lcm(1, 2)).to.equal(2);
expect(lcm(2, 4)).to.equal(4);
expect(lcm(2, 3)).to.equal(6);

});
});
30 changes: 30 additions & 0 deletions test/ts/algorithms/math/primality-test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'mocha';
import { expect } from 'chai';
import { isPrime, testPrime, isPrime2 } from '../../../../src/ts/index';

describe('Primality Tests', () => {

it('returns if the number is prime or not - isPrime', () => {
testIsPrime(isPrime);
});

it('returns if the number is prime or not - testPrime', () => {
testIsPrime(testPrime);
});

it('returns if the number is prime or not - isPrime2', () => {
testIsPrime(isPrime2);
});

function testIsPrime(primeFunction) {
expect(primeFunction(-1)).to.equal(false);
expect(primeFunction(0)).to.equal(false);
expect(primeFunction(1)).to.equal(false);
expect(primeFunction(2)).to.equal(true);
expect(primeFunction(3)).to.equal(true);
expect(primeFunction(4)).to.equal(false);
expect(primeFunction(5)).to.equal(true);
expect(primeFunction(10)).to.equal(false);
expect(primeFunction(113)).to.equal(true);
}
});
17 changes: 17 additions & 0 deletions test/ts/algorithms/math/sieve-eratosthenes.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'mocha';
import { expect } from 'chai';
import { sieveOfEratosthenes } from '../../../../src/ts/index';

describe('Sieve Of Eratosthene', () => {

it('returns the prime numbers', () => {

expect(sieveOfEratosthenes(0)).to.deep.equal([]);
expect(sieveOfEratosthenes(1)).to.deep.equal([]);
expect(sieveOfEratosthenes(2)).to.deep.equal([2]);
expect(sieveOfEratosthenes(3)).to.deep.equal([2, 3]);
expect(sieveOfEratosthenes(4)).to.deep.equal([2, 3]);
expect(sieveOfEratosthenes(5)).to.deep.equal([2, 3, 5]);

});
});
8 changes: 8 additions & 0 deletions test/ts/algorithms/search/search-algorithms-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ export function testSearchAlgorithm(
expect(searchAlgorithm(array, SIZE)).to.equal(SIZE - 1);
});

it('finds value at different positions', () => {
const array = createSortedArray();

for (let value = 1; value <= SIZE; value++) {
expect(searchAlgorithm(array, value)).to.equal(value - 1);
}
});

if (config.customEquals) {
it('finds value with custom equals function', () => {
const array = [{ key: 1 }, { key: 2 }, { key: 3 }];
Expand Down