def generate_composed_expression(self, type): if type == 'bool': operator = randomizer.choice(self.boolean_operator) return 'expression' + ' ' + operator + ' ' + 'expression' if type == 'string': operator = randomizer.choice(["+"]) return 'expression' + ' ' + operator + ' ' + 'expression' if type == 'arithmetic': operator = randomizer.choice(self.aritmetic_operators) return 'expression' + ' ' + operator + ' ' + 'expression'
def get_random_string(length, first_capital): name_length = length if first_capital: return ''.join( randomizer.choice(string.ascii_lowercase) for _ in range(name_length)).capitalize() else: return ''.join( randomizer.choice(string.ascii_lowercase) for _ in range(name_length))
def generate_base_expression(self, type): if type == 'bool': values = ['true', 'false'] return randomizer.choice(values) if type == 'string': return '"' + common.get_random_string( randomizer.randint(self.string_min_length, self.string_max_length), False) + '"' if type == 'arithmetic': return common.get_random_number(self.numeric_min_size, self.numeric_max_size)
def generate_random(self, types): size = randomizer.randint(self.base_type_min_size, self.base_type_max_size) identifier_list = [] #todo randomize identifier_list type = randomizer.choice(self.base_types) if len(types) > 0: type = randomizer.choice(types) if type == 'void': return void() if type == 'error': return error(identifier_list) if type == 'bool': return bool() if type == 'bit': return bit(size) if type == 'varbit': return varbit(size) if type == 'int': return int(size) return None
def replace(self, expression, type): if 'expression' not in expression: return expression else: if randomizer.choice([0, 1]) == 0: replace = self.generate_base_expression(type) expression = self.nth_repl(expression, 'expression', replace, 1) return self.replace(expression, type) else: replace = self.generate_composed_expression(type) expression = self.nth_repl(expression, 'expression', replace, 1) return self.replace(expression, type)
def get(type): max_string_length = 1000 max_int_size = 100000 # if type == 'error': # return error(identifier_list) if type.get_name() == 'bool': return randomizer.choice(["true", "false"]) if type.get_name() == 'bit': return '"' + common.get_random_string(type.get_size(), False) + '"' if type.get_name() == 'varbit': return '"' + bin(common.get_random_number(0, type.get_size())) + '"' if type.get_name() == 'int': return common.get_random_number(0, type.get_size()) return ''
def randomize(self): self.prefix = randomizer.choice([True, False]) self.non_type_name = non_type_name() self.non_type_name.randomize()
def get_expression_template(self): operator = randomizer.choice(self.boolean_operator) return '(expression)' + ' ' + operator + ' ' + '(expression)'