Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: smart_pointer_test.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- :: Python Smart Pointer Test ::
- Description:
- - This script demonstrates smart pointer behavior through test scenarios.
- - It includes scenarios for moving null pointers and moving non-null pointers.
- Requirements:
- - Python 3.x
- Functions:
- - move_constructor_or_get(): Executes test scenarios to demonstrate smart pointer behavior.
- Usage:
- - Run the script, and it will execute the test scenarios.
- - Review the output to observe the behavior of smart pointers.
- Additional Notes:
- - In the Python version, UniquePtr is implemented as a simple class that wraps a pointer.
- - It doesn't perform manual memory management due to Python's automatic garbage collection.
- - However, it serves the same purpose conceptually, providing a way to manage ownership of objects.
- """
- class UniquePtr:
- """A simple smart pointer class managing ownership of a pointer."""
- def __init__(self, ptr):
- """Initialize the UniquePtr with a pointer."""
- self.ptr = ptr
- def __del__(self):
- """Destructor to clean up the pointer."""
- del self.ptr
- class Spy:
- """A class used for tracking copy operations and deletions."""
- def __init__(self, is_deleted, copy_count):
- """Initialize Spy object with deletion status and copy count."""
- self.is_deleted = is_deleted
- self.copy_count = copy_count
- def __del__(self):
- """Destructor to mark the object as deleted."""
- self.is_deleted = True
- def line():
- """
- Prints a line of dashes for visual separation.
- """
- print("-" * 67)
- def move_constructor_or_get():
- """Tests scenarios to demonstrate smart pointer behavior."""
- line()
- print("\n :: [Testing scenarios to demonstrate smart pointer behavior] ::\n")
- # Scenario 1: Moving null pointers
- line()
- print("Scenario 1: \t[Moving null pointers]\n")
- print("- Creating a UniquePtr with a null pointer...")
- uptr = UniquePtr(None)
- uptr2 = UniquePtr(uptr.ptr)
- print("- Verifying that both pointers are null...")
- assert uptr.ptr is None
- assert uptr2.ptr is None
- print("\n\t\t[Both pointers are null as expected]")
- print("\nScenario 1: \t[Moving null pointers]\t\t\t [Passed!]")
- # Scenario 2: Moving non-null pointers
- line()
- print("Scenario 2: \t[Moving non-null pointers]\n")
- print("- Creating a Spy object and wrapping it in a UniquePtr...")
- copy_count = 0
- is_deleted = False
- ptr = Spy(is_deleted, copy_count)
- uptr = UniquePtr(ptr)
- print("- Moving the UniquePtr to another UniquePtr...")
- uptr2 = UniquePtr(uptr.ptr)
- print("- Verifying ownership transfer and copy count...")
- assert uptr.ptr is ptr
- assert uptr2.ptr is ptr
- assert copy_count == 0
- print("\n\t\t[Ownership transferred successfully]")
- print("\t\t[Copy count is as expected]")
- print("\nScenario 2: \t[Moving non-null pointers]\t\t [Passed!]")
- if __name__ == "__main__":
- move_constructor_or_get()
- line()
- print("\n\t\tAll test scenarios passed successfully.\n\t\t Exiting Program... GoodBye!\n")
- line()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement