From 99b9d89b7715d9a13f415ff8a5a825c44a0c6d1f Mon Sep 17 00:00:00 2001 From: Jordan Welsman Date: Wed, 8 Mar 2023 01:08:47 -0600 Subject: [PATCH 01/12] build: Incremented version number. --- jutl/__init__.py | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/jutl/__init__.py b/jutl/__init__.py index dd56f06..0104410 100644 --- a/jutl/__init__.py +++ b/jutl/__init__.py @@ -1,5 +1,5 @@ # Dunder attributes -__version__ = "0.5.1" +__version__ = "0.5.2" __author__ = "Jordan Welsman" __license__ = "MIT" __copyright__ = "Copyright 2023 Jordan Welsman" diff --git a/setup.py b/setup.py index 0060aef..94281d7 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ # Arguments git_name = "jutils" pypi_name = "jutl" -version = "0.5.1" # update __init__.py +version = "0.5.2" # update __init__.py python_version = ">=3.10" # Long description from README.md From 83617409cc421a9a0e26255704af12ff5072e256 Mon Sep 17 00:00:00 2001 From: Jordan Welsman Date: Wed, 8 Mar 2023 01:10:11 -0600 Subject: [PATCH 02/12] feat: Added base exception to inherit from. --- jutl/exceptions/jutlexception.py | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 jutl/exceptions/jutlexception.py diff --git a/jutl/exceptions/jutlexception.py b/jutl/exceptions/jutlexception.py new file mode 100644 index 0000000..3978ca6 --- /dev/null +++ b/jutl/exceptions/jutlexception.py @@ -0,0 +1,8 @@ +# Module imports + +# External class visibility +__all__ = ['JutilsException'] + +class JutilsException(Exception): + def __init__(self, message): + self.message = message \ No newline at end of file From 0521bd680e3e63ee58ef82effcf13e40c55a820b Mon Sep 17 00:00:00 2001 From: Jordan Welsman Date: Wed, 8 Mar 2023 01:10:50 -0600 Subject: [PATCH 03/12] refactor: Exceptions now inherit from JutlException(). --- jutl/exceptions/__init__.py | 6 +++++- jutl/exceptions/emptypipeline.py | 6 +++--- jutl/exceptions/emptyqueue.py | 8 ++++++++ jutl/exceptions/emptystack.py | 8 ++++++++ jutl/exceptions/fullqueue.py | 8 ++++++++ jutl/exceptions/fullstack.py | 8 ++++++++ jutl/exceptions/invalidformatting.py | 6 +++--- jutl/exceptions/missinginput.py | 6 +++--- 8 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 jutl/exceptions/emptyqueue.py create mode 100644 jutl/exceptions/emptystack.py create mode 100644 jutl/exceptions/fullqueue.py create mode 100644 jutl/exceptions/fullstack.py diff --git a/jutl/exceptions/__init__.py b/jutl/exceptions/__init__.py index 3537feb..a6d92bf 100644 --- a/jutl/exceptions/__init__.py +++ b/jutl/exceptions/__init__.py @@ -2,9 +2,13 @@ # classes and functions are usable at # 'from jutl.exceptions import _' level. from .emptypipeline import * +from .emptyqueue import * +from .emptystack import * +from .fullqueue import * +from .fullstack import * from .invalidformatting import * from .missinginput import * # Only show functions specified in # submodule files to the outside world. -__all__ = emptypipeline.__all__, invalidformatting.__all__, missinginput.__all__ +__all__ = emptypipeline.__all__, emptyqueue.__all__, emptystack.__all__, fullqueue.__all__, fullstack.__all__, invalidformatting.__all__, missinginput.__all__ diff --git a/jutl/exceptions/emptypipeline.py b/jutl/exceptions/emptypipeline.py index 166758c..6bd87b7 100644 --- a/jutl/exceptions/emptypipeline.py +++ b/jutl/exceptions/emptypipeline.py @@ -1,8 +1,8 @@ # Module imports +from jutlexception import JutilsException # External class visibility __all__ = ['EmptyPipelineError'] -class EmptyPipelineError(Exception): - def __init__(self, message): - self.message = message \ No newline at end of file +class EmptyPipelineError(JutilsException): + pass \ No newline at end of file diff --git a/jutl/exceptions/emptyqueue.py b/jutl/exceptions/emptyqueue.py new file mode 100644 index 0000000..08dd275 --- /dev/null +++ b/jutl/exceptions/emptyqueue.py @@ -0,0 +1,8 @@ +# Module imports +from jutlexception import JutilsException + +# External class visibility +__all__ = ['EmptyQueueError'] + +class EmptyQueueError(JutilsException): + pass \ No newline at end of file diff --git a/jutl/exceptions/emptystack.py b/jutl/exceptions/emptystack.py new file mode 100644 index 0000000..3c8c1ef --- /dev/null +++ b/jutl/exceptions/emptystack.py @@ -0,0 +1,8 @@ +# Module imports +from jutlexception import JutilsException + +# External class visibility +__all__ = ['EmptyStackError'] + +class EmptyStackError(JutilsException): + pass \ No newline at end of file diff --git a/jutl/exceptions/fullqueue.py b/jutl/exceptions/fullqueue.py new file mode 100644 index 0000000..0e87d37 --- /dev/null +++ b/jutl/exceptions/fullqueue.py @@ -0,0 +1,8 @@ +# Module imports +from jutlexception import JutilsException + +# External class visibility +__all__ = ['FullQueueError'] + +class FullQueueError(JutilsException): + pass \ No newline at end of file diff --git a/jutl/exceptions/fullstack.py b/jutl/exceptions/fullstack.py new file mode 100644 index 0000000..67fec41 --- /dev/null +++ b/jutl/exceptions/fullstack.py @@ -0,0 +1,8 @@ +# Module imports +from jutlexception import JutilsException + +# External class visibility +__all__ = ['FullStackError'] + +class FullStackError(JutilsException): + pass \ No newline at end of file diff --git a/jutl/exceptions/invalidformatting.py b/jutl/exceptions/invalidformatting.py index 1916473..f3df4f6 100644 --- a/jutl/exceptions/invalidformatting.py +++ b/jutl/exceptions/invalidformatting.py @@ -1,8 +1,8 @@ # Module imports +from jutlexception import JutilsException # External class visibility __all__ = ['InvalidFormattingError'] -class InvalidFormattingError(Exception): - def __init__(self, message): - self.message = message \ No newline at end of file +class InvalidFormattingError(JutilsException): + pass \ No newline at end of file diff --git a/jutl/exceptions/missinginput.py b/jutl/exceptions/missinginput.py index fba6760..8d4f48b 100644 --- a/jutl/exceptions/missinginput.py +++ b/jutl/exceptions/missinginput.py @@ -1,8 +1,8 @@ # Module imports +from jutlexception import JutilsException # External class visibility __all__ = ['MissingInputError'] -class MissingInputError(Exception): - def __init__(self, message): - self.message = message \ No newline at end of file +class MissingInputError(JutilsException): + pass \ No newline at end of file From aa7a963b3bac56a576dd383bbf256d2ad9e525ba Mon Sep 17 00:00:00 2001 From: Jordan Welsman Date: Wed, 8 Mar 2023 01:11:51 -0600 Subject: [PATCH 04/12] feat: Implemented queue datastructure. --- jutl/datastructures/__init__.py | 1 + jutl/datastructures/queue.py | 195 ++++++++++++++++++++++++++++++++ 2 files changed, 196 insertions(+) create mode 100644 jutl/datastructures/queue.py diff --git a/jutl/datastructures/__init__.py b/jutl/datastructures/__init__.py index b86d638..8a59d98 100644 --- a/jutl/datastructures/__init__.py +++ b/jutl/datastructures/__init__.py @@ -1,6 +1,7 @@ # Import submodule files so # classes and functions are usable at # 'from jutl.datastructures import _' level. +from .queue import * from .stack import * # Only show functions specified in diff --git a/jutl/datastructures/queue.py b/jutl/datastructures/queue.py new file mode 100644 index 0000000..59f28fc --- /dev/null +++ b/jutl/datastructures/queue.py @@ -0,0 +1,195 @@ +# Module imports +from __future__ import annotations +from typing import Any +from jutl.exceptions import EmptyQueueError, FullQueueError +from jutl.formatting import apply + +# External class visibility +__all__ = ['Queue'] + + +class Queue(object): + """ + Class which implements a + first-in-first-out queue object + with queue methods. + """ + def __init__(self, name: str = None, capacity: int = None) -> None: + "Initialization method." + self.name: str = name + self._queue : list = [] + self._capacity: int = capacity + + def __repr__(self) -> str: + """ + Tells the interpreter how + to represent this class. + """ + string = f"{self.__class__.__name__}(" + if self.name is not None: + string += f"{self.name}" + if len(self._queue) > 0: + string += f", " if self.name is not None else "" + string += f"{len(self._queue)}" + if self._capacity is not None: + string += f"/" if len(self) > 0 else ", " if self.name is not None else "" + string += f"{self._capacity}" + string += ")" + return string + + def __call__(self) -> list: + """ + Tells the interpreter what to + do when an object of this + class is called directly. + """ + return self._queue + + def __len__(self) -> int: + """ + Tells the interpreter what to + consider this class' length. + """ + return len(self._queue) + + def __iter__(self) -> iter: + """ + Tells the interpreter what to + iterate over when iterator methods + are called on this class. + """ + raise NotImplementedError("Queues are not iterable.") + + def __eq__(self, other) -> bool: + """ + Tells the interpreter how this class + handles equal operators. + """ + pass + + def __ne__(self, other) -> bool: + """ + Tells the interpreter how this class + handles not equal operators. + """ + pass + + def __gt__(self, other) -> bool: + """ + Tells the interpreter how this class + handles greater than operators. + """ + pass + + def __ge__(self, other) -> bool: + """ + Tells the interpreter how this class + handles greater or equal operators. + """ + pass + + def __lt__(self, other) -> bool: + """ + Tells the interpreter how this class + handles less than operators. + """ + pass + + def __le__(self, other) -> bool: + """ + Tells the interpreter how this class + handles less than or equal operators. + """ + pass + + def __add__(self, other) -> list: + """ + Tells the interpreter how to sum these objects. + """ + pass + + + def enqueue(self, *args: object) -> None: + """ + Adds an item to the back of the queue. + """ + for item in args: + if self.is_full: + raise FullQueueError("The queue is full.") + else: + self._queue.append(item) + + + def dequeue(self) -> Any: + """ + Removes the item from at front of the queue. + """ + if self.is_empty: + raise EmptyQueueError("The queue is empty.") + else: + dequeued = self.front + self._queue.pop(0) + return dequeued + + + def clear(self) -> None: + """ + Clears the queue. + """ + self._queue.clear() + + + @property + def front(self) -> Any: + """ + Returns the item at the front of + the queue without dequeueing it. + """ + if self.is_empty: + raise EmptyQueueError("The queue is empty.") + else: + return self._queue[0] + + + @property + def rear(self) -> Any: + """ + Returns the item at the rear of + the queue without dequeueing it. + """ + if self.is_empty: + raise EmptyQueueError("The queue is empty.") + else: + return self._queue[-1] + + + @property + def is_empty(self) -> bool: + """ + Returns whether the queue is empty. + """ + if len(self) <= 0: + return True + else: + return False + + + @property + def is_full(self) -> bool: + """ + Returns whether the queue is full. + """ + if len(self) >= self._capacity: + return True + else: + return False + + + def extend(self, other: Queue) -> Queue: + """ + Extends this queue with another queue. + """ + for item in other._queue: + self.enqueue(item) + return self + \ No newline at end of file From d0aa6e373a1fd791316bb9df915125ccc5ed7aa8 Mon Sep 17 00:00:00 2001 From: Jordan Welsman Date: Wed, 8 Mar 2023 01:12:29 -0600 Subject: [PATCH 05/12] fix: Fixed some issues discovered during queue implementation. --- jutl/datastructures/stack.py | 82 ++++++++++++++++++++++++++++++------ 1 file changed, 68 insertions(+), 14 deletions(-) diff --git a/jutl/datastructures/stack.py b/jutl/datastructures/stack.py index 666349f..41a2392 100644 --- a/jutl/datastructures/stack.py +++ b/jutl/datastructures/stack.py @@ -1,5 +1,7 @@ # Module imports from __future__ import annotations +from typing import Any +from jutl.exceptions import EmptyStackError, FullStackError from jutl.formatting import apply # External class visibility @@ -9,13 +11,14 @@ class Stack(object): """ Class which implements a - first-in; first-out stack object + first-in-last-out stack object with stack methods. """ - def __init__(self, name: str = None): + def __init__(self, name: str = None, capacity: int = None) -> None: "Initialization method." - self.name = name - self._stack = [] + self.name: str = name + self._stack: list = [] + self._capacity: int = capacity def __repr__(self) -> str: """ @@ -106,29 +109,80 @@ def __add__(self, other) -> list: return self.extend(other=other) - def push(self, *args: object): + def push(self, *args: object) -> None: """ Pushes an item onto the stack. """ for item in args: - self._stack.append(item) + if self.is_full: + raise FullStackError("The stack is full.") + else: + self._stack.append(item) - def pop(self) -> object: + def pop(self) -> Any: """ - Removes the last added item from the stack. + Removes the item at the top of the stack. """ - popped = self.top - self._stack.remove(self.top) - return popped + if self.is_empty: + raise EmptyStackError("The stack is empty.") + else: + popped = self.top + self._stack.pop(-1) + return popped + + + def clear(self) -> None: + """ + Clears the stack. + """ + self._stack.clear() @property - def top(self) -> object: - if len(self) > 0: + def top(self) -> Any: + """ + Returns the item at the top of + the stack without popping it. + """ + if self.is_empty: + raise EmptyStackError("The stack is empty.") + else: return self._stack[-1] + + + @property + def bottom(self) -> Any: + """ + Returns the item at the bottom + of the stack without popping it. + """ + if self.is_empty: + raise EmptyStackError("The stack is empty.") + else: + return self._stack[0] + + + @property + def is_empty(self) -> bool: + """ + Returns whether the stack is empty. + """ + if len(self) == 0: + return True + else: + return False + + + @property + def is_full(self) -> bool: + """ + Returns whether the stack is full. + """ + if len(self) >= self._capacity: + return True else: - raise IndexError("Stack is empty.") + return False def extend(self, other: Stack) -> Stack: From 175777ca5db67d7eac363178ba079de77c2e285b Mon Sep 17 00:00:00 2001 From: Jordan Welsman Date: Wed, 8 Mar 2023 01:15:22 -0600 Subject: [PATCH 06/12] build: Added JutilsException to exceptions/__init__.py. --- jutl/exceptions/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/jutl/exceptions/__init__.py b/jutl/exceptions/__init__.py index a6d92bf..912020e 100644 --- a/jutl/exceptions/__init__.py +++ b/jutl/exceptions/__init__.py @@ -7,8 +7,9 @@ from .fullqueue import * from .fullstack import * from .invalidformatting import * +from .jutlexception import * from .missinginput import * # Only show functions specified in # submodule files to the outside world. -__all__ = emptypipeline.__all__, emptyqueue.__all__, emptystack.__all__, fullqueue.__all__, fullstack.__all__, invalidformatting.__all__, missinginput.__all__ +__all__ = emptypipeline.__all__, emptyqueue.__all__, emptystack.__all__, fullqueue.__all__, fullstack.__all__, invalidformatting.__all__, jutlexception.__all__, missinginput.__all__ From d2bc005f8b751132be4106d72b7723b798757ee8 Mon Sep 17 00:00:00 2001 From: Jordan Welsman Date: Wed, 8 Mar 2023 01:39:16 -0600 Subject: [PATCH 07/12] fix: Fixed circular imports by using relative import. --- jutl/exceptions/__init__.py | 3 +-- jutl/exceptions/emptypipeline.py | 2 +- jutl/exceptions/emptyqueue.py | 2 +- jutl/exceptions/emptystack.py | 2 +- jutl/exceptions/fullqueue.py | 2 +- jutl/exceptions/fullstack.py | 2 +- jutl/exceptions/invalidformatting.py | 2 +- jutl/exceptions/missinginput.py | 2 +- 8 files changed, 8 insertions(+), 9 deletions(-) diff --git a/jutl/exceptions/__init__.py b/jutl/exceptions/__init__.py index 912020e..a6d92bf 100644 --- a/jutl/exceptions/__init__.py +++ b/jutl/exceptions/__init__.py @@ -7,9 +7,8 @@ from .fullqueue import * from .fullstack import * from .invalidformatting import * -from .jutlexception import * from .missinginput import * # Only show functions specified in # submodule files to the outside world. -__all__ = emptypipeline.__all__, emptyqueue.__all__, emptystack.__all__, fullqueue.__all__, fullstack.__all__, invalidformatting.__all__, jutlexception.__all__, missinginput.__all__ +__all__ = emptypipeline.__all__, emptyqueue.__all__, emptystack.__all__, fullqueue.__all__, fullstack.__all__, invalidformatting.__all__, missinginput.__all__ diff --git a/jutl/exceptions/emptypipeline.py b/jutl/exceptions/emptypipeline.py index 6bd87b7..4692fa2 100644 --- a/jutl/exceptions/emptypipeline.py +++ b/jutl/exceptions/emptypipeline.py @@ -1,5 +1,5 @@ # Module imports -from jutlexception import JutilsException +from .jutlexception import JutilsException # External class visibility __all__ = ['EmptyPipelineError'] diff --git a/jutl/exceptions/emptyqueue.py b/jutl/exceptions/emptyqueue.py index 08dd275..3235f08 100644 --- a/jutl/exceptions/emptyqueue.py +++ b/jutl/exceptions/emptyqueue.py @@ -1,5 +1,5 @@ # Module imports -from jutlexception import JutilsException +from .jutlexception import JutilsException # External class visibility __all__ = ['EmptyQueueError'] diff --git a/jutl/exceptions/emptystack.py b/jutl/exceptions/emptystack.py index 3c8c1ef..04b6a14 100644 --- a/jutl/exceptions/emptystack.py +++ b/jutl/exceptions/emptystack.py @@ -1,5 +1,5 @@ # Module imports -from jutlexception import JutilsException +from .jutlexception import JutilsException # External class visibility __all__ = ['EmptyStackError'] diff --git a/jutl/exceptions/fullqueue.py b/jutl/exceptions/fullqueue.py index 0e87d37..62352e1 100644 --- a/jutl/exceptions/fullqueue.py +++ b/jutl/exceptions/fullqueue.py @@ -1,5 +1,5 @@ # Module imports -from jutlexception import JutilsException +from .jutlexception import JutilsException # External class visibility __all__ = ['FullQueueError'] diff --git a/jutl/exceptions/fullstack.py b/jutl/exceptions/fullstack.py index 67fec41..1847489 100644 --- a/jutl/exceptions/fullstack.py +++ b/jutl/exceptions/fullstack.py @@ -1,5 +1,5 @@ # Module imports -from jutlexception import JutilsException +from .jutlexception import JutilsException # External class visibility __all__ = ['FullStackError'] diff --git a/jutl/exceptions/invalidformatting.py b/jutl/exceptions/invalidformatting.py index f3df4f6..9821dd7 100644 --- a/jutl/exceptions/invalidformatting.py +++ b/jutl/exceptions/invalidformatting.py @@ -1,5 +1,5 @@ # Module imports -from jutlexception import JutilsException +from .jutlexception import JutilsException # External class visibility __all__ = ['InvalidFormattingError'] diff --git a/jutl/exceptions/missinginput.py b/jutl/exceptions/missinginput.py index 8d4f48b..8dfb7dd 100644 --- a/jutl/exceptions/missinginput.py +++ b/jutl/exceptions/missinginput.py @@ -1,5 +1,5 @@ # Module imports -from jutlexception import JutilsException +from .jutlexception import JutilsException # External class visibility __all__ = ['MissingInputError'] From 3ee6f576d6067394a3f24a3a20379c4b26965d41 Mon Sep 17 00:00:00 2001 From: Jordan Welsman Date: Wed, 8 Mar 2023 01:44:59 -0600 Subject: [PATCH 08/12] fix: Fixed None capacity failing tests. --- jutl/datastructures/queue.py | 7 +++++-- jutl/datastructures/stack.py | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/jutl/datastructures/queue.py b/jutl/datastructures/queue.py index 59f28fc..620510e 100644 --- a/jutl/datastructures/queue.py +++ b/jutl/datastructures/queue.py @@ -179,8 +179,11 @@ def is_full(self) -> bool: """ Returns whether the queue is full. """ - if len(self) >= self._capacity: - return True + if self._capacity is not None: + if len(self) >= self._capacity: + return True + else: + return False else: return False diff --git a/jutl/datastructures/stack.py b/jutl/datastructures/stack.py index 41a2392..46e1d93 100644 --- a/jutl/datastructures/stack.py +++ b/jutl/datastructures/stack.py @@ -179,8 +179,11 @@ def is_full(self) -> bool: """ Returns whether the stack is full. """ - if len(self) >= self._capacity: - return True + if self._capacity is not None: + if len(self) >= self._capacity: + return True + else: + return False else: return False From 4f42b987572a8898966f66b022e45fe974a14446 Mon Sep 17 00:00:00 2001 From: Jordan Welsman Date: Wed, 8 Mar 2023 01:49:39 -0600 Subject: [PATCH 09/12] feat: Finished dunder methods. --- jutl/datastructures/queue.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/jutl/datastructures/queue.py b/jutl/datastructures/queue.py index 620510e..4cb04b2 100644 --- a/jutl/datastructures/queue.py +++ b/jutl/datastructures/queue.py @@ -65,48 +65,48 @@ def __eq__(self, other) -> bool: Tells the interpreter how this class handles equal operators. """ - pass + return self._queue == other._queue def __ne__(self, other) -> bool: """ Tells the interpreter how this class handles not equal operators. """ - pass + return self._queue != other._queue def __gt__(self, other) -> bool: """ Tells the interpreter how this class handles greater than operators. """ - pass + return len(self) > len(other) def __ge__(self, other) -> bool: """ Tells the interpreter how this class handles greater or equal operators. """ - pass + return len(self) >= len(other) def __lt__(self, other) -> bool: """ Tells the interpreter how this class handles less than operators. """ - pass + return len(self) < len(other) def __le__(self, other) -> bool: """ Tells the interpreter how this class handles less than or equal operators. """ - pass + return len(self) <= len(other) def __add__(self, other) -> list: """ Tells the interpreter how to sum these objects. """ - pass + return self.extend(other=other) def enqueue(self, *args: object) -> None: From 46aadf06a04c853e4eb8b1e9118ffc1052c0abd8 Mon Sep 17 00:00:00 2001 From: Jordan Welsman Date: Wed, 8 Mar 2023 02:10:51 -0600 Subject: [PATCH 10/12] fix: Added whitespace between classes. --- test/datastructures/test_stack.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/datastructures/test_stack.py b/test/datastructures/test_stack.py index 131c65a..6ee197c 100644 --- a/test/datastructures/test_stack.py +++ b/test/datastructures/test_stack.py @@ -1,6 +1,5 @@ # jutils/test/datastructures/test_stack.py from jutl.datastructures import Stack -from time import sleep test_name = "Test name" test_item = "Item" @@ -21,7 +20,8 @@ def test_name(self): stack = Stack(test_name) assert stack.name == test_name del(stack) - + + class TestDunder(): def test_repr(self): "Tests what is output for representation." From c14b84d57a71be46834aa59bbdc9af8387944253 Mon Sep 17 00:00:00 2001 From: Jordan Welsman Date: Wed, 8 Mar 2023 02:11:13 -0600 Subject: [PATCH 11/12] test: Completed writing tests for queue class. --- test/datastructures/test_queue.py | 226 ++++++++++++++++++++++++++++++ 1 file changed, 226 insertions(+) create mode 100644 test/datastructures/test_queue.py diff --git a/test/datastructures/test_queue.py b/test/datastructures/test_queue.py new file mode 100644 index 0000000..9cf7901 --- /dev/null +++ b/test/datastructures/test_queue.py @@ -0,0 +1,226 @@ +# jutils/test/datastructures/test_queue.py +from jutl.datastructures import Queue + +test_name = "Test name" +test_item = "Item" + +class TestInit(): + def test_def(self): + "Tests if an object can be created from the Queue class." + queue = Queue() + assert isinstance(queue, Queue) + del(queue) + + def test_name(self): + "Tests if naming a queue works." + queue = Queue() + assert queue.name is None + del(queue) + + queue = Queue(test_name) + assert queue.name == test_name + del(queue) + + +class TestDunder(): + def test_repr(self): + "Tests what is output for representation." + queue = Queue() + assert repr(queue) == f"Queue()" + del(queue) + + queue = Queue(test_name) + assert repr(queue) == f"Queue({test_name})" + del(queue) + + queue = Queue() + queue.enqueue(test_item, test_item, test_item) + assert repr(queue) == f"Queue(3)" + del(queue) + + queue = Queue(capacity=10) + assert repr(queue) == f"Queue(10)" + del(queue) + + queue = Queue(test_name) + queue.enqueue(test_item, test_item, test_item) + assert repr(queue) == f"Queue({test_name}, 3)" + del(queue) + + queue = Queue(test_name, 10) + assert repr(queue) == f"Queue({test_name}, 10)" + del(queue) + + queue = Queue(test_name, 10) + queue.enqueue(test_item, test_item, test_item) + assert repr(queue) == f"Queue({test_name}, 3/10)" + del(queue) + + + def test_len(self): + "Tests what is output for object length." + queue = Queue() + assert len(queue) == 0 + del(queue) + + queue = Queue() + queue.enqueue(test_item) + assert len(queue) == 1 + del(queue) + + queue = Queue() + queue.enqueue(test_item) + queue.enqueue(test_item) + assert len(queue) == 2 + del(queue) + + queue = Queue() + queue.enqueue(test_item, test_item, test_item) + assert len(queue) == 3 + del(queue) + + def test_equal(self): + "Tests the overridden equal function." + stack1 = Queue() + stack1.enqueue(test_item, test_item, test_item) + stack2 = Queue() + stack2.enqueue(test_item, test_item, test_item) + assert stack1 == stack2 + del(stack1) + del(stack2) + + def test_not_equal(self): + "Tests the overridden not equal function." + stack1 = Queue() + stack2 = Queue() + stack1.enqueue(test_item, test_item, test_item) + stack2.enqueue(test_item, test_item) + assert stack1 != stack2 + del(stack1) + del(stack2) + + def test_greater_than(self): + "Tests the overridden greater than function." + stack1 = Queue() + stack2 = Queue() + stack1.enqueue(test_item, test_item, test_item) + stack2.enqueue(test_item, test_item) + assert stack1 > stack2 + del(stack1) + del(stack2) + + def test_greater_equal(self): + "Tests the overridden greater or equal function." + stack1 = Queue() + stack2 = Queue() + stack1.enqueue(test_item, test_item, test_item) + stack2.enqueue(test_item, test_item) + assert stack1 >= stack2 + del(stack1) + del(stack2) + + stack1 = Queue() + stack2 = Queue() + stack1.enqueue(test_item, test_item, test_item) + stack2.enqueue(test_item, test_item, test_item) + assert stack1 >= stack2 + del(stack1) + del(stack2) + + def test_less_than(self): + "Tests the overridden less than function." + stack1 = Queue() + stack2 = Queue() + stack1.enqueue(test_item, test_item, test_item) + stack2.enqueue(test_item, test_item) + assert stack2 < stack1 + del(stack1) + del(stack2) + + def test_less_equal(self): + "Tests the overridden less or equal function." + stack1 = Queue() + stack2 = Queue() + stack1.enqueue(test_item, test_item, test_item) + stack2.enqueue(test_item, test_item) + assert stack2 <= stack1 + del(stack1) + del(stack2) + + stack1 = Queue() + stack2 = Queue() + stack1.enqueue(test_item, test_item, test_item) + stack2.enqueue(test_item, test_item, test_item) + assert stack1 <= stack2 + del(stack1) + del(stack2) + + def test_add(self): + "Tests the sum operator." + stack1 = Queue() + stack2 = Queue() + stack3 = Queue() + test_stack = Queue() + stack1.enqueue(test_item, test_item) + stack2.enqueue(test_item, test_item) + stack3.enqueue(test_item, test_item) + test_stack.enqueue(test_item, test_item, test_item, test_item, test_item, test_item) + assert stack1 + stack2 + stack3 == test_stack + del(stack1) + del(stack2) + del(stack3) + del(test_stack) + + +class TestRobustness(): + def test_push(self): + """ + Checks if pushing an + item to a queue works. + """ + queue = Queue() + queue.enqueue(test_item) + assert len(queue) == 1 + del(queue) + + queue = Queue() + queue.enqueue(test_item) + queue.enqueue(test_item) + assert len(queue) == 2 + del(queue) + + queue = Queue() + queue.enqueue(test_item, test_item, test_item) + assert len(queue) == 3 + del(queue) + + def test_pop(self): + """ + Checks if popping an + item from a queue works. + """ + queue = Queue() + queue.enqueue(test_item, test_item, test_item) + assert queue.dequeue() == test_item + assert len(queue) == 2 + del(queue) + + queue = Queue() + queue.enqueue(test_item) + assert queue.dequeue() == test_item + assert len(queue) == 0 + del(queue) + + def test_extend(self): + """ + Checks if the extend + method works correctly. + """ + stack1 = Queue() + stack2 = Queue() + test_stack = Queue() + stack1.enqueue(test_item, test_item) + stack2.enqueue(test_item, test_item) + test_stack.enqueue(test_item, test_item, test_item, test_item) + assert stack1.extend(stack2) == test_stack + From d26924ffbb0c3164cb0fdfe5647183e51d586b10 Mon Sep 17 00:00:00 2001 From: Jordan Welsman Date: Wed, 8 Mar 2023 02:18:39 -0600 Subject: [PATCH 12/12] test: Implemented tests for new exceptions. --- test/exceptions/test_emptyqueue.py | 12 ++++++++++++ test/exceptions/test_emptystack.py | 12 ++++++++++++ test/exceptions/test_fullqueue.py | 12 ++++++++++++ test/exceptions/test_fullstack.py | 12 ++++++++++++ 4 files changed, 48 insertions(+) create mode 100644 test/exceptions/test_emptyqueue.py create mode 100644 test/exceptions/test_emptystack.py create mode 100644 test/exceptions/test_fullqueue.py create mode 100644 test/exceptions/test_fullstack.py diff --git a/test/exceptions/test_emptyqueue.py b/test/exceptions/test_emptyqueue.py new file mode 100644 index 0000000..54858d5 --- /dev/null +++ b/test/exceptions/test_emptyqueue.py @@ -0,0 +1,12 @@ +#jutils/test/exceptions/test_emptyqueue.py +from jutl.exceptions import EmptyQueueError + +test_message = "Test message" +def func(): + raise EmptyQueueError(test_message) + +class TestException(): + def test_message(self): + "Tests if the exception message is correct." + exception = EmptyQueueError(test_message) + assert exception.message == test_message diff --git a/test/exceptions/test_emptystack.py b/test/exceptions/test_emptystack.py new file mode 100644 index 0000000..d02e27a --- /dev/null +++ b/test/exceptions/test_emptystack.py @@ -0,0 +1,12 @@ +#jutils/test/exceptions/test_emptyqueue.py +from jutl.exceptions import EmptyStackError + +test_message = "Test message" +def func(): + raise EmptyStackError(test_message) + +class TestException(): + def test_message(self): + "Tests if the exception message is correct." + exception = EmptyStackError(test_message) + assert exception.message == test_message diff --git a/test/exceptions/test_fullqueue.py b/test/exceptions/test_fullqueue.py new file mode 100644 index 0000000..dc58412 --- /dev/null +++ b/test/exceptions/test_fullqueue.py @@ -0,0 +1,12 @@ +#jutils/test/exceptions/test_emptyqueue.py +from jutl.exceptions import FullQueueError + +test_message = "Test message" +def func(): + raise FullQueueError(test_message) + +class TestException(): + def test_message(self): + "Tests if the exception message is correct." + exception = FullQueueError(test_message) + assert exception.message == test_message diff --git a/test/exceptions/test_fullstack.py b/test/exceptions/test_fullstack.py new file mode 100644 index 0000000..cfad59a --- /dev/null +++ b/test/exceptions/test_fullstack.py @@ -0,0 +1,12 @@ +#jutils/test/exceptions/test_emptyqueue.py +from jutl.exceptions import FullStackError + +test_message = "Test message" +def func(): + raise FullStackError(test_message) + +class TestException(): + def test_message(self): + "Tests if the exception message is correct." + exception = FullStackError(test_message) + assert exception.message == test_message