def test_crossover(chromosome): cases_a = [dtc.DefaultTestCase() for _ in range(5)] cases_b = [dtc.DefaultTestCase() for _ in range(5)] chromosome.add_tests(cases_a) other = tsc.TestSuiteChromosome() other.add_tests(cases_b) pos1 = randomness.next_int(len(cases_a)) pos2 = randomness.next_int(len(cases_b)) chromosome.set_changed(False) chromosome.cross_over(other, pos1, pos2) assert chromosome.test_chromosomes == cases_a[:pos1] + cases_b[pos2:] assert chromosome.has_changed()
def _random_insertion(self) -> bool: changed = False pos = 0 if len(self._elements) > 0: pos = randomness.next_int(0, len(self._elements) + 1) # This is so ugly... key_type = (get_args(self.ret_val.variable_type)[0] if get_args(self.ret_val.variable_type) else None) val_type = (get_args(self.ret_val.variable_type)[1] if get_args(self.ret_val.variable_type) else None) possibles_keys = self.test_case.get_objects(key_type, self.get_position()) possibles_values = self.test_case.get_objects(val_type, self.get_position()) alpha = 0.5 exponent = 1 while randomness.next_float() <= pow(alpha, exponent): exponent += 1 if len(possibles_keys) > 0 and len(possibles_values) > 0: self._elements.insert( pos, ( randomness.choice(possibles_keys), randomness.choice(possibles_values), ), ) changed = True return changed
def _create_tuple( self, test_case: tc.TestCase, parameter_type: Type, position: int, recursion_depth: int, ) -> vr.VariableReference: args = get_args(parameter_type) if len(args) == 0: # Untyped tuple, time to guess... size = randomness.next_int(0, config.configuration.collection_size) args = [ randomness.choice( self._test_cluster.get_all_generatable_types()) for _ in range(size) ] elements = [] for arg_type in args: previous_length = test_case.size() var = self._create_or_reuse_variable(test_case, arg_type, position, recursion_depth + 1, True) if var is not None: elements.append(var) position += test_case.size() - previous_length ret = test_case.add_statement( coll_stmt.TupleStatement(test_case, parameter_type, elements), position) ret.distance = recursion_depth return ret
def insert_random_statement(self, test_case: tc.TestCase, last_position: int) -> int: """Insert a random statement up to the given position. If the insertion was successful, the position at which the statement was inserted is returned, otherwise -1. Args: test_case: The test case to add the statement to last_position: The last position before that the statement is inserted Returns: The index the statement was inserted to, otherwise -1 """ old_size = test_case.size() rand = randomness.next_float() position = randomness.next_int(0, last_position + 1) if rand <= config.INSTANCE.insertion_uut: success = self.insert_random_call(test_case, position) else: success = self.insert_random_call_on_object(test_case, position) if test_case.size() - old_size > 1: position += test_case.size() - old_size - 1 if success: return position return -1
def seeded_testcase(self) -> DefaultTestCase: """Provides a random seeded test case. Returns: A random test case """ return self._testcases[randomness.next_int(0, len(self._testcases))]
def _create_dict( self, test_case: tc.TestCase, parameter_type: Type, position: int, recursion_depth: int, ) -> vr.VariableReference: args = get_args(parameter_type) if len(args) != 2: raise ConstructionFailedException() size = randomness.next_int(0, config.configuration.collection_size) elements = [] for _ in range(size): previous_length = test_case.size() key = self._create_or_reuse_variable(test_case, args[0], position, recursion_depth + 1, True) position += test_case.size() - previous_length previous_length = test_case.size() value = self._create_or_reuse_variable(test_case, args[1], position, recursion_depth + 1, True) position += test_case.size() - previous_length if key is not None and value is not None: elements.append((key, value)) ret = test_case.add_statement( coll_stmt.DictStatement(test_case, parameter_type, elements), position) ret.distance = recursion_depth return ret
def randomize_value(self) -> None: if (config.INSTANCE.constant_seeding and StaticConstantSeeding().has_strings and randomness.next_float() <= 0.90): self._value = StaticConstantSeeding().random_string else: length = randomness.next_int(0, config.INSTANCE.string_length + 1) self._value = randomness.next_string(length)
def randomize_value(self) -> None: if (config.INSTANCE.constant_seeding and StaticConstantSeeding().has_floats and randomness.next_float() <= 0.90): self._value = StaticConstantSeeding().random_float else: val = randomness.next_gaussian() * config.INSTANCE.max_int precision = randomness.next_int(0, 7) self._value = round(val, precision)
def get_test_case(self) -> tc.TestCase: test_case = dtc.DefaultTestCase() attempts = 0 size = randomness.next_int(1, config.configuration.chromosome_length + 1) while test_case.size() < size and attempts < config.configuration.max_attempts: self._test_factory.insert_random_statement(test_case, test_case.size()) attempts += 1 return test_case
def delta(self) -> None: assert self._value is not None probability = randomness.next_float() if probability < 1.0 / 3.0: self._value += randomness.next_gaussian() * config.INSTANCE.max_delta elif probability < 2.0 / 3.0: self._value += randomness.next_gaussian() else: self._value = round(self._value, randomness.next_int(0, 7))
def get_chromosome(self) -> tsc.TestSuiteChromosome: chromosome = tsc.TestSuiteChromosome(self._test_case_factory) num_tests = randomness.next_int( config.INSTANCE.min_initial_tests, config.INSTANCE.max_initial_tests + 1 ) for _ in range(num_tests): chromosome.add_test(self._test_case_factory.get_test_case()) return chromosome
def _random_insertion(working_on: List[str]) -> List[str]: pos = 0 if len(working_on) > 0: pos = randomness.next_int(0, len(working_on) + 1) alpha = 0.5 exponent = 1 while (randomness.next_float() <= pow(alpha, exponent) and len(working_on) < config.INSTANCE.string_length): exponent += 1 working_on = working_on[:pos] + [randomness.next_char() ] + working_on[pos:] return working_on
def _random_insertion(working_on: List[int]) -> List[int]: pos = 0 if len(working_on) > 0: pos = randomness.next_int(0, len(working_on) + 1) alpha = 0.5 exponent = 1 while ( randomness.next_float() <= pow(alpha, exponent) and len(working_on) < config.configuration.bytes_length ): exponent += 1 working_on = working_on[:pos] + [randomness.next_byte()] + working_on[pos:] return working_on
def randomize_value(self) -> None: use_seed = ( randomness.next_float() <= config.configuration.seeded_primitives_reuse_probability ) if ( config.configuration.dynamic_constant_seeding and dynamic_constant_seeding.has_strings and use_seed and config.configuration.constant_seeding and randomness.next_float() <= config.configuration.seeded_dynamic_values_reuse_probability ): self._value = dynamic_constant_seeding.random_string elif ( config.configuration.constant_seeding and static_constant_seeding.has_strings and use_seed ): self._value = static_constant_seeding.random_string else: length = randomness.next_int(0, config.configuration.string_length + 1) self._value = randomness.next_string(length)
def randomize_value(self) -> None: use_seed = ( randomness.next_float() <= config.configuration.seeded_primitives_reuse_probability ) if ( config.configuration.dynamic_constant_seeding and dynamic_constant_seeding.has_floats and use_seed and config.configuration.constant_seeding and randomness.next_float() <= config.configuration.seeded_dynamic_values_reuse_probability ): self._value = dynamic_constant_seeding.random_float elif ( config.configuration.constant_seeding and static_constant_seeding.has_floats and use_seed ): self._value = static_constant_seeding.random_float else: val = randomness.next_gaussian() * config.configuration.max_int precision = randomness.next_int(0, 7) self._value = round(val, precision)
def test_next_int(): rand = randomness.next_int(0, 50) assert 0 <= rand < 50
def get_index(self, population: List[T]) -> int: return randomness.next_int(0, len(population))
def randomize_value(self) -> None: length = randomness.next_int(0, config.configuration.bytes_length + 1) self._value = randomness.next_bytes(length)