def test_add_same(self): """ Try to add two objects with the same ID. """ # Instantiate a Leonard. leo = getLeonard() # Convenience. id_1 = '1' # The number of bodies in Leonard must be zero. assert len(leo.allBodies) == 0 # Create three bodies. body_1 = getRigidBody(imass=1) body_2 = getRigidBody(imass=2) body_3 = getRigidBody(imass=3) # The command queue for spawning objects must be empty. ret = leoAPI.dequeueCommands() assert ret.ok and (ret.data['spawn'] == []) # Spawn the first object, then attempt to spawn another with the same # objID *before* Leonard gets around to add even the first one --> this # must fail and not add anything. assert leoAPI.addCmdSpawn([(id_1, body_1)]).ok assert not leoAPI.addCmdSpawn([(id_1, body_2)]).ok ret = leoAPI.dequeueCommands() spawn = ret.data['spawn'] assert ret.ok and (len(spawn) == 1) and (spawn[0]['objID'] == id_1) # Similar test as before, but this time Leonard has already pulled id_1 # into the simulation *before* we (attempt to) spawn another object # with the same ID. The 'addSpawnCmd' must succeed because it cannot # reliably verify if Leonard has an object id_1 (it can only verify if # another such request is in the queue already -- see above). However, # Leonard itself must ignore that request. To verify this claim we will # now spawn a new object with the same id_1 but a different state data, # let Leonard process the queue, and then verify that it did not # add/modify the object with id_1. assert leoAPI.addCmdSpawn([(id_1, body_1)]).ok leo.processCommandsAndSync() assert leo.allBodies[id_1] == body_1 # Spawn anoter object with id_1 but different state data and verify # that Leonard did not modify the original body. assert leoAPI.addCmdSpawn([(id_1, body_3)]).ok leo.processCommandsAndSync() assert leo.allBodies[id_1] == body_1
def test_add_same(self): """ Try to add two objects with the same ID. """ # Instantiate a Leonard. leo = getLeonard() # Convenience. id_1 = 1 # The number of bodies in Leonard must be zero. assert len(leo.allBodies) == 0 # Create three bodies. body_1 = getRigidBody(imass=1) body_2 = getRigidBody(imass=2) body_3 = getRigidBody(imass=3) # The command queue for spawning objects must be empty. ret = leoAPI.dequeueCommands() assert ret.ok and (ret.data['spawn'] == []) # Spawn the first object, then attempt to spawn another with the same # objID *before* Leonard gets around to add even the first one --> this # must fail and not add anything. assert leoAPI.addCmdSpawn([(id_1, body_1)]).ok assert not leoAPI.addCmdSpawn([(id_1, body_2)]).ok ret = leoAPI.dequeueCommands() spawn = ret.data['spawn'] assert ret.ok and (len(spawn) == 1) and (spawn[0]['objID'] == id_1) # Similar test as before, but this time Leonard has already pulled id_1 # into the simulation *before* we (attempt to) spawn another object # with the same ID. The 'addSpawnCmd' must succeed because it cannot # reliably verify if Leonard has an object id_1 (it can only verify if # another such request is in the queue already -- see above). However, # Leonard itself must ignore that request. To verify this claim we will # now spawn a new object with the same id_1 but a different state data, # let Leonard process the queue, and then verify that it did not # add/modify the object with id_1. assert leoAPI.addCmdSpawn([(id_1, body_1)]).ok leo.processCommandsAndSync() assert leo.allBodies[id_1] == body_1 # Spawn anoter object with id_1 but different state data and verify # that Leonard did not modify the original body. assert leoAPI.addCmdSpawn([(id_1, body_3)]).ok leo.processCommandsAndSync() assert leo.allBodies[id_1] == body_1
def test_commandQueue(self): """ Add-, query, and remove commands from the command queue. """ # Convenience. body_1 = getRigidBody() body_2 = {'imass': 2, 'scale': 3} id_1, id_2 = '0', '1' # The command queue must be empty for every category. ret = leoAPI.dequeueCommands() assert ret.ok assert ret.data['spawn'] == [] assert ret.data['remove'] == [] assert ret.data['modify'] == [] assert ret.data['direct_force'] == [] assert ret.data['booster_force'] == [] # Spawn two objects with id_1 and id_2. tmp = [(id_1, body_1), (id_2, body_1)] assert leoAPI.addCmdSpawn(tmp).ok # Verify that the spawn commands were added. ret = leoAPI.dequeueCommands() assert ret.ok spawn = ret.data['spawn'] assert {spawn[0]['objID'], spawn[1]['objID']} == {id_1, id_2} assert ret.data['remove'] == [] assert ret.data['modify'] == [] assert ret.data['direct_force'] == [] assert ret.data['booster_force'] == [] # De-queuing the commands once more must not return any results because # they have already been removed. ret = leoAPI.dequeueCommands() assert ret.ok assert ret.data['spawn'] == [] assert ret.data['remove'] == [] assert ret.data['modify'] == [] assert ret.data['direct_force'] == [] assert ret.data['booster_force'] == [] # Modify state variable for body with id_1. newSV = {'imass': 10, 'position': [3, 4, 5]} assert leoAPI.addCmdModifyBodyState(id_1, newSV).ok ret = leoAPI.dequeueCommands() modify = ret.data['modify'] assert ret.ok and len(modify) == 1 assert modify[0]['objID'] == id_1 assert modify[0]['rbs'] == newSV del newSV # Set the direct force and torque for id_2. force, torque = [1, 2, 3], [4, 5, 6] assert leoAPI.addCmdDirectForce(id_2, force, torque).ok ret = leoAPI.dequeueCommands() fat = ret.data['direct_force'] assert ret.ok assert len(fat) == 1 assert fat[0]['objID'] == id_2 assert fat[0]['force'] == force assert fat[0]['torque'] == torque # Set the booster force and torque for id_1. force, torque = [1, 2, 3], [4, 5, 6] assert leoAPI.addCmdBoosterForce(id_1, force, torque).ok ret = leoAPI.dequeueCommands() fat = ret.data['booster_force'] assert ret.ok assert len(fat) == 1 assert fat[0]['objID'] == id_1 assert fat[0]['force'] == force assert fat[0]['torque'] == torque # Remove an object. assert leoAPI.addCmdRemoveObject(id_1).ok ret = leoAPI.dequeueCommands() assert ret.ok and ret.data['remove'][0]['objID'] == id_1 # Add commands for two objects (it is perfectly ok to add commands for # non-existing body IDs since this is just a command queue - Leonard # will skip commands for non-existing IDs automatically). force, torque = [7, 8, 9], [10, 11.5, 12.5] for objID in (id_1, id_2): assert leoAPI.addCmdSpawn([(objID, body_1)]).ok assert leoAPI.addCmdModifyBodyState(objID, body_2).ok assert leoAPI.addCmdRemoveObject(objID).ok assert leoAPI.addCmdDirectForce(objID, force, torque).ok assert leoAPI.addCmdBoosterForce(objID, force, torque).ok # De-queue all commands. ret = leoAPI.dequeueCommands() assert ret.ok assert len(ret.data['spawn']) == 2 assert len(ret.data['remove']) == 2 assert len(ret.data['modify']) == 2 assert len(ret.data['direct_force']) == 2 assert len(ret.data['booster_force']) == 2
def test_commandQueue(self): """ Add-, query, and remove commands from the command queue. """ # Convenience. body_1 = getRigidBody() body_2 = {'imass': 2, 'scale': 3} id_1, id_2 = 0, 1 # The command queue must be empty for every category. ret = leoAPI.dequeueCommands() assert ret.ok assert ret.data['spawn'] == [] assert ret.data['remove'] == [] assert ret.data['modify'] == [] assert ret.data['direct_force'] == [] assert ret.data['booster_force'] == [] # Spawn two objects with id_1 and id_2. tmp = [(id_1, body_1), (id_2, body_1)] assert leoAPI.addCmdSpawn(tmp).ok # Verify that the spawn commands were added. ret = leoAPI.dequeueCommands() assert ret.ok assert ret.data['spawn'][0]['objID'] == id_1 assert ret.data['spawn'][1]['objID'] == id_2 assert ret.data['remove'] == [] assert ret.data['modify'] == [] assert ret.data['direct_force'] == [] assert ret.data['booster_force'] == [] # De-queuing the commands once more must not return any results because # they have already been removed. ret = leoAPI.dequeueCommands() assert ret.ok assert ret.data['spawn'] == [] assert ret.data['remove'] == [] assert ret.data['modify'] == [] assert ret.data['direct_force'] == [] assert ret.data['booster_force'] == [] # Modify state variable for body with id_1. newSV = {'imass': 10, 'position': [3, 4, 5]} assert leoAPI.addCmdModifyBodyState(id_1, newSV).ok ret = leoAPI.dequeueCommands() modify = ret.data['modify'] assert ret.ok and len(modify) == 1 assert modify[0]['objID'] == id_1 assert modify[0]['rbs'] == newSV del newSV # Set the direct force and torque for id_2. force, torque = [1, 2, 3], [4, 5, 6] assert leoAPI.addCmdDirectForce(id_2, force, torque).ok ret = leoAPI.dequeueCommands() fat = ret.data['direct_force'] assert ret.ok assert len(fat) == 1 assert fat[0]['objID'] == id_2 assert fat[0]['force'] == force assert fat[0]['torque'] == torque # Set the booster force and torque for id_1. force, torque = [1, 2, 3], [4, 5, 6] assert leoAPI.addCmdBoosterForce(id_1, force, torque).ok ret = leoAPI.dequeueCommands() fat = ret.data['booster_force'] assert ret.ok assert len(fat) == 1 assert fat[0]['objID'] == id_1 assert fat[0]['force'] == force assert fat[0]['torque'] == torque # Remove an object. assert leoAPI.addCmdRemoveObject(id_1).ok ret = leoAPI.dequeueCommands() assert ret.ok and ret.data['remove'][0]['objID'] == id_1 # Add commands for two objects (it is perfectly ok to add commands for # non-existing body IDs since this is just a command queue - Leonard # will skip commands for non-existing IDs automatically). force, torque = [7, 8, 9], [10, 11.5, 12.5] for objID in (id_1, id_2): assert leoAPI.addCmdSpawn([(objID, body_1)]).ok assert leoAPI.addCmdModifyBodyState(objID, body_2).ok assert leoAPI.addCmdRemoveObject(objID).ok assert leoAPI.addCmdDirectForce(objID, force, torque).ok assert leoAPI.addCmdBoosterForce(objID, force, torque).ok # De-queue all commands. ret = leoAPI.dequeueCommands() assert ret.ok assert len(ret.data['spawn']) == 2 assert len(ret.data['remove']) == 2 assert len(ret.data['modify']) == 2 assert len(ret.data['direct_force']) == 2 assert len(ret.data['booster_force']) == 2