def test_order_model_1(self): @vsc.randobj class my_c(object): def __init__(self): self.a = vsc.rand_bit_t() self.b = vsc.rand_uint8_t() @vsc.constraint def ab_c(self): vsc.solve_order(self.a, self.b) self.a < self.b with vsc.if_then(self.a == 0): self.b < 10 i = my_c() # i.randomize() model = i.get_model() model.set_used_rand(True) info = RandInfoBuilder.build([model], []) self.assertEqual(len(info.randset_l), 2) a = model.find_field("a") b = model.find_field("b") self.assertIsNotNone(a) self.assertIsNotNone(b) self.assertIn(a, info.randset_l[0].fields()) self.assertIn(b, info.randset_l[1].fields())
def test_connected_ite_cross_block(self): @vsc.randobj class my_cls(object): def __init__(self): super().__init__() self.a = rand_bit_t(8) self.b = rand_bit_t(8) self.c = rand_bit_t(8) self.d = rand_bit_t(8) @vsc.constraint def a_c(self): self.a < self.b @vsc.constraint def b_c(self): with vsc.if_then(self.b < self.c): self.c < self.d my_cls_i = my_cls() model = my_cls_i.get_model() model.set_used_rand(True) info = RandInfoBuilder.build([model], []) self.assertEqual(len(info.randset_l), 1) self.assertEqual(len(info.randset_l[0].fields()), 4) self.assertEqual(len(info.randset_l[0].constraints()), 2) self.assertEqual(len(info.unconstrained_l), 0)
def test_two_randsets(self): @vsc.randobj class my_cls(object): def __init__(self): super().__init__() self.a = rand_bit_t(8) self.b = rand_bit_t(8) self.c = rand_bit_t(8) self.d = rand_bit_t(8) @vsc.constraint def a_c(self): self.a < self.b self.c < self.d my_cls_i = my_cls() model = my_cls_i.get_model() model.set_used_rand(True) info = RandInfoBuilder.build([model], []) self.assertEqual(len(info.randset_l), 2) self.assertEqual(len(info.randset_l[0].fields()), 2) self.assertEqual(len(info.randset_l[0].constraints()), 1) self.assertEqual(len(info.randset_l[1].fields()), 2) self.assertEqual(len(info.randset_l[1].constraints()), 1) self.assertEqual(len(info.unconstrained_l), 0)
def do_randomize(field_model_l: List[FieldModel], constraint_l: List[ConstraintModel] = None): # All fields passed to do_randomize are treated # as randomizable seed = Randomizer._rng.randint(0, (1 << 64) - 1) for f in field_model_l: f.set_used_rand(True, 0) # print("Initial Model:") # for fm in field_model_l: # print(" " + ModelPrettyPrinter.print(fm)) if constraint_l is None: constraint_l = [] # Collect all variables (pre-array) and establish bounds bounds_v = VariableBoundVisitor() bounds_v.process(field_model_l, constraint_l, False) # TODO: need to handle inline constraints that impact arrays constraints_len = len(constraint_l) for fm in field_model_l: constraint_l.extend( ArrayConstraintBuilder.build(fm, bounds_v.bound_m)) # Now, handle dist constraints DistConstraintBuilder.build(seed, fm) # If we made changes during array remodeling, # re-run bounds checking on the updated model # if len(constraint_l) != constraints_len: bounds_v.process(field_model_l, constraint_l) # print("Final Model:") # for fm in field_model_l: # print(" " + ModelPrettyPrinter.print(fm)) # for c in constraint_l: # print(" " + ModelPrettyPrinter.print(c, show_exp=True)) # First, invoke pre_randomize on all elements for fm in field_model_l: fm.pre_randomize() r = Randomizer() ri = RandInfoBuilder.build(field_model_l, constraint_l, Randomizer._rng) try: r.randomize(ri, bounds_v.bound_m) finally: # Rollback any constraints we've replaced for arrays for fm in field_model_l: ConstraintOverrideRollbackVisitor.rollback(fm) for fm in field_model_l: fm.post_randomize()
def test_no_refs(self): @vsc.randobj class my_cls(object): def __init__(self): self.a = rand_bit_t(8) self.b = rand_bit_t(8) my_cls_i = my_cls() model = my_cls_i.get_model() model.set_used_rand(True) info = RandInfoBuilder.build([model], []) self.assertEqual(len(info.randset_l), 0) self.assertEqual(len(info.unconstrained_l), 2)
def do_randomize(randstate, srcinfo: SourceInfo, field_model_l: List[FieldModel], constraint_l: List[ConstraintModel] = None, debug=0, lint=0, solve_fail_debug=0): if profile_on(): solve_info = SolveInfo() solve_info.totaltime = time.time() randomize_start(srcinfo, field_model_l, constraint_l) else: solve_info = None clear_soft_priority = ClearSoftPriorityVisitor() for f in field_model_l: f.set_used_rand(True, 0) clear_soft_priority.clear(f) if debug > 0: print("Initial Model:") for fm in field_model_l: print(" " + ModelPrettyPrinter.print(fm)) # First, invoke pre_randomize on all elements for fm in field_model_l: fm.pre_randomize() if constraint_l is None: constraint_l = [] for c in constraint_l: clear_soft_priority.clear(c) # Collect all variables (pre-array) and establish bounds bounds_v = VariableBoundVisitor() bounds_v.process(field_model_l, constraint_l, False) # TODO: need to handle inline constraints that impact arrays constraints_len = len(constraint_l) for fm in field_model_l: constraint_l.extend( ArrayConstraintBuilder.build(fm, bounds_v.bound_m)) # Now, handle dist constraints DistConstraintBuilder.build(randstate, fm) for c in constraint_l: constraint_l.extend( ArrayConstraintBuilder.build(c, bounds_v.bound_m)) # Now, handle dist constraints DistConstraintBuilder.build(randstate, c) # If we made changes during array remodeling, # re-run bounds checking on the updated model # if len(constraint_l) != constraints_len: bounds_v.process(field_model_l, constraint_l) if debug > 0: print("Final Model:") for fm in field_model_l: print(" " + ModelPrettyPrinter.print(fm)) for c in constraint_l: print(" " + ModelPrettyPrinter.print(c, show_exp=True)) # if lint > 0: # LintVisitor().lint( # field_model_l, # constraint_l) r = Randomizer(randstate, solve_info=solve_info, debug=debug, lint=lint, solve_fail_debug=solve_fail_debug) # if Randomizer._rng is None: # Randomizer._rng = random.Random(random.randrange(sys.maxsize)) ri = RandInfoBuilder.build(field_model_l, constraint_l, Randomizer._rng) try: r.randomize(ri, bounds_v.bound_m) finally: # Rollback any constraints we've replaced for arrays if solve_info is not None: solve_info.totaltime = int( (time.time() - solve_info.totaltime) * 1000) randomize_done(srcinfo, solve_info) for fm in field_model_l: ConstraintOverrideRollbackVisitor.rollback(fm) for fm in field_model_l: fm.post_randomize()