Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/test_buildingblocks.py
blob: 5c0b84b881b54c13eb88a53aaae82af5e527dd12 (plain)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# coding: UTF-8
# Copyright 2009, 2010 Thomas Jourdan
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import unittest

import ka_extensionpoint
import model_random
import model_constraintpool
import exon_color
import ka_importer
import exon_position
import exon_direction
import exon_buzzword
import test_utils
from ep_sampler_affineifs import AffineIfsSampler

class TestKandidBuildingBlocks(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_merger(self):
        self._run_mm_test('merger')

    def test_modifier(self):
        self._run_mm_test('modifier')

    def _run_mm_test(self, key):
        model_random.set_flurry(9)
        for flavor in ka_extensionpoint.list_extensions(key):
            mm1 = ka_extensionpoint.create(flavor, '/')
            mm1.randomize()
            mm1.mutate()
            mm1.swap_places()
            mm2 = ka_extensionpoint.create(flavor, '/')
            mm2.randomize()
            mm2.mutate()
            mm2.swap_places()
            mm3 = mm1.crossingover(mm2)
            mm3.mutate()
            mm3.swap_places()
            diff = not (mm3 == mm1 and mm3 == mm2)
            if not diff:
                print 'no difference found:', key, mm1, mm2, mm3
            self.assertTrue(diff)

    def test_sampler(self):
        key = 'sampler'
        model_random.set_flurry(9)
        for flavor in ka_extensionpoint.list_extensions(key):
            mm1 = ka_extensionpoint.create(flavor, '/')
            mm1.randomize()
            mm1.mutate()
            mm1.swap_places()
            mm2 = ka_extensionpoint.create(flavor, '/')
            mm2.randomize()
            mm2.mutate()
            mm2.swap_places()
            mm3 = mm1.crossingover(mm2)
            mm3.mutate()
            mm3.swap_places()
            diff = not (mm3 == mm1 and mm3 == mm2)
            if not diff:
                print 'no difference found:', key, mm1, mm2, mm3
            extend = mm3.get_sample_extent()
            self.assertEqual(2, len(extend))
            self.assertTrue(0.0 < extend[0] <= 1.0)
            self.assertTrue(0.0 < extend[1] <= 1.0)
            points = mm3.get_sample_points()
            for point in points:
                self.assertEqual(2, len(point))
            mm3.explain()

    def test_colorgamut(self):
        key = 'colorgamut'
        model_random.set_flurry(9)
        for flavor in ka_extensionpoint.list_extensions(key):
            mm1 = ka_extensionpoint.create(flavor, '/')
            mm1.randomize()
            mm1.mutate()
            color1 = mm1.get_randomized_color('/')
            mm1.adjust_color(color1)
            self.assertEqual(4, len(color1.rgba))
            class DummyFormater:
                def __init__(self):
                    self.a_text = ''
                def text_item(self, text):
                    self.a_text = text
            dummy_formater = DummyFormater()
            mm1.copy().explain(dummy_formater)
            self.assertTrue(len(dummy_formater.a_text) > 0)

    def test_stamp(self):
        cpool = model_constraintpool.ConstraintPool.get_pool()
        for theme in ka_importer.get_theme_list():
            cpool.set('*/themeconstraint',
                      'themeconstraint', theme)
            self._run_stamp_breed_test(2)
        cpool.set('*/themeconstraint',
                  'hemeconstraint', 'non_existing_theme')
        self._run_stamp_breed_test(99)
        cpool.set('*/themeconstraint',
                  'themeconstraint', ka_importer.get_theme_list())
        self._run_stamp_breed_test(0)

    def _run_stamp_breed_test(self, max_states):
        model_random.set_flurry(9)
        for flavor in ka_extensionpoint.list_extensions('stamp'):
            mm1 = ka_extensionpoint.create(flavor, '/', max_states)
            mm1.randomize()
            mm1.mutate()
            mm1.swap_places()
            mm2 = ka_extensionpoint.create(flavor, '/', max_states)
            mm2.randomize()
            mm2.mutate()
            mm2.swap_places()
            mm3 = mm1.crossingover(mm2)
            mm3.mutate()

    def test_positionconstraint(self):
        self._run_ep_constarint_test('position')

    def test_directionconstraint(self):
        self._run_ep_constarint_test('direction')

    def _run_ep_constarint_test(self, key):
        for flavor in ka_extensionpoint.list_extensions(key + 'constraint'):
            constraint = ka_extensionpoint.create(flavor, '/')
            x_pos, y_pos = 0.1, 0.2
            x_pos, y_pos = constraint.filter(x_pos, y_pos)
            x_pos, y_pos = constraint.randomize()
            x_pos, y_pos = constraint.mutate(x_pos, y_pos)
            self.assertTrue(isinstance(x_pos, float))
            self.assertTrue(isinstance(y_pos, float))

    def test_exon_position(self):
        self._run_exon_test('position', exon_position.Position, 0.5, 0.5)
            
    def test_exon_direction(self):
        self._run_exon_test('direction', exon_direction.Direction, 0.3, 0.4)
            
    def test_exon_buzzword(self):
        self._run_exon_test('buzzword', exon_buzzword.Buzzword, [u'a', u'b'])
            
    def _run_exon_test(self, key, constructor, *params):
        model_random.set_flurry(9)
        test_utils.neutral()
        
        cpool = model_constraintpool.ConstraintPool.get_pool()
        for constraint in ka_extensionpoint.list_extensions(key + 'constraint'):
            cpool.set('/' + key.capitalize(), key + 'constraint', [constraint])
            element1 = constructor('/', *params)
            element2 = element1.copy()
            for dummy in range(32):
                element3 = element1.crossingover(element2)
                self.assertEqual(element1, element2)
                self.assertEqual(element1, element3)
            equal = True
            for dummy in range(32):
                element2.randomize()
                element2.mutate()
                element2.swap_places()
                equal = equal and element1 == element2
            self.assertFalse(equal)
            self.assertEqual(element1, element3)
            
    def test_exon_color(self):
        model_random.set_flurry(9)
        cpool = model_constraintpool.ConstraintPool.get_pool()
        cpool.clear_all()

        key = 'color'
        cpool = model_constraintpool.ConstraintPool.get_pool()

        for constraint in ka_extensionpoint.list_extensions(key + 'constraint'):
            cpool.set('/' + key.capitalize(), key + 'constraint', [constraint])
            a_color = exon_color.Color('/', 0.5, 0.5, 0.5, 1.0)
            self.assertFalse(a_color is a_color.crossingover(\
                                    exon_color.Color('/', 0.5, 0.5, 0.5, 1.0)))
            self.assertEqual(4, len(a_color.copy().rgba))
            a_color.randomize()
            self.assertEqual(4, len(a_color.rgba))
            for dummy in range(32):
                a_color.mutate()
            self.assertEqual(4, len(a_color.rgba))

        test_utils.neutral()
        color1 = exon_color.Color('/', 1.0, 1.0, 1.0, 1.0)
        color2 = color1.copy()
        self.assertTrue(color2 is not color1)
        self.assertTrue(color2 == color1)
        color1.randomize()
        self.assertFalse(color2 == color1)
        for dummy in range(32):
            color3 = color1.crossingover(color2)
            self.assertTrue(color3 is not color1)
            self.assertTrue(color3 is not color2)
            self.assertTrue(color3 == color1 or color3 == color2)
        color4 = color3.copy()
        eql = True
        for dummy in range(32):
            color4.mutate()
            color4.swap_places()
            eql = eql and color4 == color3
        self.assertFalse(eql)
        
    def test_ifs(self):
        model_random.set_flurry(9)
        ifs = AffineIfsSampler('/')
        ifs.randomize()
        ifs.orbits = 10
        ifs.symmetry = 5
        point_list = ifs.get_sample_points()
        self.assertTrue(len(point_list) == 10)
#        self.assertTrue(len(point_list[0]) == 3)
        self.assertTrue(len(point_list[0]) == 2)