Skip to content

Commit

Permalink
Add: Individual tests started
Browse files Browse the repository at this point in the history
  • Loading branch information
CristianAbrante committed Mar 24, 2019
1 parent 8cebdef commit eee0352
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"types": "lib/index.d.ts",
"scripts": {
"test": "jest --config jestconfig.json --coverage --coverageReporters=text-lcov | coveralls",
"testfile": "jest --config jestconfig.json",
"build": "tsc",
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
"lint": "tslint -p tsconfig.json",
Expand Down
101 changes: 101 additions & 0 deletions src/__tests__/individual/base/Individual.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* @license
* Copyright (c) 2019 Cristian Abrante. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*/

import Binary from './../../../lib/individual/numeric/binary';

const testIndividuals = [
{
genotype: [false, true, false, false, true, false, false, false],
initialization: "01001000",
set: [
{
newValue: false,
position: 4
},
{
newValue: true,
position: 0
},
{
newValue: false,
position: 0
},
{
newValue: true,
position: 7
}
],
type: Binary.Individual
}
];

testIndividuals.forEach((individual) => {
const individualName = individual.initialization;
const expectedGenotype = individual.genotype;
let ind = new individual.type(individualName);

beforeEach(() => {
ind = new individual.type(individualName);
});

test(`Creation test`, () => {
expect(ind.genotype).toEqual(individual.genotype);
});

test(`Iteration test`, () => {
let index = 0;
for (const gene of ind) {
expect(gene).toBe(expectedGenotype[index++]);
}
});

test(`get test`, () => {
expectedGenotype.forEach((expectedGene, geneIndex) => {
expect(ind.get(geneIndex)).toBe(expectedGene);
})
});

test(`get throws positive`, () => {
expect(() => ind.get(10000)).toThrow(RangeError);
});

test(`get throws negative`, () => {
expect(() => ind.get(-10)).toThrow(RangeError);
});

test('set test', () => {
const setTests = individual.set;
setTests.forEach(test => {
ind.set(test.position, test.newValue);
expect(ind.get(test.position)).toBe(test.newValue);
})
});

test(`set throws positive`, () => {
expect(() => ind.get(10000)).toThrow(RangeError);
});

test(`set throws negative`, () => {
expect(() => ind.get(-10)).toThrow(RangeError);
});

test('length test', () => {
expect(ind.length()).toBe(expectedGenotype.length);
});

test('entries test', () => {
const iterator = ind.entries();
expectedGenotype.forEach((expectedGene, geneIndex) => {
const next = iterator.next();
expect(next.value[0]).toBe(geneIndex);
expect(next.value[1]).toBe(expectedGene);
expect(next.done).toBeFalsy();
});
const nextOut = iterator.next();
expect(nextOut.value).toBeUndefined();
expect(nextOut.done).toBeTruthy();
})
});
4 changes: 4 additions & 0 deletions src/lib/individual/base/Individual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ abstract class Individual<T> implements Iterable<T> {
return this.genotype[Symbol.iterator]();
}

public copy(other: Individual<T>) {
this.setGenotype(other.genotype);
}

/**
* Returns the gene at specified index.
* @param geneIndex index of the gene to be accessed.
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"module": "commonjs",
"declaration": true,
"outDir": "./lib",
"strict": true
"strict": true,
"downlevelIteration": true
},
"include": ["src"],
"exclude": ["node_modules", "**/__tests__/*"]
Expand Down

0 comments on commit eee0352

Please sign in to comment.