# 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)