def makeAssignState(meth): r = random.random() * 10 if (r < 2): # 参数赋值 if (meth.argNames is not None): i = int(random.random() * len(meth.argNames)) v = meth.argNames[i] d = JOcHelper.randValue(meth.argTypes[i]) return v + ' = ' + d + ';' elif (r < 4): # 局部变量赋值 var_map = meth.variables if (var_map is not None) and (len(var_map) > 0): v = random.choice(list(var_map.keys())) d = JOcHelper.randValue(var_map[v][0]) # meth.variables[v][1] = d return v + ' = ' + d + ';' elif (r < 6): # 类变量赋值 var_map = meth.mClass.variables if (var_map is not None) and (len(var_map) > 0) and (not JOcMixHelper.isClassMethod(meth.scope)): v = random.choice(list(var_map.keys())) d = JOcHelper.randValue(var_map[v][0]) # meth.mClass.variables[v][1] = d return v + ' = ' + d + ';' elif (r < 9): # 变量互相赋值 tdict = JOcMixHelper.getVisibleVars(meth) # for t in tdict: # if (t.startswith('const')): # tdict[t] = [] # 去除常量,因为常量不能赋值。 vls = list(tdict.values()) for i in range(len(vls)-1, -1, -1): if (vls[i] is None) or (len(vls[i]) < 2): del vls[i] if (len(vls) > 0): vls = vls[int(random.random() * len(vls))] i = int(random.random() * len(vls)) v1 = vls[i] del vls[i] i = int(random.random() * len(vls)) v2 = vls[i] return v1 + ' = ' + v2 + ';'
def addVarialbe(self, type=None): var_list = [] var_list.extend(self.variables.keys()) if (self.argNames is not None): var_list.extend(self.argNames) v = JOcHelper.var(var_list) if (type is None): type = random.choice(OC_BASE) d = JOcHelper.randValue(type) self.variables[v] = [type, d] return v
def callMethod(meth, cl = None, fromMeth = None): s = ('[' + cl.className) if (cl is not None) else '' vard = None if (fromMeth is None) else JOcMixHelper.getVisibleVars(fromMeth) msgs = meth.messages args = meth.argTypes for i in range(len(msgs)): s += ' ' + msgs[i] if (args is not None): t = args[i] if (vard is not None): vars = vard.get(t) if (vars is not None): s += ':' + vars[int(random.random() * len(vars))] continue s += ':' + JOcHelper.randValue(t) s += '];' return s
def makeRet(meth): vars = [] r = meth.ret if (meth.variables is not None): for v in meth.variables: t = meth.variables[v][0] if (t == r): vars.append(v) elif (JOcHelper.isNumber(r) and JOcHelper.isNumber(t)): vars.append('(' + r + ')' + v) if (meth.argNames is not None): for i in range(len(meth.argNames)): t = meth.argTypes[i] if (t == r): vars.append(meth.argNames[i]) elif (JOcHelper.isNumber(r) and JOcHelper.isNumber(t)): vars.append('(' + r + ')' + meth.argNames[i]) if len(vars) > 0: return random.choice(vars) else: return JOcHelper.randValue(r)