def test_run(auto_create_group, samples_per_group, num_vertices,
             num_nets_per_vertex, error, error_code, record, reinject_packets):
    """Make sure that the run command carries out an experiment as would be
    expected."""
    machine = Machine(3, 1)

    mock_mc = Mock()
    mock_mc.get_machine.return_value = machine

    mock_application_ctx = Mock()
    mock_application_ctx.__enter__ = Mock()
    mock_application_ctx.__exit__ = Mock()
    mock_mc.application.return_value = mock_application_ctx

    mock_mc.get_machine.return_value = machine

    if reinject_packets:
        # If reinjecting, a core is added to every chip
        mock_mc.wait_for_cores_to_reach_state.return_value = len(list(machine))
    else:
        # If not reinjecting, only the vertices are given cores
        mock_mc.wait_for_cores_to_reach_state.return_value = num_vertices

    def mock_sdram_file_read(size):
        return error_code + b"\0"*(size - 4)
    mock_sdram_file = Mock()
    mock_sdram_file.read.side_effect = mock_sdram_file_read

    mock_mc.sdram_alloc_as_filelike.return_value = mock_sdram_file

    e = Experiment(mock_mc)
    e.timestep = 1e-6
    e.warmup = 0.01
    e.duration = 0.01
    e.cooldown = 0.01
    e.flush_time = 0.01

    # Record the result of _construct_vertex_commands to allow checking of
    # memory allocation sizes
    construct_vertex_commands = e._construct_vertex_commands
    vertices_commands = {}

    def wrapped_construct_vertex_commands(vertex, *args, **kwargs):
        commands = construct_vertex_commands(vertex=vertex, *args, **kwargs)
        vertices_commands[vertex] = commands
        return commands
    e._construct_vertex_commands = Mock(
        side_effect=wrapped_construct_vertex_commands)

    if record:
        e.record_sent = True

    e.reinject_packets = reinject_packets

    # Create example vertices
    vertices = [e.new_vertex() for _ in range(num_vertices)]
    for v in vertices:
        for _ in range(num_nets_per_vertex):
            e.new_net(v, v)

    # Vertices are placed on sequential chips along the x-axis
    e.placements = {v: (x, 0) for x, v in enumerate(vertices)}

    # Create example groups
    for num_samples in samples_per_group:
        with e.new_group():
            e.record_interval = e.duration / float(num_samples)

    # The run should fail with an exception when expected.
    if error and (num_vertices > 0 or reinject_packets):
        with pytest.raises(NetworkTesterError) as exc_info:
            e.run(0x33, create_group_if_none_exist=auto_create_group)
        results = exc_info.value.results
    else:
        results = e.run(0x33, create_group_if_none_exist=auto_create_group)

    # The results should be of the correct type...
    assert isinstance(results, Results)

    # The results returned should be all zeros (since that is what was written
    # back)
    if record and num_vertices > 0 and num_nets_per_vertex > 0:
        assert sum(results.totals()["sent"]) == 0

    # The supplied app ID should be used
    mock_mc.application.assert_called_once_with(0x33)

    # If reinjection is enabled, the binary should have been loaded
    reinjector_loaded = any("reinjector.aplx" in call[1][0]
                            for call in mock_mc.load_application.mock_calls)
    if reinject_packets:
        assert reinjector_loaded
    else:
        assert not reinjector_loaded

    # Each chip should have been issued with a suitable malloc for any vertices
    # on it.
    for x, vertex in enumerate(vertices):
        cmds_size = vertices_commands[vertex].size
        if record:
            # The space required to record the sent counters.
            result_size = (1 + (num_nets_per_vertex *
                                sum(samples_per_group))) * 4
        else:
            result_size = 4  # Just the status value
        size = max(cmds_size, result_size)
        if reinject_packets:
            # During packet reinjection, core 1 is used.
            core_num = 2
        else:
            core_num = 1
        mock_mc.sdram_alloc_as_filelike.assert_any_call(size, x=x, y=0,
                                                        tag=core_num)

    # The correct number of barriers should have been reached
    num_groups = len(samples_per_group)
    if auto_create_group:
        num_groups = max(1, num_groups)
    assert len(mock_mc.send_signal.mock_calls) == num_groups
