-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobo_individual_test.cpp
193 lines (172 loc) · 6.17 KB
/
jobo_individual_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "jobo_individual.h"
#include <cassert>
#include <string>
#include <stdexcept>
#include <iostream>
#include <fstream>
#include <boost/test/unit_test.hpp>
// Boost.Test does not play well with -Weffc++
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
using namespace jobo;
BOOST_AUTO_TEST_CASE(test_jobo_individual_has_a_genotype)
{
//An individual has a genotype
const std::string genotype("ab");
const individual i(genotype);
BOOST_CHECK_EQUAL(i.get_genotype(), genotype);
}
BOOST_AUTO_TEST_CASE(test_jobo_calc_fitness_abuse)
{
//Fitness calculation for genotypes of odd lengths should throw an exception
BOOST_CHECK_THROW(calc_fitness("abc"), std::invalid_argument);
//Genotypes should be letters only
BOOST_CHECK_THROW(calc_fitness(" "), std::invalid_argument);
BOOST_CHECK_THROW(calc_fitness("++"), std::invalid_argument);
BOOST_CHECK_THROW(calc_fitness("--"), std::invalid_argument);
BOOST_CHECK_THROW(calc_fitness("()"), std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(test_jobo_cannot_create_negative_initial_genotype)
{
BOOST_CHECK_THROW(
create_initial_genotype(-2),
std::invalid_argument
);
}
BOOST_AUTO_TEST_CASE(test_jobo_genotype_has_number_of_loci)
{
//A genotype has a number of loci
const std::string genotype("ab");
const individual i(genotype);
BOOST_CHECK_EQUAL(i.get_n_loci(), genotype.size());
}
BOOST_AUTO_TEST_CASE(test_jobo_copy_individual_is_identical)
{
//A copy of an individual is identical
const std::string genotype("ab");
const individual i(genotype);
const individual j(i);
BOOST_CHECK_EQUAL(i,j);
}
BOOST_AUTO_TEST_CASE(test_jobo_fitness_calculation_of_genotype)
{
//Fitness calculation of genotype with 2 characters
BOOST_CHECK_EQUAL(calc_fitness("ab"),1.0);
BOOST_CHECK_EQUAL(calc_fitness("Ab"),1.0);
BOOST_CHECK_EQUAL(calc_fitness("aB"),0.0);
BOOST_CHECK_EQUAL(calc_fitness("AB"),1.0);
//Fitness calculation of genotype with 2 characters
BOOST_CHECK_EQUAL(calc_fitness("ab"),1.0);
BOOST_CHECK_EQUAL(calc_fitness("Ab"),1.0);
BOOST_CHECK_EQUAL(calc_fitness("aB"),0.0);
BOOST_CHECK_EQUAL(calc_fitness("AB"),1.0);
}
BOOST_AUTO_TEST_CASE(test_jobo_fitness_calculation_of_4_character_genotype)
{
//Fitness calculation of genotype with 4 characters
BOOST_CHECK_EQUAL(calc_fitness("abcd"),1.0);
BOOST_CHECK_EQUAL(calc_fitness("AbCd"),1.0);
BOOST_CHECK_EQUAL(calc_fitness("aBCD"),0.0);
BOOST_CHECK_EQUAL(calc_fitness("AbcD"),0.0);
BOOST_CHECK_EQUAL(calc_fitness("abCD"),1.0);
BOOST_CHECK_EQUAL(calc_fitness("ABcd"),1.0);
BOOST_CHECK_EQUAL(calc_fitness("aBcD"),0.0);
}
BOOST_AUTO_TEST_CASE(test_jobo_fitness_calculation_of_6_character_genotype)
{
//Fitness calculation of genotype with 6 characters
BOOST_CHECK_EQUAL(calc_fitness("abcdef"),1.0);
BOOST_CHECK_EQUAL(calc_fitness("AbCdEf"),1.0);
BOOST_CHECK_EQUAL(calc_fitness("ABCDeF"),0.0);
BOOST_CHECK_EQUAL(calc_fitness("ABcdef"),1.0);
BOOST_CHECK_EQUAL(calc_fitness("abCDef"),1.0);
BOOST_CHECK_EQUAL(calc_fitness("abcdEF"),1.0);
}
BOOST_AUTO_TEST_CASE(test_jobo_recombination)
{
// Test recombination function with two complete different parents
std::mt19937 rng_engine(42);
const genotype p("abcdefghijklmnopqrstuvwxyz");
const genotype q("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
const genotype r = recombine(p, q, rng_engine);
BOOST_CHECK (r!=p);
BOOST_CHECK (r!=q);
}
BOOST_AUTO_TEST_CASE(test_jobo_create_offspring_with_recombined_genotype)
{
// Create_offspring with recombined genotype
std::mt19937 rng_engine(42);
const genotype p("abcdefghijklmnopqrstuvwxyz");
const genotype q("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
const individual mother(p);
const individual father(q);
const individual child = create_offspring(mother,father,rng_engine);
const genotype r = child.get_genotype();
BOOST_CHECK (child!=mother);
BOOST_CHECK (child!=father);
BOOST_CHECK (r!=p);
BOOST_CHECK (r!=q);
}
BOOST_AUTO_TEST_CASE(test_jobo_create_offspring_with_uneven_genotype)
{
// Create_offspring with recombined genotype
std::mt19937 rng_engine(42);
const genotype p("abcdefghijklmnopqrstuvwxy");
const genotype q("ABCDEFGHIJKLMNOPQRSTUVWXY");
const individual mother(p);
const individual father(q);
BOOST_CHECK_THROW(create_offspring(mother,father,rng_engine), std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(test_jobo_mutation_check_all_loci)
{
// Test Mutation_check_all_loci function for genotype of 26 loci
std::mt19937 rng_engine(42);
const double mutation_rate{0.5};
const genotype r("aBcDeFgHiJkLmNoPqRsTuVwXyZ");
const genotype v = mutation_check_all_loci(r,mutation_rate,rng_engine);
BOOST_CHECK (r!=v);
}
BOOST_AUTO_TEST_CASE(test_jobo_create_mutation)
{
// Create individual with mutation with create_mutation function
std::mt19937 rng_engine(42);
const double mutation_rate{0.5};
const genotype r("AbCdEfGhIjKlMnOpQrStUvWxYz");
const individual before_mutation(r);
const individual after_mutation = create_mutation(before_mutation,mutation_rate,rng_engine);
BOOST_CHECK (before_mutation!=after_mutation);
}
BOOST_AUTO_TEST_CASE(test_jobo_recombine)
{
// Create individual with mutation with create_mutation function
std::mt19937 rng_engine(42);
const genotype i("AbCdEfGhIjKlMnOpQrStUvWxYz");
const genotype j("AbCdEfGhIjKlMnOpQrStUvWxY");
BOOST_CHECK_THROW(
recombine(i,j,rng_engine),
std::invalid_argument
);
}
BOOST_AUTO_TEST_CASE(test_is_viable_species)
{
const genotype r("AbCdEfGhIjKlMnOpQrStUvWxYz");
BOOST_CHECK_EQUAL ((is_viable_species(r)),1);
const genotype q("AbCdEfGhIjKlmNOpQrStUvWxYz");
BOOST_CHECK_EQUAL ((is_viable_species(q)),0);
const genotype s("aBCdEfGhIjKlmNOpQrStUvWxYz");
BOOST_CHECK_EQUAL ((is_viable_species(q)),0);
}
BOOST_AUTO_TEST_CASE(test_jobo_create_offspring)
{
// Create individual with mutation with create_mutation function
std::mt19937 rng_engine(42);
const genotype i("AbCdEfGhIjKlMnOpQrStUvWxYz");
const genotype j("AbCdEfGhIjKlMnOpQrStUvWxY");
const individual k(i);
const individual l(j);
BOOST_CHECK_THROW(
create_offspring(k,l,rng_engine),
std::invalid_argument
);
}
#pragma GCC diagnostic pop