Test that the rotate_left and rotate_right functions work.
()
| 524 | |
| 525 | |
| 526 | def test_rotations() -> bool: |
| 527 | """Test that the rotate_left and rotate_right functions work.""" |
| 528 | # Make a tree to test on |
| 529 | tree = RedBlackTree(0) |
| 530 | tree.left = RedBlackTree(-10, parent=tree) |
| 531 | tree.right = RedBlackTree(10, parent=tree) |
| 532 | tree.left.left = RedBlackTree(-20, parent=tree.left) |
| 533 | tree.left.right = RedBlackTree(-5, parent=tree.left) |
| 534 | tree.right.left = RedBlackTree(5, parent=tree.right) |
| 535 | tree.right.right = RedBlackTree(20, parent=tree.right) |
| 536 | # Make the right rotation |
| 537 | left_rot = RedBlackTree(10) |
| 538 | left_rot.left = RedBlackTree(0, parent=left_rot) |
| 539 | left_rot.left.left = RedBlackTree(-10, parent=left_rot.left) |
| 540 | left_rot.left.right = RedBlackTree(5, parent=left_rot.left) |
| 541 | left_rot.left.left.left = RedBlackTree(-20, parent=left_rot.left.left) |
| 542 | left_rot.left.left.right = RedBlackTree(-5, parent=left_rot.left.left) |
| 543 | left_rot.right = RedBlackTree(20, parent=left_rot) |
| 544 | tree = tree.rotate_left() |
| 545 | if tree != left_rot: |
| 546 | return False |
| 547 | tree = tree.rotate_right() |
| 548 | tree = tree.rotate_right() |
| 549 | # Make the left rotation |
| 550 | right_rot = RedBlackTree(-10) |
| 551 | right_rot.left = RedBlackTree(-20, parent=right_rot) |
| 552 | right_rot.right = RedBlackTree(0, parent=right_rot) |
| 553 | right_rot.right.left = RedBlackTree(-5, parent=right_rot.right) |
| 554 | right_rot.right.right = RedBlackTree(10, parent=right_rot.right) |
| 555 | right_rot.right.right.left = RedBlackTree(5, parent=right_rot.right.right) |
| 556 | right_rot.right.right.right = RedBlackTree(20, parent=right_rot.right.right) |
| 557 | return tree == right_rot |
| 558 | |
| 559 | |
| 560 | def test_insertion_speed() -> bool: |
no test coverage detected