def test_alloc_board(self): # If no single boards have been allocated yet, a whole triad should be # allocated and a (live) board from that set be returned. next_id = 10 a = Allocator(2, 1, dead_boards=set( [(1, 0, 1)] + [(0, 0, z) for z in range(3)]), next_id=next_id) # Should get two boards in total all_boards = set() for _ in range(2): allocation_id, boards, periphery, torus = a._alloc_board() assert allocation_id == next_id next_id += 1 assert len(boards) == 1 x, y, z = next(iter(boards)) assert (x, y, z) not in all_boards all_boards.add((x, y, z)) assert periphery == set((x, y, z, link) for link in Links) assert torus is WrapAround.none assert all_boards == set([(1, 0, 0), (1, 0, 2)]) # Should not be able to allocate any more! assert a._alloc_board() is None
def test_alloc_existing_specific_board(self, last_remaining): # Attempt to allocate a specific board next_id = 10 a = Allocator(1, 1, next_id=next_id) # Manually add a triad to the set available assert a.pack_tree.request(0, 0) a.single_board_triads[(0, 0)] = set( [1] if last_remaining else range(3)) # Should be able to get the board we want! allocation_id, boards, periphery, torus = a._alloc_board(0, 0, 1) assert allocation_id == next_id assert len(boards) == 1 assert next(iter(boards)) == (0, 0, 1) assert periphery == set((0, 0, 1, link) for link in Links) assert torus is WrapAround.none # If exhausted, the boards should be removed from the single board # triad set. if last_remaining: assert (0, 0) not in a.single_board_triads assert (0, 0) in a.full_single_board_triads else: assert (0, 0) in a.single_board_triads assert (0, 0) not in a.full_single_board_triads
def test_alloc_board_specific(self): # If no triad containing the requested board is already allocated, we # should allocate it. next_id = 10 a = Allocator(1, 1, next_id=next_id) # Should get two boards in total allocation_id, boards, periphery, torus = a._alloc_board(0, 0, 1) assert allocation_id == next_id assert len(boards) == 1 assert next(iter(boards)) == (0, 0, 1) assert periphery == set((0, 0, 1, link) for link in Links) # Should not be able to allocate that board any more! assert a._alloc_board(0, 0, 1) is None assert torus is WrapAround.none
def test_alloc_existing_used_board(self): next_id = 10 a = Allocator(1, 1, next_id=next_id) # Manually add a triad to the set available assert a.pack_tree.request(0, 0) a.single_board_triads[(0, 0)] = set([0, 2]) # Shouldn't be able to get the board we want since it is already # allocated. assert a._alloc_board(0, 0, 1) is None
def test_alloc_existing_triad(self): # Attempt to allocate based on an already allocated triad. next_id = 10 a = Allocator(1, 1, next_id=next_id) # Manually add a triad to the set available assert a.pack_tree.request(0, 0) a.single_board_triads[(0, 0)] = set(range(3)) # Should get the three boards from the triad all_boards = set() for _ in range(3): assert (0, 0) in a.single_board_triads assert (0, 0) not in a.full_single_board_triads allocation_id, boards, periphery, torus = a._alloc_board() assert allocation_id == next_id next_id += 1 assert len(boards) == 1 x, y, z = next(iter(boards)) assert x == y == 0 assert 0 <= x < 3 assert (x, y, z) not in all_boards all_boards.add((x, y, z)) assert periphery == set((x, y, z, link) for link in Links) assert torus is WrapAround.none assert all_boards == set((0, 0, z) for z in range(3)) # Once exhausted, the boards should be removed from the single board # triad set. assert (0, 0) not in a.single_board_triads assert (0, 0) in a.full_single_board_triads
def test_free_board(self): a = Allocator(2, 1, dead_boards=set([(0, 0, 1)])) # Allocate the two boards on triad 0, 0 id00, _1, _2, _3 = a._alloc_board(0, 0, 0) id02, _1, _2, _3 = a._alloc_board(0, 0, 2) # Allocate the three boards on triad 1, 0 id10, _1, _2, _3 = a._alloc_board(1, 0, 0) id11, _1, _2, _3 = a._alloc_board(1, 0, 1) id12, _1, _2, _3 = a._alloc_board(1, 0, 2) # No board triads should be available assert len(a.single_board_triads) == 0 assert a.full_single_board_triads == set([(0, 0), (1, 0)]) # The pack tree should be full assert a.pack_tree.allocated is False assert a.pack_tree.children is not None assert a.pack_tree.children[0].allocated is True assert a.pack_tree.children[1].allocated is True # Freeing a board should bring it back into the single boards triads # dictionary a.free(id00) assert a.single_board_triads == {(0, 0): set([0])} assert a.full_single_board_triads == set([(1, 0)]) # The pack tree should still be full assert a.pack_tree.allocated is False assert a.pack_tree.children is not None assert a.pack_tree.children[0].allocated is True assert a.pack_tree.children[1].allocated is True # Freeing the only other working board in a triad should remove the # triad entirely a.free(id02) assert a.single_board_triads == {} assert a.full_single_board_triads == set([(1, 0)]) # ...and the corresponding part of the pack_tree should be free too assert a.pack_tree.allocated is False assert a.pack_tree.children is not None assert a.pack_tree.children[0].allocated is False assert a.pack_tree.children[1].allocated is True # Freeing should move into the single boards dictionary a.free(id10) assert a.single_board_triads == {(1, 0): set([0])} assert a.full_single_board_triads == set() # The pack_tree should be unchanged. assert a.pack_tree.allocated is False assert a.pack_tree.children is not None assert a.pack_tree.children[0].allocated is False assert a.pack_tree.children[1].allocated is True # Freeing another board in the same triad (but not the last) should # just update the dictionary. a.free(id11) assert a.single_board_triads == {(1, 0): set([0, 1])} assert a.full_single_board_triads == set() # The pack_tree should be unchanged. assert a.pack_tree.allocated is False assert a.pack_tree.children is not None assert a.pack_tree.children[0].allocated is False assert a.pack_tree.children[1].allocated is True # Freeing the last board should remove it as before... a.free(id12) assert a.single_board_triads == {} assert a.full_single_board_triads == set() # The pack tree should now be empty assert a.pack_tree.allocated is False assert a.pack_tree.children is None
def test_alloc_board_dead(self): # Should fail if a dead board is requested a = Allocator(3, 4, dead_boards=set([(3, 2, 1)])) assert a._alloc_board(3, 2, 1) is None