Advertisement
Python253

smart_pointer_test

May 22nd, 2024
650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.25 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: smart_pointer_test.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. :: Python Smart Pointer Test ::
  9.  
  10. Description:
  11.    - This script demonstrates smart pointer behavior through test scenarios.
  12.    - It includes scenarios for moving null pointers and moving non-null pointers.
  13.  
  14. Requirements:
  15.    - Python 3.x
  16.  
  17. Functions:
  18.    - move_constructor_or_get(): Executes test scenarios to demonstrate smart pointer behavior.
  19.  
  20. Usage:
  21.    - Run the script, and it will execute the test scenarios.
  22.    - Review the output to observe the behavior of smart pointers.
  23.  
  24. Additional Notes:
  25.    - In the Python version, UniquePtr is implemented as a simple class that wraps a pointer.
  26.    - It doesn't perform manual memory management due to Python's automatic garbage collection.
  27.    - However, it serves the same purpose conceptually, providing a way to manage ownership of objects.
  28. """
  29.  
  30. class UniquePtr:
  31.     """A simple smart pointer class managing ownership of a pointer."""
  32.  
  33.     def __init__(self, ptr):
  34.         """Initialize the UniquePtr with a pointer."""
  35.         self.ptr = ptr
  36.  
  37.     def __del__(self):
  38.         """Destructor to clean up the pointer."""
  39.         del self.ptr
  40.  
  41. class Spy:
  42.     """A class used for tracking copy operations and deletions."""
  43.  
  44.     def __init__(self, is_deleted, copy_count):
  45.         """Initialize Spy object with deletion status and copy count."""
  46.         self.is_deleted = is_deleted
  47.         self.copy_count = copy_count
  48.  
  49.     def __del__(self):
  50.         """Destructor to mark the object as deleted."""
  51.         self.is_deleted = True
  52.  
  53. def line():
  54.     """
  55.    Prints a line of dashes for visual separation.
  56.    """
  57.     print("-" * 67)
  58.  
  59. def move_constructor_or_get():
  60.     """Tests scenarios to demonstrate smart pointer behavior."""
  61.     line()
  62.     print("\n  :: [Testing scenarios to demonstrate smart pointer behavior] ::\n")
  63.  
  64.     # Scenario 1: Moving null pointers
  65.     line()
  66.     print("Scenario 1: \t[Moving null pointers]\n")
  67.     print("- Creating a UniquePtr with a null pointer...")
  68.     uptr = UniquePtr(None)
  69.     uptr2 = UniquePtr(uptr.ptr)
  70.     print("- Verifying that both pointers are null...")
  71.     assert uptr.ptr is None
  72.     assert uptr2.ptr is None
  73.     print("\n\t\t[Both pointers are null as expected]")
  74.     print("\nScenario 1: \t[Moving null pointers]\t\t\t  [Passed!]")
  75.  
  76.     # Scenario 2: Moving non-null pointers
  77.     line()
  78.     print("Scenario 2: \t[Moving non-null pointers]\n")
  79.     print("- Creating a Spy object and wrapping it in a UniquePtr...")
  80.     copy_count = 0
  81.     is_deleted = False
  82.     ptr = Spy(is_deleted, copy_count)
  83.     uptr = UniquePtr(ptr)
  84.     print("- Moving the UniquePtr to another UniquePtr...")
  85.     uptr2 = UniquePtr(uptr.ptr)
  86.     print("- Verifying ownership transfer and copy count...")
  87.     assert uptr.ptr is ptr
  88.     assert uptr2.ptr is ptr
  89.     assert copy_count == 0
  90.     print("\n\t\t[Ownership transferred successfully]")
  91.     print("\t\t[Copy count is as expected]")
  92.     print("\nScenario 2: \t[Moving non-null pointers]\t\t  [Passed!]")
  93.  
  94. if __name__ == "__main__":
  95.     move_constructor_or_get()
  96.     line()
  97.     print("\n\t\tAll test scenarios passed successfully.\n\t\t   Exiting Program...    GoodBye!\n")
  98.     line()
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement