From 77f4916ea396f4bd5e4a8062ea1602dc1419c2c0 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 08:32:05 +0200 Subject: [PATCH] Add tests for Chromosome and Generation. --- configure.ac | 2 ++ tests/Makefile.am | 5 +++- tests/chromosome/Makefile.am | 16 ++++++++++++ tests/chromosome/copy.cpp | 42 ++++++++++++++++++++++++++++++ tests/chromosome/create.cpp | 39 ++++++++++++++++++++++++++++ tests/gene/Makefile.am | 2 +- tests/generation/Makefile.am | 16 ++++++++++++ tests/generation/copy.cpp | 50 ++++++++++++++++++++++++++++++++++++ tests/generation/create.cpp | 48 ++++++++++++++++++++++++++++++++++ 9 files changed, 218 insertions(+), 2 deletions(-) create mode 100644 tests/chromosome/Makefile.am create mode 100644 tests/chromosome/copy.cpp create mode 100644 tests/chromosome/create.cpp create mode 100644 tests/generation/Makefile.am create mode 100644 tests/generation/copy.cpp create mode 100644 tests/generation/create.cpp diff --git a/configure.ac b/configure.ac index 25a8fed..28bc56e 100644 --- a/configure.ac +++ b/configure.ac @@ -8,6 +8,8 @@ AC_CONFIG_FILES([ Makefile tests/Makefile tests/gene/Makefile + tests/chromosome/Makefile + tests/generation/Makefile ]) AC_OUTPUT diff --git a/tests/Makefile.am b/tests/Makefile.am index 417b211..b869300 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,4 +1,7 @@ -SUBDIRS = gene +SUBDIRS = \ + gene \ + chromosome \ + generation clean-local: rm -f *.log \ No newline at end of file diff --git a/tests/chromosome/Makefile.am b/tests/chromosome/Makefile.am new file mode 100644 index 0000000..51f78d9 --- /dev/null +++ b/tests/chromosome/Makefile.am @@ -0,0 +1,16 @@ +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/chromosome/copy.cpp b/tests/chromosome/copy.cpp new file mode 100644 index 0000000..d5a34ba --- /dev/null +++ b/tests/chromosome/copy.cpp @@ -0,0 +1,42 @@ +#include +#include + +#include "gene.h" +#include "chromosome.h" + +using namespace std; +using namespace genetic; + +int main() { + typedef Gene _Gene; + typedef Chromosome<_Gene> _Chromosome; + + const unsigned int chromosomeSize = 20; + + std::vector<_Gene> data; + + for (unsigned int i = 0; i < chromosomeSize; i++) { + data.push_back(_Gene(i)); + } + + _Chromosome chromosome(data); + _Chromosome chromosome2; + + chromosome2 = chromosome; + + if (chromosome2.size() != chromosomeSize) { + cout << "Chromosome contained incorrect size after copying\n"; + return 1; + } + + for (unsigned int i = 0; i < chromosome2.size(); i++) { + if (chromosome2[i].get() != i) { + cout << "Chromosome contained bad data after copying\n"; + return 1; + } + } + + cout << "Chromosome copies data correctly\n"; + return 0; +} + diff --git a/tests/chromosome/create.cpp b/tests/chromosome/create.cpp new file mode 100644 index 0000000..37e97fd --- /dev/null +++ b/tests/chromosome/create.cpp @@ -0,0 +1,39 @@ +#include +#include + +#include "gene.h" +#include "chromosome.h" + +using namespace std; +using namespace genetic; + +int main() { + typedef Gene _Gene; + typedef Chromosome<_Gene> _Chromosome; + + const unsigned int chromosomeSize = 20; + + std::vector<_Gene> data; + + for (unsigned int i = 0; i < chromosomeSize; i++) { + data.push_back(_Gene(i)); + } + + _Chromosome chromosome(data); + + if (chromosome.size() != chromosomeSize) { + cout << "Chromosome contained incorrect size after creation\n"; + return 1; + } + + for (unsigned int i = 0; i < chromosome.size(); i++) { + if (chromosome[i].get() != i) { + cout << "Chromosome contained bad data after creation\n"; + return 1; + } + } + + cout << "Chromosome is created correctly\n"; + return 0; +} + diff --git a/tests/gene/Makefile.am b/tests/gene/Makefile.am index 85ec6e8..cfb4b12 100644 --- a/tests/gene/Makefile.am +++ b/tests/gene/Makefile.am @@ -10,7 +10,7 @@ TESTS = \ create.test \ copy.test -check-gene: create.test copy.test all +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 new file mode 100644 index 0000000..429c927 --- /dev/null +++ b/tests/generation/Makefile.am @@ -0,0 +1,16 @@ +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/generation/copy.cpp b/tests/generation/copy.cpp new file mode 100644 index 0000000..9543483 --- /dev/null +++ b/tests/generation/copy.cpp @@ -0,0 +1,50 @@ +#include +#include + +#include "gene.h" +#include "chromosome.h" +#include "generation.h" + +using namespace std; +using namespace genetic; + +int main() { + typedef Gene _Gene; + typedef Chromosome<_Gene> _Chromosome; + typedef Generation<_Chromosome> _Generation; + + const unsigned int chromosomeSize = 20; + const unsigned int generationSize = 100; + + std::vector<_Gene> chromosomeData; + std::vector<_Chromosome> generationData; + + for (unsigned int i = 0; i < generationSize; i++) { + for (unsigned int j = 0; j < chromosomeSize; j++) { + chromosomeData.push_back(_Gene(i*j)); + } + generationData.push_back(_Chromosome(chromosomeData)); + } + + _Generation generation(generationData); + + _Generation generation2 = generation; + + if (generation2.size() != generation.size()) { + cout << "Generation contained incorrect size after copying\n"; + return 1; + } + + for (unsigned int i = 0; i < generation.size(); i++) { + for (unsigned int j = 0; j < generation[i].size(); j++) { + if (generation[i][j].get() != generationData[i][j].get()) { + cout << "Generation contained bad data after copying\n"; + return 1; + } + } + } + + cout << "Generation is created correctly\n"; + return 0; +} + diff --git a/tests/generation/create.cpp b/tests/generation/create.cpp new file mode 100644 index 0000000..730713d --- /dev/null +++ b/tests/generation/create.cpp @@ -0,0 +1,48 @@ +#include +#include + +#include "gene.h" +#include "chromosome.h" +#include "generation.h" + +using namespace std; +using namespace genetic; + +int main() { + typedef Gene _Gene; + typedef Chromosome<_Gene> _Chromosome; + typedef Generation<_Chromosome> _Generation; + + const unsigned int chromosomeSize = 20; + const unsigned int generationSize = 100; + + std::vector<_Gene> chromosomeData; + std::vector<_Chromosome> generationData; + + for (unsigned int i = 0; i < generationSize; i++) { + for (unsigned int j = 0; j < chromosomeSize; j++) { + chromosomeData.push_back(_Gene(i*j)); + } + generationData.push_back(_Chromosome(chromosomeData)); + } + + _Generation generation(generationData); + + if (generation.size() != generationSize) { + cout << "Generation contained incorrect size after creation\n"; + return 1; + } + + for (unsigned int i = 0; i < generation.size(); i++) { + for (unsigned int j = 0; j < generation[i].size(); j++) { + if (generation[i][j].get() != generationData[i][j].get()) { + cout << "Generation contained bad data after creation\n"; + return 1; + } + } + } + + cout << "Generation is created correctly\n"; + return 0; +} + -- 2.30.2