def test_sim_many_packets(mbed): """Tests that sending many packets, each 10ms apart, gives a duration""" # Ensure we are triggered mbed.trigger() # Construct packets pkts = [] pkts += [spinn_2_to_7(DVSPacket(10, 20, 1), SpiNNMode.SPINN_MODE_128)] pkts += [spinn_2_to_7(DVSPacket(43, 19, 1), SpiNNMode.SPINN_MODE_128)] pkts += [spinn_2_to_7(DVSPacket(131, 0, 1), SpiNNMode.SPINN_MODE_128)] pkts += [spinn_2_to_7(DVSPacket(85, 67, 1), SpiNNMode.SPINN_MODE_128)] pkts += [spinn_2_to_7(DVSPacket(127, 127, 1), SpiNNMode.SPINN_MODE_128)] for pkt in pkts: mbed.send_sim(pkt) time.sleep(0.01) # Retrieve all packets and check their contents and total duration (duration, _, rx_pkts) = mbed.get_spinn() assert duration # Check duration is +/- 40ms assert (len(pkts) - 4) * 10000 <= duration <= (len(pkts) + 4) * 10000 assert rx_pkts for true_pkt, rx_pkt in zip(pkts, rx_pkts): assert true_pkt.data == rx_pkt.data
def test_single_packet(mbed, board, log): """Test that DVS packet sent to board is sent to MBED correctly""" # Trigger the MBED to ensure state is correct mbed.trigger() # Ensure board is in correct mode board_assert_equal(board.set_mode_spinn(SpiNNMode.SPINN_MODE_128.value), RESPONSES["success"]) # Send a DVS packet to the board dvs_pkt = DVSPacket(10, 30, 1) board_assert_equal(board.use_dvs(dvs_pkt), RESPONSES["success"]) pkt = spinn_2_to_7(dvs_pkt, SpiNNMode.SPINN_MODE_128) # Give a brief period of time to forward to the MBED time.sleep(0.1) # Retrieve packets from MBED (_, _, rx_pkt) = mbed.get_spinn() # Check results are identical assert rx_pkt for rxp in rx_pkt: board_assert_isinstance(rxp, SpiNNPacket) log.info("Got packet data: {}".format([hex(x) for x in rx_pkt[0].data])) log.info("Calculated data: {}".format([hex(x) for x in pkt.data])) board_assert_equal(pkt.data, rx_pkt[0].data)
def test_spinn_nocrash(board): """Test that sending a DVS packet with no forwarding does not crash board""" pkt = DVSPacket(10, 30, 1) # Send packet board_assert_equal(board.use_dvs(pkt), RESPONSES["success"]) # Echo to check that the board replies board_assert_equal(board.echo("test"), "test")
def test_trigger(mbed): """Tests that triggering the MBED allows values to be captured""" mbed.trigger() # Send a simulated packet mbed.send_sim(spinn_2_to_7(DVSPacket(10, 20, 1), SpiNNMode.SPINN_MODE_128)) # Check that packet is returned (_, _, packet) = mbed.get_spinn() assert packet
def test_wait(mbed): """Tests that telling the MBED to wait doesn't log values""" mbed.wait() # Send a simulated packet mbed.send_sim(spinn_2_to_7(DVSPacket(10, 20, 1), SpiNNMode.SPINN_MODE_128)) # Check that nothing is recorded (_, _, packets) = mbed.get_spinn() assert not packets
def test_spinn_fwd_received(board): """Tests that sending a DVS packet and receiving some data works""" # Turn on forwarding to check that any packet comes back board_assert_equal(board.forward_spinn(0), RESPONSES["success"]) # Send a test packet test_pkt = DVSPacket(10, 30, 1) board_assert_equal(board.use_dvs(test_pkt), RESPONSES["success"]) pkt = board.get_spinn() board_assert_isinstance(pkt, SpiNNPacket)
def test_wait_trigger(mbed): """Tests that triggering after a wait allows capture""" # Initial wait command mbed.wait() # Send a simulated packet mbed.send_sim(spinn_2_to_7(DVSPacket(10, 20, 1), SpiNNMode.SPINN_MODE_128)) # Check that nothing is recorded (_, _, packets) = mbed.get_spinn() assert not packets # Trigger recording again mbed.trigger() # Send a simulated packet mbed.send_sim(spinn_2_to_7(DVSPacket(10, 20, 1), SpiNNMode.SPINN_MODE_128)) # Check that packet is returned (_, _, packet) = mbed.get_spinn() assert packet
def test_dvs_use_pkt(board): """Tests that sending simulated packet is sent back""" # Turn on forwarding to check that any packet comes back board_assert_equal(board.forward_dvs(0), RESPONSES["success"]) # Send a test packet test_pkt = DVSPacket(10, 30, 1) board_assert_equal(board.use_dvs(test_pkt), RESPONSES["success"]) pkt = board.get_dvs() board_assert_isinstance(pkt, DVSPacket) board_assert_equal(pkt.x, test_pkt.x) board_assert_equal(pkt.y, test_pkt.y) board_assert_equal(pkt.pol, test_pkt.pol)
def get_dvs(self): """Attempts to retrieve a DVS packet if it exists""" if self.ser is None: self.log.error("No serial device connected!") return "" self.log.info("Attempting to read 3 bytes for 1 packet") # Retrieve packet or time out pkt = self._read() if len(pkt) >= 3: return DVSPacket(ord(pkt[0]), ord(pkt[1]), ord(pkt[2])) else: return None
def test_read_clears(mbed): """Tests that reading a packet from the MBED then resetting works""" # Ensure MBED is empty _ = mbed.get_spinn() # Send a simulated packet mbed.send_sim(spinn_2_to_7(DVSPacket(10, 20, 1), SpiNNMode.SPINN_MODE_128)) # Check that there is a returned value (_, _, packets) = mbed.get_spinn() assert packets # Read again and check that there is nothing (_, _, packets) = mbed.get_spinn() assert not packets
def test_many_packets(mbed, board, log, packets): """Tests the time taken for many packets to be sent is small""" # Clear the MBED and tell it to wait mbed.get_spinn() mbed.wait() # Work out x limits for 20 packets x_min = 12 x_step = 6 x_max = 100 + (packets) * x_step y_max = 90 y_step = 3 y_min = y_max - (packets) * y_step # Form list of test packets and expected results dvs_data = [ DVSPacket(x, y, y % 2) for x, y in zip(range(x_min, x_max, x_step), range(y_max, y_min, -y_step)) ] exp_data = [spinn_2_to_7(x, SpiNNMode.SPINN_MODE_128) for x in dvs_data] for dvs_pkt in dvs_data: board_assert_equal(board.use_dvs(dvs_pkt), RESPONSES["success"]) # Trigger the MBED to start the speed test mbed.trigger() # Give period of time to forward to the MBED time.sleep(packets * 0.05) # Retrieve packets (duration, count, rx_data) = mbed.get_spinn() # Check results are identical assert rx_data assert len(rx_data) == len(exp_data) assert len(rx_data) == count for exp, rxp in zip(rx_data, exp_data): assert exp.data == rxp.data # Check duration is reasonable - packets times 15ms assert duration < len(exp_data) * 15000 log.info("Test for %d packets took %lfs with %lfs per packet" % (len(exp_data), duration / 1000000.0, duration / (1000000.0 * packets)))
def test_res_128(board): """Test that resolution 128x128 is unchanged""" board_assert_equal(board.set_mode_spinn(SpiNNMode.SPINN_MODE_128.value), RESPONSES["success"]) # Forward all packets to PC board_assert_equal(board.forward_dvs(0), RESPONSES["success"]) # Send several 128x128 DVS Packets for param in range(10, 120, 20): pkt = DVSPacket(param, param, param % 2) board_assert_equal(board.use_dvs(pkt), RESPONSES["success"]) rx_pkt = board.get_dvs() board_assert_isinstance(rx_pkt, DVSPacket) board_assert_equal(rx_pkt.x, pkt.x) board_assert_equal(rx_pkt.y, pkt.y) board_assert_equal(rx_pkt.pol, pkt.pol)
def test_sim_spinn(mbed, log): """Tests that a single SpiNNaker packet is sent back correctly""" # Ensure we are triggered mbed.trigger() # Construct packet pkt = spinn_2_to_7(DVSPacket(10, 20, 1), SpiNNMode.SPINN_MODE_128) # Send simulated packet mbed.send_sim(pkt) # Retrieve packet and check (_, _, rx_pkt) = mbed.get_spinn() assert rx_pkt log.info("Got packet %s", rx_pkt[0].data) log.info("Expected %s", pkt.data) assert rx_pkt[0].data == pkt.data
def test_sim_duration(mbed): """Tests that sending two packets roughly 100ms apart gives a duration""" # Ensure we are triggered mbed.trigger() # Construct packet pkt = spinn_2_to_7(DVSPacket(10, 20, 1), SpiNNMode.SPINN_MODE_128) # Send simulated packet twice mbed.send_sim(pkt) time.sleep(0.1) mbed.send_sim(pkt) # Retrieve packet and check (duration, _, _) = mbed.get_spinn() assert duration # Check duration is +/- 40ms assert 60000 <= duration <= 140000
def dvs_offset(pkt_list, x, y, _mod): base_x = x - (x % _mod) base_y = y - (y % _mod) return [ DVSPacket(pkt.x + base_x, pkt.y + base_y, pkt.pol) for pkt in pkt_list ]
"""Module to test that the downscaling of the DVS is correct """ import pytest from common import board_assert_equal, board_assert_isinstance, SpiNNMode from fixtures import board from dvs_packet import DVSPacket from controller import RESPONSES # Generate the test arrays for 64x64, 32x32, 16x16 TOO_FEW_64 = [DVSPacket(0, 0, 1)] JUST_ENOUGH_64 = [DVSPacket(0, 0, 1), DVSPacket(1, 1, 1)] EQUAL_64 = [ DVSPacket(0, 0, 1), DVSPacket(1, 1, 1), DVSPacket(0, 1, 0), DVSPacket(1, 0, 0) ] MANY_NEG_64 = [DVSPacket(0, 1, 0), DVSPacket(1, 1, 0), DVSPacket(1, 0, 0)] JUST_ENOUGH_32 = [ DVSPacket(0, 0, 1), DVSPacket(3, 0, 1), DVSPacket(1, 1, 1), DVSPacket(2, 1, 1), DVSPacket(1, 2, 1), DVSPacket(2, 2, 1), DVSPacket(3, 0, 1), DVSPacket(3, 3, 1) ]
"""Module to unit test common methods""" import pytest from common import spinn_2_to_7, SpiNNMode from dvs_packet import DVSPacket @pytest.mark.parametrize("dvs_pkt,mode,result", [ (DVSPacket(10, 30, 1), SpiNNMode.SPINN_MODE_128, [0x11, 0x11, 0x44, 0x11, 0x09, 0x03, 0x11, 0x11, 0x14, 0x11, 0x60]), (DVSPacket(10, 30, 1), SpiNNMode.SPINN_MODE_64, [0x11, 0x11, 0x22, 0x03, 0x18, 0x03, 0x11, 0x11, 0x14, 0x11, 0x60]), (DVSPacket(10, 30, 1), SpiNNMode.SPINN_MODE_32, [0x11, 0x11, 0x14, 0x0C, 0x11, 0x03, 0x11, 0x11, 0x14, 0x11, 0x60]), (DVSPacket(10, 30, 1), SpiNNMode.SPINN_MODE_16, [0x12, 0x11, 0x12, 0x18, 0x11, 0x03, 0x11, 0x11, 0x14, 0x11, 0x60]), (DVSPacket(54, 99, 0), SpiNNMode.SPINN_MODE_128, [0x12, 0x11, 0x24, 0x48, 0x12, 0x48, 0x11, 0x11, 0x14, 0x11, 0x60]), (DVSPacket(54, 99, 0), SpiNNMode.SPINN_MODE_64, [0x11, 0x11, 0x48, 0x22, 0x03, 0x41, 0x11, 0x11, 0x14, 0x11, 0x60]), (DVSPacket(54, 99, 0), SpiNNMode.SPINN_MODE_32, [0x11, 0x11, 0x06, 0x11, 0x18, 0x41, 0x11, 0x11, 0x14, 0x11, 0x60]), (DVSPacket(54, 99, 0), SpiNNMode.SPINN_MODE_16, [0x12, 0x11, 0x24, 0x03, 0x11, 0x41, 0x11, 0x11, 0x14, 0x11, 0x60]), ]) def test_encode_dvs(dvs_pkt, mode, result): """Tests that encoding a DVS packet matches the hand encoded version""" assert spinn_2_to_7(dvs_pkt, mode).data == result
@pytest.mark.parametrize("mode", range(4)) def test_spinn_set_mode_correct(board, mode): board_assert_equal(board.set_mode_spinn(mode), RESPONSES["success"]) @pytest.mark.parametrize("mode", [255, 4, 5]) def test_spinn_set_mode_incorrect(board, mode): board_assert_equal(board.set_mode_spinn(mode), RESPONSES["bad_param"]) @pytest.mark.parametrize( "dvs_pkt", [ DVSPacket(10, 20, 1), # Normal data DVSPacket(10, 20, 0), # Another polarity DVSPacket(0, 0, 0), # Min values DVSPacket(127, 127, 1), # Max values ]) def test_spinn_encode(board, dvs_pkt, log): """Test that board encodes a packet the same as the test system for mode 128""" # Turn on forwarding to check that any packet comes back board_assert_equal(board.forward_spinn(0), RESPONSES["success"]) # Set mode to the mode under test board_assert_equal(board.set_mode_spinn(SpiNNMode.SPINN_MODE_128.value), RESPONSES["success"]) # For each packet, test all modes