def test_add_cells_at_same_slot_offset(sim_engine): sim_engine = sim_engine() # need for log slotframe = SlotFrame(None, 1, 101) cell_1 = Cell(1, 5, [d.CELLOPTION_TX], 'test_mac_addr_1') cell_2 = Cell(1, 5, [d.CELLOPTION_RX], 'test_mac_addr_2') assert slotframe.get_cells_by_slot_offset(1) == [] slotframe.add(cell_1) slotframe.add(cell_2) assert slotframe.get_cells_by_slot_offset(1) == [cell_1, cell_2] assert slotframe.get_cells_by_mac_addr('test_mac_addr_1') == [cell_1] assert slotframe.get_cells_by_mac_addr('test_mac_addr_2') == [cell_2] slotframe.delete(cell_1) assert slotframe.get_cells_by_mac_addr('test_mac_addr_1') == [] assert slotframe.get_cells_by_mac_addr('test_mac_addr_2') == [cell_2] slotframe.delete(cell_2) assert slotframe.get_cells_by_mac_addr('test_mac_addr_1') == [] assert slotframe.get_cells_by_mac_addr('test_mac_addr_2') == []
def test_add_cells_at_same_slot_offset(): slotframe = SlotFrame(101) cell_1 = Cell(1, 5, [d.CELLOPTION_TX], 'test_mac_addr_1') cell_2 = Cell(1, 5, [d.CELLOPTION_RX], 'test_mac_addr_2') assert slotframe.get_cells_by_slot_offset(1) == [] slotframe.add(cell_1) slotframe.add(cell_2) assert slotframe.get_cells_by_slot_offset(1) == [cell_1, cell_2] assert slotframe.get_cells_by_mac_addr('test_mac_addr_1') == [cell_1] assert slotframe.get_cells_by_mac_addr('test_mac_addr_2') == [cell_2]
def test_add(sim_engine, fixture_neighbor_mac_addr): sim_engine = sim_engine() # need for log slotframe = SlotFrame(None, 1, 101) cell = Cell(0, 0, all_options_on, fixture_neighbor_mac_addr) slotframe.add(cell) assert slotframe.get_cells_at_asn(0) == [cell] assert slotframe.get_cells_at_asn(1) == [] assert slotframe.get_cells_at_asn(100) == [] assert slotframe.get_cells_at_asn(101) == [cell] assert slotframe.get_cells_by_slot_offset(0) == [cell] assert slotframe.get_cells_by_slot_offset(1) == [] assert slotframe.get_cells_by_slot_offset(100) == [] assert slotframe.get_cells_by_mac_addr(fixture_neighbor_mac_addr) == [cell] assert slotframe.get_cells_by_mac_addr('dummy_mac_addr') == [] assert ([ cell for cell in slotframe.get_cells_by_mac_addr(fixture_neighbor_mac_addr) if cell.options == [d.CELLOPTION_TX, d.CELLOPTION_RX, d.CELLOPTION_SHARED] ] == [cell])
def test_slotframe_get_cells_filtered(sim_engine): """ Unit test for Slotframe class method slotframe_get_cells_filtered Test if we can get the cells filtered by options, MAC address or both """ sim_engine = sim_engine() # need for log neighbor_mac_addr_1 = 'test_mac_addr_1' neighbor_mac_addr_2 = 'test_mac_addr_2' neighbor_mac_addr_3 = None slotframe = SlotFrame(None, 1, 101) # create cells cell_tx_1 = Cell(0, 0, [d.CELLOPTION_TX], neighbor_mac_addr_1) cell_rx_1 = Cell(1, 0, [d.CELLOPTION_RX], neighbor_mac_addr_2) cell_rx_2 = Cell(1, 0, [d.CELLOPTION_RX], neighbor_mac_addr_3) cells = [cell_tx_1, cell_rx_1, cell_rx_2] # add cells to slotframe for c in cells: slotframe.add(c) # check if all cells are returned () assert slotframe.get_cells_filtered() == cells # check if only tx cells are returned assert slotframe.get_cells_filtered(cell_options=[d.CELLOPTION_TX]) == [ cell_tx_1 ] # check if only rx cells are returned assert slotframe.get_cells_filtered(cell_options=[d.CELLOPTION_RX]) == [ cell_rx_1, cell_rx_2 ] # check if only cells that match mac_address are returned assert slotframe.get_cells_filtered(mac_addr=neighbor_mac_addr_1) == \ [c for c in cells if c.mac_addr == neighbor_mac_addr_1] # check if only cells that match None mac_address are returned assert slotframe.get_cells_filtered(mac_addr=None) == \ [c for c in cells if c.mac_addr is None]
def test_slotframe_get_available_slot_offsets(): """ Unit test for Slotframe class method get_available_slot_offsets Test if we can get the list of offsets for slots that are not in use """ neighbor_mac_addr_1 = 'test_mac_addr_1' neighbor_mac_addr_2 = 'test_mac_addr_2' slotframe = SlotFrame(101) # create cells cell_tx_1 = Cell(0, 0, [d.CELLOPTION_TX], neighbor_mac_addr_1) cell_rx_1 = Cell(1, 0, [d.CELLOPTION_RX], neighbor_mac_addr_2) cells = [cell_tx_1, cell_rx_1] # add cells to slotframe for c in cells: slotframe.add(c) # check if all slot offsets are returned except the one reserved assert slotframe.get_available_slot_offsets() == [i for i in range(2, 101)]
def test_print_slotframe(fixture_num_cells): slotframe = SlotFrame(101) # install cells for i in range(fixture_num_cells): slot_offset = i channel_offset = random.randint(0, 65535) slotframe.add(Cell(slot_offset, channel_offset, [])) print slotframe str_slotframe = str(slotframe) assert 'length: 101' in str_slotframe assert 'num_cells: {0}'.format(fixture_num_cells) in str_slotframe
def test_print_slotframe(sim_engine, fixture_num_cells): sim_engine = sim_engine() # need for log slotframe = SlotFrame(None, 1, 101) # install cells for i in range(fixture_num_cells): slot_offset = i channel_offset = random.randint(0, 65535) slotframe.add(Cell(slot_offset, channel_offset, [])) print(slotframe) str_slotframe = str(slotframe) assert 'length: 101' in str_slotframe assert 'num_cells: {0}'.format(fixture_num_cells) in str_slotframe
def test_slotframe_set_length_with_cells(sim_engine): """ Test if we can change the slotframe length when cells are allocated """ sim_engine = sim_engine() # need for log neighbor_mac_addr_1 = 'test_mac_addr_1' neighbor_mac_addr_2 = 'test_mac_addr_2' slotframe = SlotFrame(None, 1, 101) # create cells cell_0 = Cell(0, 0, [d.CELLOPTION_TX], neighbor_mac_addr_1) cell_70 = Cell(70, 0, [d.CELLOPTION_TX], neighbor_mac_addr_2) cells = [cell_0, cell_70] # add cells to slotframe for c in cells: slotframe.add(c) # decrease the slotframe length assert len(slotframe.get_busy_slots()) == len(cells) new_length = 50 slotframe.set_length(new_length) assert slotframe.length == new_length assert len(slotframe.get_busy_slots() ) == len(cells) - 1 # make sure cell is delete # make sure we cannot add a cell after new length with pytest.raises(Exception): slotframe.add(cell_70) # increase the slotframe length new_length = 150 slotframe.set_length(new_length) assert slotframe.length == new_length assert len(slotframe.get_busy_slots( )) == len(cells) - 1 # make sure we have the right amount of cells
def test_delete_cell(): neighbor_mac_addr = 'test_mac_addr' slotframe = SlotFrame(101) cell = Cell(0, 0, all_options_on, neighbor_mac_addr) assert slotframe.get_cells_by_mac_addr(neighbor_mac_addr) == [] assert slotframe.get_cells_by_slot_offset(0) == [] slotframe.add(cell) assert slotframe.get_cells_by_mac_addr(neighbor_mac_addr) == [cell] assert slotframe.get_cells_by_slot_offset(0) == [cell] slotframe.delete(cell) assert slotframe.get_cells_by_mac_addr(neighbor_mac_addr) == [] assert slotframe.get_cells_by_slot_offset(0) == []
def test_print_cell(sim_engine, fixture_cell_options, fixture_mac_addr): sim_engine = sim_engine() if fixture_mac_addr is True: mac_addr = sim_engine.motes[0].get_mac_addr() else: mac_addr = fixture_mac_addr slot_offset = random.randint(0, 65535) channel_offset = random.randint(0, 65535) cell = Cell(slot_offset, channel_offset, fixture_cell_options, mac_addr) print cell str_cell = str(cell) assert 'slot_offset: {0}'.format(slot_offset) in str_cell assert 'channel_offset: {0}'.format(channel_offset) in str_cell assert 'mac_addr: {0}'.format(mac_addr) in str_cell assert 'options: [{0}]'.format(', '.join(fixture_cell_options)) in str_cell
def test_delete_cell(sim_engine): sim_engine = sim_engine() # need for log neighbor_mac_addr = 'test_mac_addr' slotframe = SlotFrame(None, 1, 101) cell = Cell(0, 0, all_options_on, neighbor_mac_addr) assert slotframe.get_cells_by_mac_addr(neighbor_mac_addr) == [] assert slotframe.get_cells_by_slot_offset(0) == [] slotframe.add(cell) assert slotframe.get_cells_by_mac_addr(neighbor_mac_addr) == [cell] assert slotframe.get_cells_by_slot_offset(0) == [cell] slotframe.delete(cell) assert slotframe.get_cells_by_mac_addr(neighbor_mac_addr) == [] assert slotframe.get_cells_by_slot_offset(0) == []
def test_add(fixture_neighbor_mac_addr): slotframe = SlotFrame(101) cell = Cell(0, 0, all_options_on, fixture_neighbor_mac_addr) slotframe.add(cell) assert slotframe.get_cells_at_asn(0) == [cell] assert slotframe.get_cells_at_asn(1) == [] assert slotframe.get_cells_at_asn(100) == [] assert slotframe.get_cells_at_asn(101) == [cell] assert slotframe.get_cells_by_slot_offset(0) == [cell] assert slotframe.get_cells_by_slot_offset(1) == [] assert slotframe.get_cells_by_slot_offset(100) == [] assert slotframe.get_cells_by_mac_addr(fixture_neighbor_mac_addr) == [cell] assert slotframe.get_cells_by_mac_addr('dummy_mac_addr') == [] assert (filter( lambda cell: cell.options == [d.CELLOPTION_TX, d.CELLOPTION_RX, d.CELLOPTION_SHARED], slotframe.get_cells_by_mac_addr(fixture_neighbor_mac_addr)) == [cell])