<commit_before>import 'babel/polyfill';
import expect from 'expect.js';
import sinon from 'sinon';
import GeneticAlgorithm from '../../core/genetic-algorithm';
import Selection from '../../core/selection';
import Recombination from '../../core/recombination';
import Mutation from '../../core/mutation';
import { merge } from '../../utils';


function createGeneticAlgorithm(options = {}) {
    var defaultOptions = {
        individual: () => [],
        selection: new Selection(),
        recombination: new Recombination(),
        mutation: new Mutation()
    };
    return new GeneticAlgorithm(merge(defaultOptions, options));
}


describe('GeneticAlgorithm', () => {
    it('should throw error when required properties are not given', () => {
        expect(() => new GeneticAlgorithm()).to.throwError();
    });

    it('should throw error when properties are of not the expected type',
        () => {
            var options = { populationSize: 'big' };
            expect(() => createGeneticAlgorithm(options)).to.throwError();
        }
    );

    it('should throw error when properties are of not the expected class',
        () => {
            var options = { selection: new Object() };
            expect(() => createGeneticAlgorithm(options)).to.throwError();
        }
    );

    it('should create instance when given properties are correct',
        () => {
            expect(() => createGeneticAlgorithm()).to.not.throwError();
        }
    );

    describe('generatePopulation()', () => {
        it('should generate a population of correct size', () => {
            var instance = createGeneticAlgorithm();
            var population = instance.generatePopulation();
            expect(population.length).to.be(instance.populationSize);
        });

        it('should use `individual` method to create individuals', () => {
            var individual = sinon.spy();
            var instance = createGeneticAlgorithm({ individual });
            var population = instance.generatePopulation();
            expect(individual.called).to.be(true);
            expect(individual.callCount).to.be(instance.populationSize);
        });
    });
});<commit_msg>added test case for generate population<commit_after>import 'babel/polyfill';
import expect from 'expect.js';
import sinon from 'sinon';
import GeneticAlgorithm from '../../core/genetic-algorithm';
import Individual from '../../core/individual';
import Selection from '../../core/selection';
import Recombination from '../../core/recombination';
import Mutation from '../../core/mutation';
import { merge } from '../../utils';


function createGeneticAlgorithm(options = {}) {
    var defaultOptions = {
        individual: () => new Individual(),
        selection: new Selection(),
        recombination: new Recombination(),
        mutation: new Mutation()
    };
    return new GeneticAlgorithm(merge(defaultOptions, options));
}


describe('GeneticAlgorithm', () => {
    it('should throw error when required properties are not given', () => {
        expect(() => new GeneticAlgorithm()).to.throwError();
    });

    it('should throw error when properties are of not the expected type',
        () => {
            var options = { populationSize: 'big' };
            expect(() => createGeneticAlgorithm(options)).to.throwError();
        }
    );

    it('should throw error when properties are of not the expected class',
        () => {
            var options = { selection: new Object() };
            expect(() => createGeneticAlgorithm(options)).to.throwError();
        }
    );

    it('should create instance when given properties are correct',
        () => {
            expect(() => createGeneticAlgorithm()).to.not.throwError();
        }
    );

    describe('generatePopulation()', () => {
        it('should generate a population of correct size', () => {
            var instance = createGeneticAlgorithm();
            var population = instance.generatePopulation();
            expect(population.length).to.be(instance.populationSize);
        });

        it('should generate instances of `Individual` class', () => {
            var instance = createGeneticAlgorithm();
            var population = instance.generatePopulation();
            population.forEach(individual => expect(individual)
                .to.be.an(Individual));
        });

        it('should use `individual` method to create individuals', () => {
            var individual = sinon.spy();
            var instance = createGeneticAlgorithm({ individual });
            var population = instance.generatePopulation();
            expect(individual.called).to.be(true);
            expect(individual.callCount).to.be(instance.populationSize);
        });
    });
});
{'fraction_non_alphanumeric': 0.14448188711036225, 'fraction_numerical': 0.0, 'mean_word_length': 2.6196646341463414, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5383810', 'n_tokens_mistral': 1170, 'n_tokens_neox': 1121, 'n_words': 306}