| 1260 | |
| 1261 | |
| 1262 | def test_bst_generation() -> None: |
| 1263 | for invalid_height in ["foo", -1, None]: |
| 1264 | with pytest.raises(TreeHeightError) as err: |
| 1265 | bst(height=invalid_height) # type: ignore |
| 1266 | assert str(err.value) == "height must be an int between 0 - 9" |
| 1267 | |
| 1268 | root = bst(height=0) |
| 1269 | assert root is not None |
| 1270 | root.validate() |
| 1271 | assert root.height == 0 |
| 1272 | assert root.left is None |
| 1273 | assert root.right is None |
| 1274 | assert isinstance(root.val, int) |
| 1275 | |
| 1276 | for _ in range(REPETITIONS): |
| 1277 | random_height = random.randint(1, 9) |
| 1278 | root = bst(random_height) |
| 1279 | assert root is not None |
| 1280 | root.validate() |
| 1281 | assert root.is_bst is True |
| 1282 | assert root.height == random_height |
| 1283 | |
| 1284 | for _ in range(REPETITIONS): |
| 1285 | random_height = random.randint(1, 9) |
| 1286 | root = bst(random_height, letters=True) |
| 1287 | assert root is not None |
| 1288 | root.validate() |
| 1289 | assert root.is_bst is True |
| 1290 | assert root.height == random_height |
| 1291 | |
| 1292 | for _ in range(REPETITIONS): |
| 1293 | random_height = random.randint(1, 9) |
| 1294 | root = bst(random_height, is_perfect=True) |
| 1295 | assert root is not None |
| 1296 | root.validate() |
| 1297 | assert root.height == random_height |
| 1298 | |
| 1299 | if not root.is_bst: |
| 1300 | raise Exception("boo") |
| 1301 | |
| 1302 | assert root.is_bst is True |
| 1303 | assert root.is_perfect is True |
| 1304 | assert root.is_balanced is True |
| 1305 | assert root.is_strict is True |
| 1306 | |
| 1307 | for _ in range(REPETITIONS): |
| 1308 | random_height = random.randint(1, 9) |
| 1309 | root = bst(random_height, letters=True, is_perfect=True) |
| 1310 | assert root is not None |
| 1311 | root.validate() |
| 1312 | assert root.height == random_height |
| 1313 | |
| 1314 | if not root.is_bst: |
| 1315 | raise Exception("boo") |
| 1316 | |
| 1317 | assert root.is_bst is True |
| 1318 | assert root.is_perfect is True |
| 1319 | assert root.is_balanced is True |