Skip to content

Commit baf5381

Browse files
committed
Added test for backend argument
1 parent f135504 commit baf5381

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

pydatastructs/miscellaneous_data_structures/stack.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,10 @@ class ArrayStack(Stack):
8888

8989
__slots__ = ['items']
9090

91-
def __new__(cls, items=None, dtype=NoneType):
91+
def __new__(cls, items=None, dtype=NoneType,
92+
**kwargs):
93+
raise_if_backend_is_not_python(
94+
cls, kwargs.get('backend', Backend.PYTHON))
9295
if items is None:
9396
items = DynamicOneDimensionalArray(dtype, 0)
9497
else:
@@ -137,7 +140,9 @@ class LinkedListStack(Stack):
137140

138141
__slots__ = ['stack']
139142

140-
def __new__(cls, items=None):
143+
def __new__(cls, items=None, **kwargs):
144+
raise_if_backend_is_not_python(
145+
cls, kwargs.get('backend', Backend.PYTHON))
141146
obj = object.__new__(cls)
142147
obj.stack = SinglyLinkedList()
143148
if items is None:

pydatastructs/trees/heaps.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ class DHeap(Heap):
7474
"""
7575
__slots__ = ['_comp', 'heap', 'd', 'heap_property', '_last_pos_filled']
7676

77-
def __new__(cls, elements=None, heap_property="min", d=4):
77+
def __new__(cls, elements=None, heap_property="min", d=4,
78+
**kwargs):
79+
raise_if_backend_is_not_python(
80+
cls, kwargs.get('backend', Backend.PYTHON))
7881
obj = Heap.__new__(cls)
7982
obj.heap_property = heap_property
8083
obj.d = d
@@ -265,7 +268,10 @@ class BinaryHeap(DHeap):
265268
266269
.. [1] https://en.m.wikipedia.org/wiki/Binary_heap
267270
"""
268-
def __new__(cls, elements=None, heap_property="min"):
271+
def __new__(cls, elements=None, heap_property="min",
272+
**kwargs):
273+
raise_if_backend_is_not_python(
274+
cls, kwargs.get('backend', Backend.PYTHON))
269275
obj = DHeap.__new__(cls, elements, heap_property, 2)
270276
return obj
271277

@@ -329,7 +335,10 @@ class TernaryHeap(DHeap):
329335
.. [1] https://en.wikipedia.org/wiki/D-ary_heap
330336
.. [2] https://ece.uwaterloo.ca/~dwharder/aads/Algorithms/d-ary_heaps/Ternary_heaps/
331337
"""
332-
def __new__(cls, elements=None, heap_property="min"):
338+
def __new__(cls, elements=None, heap_property="min",
339+
**kwargs):
340+
raise_if_backend_is_not_python(
341+
cls, kwargs.get('backend', Backend.PYTHON))
333342
obj = DHeap.__new__(cls, elements, heap_property, 3)
334343
return obj
335344

@@ -369,7 +378,9 @@ class BinomialHeap(Heap):
369378
"""
370379
__slots__ = ['root_list']
371380

372-
def __new__(cls, root_list=None):
381+
def __new__(cls, root_list=None, **kwargs):
382+
raise_if_backend_is_not_python(
383+
cls, kwargs.get('backend', Backend.PYTHON))
373384
if root_list is None:
374385
root_list = []
375386
if not all((_check_type(root, BinomialTree))

pydatastructs/utils/tests/test_code_quality.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os, re, sys, pydatastructs, inspect
2+
from typing import Type
23

34
def _list_files():
45
root_path = os.path.abspath(
@@ -129,10 +130,25 @@ def test_public_api():
129130
_class, method
130131
))
131132

132-
# def test_backend_argument():
133-
# apis = _apis()
134-
# for api in apis:
135-
# try:
136-
# api()
137-
# except ValueError as error:
138-
# assert str(api) in error.args[0]
133+
def test_backend_argument():
134+
135+
def call_and_raise(api, pos_args_count=0):
136+
try:
137+
if pos_args_count == 0:
138+
api(backend=None)
139+
elif pos_args_count == 1:
140+
api(None, backend=None)
141+
elif pos_args_count == 2:
142+
api(None, None, backend=None)
143+
except ValueError as value_error:
144+
assert str(api) in value_error.args[0]
145+
except TypeError as type_error:
146+
max_pos_args_count = 2
147+
if pos_args_count <= max_pos_args_count:
148+
call_and_raise(api, pos_args_count + 1)
149+
else:
150+
raise type_error
151+
152+
apis = _apis()
153+
for api in apis:
154+
call_and_raise(api, 0)

0 commit comments

Comments
 (0)