|
1 | 1 | import pytest
|
2 | 2 | from utils import * # noqa
|
3 |
| - |
| 3 | +import random |
4 | 4 |
|
5 | 5 | def test_removeall_list():
|
6 | 6 | assert removeall(4, []) == []
|
@@ -189,6 +189,53 @@ def test_expr():
|
189 | 189 | assert (expr('GP(x, z) <== P(x, y) & P(y, z)')
|
190 | 190 | == Expr('<==', GP(x, z), P(x, y) & P(y, z)))
|
191 | 191 |
|
| 192 | +def test_FIFOQueue() : |
| 193 | + # Create an object |
| 194 | + queue = FIFOQueue() |
| 195 | + # Generate an array of number to be used for testing |
| 196 | + test_data = [ random.choice(range(100)) for i in range(100) ] |
| 197 | + # Index of the element to be added in the queue |
| 198 | + front_head = 0 |
| 199 | + # Index of the element to be removed from the queue |
| 200 | + back_head = 0 |
| 201 | + while front_head < 100 or back_head < 100 : |
| 202 | + if front_head == 100 : # only possible to remove |
| 203 | + # check for pop and append method |
| 204 | + assert queue.pop() == test_data[back_head] |
| 205 | + back_head += 1 |
| 206 | + elif back_head == front_head : # only possible to push element into queue |
| 207 | + queue.append(test_data[front_head]) |
| 208 | + front_head += 1 |
| 209 | + # else do it in a random manner |
| 210 | + elif random.random() < 0.5 : |
| 211 | + assert queue.pop() == test_data[back_head] |
| 212 | + back_head += 1 |
| 213 | + else : |
| 214 | + queue.append(test_data[front_head]) |
| 215 | + front_head += 1 |
| 216 | + # check for __len__ method |
| 217 | + assert len(queue) == front_head - back_head |
| 218 | + # chek for __contains__ method |
| 219 | + if front_head - back_head > 0 : |
| 220 | + assert random.choice(test_data[back_head:front_head]) in queue |
| 221 | + |
| 222 | + # check extend method |
| 223 | + test_data1 = [ random.choice(range(100)) for i in range(50) ] |
| 224 | + test_data2 = [ random.choice(range(100)) for i in range(50) ] |
| 225 | + # append elements of test data 1 |
| 226 | + queue.extend(test_data1) |
| 227 | + # append elements of test data 2 |
| 228 | + queue.extend(test_data2) |
| 229 | + # reset front_head |
| 230 | + front_head = 0 |
| 231 | + |
| 232 | + while front_head < 50 : |
| 233 | + assert test_data1[front_head] == queue.pop() |
| 234 | + front_head += 1 |
| 235 | + |
| 236 | + while front_head < 100 : |
| 237 | + assert test_data2[front_head - 50] == queue.pop() |
| 238 | + front_head += 1 |
192 | 239 |
|
193 | 240 | if __name__ == '__main__':
|
194 | 241 | pytest.main()
|
0 commit comments