示例#1
0
def test_bitfield_enums():
    """Test bitfield enums."""
    parent_reg = RegsRegister(
        TEST_REG_NAME,
        TEST_REG_OFFSET,
        TEST_REG_WIDTH,
        TEST_REG_DESCR,
        TEST_REG_REV,
        TEST_REG_ACCESS,
    )

    bitfield = RegsBitField(
        parent_reg,
        TEST_BITFIELD_NAME,
        TEST_BITFIELD_OFFSET,
        TEST_BITFIELD_WIDTH,
        TEST_BITFIELD_DESCR,
        TEST_BITFIELD_RESET_VAL,
        TEST_BITFIELD_ACCESS,
    )

    parent_reg.add_bitfield(bitfield)

    enums = []
    for index in range((1 << TEST_BITFIELD_WIDTH) - 1):
        enum = RegsEnum(f"{TEST_ENUM_NAME}{index}", index,
                        f"{TEST_ENUM_DESCR}{index}", TEST_BITFIELD_WIDTH)
        enums.append(enum)
        bitfield.add_enum(enum)

    enum_names = bitfield.get_enum_names()

    for index in range((1 << TEST_BITFIELD_WIDTH) - 1):
        assert index == bitfield.get_enum_constant(f"{TEST_ENUM_NAME}{index}")
        assert enums[index].name in enum_names

    for index in range((1 << TEST_BITFIELD_WIDTH)):
        bitfield.set_value(index)
        if index < (1 << TEST_BITFIELD_WIDTH) - 1:
            assert f"{TEST_ENUM_NAME}{index}" == bitfield.get_enum_value()
        else:
            assert index == bitfield.get_enum_value()

    for index in range((1 << TEST_BITFIELD_WIDTH) - 1):
        bitfield.set_enum_value(f"{TEST_ENUM_NAME}{index}")
        assert index == bitfield.get_value()

    for index in range((1 << TEST_BITFIELD_WIDTH) - 1):
        bitfield.set_enum_value(f"{index}")
        assert index == bitfield.get_value()

    with pytest.raises(SPSDKRegsErrorEnumNotFound):
        bitfield.get_enum_constant("Invalid name")
示例#2
0
def test_bitfield_enums():
    parent_reg = RegsRegister(TEST_REG_NAME, TEST_REG_OFFSET, TEST_REG_WIDTH,
                              TEST_REG_DESCR, TEST_REG_REV, TEST_REG_ACCESS)

    bitfield = RegsBitField(parent_reg, TEST_BITFIELD_NAME,
                            TEST_BITFILED_OFFSET, TEST_BITFILED_WIDTH,
                            TEST_BITFIELD_DESCR, TEST_BITFIELD_RESET_VAL,
                            TEST_BITFIELD_ACCESS)

    parent_reg.add_bitfield(bitfield)

    enums = []
    for n in range((1 << TEST_BITFILED_WIDTH) - 1):
        enum = RegsEnum(f"{TEST_ENUM_NAME}{n}", n, f"{TEST_ENUM_DESCR}{n}",
                        TEST_BITFILED_WIDTH)
        enums.append(enum)
        bitfield.add_enum(enum)

    enum_names = bitfield.get_enum_names()

    for n in range((1 << TEST_BITFILED_WIDTH) - 1):
        assert n == bitfield.get_enum_constant(f"{TEST_ENUM_NAME}{n}")
        assert enums[n].name in enum_names

    for n in range((1 << TEST_BITFILED_WIDTH)):
        bitfield.set_value(n)
        if n < (1 << TEST_BITFILED_WIDTH) - 1:
            assert f"{TEST_ENUM_NAME}{n}" == bitfield.get_enum_value()
        else:
            assert n == bitfield.get_enum_value()

    for n in range((1 << TEST_BITFILED_WIDTH) - 1):
        bitfield.set_enum_value(f"{TEST_ENUM_NAME}{n}")
        assert n == bitfield.get_value()

    with pytest.raises(EnumNotFound):
        bitfield.get_enum_constant("Invalid name")

    regs = Registers(TEST_DEVICE_NAME)

    regs.add_register(parent_reg)