Exemple #1
0
    def test_in_1(self):
        @vsc.randobj
        class my_c(object):
            def __init__(self):
                self.a = vsc.rand_uint32_t()

            @vsc.constraint
            def ab_c(self):
                self.a in vsc.rangelist(1, 4, 8)

        my_item = my_c()
        model = my_item.get_model()

        visitor = VariableBoundVisitor()
        visitor.process([model], [])

        a_model = model.find_field("a")

        self.assertIsNotNone(a_model)

        self.assertTrue(a_model in visitor.bound_m.keys())

        a_bounds = visitor.bound_m[a_model]

        self.assertEqual(3, len(a_bounds.domain.range_l))

        # a in 1, 4, 8
        self.assertEqual(1, a_bounds.domain.range_l[0][0])
        self.assertEqual(1, a_bounds.domain.range_l[0][1])
        self.assertEqual(4, a_bounds.domain.range_l[1][0])
        self.assertEqual(4, a_bounds.domain.range_l[1][1])
        self.assertEqual(8, a_bounds.domain.range_l[2][0])
        self.assertEqual(8, a_bounds.domain.range_l[2][1])
Exemple #2
0
    def test_in_lt_limited(self):
        @vsc.randobj
        class my_c(object):
            def __init__(self):
                self.a = vsc.rand_uint32_t()
                self.b = vsc.rand_uint16_t()
                self.c = vsc.rand_uint16_t()

            @vsc.constraint
            def ab_c(self):
                self.a < 8
                self.a in vsc.rangelist(1, 4, 8)
                self.b in vsc.rangelist((2, 4), (8, 12))
                self.b < 11
                self.c in vsc.rangelist((2, 4), (8, 12))
                self.c < 4

        my_item = my_c()
        model = my_item.get_model()

        visitor = VariableBoundVisitor()
        visitor.process([model], [])

        a_model = model.find_field("a")
        b_model = model.find_field("b")
        c_model = model.find_field("c")

        self.assertIsNotNone(a_model)
        self.assertIsNotNone(b_model)
        self.assertIsNotNone(c_model)

        self.assertTrue(a_model in visitor.bound_m.keys())
        self.assertTrue(b_model in visitor.bound_m.keys())
        self.assertTrue(c_model in visitor.bound_m.keys())

        a_bounds = visitor.bound_m[a_model]
        b_bounds = visitor.bound_m[b_model]
        c_bounds = visitor.bound_m[c_model]

        self.assertEqual(2, len(a_bounds.domain.range_l))
        self.assertEqual(2, len(b_bounds.domain.range_l))
        self.assertEqual(1, len(c_bounds.domain.range_l))

        # a in 1, 4, 8
        self.assertEqual(1, a_bounds.domain.range_l[0][0])
        self.assertEqual(1, a_bounds.domain.range_l[0][1])
        self.assertEqual(4, a_bounds.domain.range_l[1][0])
        self.assertEqual(4, a_bounds.domain.range_l[1][1])

        # b in [2..4] [8..12]
        self.assertEqual(2, b_bounds.domain.range_l[0][0])
        self.assertEqual(4, b_bounds.domain.range_l[0][1])
        self.assertEqual(8, b_bounds.domain.range_l[1][0])
        self.assertEqual(10, b_bounds.domain.range_l[1][1])

        self.assertEqual(2, c_bounds.domain.range_l[0][0])
        self.assertEqual(3, c_bounds.domain.range_l[0][1])
Exemple #3
0
    def test_enum_range_limited(self):
        class my_e(IntEnum):
            A = 0
            B = auto()
            C = auto()
            D = auto()

        @vsc.randobj
        class my_c(object):
            def __init__(self):
                self.a = vsc.rand_enum_t(my_e)

            @vsc.constraint
            def ab_c(self):
                self.a.inside(vsc.rangelist(vsc.rng(my_e.A, my_e.C), my_e.D))
                self.a >= my_e.A
                self.a <= my_e.C

        my_item = my_c()
        model = my_item.get_model()

        visitor = VariableBoundVisitor()
        visitor.process([model], [])

        a_model = model.find_field("a")

        self.assertIsNotNone(a_model)

        self.assertTrue(a_model in visitor.bound_m.keys())

        a_bounds = visitor.bound_m[a_model]

        self.assertEqual(3, len(a_bounds.domain.range_l))

        # a in 1, 4, 8
        self.assertEqual(0, a_bounds.domain.range_l[0][0])
        self.assertEqual(0, a_bounds.domain.range_l[0][1])
        self.assertEqual(1, a_bounds.domain.range_l[1][0])
        self.assertEqual(1, a_bounds.domain.range_l[1][1])
        self.assertEqual(2, a_bounds.domain.range_l[2][0])
        self.assertEqual(2, a_bounds.domain.range_l[2][1])
Exemple #4
0
    def test_smoke(self):
        @vsc.randobj
        class my_c(object):
            def __init__(self):
                self.a = vsc.rand_uint32_t()
                self.b = vsc.rand_uint16_t()

            @vsc.constraint
            def ab_c(self):
                self.a < 10
                self.b < 4

        my_item = my_c()
        model = my_item.get_model()

        visitor = VariableBoundVisitor()
        visitor.process([model], [])

        a_model = model.find_field("a")
        b_model = model.find_field("b")

        self.assertIsNotNone(a_model)
        self.assertIsNotNone(b_model)

        self.assertTrue(a_model in visitor.bound_m.keys())
        self.assertTrue(b_model in visitor.bound_m.keys())

        a_bounds = visitor.bound_m[a_model]
        b_bounds = visitor.bound_m[b_model]

        self.assertEqual(1, len(a_bounds.domain.range_l))
        self.assertEqual(1, len(b_bounds.domain.range_l))

        # a in [0..9]
        self.assertEqual(0, a_bounds.domain.range_l[0][0])
        self.assertEqual(9, a_bounds.domain.range_l[0][1])

        # b in [0..9]
        self.assertEqual(0, b_bounds.domain.range_l[0][0])
        self.assertEqual(3, b_bounds.domain.range_l[0][1])
Exemple #5
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()
Exemple #6
0
    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()