-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_string_org.py
123 lines (98 loc) · 4.01 KB
/
test_string_org.py
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
import unittest
import string_org as so
class TestStringOrg(unittest.TestCase):
"""Testing real_value_vector_org.py"""
def setUp(self):
so.TARGET_STRING = "CC"
so.LETTERS = "ABC"
def test_init(self):
org = so.StringOrg()
self.assertEqual(len(so.TARGET_STRING), len(org.genotype))
def test_random_range(self):
org = so.StringOrg()
for locus in org.genotype:
self.assertIn(locus, so.LETTERS)
def test_init_genotype(self):
genotype = "AA"
org = so.StringOrg(genotype)
self.assertEqual(genotype, org.genotype)
def test_fitness(self):
org = so.StringOrg("AA")
fitness = org.fitness(so.default_environment)
self.assertEqual(fitness, 0)
def test_eq_true(self):
org1 = so.StringOrg("AA")
org2 = so.StringOrg("AA")
self.assertTrue(org1 == org2)
def test_eq_false(self):
org1 = so.StringOrg("AA")
org2 = so.StringOrg("BB")
self.assertFalse(org1 == org2)
def test_get_clone(self):
org1 = so.StringOrg("AA")
org2 = org1.get_clone()
self.assertEqual(org1, org2)
def test_get_mutant(self):
org1 = so.StringOrg("AA")
org2 = org1.get_mutant()
self.assertNotEqual(org1, org2)
def test_str(self):
org1 = so.StringOrg("CC")
org2 = so.StringOrg("AA")
self.assertIs(type(str(org1)), type("CC"))
self.assertNotEqual(str(org1), str(org2))
def test_repr(self):
org1 = so.StringOrg("CC")
org2 = so.StringOrg("AA")
self.assertIs(type(repr(org1)), type("CC"))
self.assertNotEqual(repr(org1), repr(org2))
def test_is_better_than(self):
org1 = so.StringOrg("CC")
org2 = so.StringOrg("AA")
self.assertTrue(org1.is_better_than(org2, so.default_environment))
self.assertFalse(org2.is_better_than(org1, so.default_environment))
def test_get_fitness_perfect(self):
org = so.StringOrg(so.TARGET_STRING)
fitness = org.fitness(so.default_environment)
self.assertEqual(fitness, len(so.TARGET_STRING))
def test_get_fitness_worst(self):
terrible_org_genotype = "0" * len(so.TARGET_STRING)
org = so.StringOrg(terrible_org_genotype)
fitness = org.fitness(so.default_environment)
self.assertEqual(fitness, 0)
def test_get_fitness_one_off(self):
one_off_genotype = so.TARGET_STRING[:-1] + "0"
org = so.StringOrg(one_off_genotype)
fitness = org.fitness(so.default_environment)
self.assertEqual(fitness, len(so.TARGET_STRING) - 1)
def test_get_mutated_organism(self):
org = so.StringOrg()
mutated_org = org.get_mutant()
fit = org.fitness(so.default_environment)
fit_mutated = mutated_org.fitness(so.default_environment)
fit_difference = abs(fit - fit_mutated)
self.assertLessEqual(fit_difference, 1)
class StringOrgFunctions(unittest.TestCase):
"""Testing the internal functions of the module."""
def setUp(self):
so.TARGET_STRING = "CC"
so.LETTERS = "ABC"
def test_get_mutant_genotype(self):
genotype = "AA"
mutated_genotype = so._get_mutated_genotype(genotype)
self.assertNotEqual(genotype, mutated_genotype)
def test_create_random_genotype(self):
genotype = so._create_random_genotype()
self.assertEqual(len(so.TARGET_STRING), len(genotype))
def test_default_environment_worst(self):
org = so.StringOrg("AA")
self.assertEqual(so.default_environment(org.genotype), 0)
def test_default_environment_okay(self):
org = so.StringOrg("AC")
self.assertEqual(so.default_environment(org.genotype), 1)
def test_default_environment_best(self):
org = so.StringOrg("CC")
self.assertEqual(so.default_environment(org.genotype), len(so.TARGET_STRING))
def test_hash_environment(self):
org = so.StringOrg("AA")
self.assertIs(type(so.hash_environment(org.genotype)), int)