|
| 1 | +import pytest |
| 2 | +from rig.routing_table import RoutingTableEntry as RTE |
| 3 | +from rig.routing_table import Routes, MinimisationFailedError |
| 4 | +from rig_routing_tables.ordered_covering import minimise |
| 5 | + |
| 6 | + |
| 7 | +@pytest.mark.parametrize("target_length, no_raise", |
| 8 | + ((None, False), (0, True))) |
| 9 | +def test_ordered_covering_simple(target_length, no_raise): |
| 10 | + """Test that a very simple routing table can be minimised, only one |
| 11 | + merge should be made as there are no conflicts. |
| 12 | +
|
| 13 | + Table:: |
| 14 | +
|
| 15 | + 0000 -> N, NE |
| 16 | + 0001 -> N, NE |
| 17 | + 001X -> S |
| 18 | +
|
| 19 | + Can be minimised to:: |
| 20 | +
|
| 21 | + 000X -> N, NE |
| 22 | + 001X -> S |
| 23 | + """ |
| 24 | + # Original table |
| 25 | + table = [ |
| 26 | + RTE({Routes.north, Routes.north_east}, 0b0000, 0b1111), |
| 27 | + RTE({Routes.north, Routes.north_east}, 0b0001, 0b1111), |
| 28 | + RTE({Routes.south}, 0b0010, 0b1110), |
| 29 | + ] |
| 30 | + |
| 31 | + # Expected table |
| 32 | + expected_table = [ |
| 33 | + RTE({Routes.north, Routes.north_east}, 0b0000, 0b1110), |
| 34 | + RTE({Routes.south}, 0b0010, 0b1110), |
| 35 | + ] |
| 36 | + |
| 37 | + # Minimise and check the result |
| 38 | + new_table = minimise(table, target_length, no_raise) |
| 39 | + |
| 40 | + assert new_table == expected_table |
| 41 | + |
| 42 | + |
| 43 | +def test_ordered_covering_simple_fails_if_too_large(): |
| 44 | + """Test that a very simple routing table can be minimised, and that an |
| 45 | + exception is raised if that minimisation is still too large. |
| 46 | +
|
| 47 | + Table:: |
| 48 | +
|
| 49 | + 0000 -> N, NE |
| 50 | + 0001 -> N, NE |
| 51 | + 001X -> S |
| 52 | + """ |
| 53 | + # Original table |
| 54 | + table = [ |
| 55 | + RTE({Routes.north, Routes.north_east}, 0b0000, 0b1111), |
| 56 | + RTE({Routes.north, Routes.north_east}, 0b0001, 0b1111), |
| 57 | + RTE({Routes.south}, 0b0010, 0b1110), |
| 58 | + ] |
| 59 | + |
| 60 | + with pytest.raises(MinimisationFailedError) as exc: |
| 61 | + minimise(table, target_length=1) |
| 62 | + |
| 63 | + assert exc.value.target_length == 1 |
| 64 | + assert exc.value.final_length == 2 |
| 65 | + assert exc.value.chip is None |
0 commit comments