def test_construct_vertex_commands(router_access_vertex):
    # XXX: This test is *very* far from being complete. In particular, though
    # the problem supplied is realistic, the output is not checked thouroughly
    # enough.
    e = Experiment(Mock())

    # A simple example network as follows:
    #       n0
    #  v0 ---+--> v1
    #   |    '--> v2
    #   +-------> v3
    #      n1
    # We'll pretend all of them are on the same chip and vertex0 is the one
    # designated as the router-value recording vertex.

    vertex0 = e.new_vertex()
    vertex1 = e.new_vertex()
    vertex2 = e.new_vertex()
    vertex3 = e.new_vertex()

    vertices = [vertex0, vertex1, vertex2, vertex3]

    net0 = e.new_net(vertex0, [vertex1, vertex2])
    net1 = e.new_net(vertex0, vertex3)

    # Seed randomly
    e.seed = None

    e.timestep = 1e-6
    e.warmup = 0.001
    e.duration = 1.0
    e.cooldown = 0.001
    e.flush_time = 0.01

    e.record_sent = True

    e.reinject_packets = True

    # By default, nothing should send and everything should consume
    e.probability = 0.0
    e.consume = True
    e.router_timeout = 240

    # In group0, everything consumes and n0 sends 100% packets and n1 sends 50%
    # packets.
    with e.new_group():
        net0.probability = 1.0
        net1.probability = 0.5

    # In group1, nothing consumes and n0 and n1 send 100%
    with e.new_group():
        net0.probability = 1.0
        net1.probability = 1.0
        e.consume = False
        e.router_timeout = 0

    # In group2, we have a timeout with emergency routing enabled
    with e.new_group():
        e.router_timeout = (16, 16)

    net_keys = {net0: 0xAA00, net1: 0xBB00}

    vertices_source_nets = {
        vertex0: [net0, net1],
        vertex1: [],
        vertex2: [],
        vertex3: [],
    }
    vertices_sink_nets = {
        vertex0: [],
        vertex1: [net0],
        vertex2: [net0],
        vertex3: [net1],
    }

    vertex_commands = {
        vertex: e._construct_vertex_commands(
            vertex=vertex,
            source_nets=vertices_source_nets[vertex],
            sink_nets=vertices_sink_nets[vertex],
            net_keys=net_keys,
            records=[Counters.sent],
            router_access_vertex=router_access_vertex).pack()
        for vertex in vertices
    }

    # Make sure all vertices have the right number of sources/sinks set
    for vertex in vertices:
        commands = vertex_commands[vertex]
        num_sources = len(vertices_source_nets[vertex])
        num_sinks = len(vertices_sink_nets[vertex])
        ref_cmd = struct.pack("<II", NT_CMD.NUM,
                              (num_sources | num_sinks << 8))
        assert ref_cmd in commands

    # Make sure all vertices have the right set of sources and sinks
    for vertex in vertices:
        commands = vertex_commands[vertex]
        sources = vertices_source_nets[vertex]
        sinks = vertices_sink_nets[vertex]

        for source_num, source_net in enumerate(sources):
            ref_cmd = struct.pack("<II", NT_CMD.SOURCE_KEY | (source_num << 8),
                                  net_keys[source_net])
            assert ref_cmd in commands

        for sink_num, sink_net in enumerate(sinks):
            ref_cmd = struct.pack("<II", NT_CMD.SINK_KEY | (sink_num << 8),
                                  net_keys[sink_net])
            assert ref_cmd in commands

    # Make sure all vertices have the right set of timing values
    for vertex in vertices:
        commands = vertex_commands[vertex]

        ref_cmd = struct.pack("<II", NT_CMD.TIMESTEP, 1000)
        assert ref_cmd in commands

    # Make sure all vertices have the right timeout set
    for vertex in vertices:
        commands = vertex_commands[vertex]

        ref_cmd0 = struct.pack("<I", NT_CMD.ROUTER_TIMEOUT)
        ref_cmd1 = struct.pack("<I", NT_CMD.ROUTER_TIMEOUT_RESTORE)
        if router_access_vertex:
            assert ref_cmd0 in commands
            assert ref_cmd1 in commands
        else:
            assert ref_cmd0 not in commands
            assert ref_cmd1 not in commands

    # Make sure all vertices packet reinjection enabled if requested
    for vertex in vertices:
        commands = vertex_commands[vertex]

        ref_cmd0 = struct.pack("<I", NT_CMD.REINJECTION_ENABLE)
        ref_cmd1 = struct.pack("<I", NT_CMD.REINJECTION_DISABLE)
        if router_access_vertex:
            assert ref_cmd0 in commands
            assert ref_cmd1 in commands
        else:
            assert ref_cmd0 not in commands
            assert ref_cmd1 not in commands