From c9db29f895be287471f280bbed6cfb5271de3803 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rafa=C5=82=20D=C5=82ugo=C5=82=C4=99cki?= Date: Wed, 8 Apr 2015 19:49:44 +0200 Subject: [PATCH] Add BitGenerator tests. Restructure tests. --- configure.ac | 3 -- src/generator/{bit.h => bitGenerator.h} | 4 +-- src/main.cpp | 4 +-- tests/Makefile.am | 33 +++++++++++++++--- tests/chromosome/Makefile.am | 16 --------- tests/gene/Makefile.am | 16 --------- tests/generation/Makefile.am | 16 --------- tests/generator/bitGenerator/breed.cpp | 45 +++++++++++++++++++++++++ 8 files changed, 77 insertions(+), 60 deletions(-) rename src/generator/{bit.h => bitGenerator.h} (92%) delete mode 100644 tests/chromosome/Makefile.am delete mode 100644 tests/gene/Makefile.am delete mode 100644 tests/generation/Makefile.am create mode 100644 tests/generator/bitGenerator/breed.cpp diff --git a/configure.ac b/configure.ac index 6777448..e8b6f95 100644 --- a/configure.ac +++ b/configure.ac @@ -7,9 +7,6 @@ AC_PROG_CXX AC_CONFIG_FILES([ Makefile tests/Makefile - tests/gene/Makefile - tests/chromosome/Makefile - tests/generation/Makefile ]) AC_OUTPUT diff --git a/src/generator/bit.h b/src/generator/bitGenerator.h similarity index 92% rename from src/generator/bit.h rename to src/generator/bitGenerator.h index 7bd5007..8e92521 100644 --- a/src/generator/bit.h +++ b/src/generator/bitGenerator.h @@ -18,7 +18,7 @@ namespace genetic { * Generator for generating entire Generation of Chromosomes (individuals) */ template < typename _Chromosome > - class Bit : public Generator<_Chromosome> { + class BitGenerator : public Generator<_Chromosome> { public: /** * Type representing Chromosome Gene @@ -52,7 +52,7 @@ namespace genetic { * @param generationSize Indicates size of the generation * @param chromosomeSize Indicates size of the chromosome */ - Bit(unsigned int generationSize, unsigned int chromosomeSize) + BitGenerator(unsigned int generationSize, unsigned int chromosomeSize) : Generator<_Chromosome>(generationSize, chromosomeSize) { } }; diff --git a/src/main.cpp b/src/main.cpp index 51beb23..336285a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,7 @@ #include "gene.h" #include "chromosome.h" #include "generation.h" -#include "generator/bit.h" +#include "generator/bitGenerator.h" #include "selection/rouletteSelection.h" #include "selection/linearRankSelection.h" @@ -30,7 +30,7 @@ int main() { typedef Crossover<_Chromosome> _Crossover; typedef Mutation<_Chromosome> _Mutation; - typedef generator::Bit<_Chromosome> _Generator; + typedef generator::BitGenerator<_Chromosome> _Generator; typedef Algorithm<_Chromosome, _Selection, _Crossover, _Mutation, _Fitness> _Algorithm; typedef GenerationLimit<_Chromosome> _Condition; diff --git a/tests/Makefile.am b/tests/Makefile.am index b869300..6dcf89a 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,7 +1,30 @@ -SUBDIRS = \ - gene \ - chromosome \ - generation +AUTOMAKE_OPTIONS = subdir-objects + +TESTS = \ + gene_create.test \ + gene_copy.test \ + chromosome_create.test\ + chromosome_copy.test \ + generation_create.test\ + generation_copy.test \ + generator_bitGeneratorBreed.test + +noinst_PROGRAMS = $(TESTS) + +AM_CPPFLAGS = -O2 -I$(top_srcdir)/src + +gene_create_test_SOURCES = gene/create.cpp +gene_copy_test_SOURCES = gene/copy.cpp + +chromosome_create_test_SOURCES = chromosome/create.cpp +chromosome_copy_test_SOURCES = chromosome/copy.cpp + +generation_create_test_SOURCES = generation/create.cpp +generation_copy_test_SOURCES = generation/copy.cpp + +generator_bitGeneratorBreed_test_SOURCES = generator/bitGenerator/breed.cpp clean-local: - rm -f *.log \ No newline at end of file + rm -f *.log + +all-local: \ No newline at end of file diff --git a/tests/chromosome/Makefile.am b/tests/chromosome/Makefile.am deleted file mode 100644 index 51f78d9..0000000 --- a/tests/chromosome/Makefile.am +++ /dev/null @@ -1,16 +0,0 @@ -noinst_PROGRAMS = create.test copy.test - -create_test_SOURCES = create.cpp -create_test_CPPFLAGS = -I$(top_srcdir)/src - -copy_test_SOURCES = copy.cpp -copy_test_CPPFLAGS = -I$(top_srcdir)/src - -TESTS = \ - create.test \ - copy.test - -check-chromosome: create.test copy.test - -clean-local: - rm -f *.test \ No newline at end of file diff --git a/tests/gene/Makefile.am b/tests/gene/Makefile.am deleted file mode 100644 index cfb4b12..0000000 --- a/tests/gene/Makefile.am +++ /dev/null @@ -1,16 +0,0 @@ -noinst_PROGRAMS = create.test copy.test - -create_test_SOURCES = create.cpp -create_test_CPPFLAGS = -I$(top_srcdir)/src - -copy_test_SOURCES = copy.cpp -copy_test_CPPFLAGS = -I$(top_srcdir)/src - -TESTS = \ - create.test \ - copy.test - -check-gene: create.test copy.test - -clean-local: - rm -f *.test \ No newline at end of file diff --git a/tests/generation/Makefile.am b/tests/generation/Makefile.am deleted file mode 100644 index 429c927..0000000 --- a/tests/generation/Makefile.am +++ /dev/null @@ -1,16 +0,0 @@ -noinst_PROGRAMS = create.test copy.test - -create_test_SOURCES = create.cpp -create_test_CPPFLAGS = -I$(top_srcdir)/src - -copy_test_SOURCES = copy.cpp -copy_test_CPPFLAGS = -I$(top_srcdir)/src - -TESTS = \ - create.test \ - copy.test - -check-generation: create.test copy.test - -clean-local: - rm -f *.test \ No newline at end of file diff --git a/tests/generator/bitGenerator/breed.cpp b/tests/generator/bitGenerator/breed.cpp new file mode 100644 index 0000000..d6ec7c3 --- /dev/null +++ b/tests/generator/bitGenerator/breed.cpp @@ -0,0 +1,45 @@ +#include +#include + +#include "gene.h" +#include "chromosome.h" +#include "generation.h" +#include "generator/bitGenerator.h" + +using namespace std; +using namespace genetic; + +int main() { + typedef Gene _Gene; + typedef Chromosome<_Gene> _Chromosome; + typedef Generation<_Chromosome> _Generation; + typedef generator::BitGenerator<_Chromosome> _Generator; + + const unsigned int chromosomeSize = 20; + const unsigned int generationSize = 100; + + _Generator generator(generationSize, chromosomeSize); + _Generation generation = generator.breed(); + + if (generation.size() != generationSize) { + cout << "Generation contained incorrect size after breeding\n"; + return 1; + } + + for (unsigned int i = 0; i < generation.size(); i++) { + if (generation[i].size() != chromosomeSize) { + cout << "Chromosome contained bad size after breeding\n"; + return 1; + } + for (unsigned int j = 0; j < generation[i].size(); j++) { + if ((generation[i][j].get() != 0) && + (generation[i][j].get() != 1)) { + cout << "Chromosome breeded by BitGenerator does not have bit Genes\n"; + } + } + } + + cout << "BitGenerator breeded correctly\n"; + return 0; +} + -- 2.30.2