def test_PortAccess(self): # create and initialize a test port Port = CTestPort() MyRegister = 42 Port.CreateEntry(0x00ff, "uint32_t", MyRegister, RW, LittleEndian) """[ GenApiTest@PortTestSuite_TestPortAccess.xml|gxml <Port Name="MyPort" > </Port> """ # create and connect a camera object Camera = CNodeMapRef() Camera._LoadXMLFromFile("GenApiTest", "PortTestSuite_TestPortAccess") Camera._Connect(Port, "MyPort") port = Camera._GetNode("MyPort") # read back value Buffer = port.Read(0x00ff, sizeof("uint32_t")) self.assertEqual(MyRegister, cast_buffer("uint32_t", LittleEndian, Buffer)) # write a new value MyNewRegister = 99 port.Write(0x00ff, cast_data("uint32_t", LittleEndian, MyNewRegister)) Buffer = port.Read(0x00ff, sizeof("uint32_t")) self.assertEqual(MyNewRegister, cast_buffer("uint32_t", LittleEndian, Buffer))
def test_PortRef(self): # create and initialize a test port Port = CTestPort() MyRegister = 0 Port.CreateEntry(0x00ff, "uint32_t", MyRegister, RW, LittleEndian) """[ GenApiTest@PortTestSuite_TestPortRef.xml|gxml <Port Name="MyPort" > </Port> <IntReg Name="TestReg"> <Address>0x00ff</Address> <Length>1</Length> <AccessMode>RW</AccessMode> <pPort>MyPort</pPort> <Sign>Unsigned</Sign> <Endianess>BigEndian</Endianess> </IntReg> """ # create and connect a camera object Camera = CNodeMapRef() Camera._LoadXMLFromFile("GenApiTest", "PortTestSuite_TestPortRef") Camera._Connect(Port, "MyPort") # Create a pointer to MyPort, a pointer to the IntReg and a CPortRef object referencing MyPort port = Camera._GetNode("MyPort") testReg = Camera._GetNode("TestReg") # Write and read back using CPortRef WriteBuffer = 3 port.Write(0x00ff, cast_data("uint32_t", LittleEndian, WriteBuffer)) ReadBuffer = port.Read(0x00ff, sizeof("uint32_t")) self.assertTrue( cast_buffer("uint32_t", LittleEndian, ReadBuffer) == WriteBuffer) # Write and read back using IntReg testReg.SetValue(25) self.assertTrue(testReg.GetValue() == 25) # Write using pointer to IntReg, read back through CPortRef testReg.SetValue(42) ReadBuffer = port.Read(0x00ff, sizeof("uint32_t")) self.assertTrue( cast_buffer("uint32_t", LittleEndian, ReadBuffer) == 42) # Write through CPortRef, read back through pointer to IntReg # The values should only match once the IntReg cache has been invalidated. WriteBuffer = 31 port.Write(0x00ff, cast_data("uint32_t", LittleEndian, WriteBuffer)) self.assertTrue(testReg.GetValue() != WriteBuffer) # Invalidate the port's node cache and test for correct read back Port.InvalidateNode() self.assertTrue(testReg.GetValue() == WriteBuffer) # Make sure the failed write didn't modify the value of the node. self.assertTrue(testReg.GetValue() == WriteBuffer)
def test_Command01(self): """[ GenApiTest@CommandTestSuite_TestCommand01.xml|gxml <Category Name="Root"> <pFeature>command</pFeature> </Category> <Command Name="command"> <pValue>myIntReg</pValue> <CommandValue>0x4321</CommandValue> </Command> <IntReg Name="myIntReg"> <Address>0x00ff</Address> <Length>4</Length> <AccessMode>RW</AccessMode> <pPort>MyPort</pPort> <Sign>Unsigned</Sign> <Endianess>BigEndian</Endianess> </IntReg> <Port Name="MyPort"/> """ Camera = CNodeMapRef() Camera._LoadXMLFromFile("GenApiTest", "CommandTestSuite_TestCommand01") # create and initialize a test port Port = CTestPort() Port.CreateEntry(0x00ff, "uint32_t", 42, RW, BigEndian) # connect the node map to the port Camera._Connect(Port, "MyPort") # # Test the ICommand interface # # retrieve the node and convert it to ICommand Node = Camera.GetNode("command") Cmd = Node self.assertTrue(bool(Node)) self.assertTrue(bool(Cmd)) self.assertEqual(intfICommand, Cmd.GetNode().GetPrincipalInterfaceType()) ##### getproperty of command pointer - try: ValueStr, AttributeStr = Cmd.GetNode().GetProperty("CommandValue") except: pass # execute function Cmd.Execute() # is executing? self.assertEqual(False, Cmd.IsDone()) # manipulate the command register in order to simulate a self-resetting toggle # CLittleEndian<int32_t> tmp tmp_data = Port.Read(0x00ff, sizeof("int32_t")) self.assertEqual(0x21430000, cast_buffer("int32_t", LittleEndian, tmp_data)) tmp = 0 Port.Write(0x00ff, cast_data("int32_t", LittleEndian, tmp)) self.assertEqual(True, Cmd.IsDone()) # once again with verification Cmd.Execute() self.assertEqual(False, Cmd.IsDone(True)) Port.Write(0x00ff, cast_data("int32_t", LittleEndian, tmp)) self.assertEqual(True, Cmd.IsDone(True)) # happy path (what to test on execution without verification?) Cmd.Execute(False) # execute function Cmd() # is executing? self.assertEqual(False, Cmd.IsDone()) # manipulate the command register in order to simulate a self-resetting toggle tmp_data = Port.Read(0x00ff, sizeof("int32_t")) self.assertEqual(0x21430000, cast_buffer("int32_t", LittleEndian, tmp_data)) tmp = 0 Port.Write(0x00ff, cast_data("int32_t", LittleEndian, tmp)) self.assertEqual(True, Cmd.IsDone()) # TestAccessMode self.assertEqual(RW, Cmd.GetAccessMode()) # # Test the IValue interface # Value = Cmd strExec = "0" # function is done => value is "0" self.assertEqual(strExec, Value.ToString()) # execute using a string strExec = "1" Value.FromString(strExec) # function is executing => value is "1" self.assertEqual(strExec, Value.ToString()) self.assertTrue(None != Value.GetNode()) # try to use an illegal string with self.assertRaises(InvalidArgumentException): Value.FromString("foo") Value.FromString("true")
def test_TestPort(self): # some basics # create and initialize a test port Port = CTestPort() MyRegister = 42 Port.CreateEntry(0x0000, "uint32_t", 42, RW, LittleEndian) Port.CreateEntry(0x000f, "uint32_t", MyRegister, RW, LittleEndian) MyRegisterLittleEndian = MyRegister Port.CreateEntry(0x00ff, "uint32_t", MyRegisterLittleEndian, RW, LittleEndian) # Test IPort::Read method Port = CTestPort() # write something to the register map MyRegister = 42 Port.CreateEntry(0x00ff, "uint32_t", MyRegister, RO, LittleEndian) # check if it's there Buffer = Port.Read(0x00ff, sizeof("uint32_t")) self.assertEqual(MyRegister, cast_buffer("uint32_t", LittleEndian, Buffer)) # try to read from an invalid address with self.assertRaises(GenericException): Port.Read(0x0000, sizeof("uint32_t")) # try to read with invalid length with self.assertRaises(GenericException): Port.Read(0x00ff, 8) # Create an WO entry MyRegister = 43 Port.CreateEntry(0x000f, "uint32_t", MyRegister, WO, LittleEndian) # try to read from a WO address with self.assertRaises(GenericException): Port.Read(0x000f, sizeof("uint32_t")) # Test IPort::Write method Port = CTestPort() # write something to the register map MyRegister = 0 Port.CreateEntry(0x00ff, "uint32_t", MyRegister, RW, LittleEndian) # write at the location MyRegister = 42 Port.Write(0x00ff, cast_data("uint32_t", LittleEndian, MyRegister)) # read back Buffer = Port.Read(0x00ff, sizeof("uint32_t")) self.assertEqual(MyRegister, cast_buffer("uint32_t", LittleEndian, Buffer)) # try to write at an invalid address self.assertRaises( GenericException, Port.Write(0x0000, cast_data("uint32_t", LittleEndian, MyRegister))) # try to write with invalid length with self.assertRaises(GenericException): Port.Write(0x00ff, bytes(8)) # make the entry RO MyRegister = 0 Port.CreateEntry(0x000f, "uint32_t", MyRegister, RO, LittleEndian) # try to write to a RO address with self.assertRaises(GenericException): Port.Write(0x000f, cast_data("uint32_t", LittleEndian, MyRegister))