def test_basic_regs(tmpdir): """Basic test of registers class.""" regs = Registers(TEST_DEVICE_NAME) assert regs.dev_name == TEST_DEVICE_NAME reg1 = RegsRegister( TEST_REG_NAME, TEST_REG_OFFSET, TEST_REG_WIDTH, TEST_REG_DESCR, TEST_REG_REV, TEST_REG_ACCESS, ) with pytest.raises(SPSDKRegsErrorRegisterNotFound): regs.find_reg("NonExisting") # The Registers MUST return empty array assert regs.get_reg_names() == [] with pytest.raises(SPSDKError): regs.remove_register("String") with pytest.raises(ValueError): regs.remove_register(reg1) # Now we could do tests with a register added to list regs.add_register(reg1) regs.remove_register_by_name(["String"]) assert TEST_REG_NAME in regs.get_reg_names() regt = regs.find_reg(TEST_REG_NAME) assert regt == reg1 with pytest.raises(SPSDKError): regs.add_register("Invalid Parameter") regt.set_value(TEST_REG_VALUE) assert reg1.get_value() == TEST_REG_VALUE filename = os.path.join(tmpdir, TEST_XML_FILE) regs.write_xml(filename) assert os.path.isfile(filename) printed_str = str(regs) assert TEST_DEVICE_NAME in printed_str assert TEST_REG_NAME in printed_str regs.remove_register_by_name([TEST_REG_NAME]) with pytest.raises(SPSDKRegsErrorRegisterNotFound): regs.find_reg(TEST_REG_NAME) assert False
def create_simple_regs(): """Create siple reg structure with basic cases.""" regs = Registers(TEST_DEVICE_NAME) reg1 = RegsRegister( TEST_REG_NAME, TEST_REG_OFFSET, TEST_REG_WIDTH, TEST_REG_DESCR, TEST_REG_REV, TEST_REG_ACCESS, ) reg2 = RegsRegister( TEST_REG_NAME + "_2", TEST_REG_OFFSET + 4, TEST_REG_WIDTH, TEST_REG_DESCR + "_2", TEST_REG_REV, TEST_REG_ACCESS, ) bitfield1 = RegsBitField( reg2, TEST_BITFIELD_NAME, TEST_BITFIELD_OFFSET, TEST_BITFIELD_WIDTH, TEST_BITFIELD_DESCR, TEST_BITFIELD_RESET_VAL, TEST_BITFIELD_ACCESS, ) bitfield2 = RegsBitField( reg2, TEST_BITFIELD_NAME + "_2", TEST_BITFIELD_OFFSET + TEST_BITFIELD_WIDTH, 1, ".", 0, TEST_BITFIELD_ACCESS, ) enum1 = RegsEnum(TEST_ENUM_NAME, 0, TEST_ENUM_DESCR, 1) enum2 = RegsEnum(TEST_ENUM_NAME + "_2", 0, TEST_ENUM_DESCR + "_2", 1) bitfield2.add_enum(enum1) bitfield2.add_enum(enum2) reg2.add_bitfield(bitfield1) reg2.add_bitfield(bitfield2) regs.add_register(reg1) regs.add_register(reg2) return regs
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)
def test_register_duplicate(): """Test registers add duplicate.""" reg = RegsRegister( TEST_REG_NAME, TEST_REG_OFFSET, TEST_REG_WIDTH, TEST_REG_DESCR, TEST_REG_REV, TEST_REG_ACCESS, ) reg1 = RegsRegister( TEST_REG_NAME, TEST_REG_OFFSET, TEST_REG_WIDTH, TEST_REG_DESCR, TEST_REG_REV, TEST_REG_ACCESS, ) regs = Registers(TEST_DEVICE_NAME) regs.add_register(reg) with pytest.raises(SPSDKRegsError): regs.add_register(reg1)