|
| 1 | +from server.game_connection_matrix import ConnectionMatrix |
| 2 | + |
| 3 | + |
| 4 | +def test_all_connected(): |
| 5 | + # One by hand example |
| 6 | + matrix = ConnectionMatrix( |
| 7 | + established_peers={ |
| 8 | + 0: {1, 2, 3}, |
| 9 | + 1: {0, 2, 3}, |
| 10 | + 2: {0, 1, 3}, |
| 11 | + 3: {0, 1, 2}, |
| 12 | + }, |
| 13 | + ) |
| 14 | + assert matrix.get_unconnected_peer_ids() == set() |
| 15 | + |
| 16 | + # Check every fully connected grid, including the empty grid |
| 17 | + for num_players in range(0, 16 + 1): |
| 18 | + matrix = ConnectionMatrix( |
| 19 | + established_peers={ |
| 20 | + player_id: { |
| 21 | + peer_id |
| 22 | + for peer_id in range(num_players) |
| 23 | + if peer_id != player_id |
| 24 | + } |
| 25 | + for player_id in range(num_players) |
| 26 | + }, |
| 27 | + ) |
| 28 | + assert matrix.get_unconnected_peer_ids() == set() |
| 29 | + |
| 30 | + |
| 31 | +def test_1v1_not_connected(): |
| 32 | + matrix = ConnectionMatrix( |
| 33 | + established_peers={ |
| 34 | + 0: set(), |
| 35 | + 1: set(), |
| 36 | + }, |
| 37 | + ) |
| 38 | + assert matrix.get_unconnected_peer_ids() == {0, 1} |
| 39 | + |
| 40 | + |
| 41 | +def test_2v2_one_player_not_connected(): |
| 42 | + # 0 is not connected to anyone |
| 43 | + matrix = ConnectionMatrix( |
| 44 | + established_peers={ |
| 45 | + 0: set(), |
| 46 | + 1: {2, 3}, |
| 47 | + 2: {1, 3}, |
| 48 | + 3: {1, 2}, |
| 49 | + }, |
| 50 | + ) |
| 51 | + assert matrix.get_unconnected_peer_ids() == {0} |
| 52 | + |
| 53 | + |
| 54 | +def test_2v2_two_players_not_connected(): |
| 55 | + # 0 is not connected to anyone |
| 56 | + # 1 is not connected to anyone |
| 57 | + matrix = ConnectionMatrix( |
| 58 | + established_peers={ |
| 59 | + 0: set(), |
| 60 | + 1: set(), |
| 61 | + 2: {3}, |
| 62 | + 3: {2}, |
| 63 | + }, |
| 64 | + ) |
| 65 | + assert matrix.get_unconnected_peer_ids() == {0, 1} |
| 66 | + |
| 67 | + |
| 68 | +def test_2v2_not_connected(): |
| 69 | + # Not possible for only 3 players to be completely disconnected in a 4 |
| 70 | + # player game. Either 1, 2, or all can be disconnected. |
| 71 | + matrix = ConnectionMatrix( |
| 72 | + established_peers={ |
| 73 | + 0: set(), |
| 74 | + 1: set(), |
| 75 | + 2: set(), |
| 76 | + 3: set(), |
| 77 | + }, |
| 78 | + ) |
| 79 | + assert matrix.get_unconnected_peer_ids() == {0, 1, 2, 3} |
| 80 | + |
| 81 | + |
| 82 | +def test_2v2_one_pair_not_connected(): |
| 83 | + # 0 and 1 are not connected to each other |
| 84 | + matrix = ConnectionMatrix( |
| 85 | + established_peers={ |
| 86 | + 0: {2, 3}, |
| 87 | + 1: {2, 3}, |
| 88 | + 2: {0, 1, 3}, |
| 89 | + 3: {0, 1, 2}, |
| 90 | + }, |
| 91 | + ) |
| 92 | + assert matrix.get_unconnected_peer_ids() == {0, 1} |
| 93 | + |
| 94 | + |
| 95 | +def test_2v2_two_pairs_not_connected(): |
| 96 | + # 0 and 1 are not connected to each other |
| 97 | + # 1 and 2 are not connected to each other |
| 98 | + matrix = ConnectionMatrix( |
| 99 | + established_peers={ |
| 100 | + 0: {2, 3}, |
| 101 | + 1: {3}, |
| 102 | + 2: {0, 3}, |
| 103 | + 3: {0, 1, 2}, |
| 104 | + }, |
| 105 | + ) |
| 106 | + assert matrix.get_unconnected_peer_ids() == {1} |
| 107 | + |
| 108 | + |
| 109 | +def test_2v2_two_disjoint_pairs_not_connected(): |
| 110 | + # 0 and 1 are not connected to each other |
| 111 | + # 2 and 3 are not connected to each other |
| 112 | + matrix = ConnectionMatrix( |
| 113 | + established_peers={ |
| 114 | + 0: {2, 3}, |
| 115 | + 1: {2, 3}, |
| 116 | + 2: {0, 1}, |
| 117 | + 3: {0, 1}, |
| 118 | + }, |
| 119 | + ) |
| 120 | + assert matrix.get_unconnected_peer_ids() == {0, 1, 2, 3} |
| 121 | + |
| 122 | + |
| 123 | +def test_2v2_three_pairs_not_connected(): |
| 124 | + # 0 and 1 are not connected to each other |
| 125 | + # 1 and 2 are not connected to each other |
| 126 | + # 2 and 3 are not connected to each other |
| 127 | + matrix = ConnectionMatrix( |
| 128 | + established_peers={ |
| 129 | + 0: {2, 3}, |
| 130 | + 1: {3}, |
| 131 | + 2: {0}, |
| 132 | + 3: {0, 1}, |
| 133 | + }, |
| 134 | + ) |
| 135 | + assert matrix.get_unconnected_peer_ids() == {1, 2} |
| 136 | + |
| 137 | + |
| 138 | +def test_3v3_one_player_not_connected(): |
| 139 | + # 0 is not connected to anyone |
| 140 | + matrix = ConnectionMatrix( |
| 141 | + established_peers={ |
| 142 | + 0: set(), |
| 143 | + 1: {2, 3, 4, 5}, |
| 144 | + 2: {1, 3, 4, 5}, |
| 145 | + 3: {1, 2, 4, 5}, |
| 146 | + 4: {1, 2, 3, 5}, |
| 147 | + 5: {1, 2, 3, 4}, |
| 148 | + }, |
| 149 | + ) |
| 150 | + assert matrix.get_unconnected_peer_ids() == {0} |
| 151 | + |
| 152 | + |
| 153 | +def test_3v3_two_players_not_connected(): |
| 154 | + # 0 is not connected to anyone |
| 155 | + # 1 is not connected to anyone |
| 156 | + matrix = ConnectionMatrix( |
| 157 | + established_peers={ |
| 158 | + 0: set(), |
| 159 | + 1: set(), |
| 160 | + 2: {3, 4, 5}, |
| 161 | + 3: {2, 4, 5}, |
| 162 | + 4: {2, 3, 5}, |
| 163 | + 5: {2, 3, 4}, |
| 164 | + }, |
| 165 | + ) |
| 166 | + assert matrix.get_unconnected_peer_ids() == {0, 1} |
| 167 | + |
| 168 | + |
| 169 | +def test_3v3_three_players_not_connected(): |
| 170 | + # 0 is not connected to anyone |
| 171 | + # 1 is not connected to anyone |
| 172 | + # 2 is not connected to anyone |
| 173 | + matrix = ConnectionMatrix( |
| 174 | + established_peers={ |
| 175 | + 0: set(), |
| 176 | + 1: set(), |
| 177 | + 2: set(), |
| 178 | + 3: {4, 5}, |
| 179 | + 4: {3, 5}, |
| 180 | + 5: {3, 4}, |
| 181 | + }, |
| 182 | + ) |
| 183 | + assert matrix.get_unconnected_peer_ids() == {0, 1, 2} |
| 184 | + |
| 185 | + |
| 186 | +def test_3v3_four_players_not_connected(): |
| 187 | + # 0 is not connected to anyone |
| 188 | + # 1 is not connected to anyone |
| 189 | + # 2 is not connected to anyone |
| 190 | + # 3 is not connected to anyone |
| 191 | + matrix = ConnectionMatrix( |
| 192 | + established_peers={ |
| 193 | + 0: set(), |
| 194 | + 1: set(), |
| 195 | + 2: set(), |
| 196 | + 3: set(), |
| 197 | + 4: {5}, |
| 198 | + 5: {4}, |
| 199 | + }, |
| 200 | + ) |
| 201 | + assert matrix.get_unconnected_peer_ids() == {0, 1, 2, 3} |
| 202 | + |
| 203 | + |
| 204 | +def test_3v3_not_connected(): |
| 205 | + matrix = ConnectionMatrix( |
| 206 | + established_peers={ |
| 207 | + 0: set(), |
| 208 | + 1: set(), |
| 209 | + 2: set(), |
| 210 | + 3: set(), |
| 211 | + 4: set(), |
| 212 | + 5: set(), |
| 213 | + }, |
| 214 | + ) |
| 215 | + assert matrix.get_unconnected_peer_ids() == {0, 1, 2, 3, 4, 5} |
| 216 | + |
| 217 | + |
| 218 | +def test_3v3_one_pair_not_connected(): |
| 219 | + # 0 and 1 are not connected to each other |
| 220 | + matrix = ConnectionMatrix( |
| 221 | + established_peers={ |
| 222 | + 0: {2, 3, 4, 5}, |
| 223 | + 1: {2, 3, 4, 5}, |
| 224 | + 2: {0, 1, 3, 4, 5}, |
| 225 | + 3: {0, 1, 2, 4, 5}, |
| 226 | + 4: {0, 1, 2, 3, 5}, |
| 227 | + 5: {0, 1, 2, 3, 4}, |
| 228 | + }, |
| 229 | + ) |
| 230 | + assert matrix.get_unconnected_peer_ids() == {0, 1} |
| 231 | + |
| 232 | + |
| 233 | +def test_3v3_one_player_and_one_pair_not_connected(): |
| 234 | + # 0 is not connected to anyone |
| 235 | + # 1 and 2 are not connected to each other |
| 236 | + matrix = ConnectionMatrix( |
| 237 | + established_peers={ |
| 238 | + 0: set(), |
| 239 | + 1: {3, 4, 5}, |
| 240 | + 2: {3, 4, 5}, |
| 241 | + 3: {1, 2, 4, 5}, |
| 242 | + 4: {1, 2, 3, 5}, |
| 243 | + 5: {1, 2, 3, 4}, |
| 244 | + }, |
| 245 | + ) |
| 246 | + assert matrix.get_unconnected_peer_ids() == {0, 1, 2} |
0 commit comments