Esempio n. 1
0
    def test_fields(self):
        def test_field(pe: Entity, field: str, val):
            pe.proto.Clear()
            setattr(pe, field, val)
            self.assertEqual(getattr(pe.proto, field), val)

        pe = Entity(protos.Entity())
        test_field(pe, 'name', 'test')
        test_field(pe, 'x', 5)
        test_field(pe, 'y', 5)
        test_field(pe, 'vx', 5)
        test_field(pe, 'vy', 5)
        test_field(pe, 'r', 5)
        test_field(pe, 'mass', 5)
        test_field(pe, 'heading', 5)
        test_field(pe, 'spin', 5)
        test_field(pe, 'fuel', 5)
        test_field(pe, 'throttle', 5)
        test_field(pe, 'landed_on', 'other_test')
        test_field(pe, 'broken', True)
        test_field(pe, 'artificial', True)
Esempio n. 2
0
class PhysicsStateTestCase(unittest.TestCase):
    """Tests state.PhysicsState accessors and setters."""

    proto_state = protos.PhysicalState(
        timestamp=5,
        entities=[
            protos.Entity(name='First',
                          mass=100,
                          r=200,
                          x=10,
                          y=20,
                          vx=30,
                          vy=40,
                          heading=7,
                          spin=50,
                          fuel=60,
                          throttle=70),
            protos.Entity(name='Second',
                          mass=101,
                          r=201,
                          artificial=True,
                          x=11,
                          y=21,
                          vx=31,
                          vy=41,
                          heading=2,
                          spin=51,
                          fuel=61,
                          throttle=71,
                          landed_on='First',
                          broken=True)
        ],
        engineering=protos.EngineeringState(
            components=[protos.EngineeringState.Component()] * _N_COMPONENTS,
            coolant_loops=[protos.EngineeringState.CoolantLoop()] *
            _N_COOLANT_LOOPS,
            radiators=[protos.EngineeringState.Radiator()] * _N_RADIATORS))

    def test_landed_on(self):
        """Test that the special .landed_on field is properly set."""
        ps = PhysicsState(None, self.proto_state)
        self.assertEqual(ps['First'].landed_on, '')
        self.assertEqual(ps['Second'].landed_on, 'First')

    def test_y_vector_init(self):
        """Test that initializing with a y-vector uses y-vector values."""
        y0 = np.concatenate((
            np.array([
                10,
                20,  # x
                30,
                40,  # y
                50,
                60,  # vx
                0,
                0,  # vy
                0,
                0,  # heading
                70,
                80,  # spin
                90,
                100,  # fuel
                0,
                0,  # throttle
                1,
                -1,  # only First is landed on Second
                0,
                1,  # Second is broken
                common.SRB_EMPTY,
                1  # time_acc
            ]),
            np.zeros(EngineeringState.N_ENGINEERING_FIELDS)))

        ps = PhysicsState(y0, self.proto_state)
        self.assertTrue(np.array_equal(ps.y0(), y0.astype(ps.y0().dtype)))
        self.assertEqual(ps['First'].landed_on, 'Second')

        proto_state = ps.as_proto()
        proto_state.timestamp = 50
        self.assertEqual(proto_state.timestamp, 50)
        self.assertEqual(proto_state.entities[0].fuel, 90)
        self.assertTrue(proto_state.entities[1].broken)

    def test_get_set(self):
        """Test __getitem__ and __setitem__."""
        ps = PhysicsState(None, self.proto_state)
        entity = ps[0]
        entity.landed_on = 'Second'
        ps[0] = entity
        self.assertEqual(ps[0].landed_on, 'Second')

    def test_entity_view(self):
        """Test that setting and getting _EntityView attrs propagate."""
        ps = PhysicsState(None, self.proto_state)
        self.assertEqual(ps[0].name, 'First')
        entity = ps[0]
        self.assertTrue(isinstance(entity, _EntityView))

        self.assertEqual(entity.x, 10)
        self.assertEqual(entity.y, 20)
        self.assertEqual(entity.vx, 30)
        self.assertEqual(entity.vy, 40)
        self.assertEqual(entity.spin, 50)
        self.assertEqual(entity.fuel, 60)
        self.assertEqual(entity.landed_on, '')
        self.assertEqual(entity.throttle, 70)

        ps.y0()
        self.assertEqual(entity.heading, 7 % (2 * np.pi))

        ps[0].landed_on = 'Second'
        self.assertEqual(entity.landed_on, 'Second')
        entity.x = 500
        self.assertEqual(ps[0].x, 500)
        entity.pos = np.array([55, 66])
        self.assertEqual(ps['First'].x, 55)
        self.assertEqual(ps['First'].y, 66)