From eee03529a849f299a77c81797393e91c5bbb5e38 Mon Sep 17 00:00:00 2001 From: Cristian Abrante Date: Sun, 24 Mar 2019 19:56:44 +0000 Subject: [PATCH] Add: Individual tests started --- package.json | 1 + .../individual/base/Individual.test.ts | 101 ++++++++++++++++++ src/lib/individual/base/Individual.ts | 4 + tsconfig.json | 3 +- 4 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 src/__tests__/individual/base/Individual.test.ts diff --git a/package.json b/package.json index b4090c3..839676f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/__tests__/individual/base/Individual.test.ts b/src/__tests__/individual/base/Individual.test.ts new file mode 100644 index 0000000..16c0852 --- /dev/null +++ b/src/__tests__/individual/base/Individual.test.ts @@ -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(); + }) +}); \ No newline at end of file diff --git a/src/lib/individual/base/Individual.ts b/src/lib/individual/base/Individual.ts index 518859b..55f1072 100644 --- a/src/lib/individual/base/Individual.ts +++ b/src/lib/individual/base/Individual.ts @@ -68,6 +68,10 @@ abstract class Individual implements Iterable { return this.genotype[Symbol.iterator](); } + public copy(other: Individual) { + this.setGenotype(other.genotype); + } + /** * Returns the gene at specified index. * @param geneIndex index of the gene to be accessed. diff --git a/tsconfig.json b/tsconfig.json index 25baf71..f9e20e0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,7 +4,8 @@ "module": "commonjs", "declaration": true, "outDir": "./lib", - "strict": true + "strict": true, + "downlevelIteration": true }, "include": ["src"], "exclude": ["node_modules", "**/__tests__/*"]