fvutils / pyvsc

Python packages providing a library for Verification Stimulus and Coverage
https://fvutils.github.io/pyvsc
Apache License 2.0
113 stars 26 forks source link

Issue with Enum-type attributes randomization #9

Closed hodjat91 closed 4 years ago

hodjat91 commented 4 years ago

Hello,

I'm trying to randomize an enum-type attribute (it's been mentioned in the manual that Enum and IntEnum are supported by PyVSC). When I try this example from the manual:

class my_e(Enum):
  A = auto()
  B = auto()

@vsc.randobj
class my_s(object):

    def __init__(self):
        self.a = vsc.rand_enum_t(my_e)
        self.b = vsc.enum_t(my_e)
inst = my_s()

Then this error happens in the last line: TypeError: int() argument must be a string, a bytes-like object or a number, not 'my_e'. However, with IntEnum (instead of Enum), it works but seems like doesn't actually do the randomization, below code doesn't randomize the enum (inst.a), but it does randomize the uint8 attribute:

from enum import *
import vsc

class my_e(IntEnum):
  A = auto()
  B = auto()

@vsc.randobj
class my_s(object):

    def __init__(self):
        self.a = vsc.rand_enum_t(my_e)
        self.b = vsc.enum_t(my_e)
        self.c = vsc.rand_uint8_t()
inst = my_s()
inst.randomize()
print('{}{}{}'.format(inst.a, inst.b, inst.c))

I think I'm missing something here... Could someone please explain this behavior?

Thanks, Hodjat

mballance commented 4 years ago

Hi Hodjat, Thank you for pointing this out. There actually was an issue with support of plain (non-IntEnum) enumerated types. Also, enumerated types in general were not being properly biased for randomization, and the distribution was off. I've corrected both of these issues with a9d7d1a. I also created PyPi release 0.0.3 from this. Thanks again for the report and testcase!

Best Regards, Matthew

hodjat91 commented 4 years ago

Thank you Matthew.