def testCallbackGenerator(self): self.src_templates = [] self.dst_templates = [] self.cb_call_count = 0 def cb_gen(src, dst): self.src_templates.append(src) self.dst_templates.append(dst) self.cb_call_count += 1 return self.cb_call_count first_time = True model = RandomSequenceModel(name='uut', callback_generator=cb_gen, num_mutations=100, max_sequence=13) for template in self.templates: model.add_template(template) while model.mutate(): # this ugly piece of code is needed because we want to ignore the # default sequence generation delta = 0 def_len = len(model._default_sequence) if first_time: self.src_templates = self.src_templates[def_len:] self.dst_templates = self.dst_templates[def_len:] self.cb_call_count -= def_len delta = def_len first_time = False sequence = model.get_sequence() self.assertEqual(len(sequence), self.cb_call_count) for i in range(len(sequence)): self.assertEqual(self.src_templates[i], sequence[i].src) self.assertEqual(self.dst_templates[i], sequence[i].dst) self.assertEqual(delta + i + 1, sequence[i].callback) self.src_templates = [] self.dst_templates = [] self.cb_call_count = 0
def test_length_not_overflow(self): max_sequence = 5 model = RandomSequenceModel(name='uut', seed=1111, max_sequence=5) for template in self.templates: model.add_template(template) while model.mutate(): sequence = model.get_sequence() self.assertLessEqual(len(sequence), max_sequence)
def testSkipHalf(self): model = RandomSequenceModel(num_mutations=120) for template in self.templates: model.add_template(template) m_num_mutations = model.num_mutations() to_skip = m_num_mutations // 2 expected_skipped = to_skip expected_mutated = m_num_mutations - expected_skipped self._check_skip(model, to_skip, expected_skipped, expected_mutated)
def testSameSeed(self): seed = 1111 model1 = RandomSequenceModel(seed=seed, max_sequence=20) model2 = RandomSequenceModel(seed=seed, max_sequence=20) for template in self.templates: model1.add_template(template) model2.add_template(template) for _ in range(1000): self.assertEqual(model1.mutate(), model2.mutate())
def testSkipTooMuch(self): model = RandomSequenceModel(num_mutations=120) for template in self.templates: model.add_template(template) m_num_mutations = model.num_mutations() to_skip = m_num_mutations + 10 expected_skipped = m_num_mutations expected_mutated = m_num_mutations - expected_skipped self._check_skip(model, to_skip, expected_skipped, expected_mutated)
def test_skip_too_much(self): model = RandomSequenceModel(num_mutations=120) for template in self.templates: model.add_template(template) m_num_mutations = model.num_mutations() to_skip = m_num_mutations + 10 expected_skipped = m_num_mutations expected_mutated = m_num_mutations - expected_skipped self._check_skip(model, to_skip, expected_skipped, expected_mutated)
def test_num_mutations(self): num_mutation_list = [0, 1, 500, 1000, 10000] for expected_num_mutations in num_mutation_list: model = RandomSequenceModel(num_mutations=expected_num_mutations) for template in self.templates: model.add_template(template) m_num_mutations = model.num_mutations() self.assertEqual(expected_num_mutations, m_num_mutations) actual_mutations = 0 while model.mutate(): actual_mutations += 1 self.assertEqual(expected_num_mutations, actual_mutations)
def test_different_seed(self): seed1 = 1111 seed2 = 1112 model1 = RandomSequenceModel(seed=seed1, max_sequence=20) model2 = RandomSequenceModel(seed=seed2, max_sequence=20) for template in self.templates: model1.add_template(template) model2.add_template(template) seqs1 = [] seqs2 = [] while model1.mutate() and model2.mutate(): seqs1.append(model1.get_sequence()) seqs2.append(model2.get_sequence()) self.assertNotEqual(seqs1, seqs2)
def test_same_seed(self): seed = 1111 model1 = RandomSequenceModel(seed=seed, max_sequence=20) model2 = RandomSequenceModel(seed=seed, max_sequence=20) for template in self.templates: model1.add_template(template) model2.add_template(template) for i in range(1000): self.assertEqual(model1.mutate(), model2.mutate())
def testCallbackGenerator(self): self.src_templates = [] self.dst_templates = [] self.cb_call_count = 0 def cb_gen(src, dst): self.src_templates.append(src) self.dst_templates.append(dst) self.cb_call_count += 1 return self.cb_call_count model = RandomSequenceModel(name='uut', callback_generator=cb_gen, num_mutations=100, max_sequence=13) for template in self.templates: model.add_template(template) while model.mutate(): sequence = model.get_sequence() self.assertEqual(len(sequence), self.cb_call_count) for i in range(len(sequence)): self.assertEqual(self.src_templates[i], sequence[i].src) self.assertEqual(self.dst_templates[i], sequence[i].dst) self.assertEqual(i + 1, sequence[i].callback) self.src_templates = [] self.dst_templates = [] self.cb_call_count = 0
def testRandomLength(self): model = RandomSequenceModel(name='uut', seed=1111, max_sequence=20) for template in self.templates: model.add_template(template) sequences = set() iterations = 100 for _ in range(iterations): model.mutate() sequence = model.get_sequence() sequences.add(len(sequence)) self.assertGreater(len(sequences), iterations // 8)
def testLengthNotOverflow(self): max_sequence = 5 model = RandomSequenceModel(name='uut', seed=1111, max_sequence=5) for template in self.templates: model.add_template(template) while model.mutate(): sequence = model.get_sequence() self.assertLessEqual(len(sequence), max_sequence)
def test_callback_generator(self): self.src_templates = [] self.dst_templates = [] self.cb_call_count = 0 def cb_gen(src, dst): self.src_templates.append(src) self.dst_templates.append(dst) self.cb_call_count += 1 return self.cb_call_count model = RandomSequenceModel(name='uut', callback_generator=cb_gen, num_mutations=100, max_sequence=13) for template in self.templates: model.add_template(template) while model.mutate(): sequence = model.get_sequence() self.assertEqual(len(sequence), self.cb_call_count) for i in range(len(sequence)): self.assertEqual(self.src_templates[i], sequence[i].src) self.assertEqual(self.dst_templates[i], sequence[i].dst) self.assertEqual(i + 1, sequence[i].callback) self.src_templates = [] self.dst_templates = [] self.cb_call_count = 0
def test_random_length(self): model = RandomSequenceModel(name='uut', seed=1111, max_sequence=20) for template in self.templates: model.add_template(template) sequences = set() iterations = 100 for i in range(iterations): model.mutate() sequence = model.get_sequence() sequences.add(len(sequence)) self.assertGreater(len(sequences), iterations / 8)
def testNumMutations(self): num_mutation_list = [0, 1, 500, 1000, 10000] for expected_num_mutations in num_mutation_list: model = RandomSequenceModel(num_mutations=expected_num_mutations) for template in self.templates: model.add_template(template) m_num_mutations = model.num_mutations() self.assertEqual(expected_num_mutations, m_num_mutations) actual_mutations = 0 while model.mutate(): actual_mutations += 1 self.assertEqual(expected_num_mutations, actual_mutations)
def setUp(self): self.logger = get_test_logger() self.logger.debug('TESTING METHOD: %s', self._testMethodName) self.model = RandomSequenceModel() self.templates = self.get_templates() self.todo = []
def testDifferentSeed(self): seed1 = 1111 seed2 = 1112 model1 = RandomSequenceModel(seed=seed1, max_sequence=20) model2 = RandomSequenceModel(seed=seed2, max_sequence=20) for template in self.templates: model1.add_template(template) model2.add_template(template) seqs1 = [] seqs2 = [] while model1.mutate() and model2.mutate(): seqs1.append(model1.get_sequence()) seqs2.append(model2.get_sequence()) self.assertNotEqual(seqs1, seqs2)