Commit 84e0507e authored by Xavier Thompson's avatar Xavier Thompson

Clarify cypclass builtin unit tests

parent fc5bba37
...@@ -14,9 +14,9 @@ cdef cypclass Index: ...@@ -14,9 +14,9 @@ cdef cypclass Index:
__init__(self, int i): __init__(self, int i):
self.index = i self.index = i
def test_comp_iteration(): def test_setitem_and_iteration():
""" """
>>> test_comp_iteration() >>> test_setitem_and_iteration()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
""" """
d = cypdict[Index, Value]() d = cypdict[Index, Value]()
...@@ -25,9 +25,9 @@ def test_comp_iteration(): ...@@ -25,9 +25,9 @@ def test_comp_iteration():
return [key.index for key in d] return [key.index for key in d]
def test_nogil_iteration(): def test_nogil_setitem_and_iteration():
""" """
>>> test_nogil_iteration() >>> test_nogil_setitem_and_iteration()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
""" """
indices = [] indices = []
...@@ -43,9 +43,9 @@ def test_nogil_iteration(): ...@@ -43,9 +43,9 @@ def test_nogil_iteration():
return indices return indices
def test_comp_keys_iteration(): def test_setitem_and_keys_iteration():
""" """
>>> test_comp_keys_iteration() >>> test_setitem_and_keys_iteration()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
""" """
d = cypdict[Index, Value]() d = cypdict[Index, Value]()
...@@ -54,9 +54,9 @@ def test_comp_keys_iteration(): ...@@ -54,9 +54,9 @@ def test_comp_keys_iteration():
return [key.index for key in d.keys()] return [key.index for key in d.keys()]
def test_nogil_keys_iteration(): def test_nogil_setitem_and_keys_iteration():
""" """
>>> test_nogil_keys_iteration() >>> test_nogil_setitem_and_keys_iteration()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
""" """
indices = [] indices = []
...@@ -72,9 +72,9 @@ def test_nogil_keys_iteration(): ...@@ -72,9 +72,9 @@ def test_nogil_keys_iteration():
return indices return indices
def test_comp_values_iteration(): def test_setitem_and_values_iteration():
""" """
>>> test_comp_values_iteration() >>> test_setitem_and_values_iteration()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
""" """
d = cypdict[Index, Value]() d = cypdict[Index, Value]()
...@@ -83,9 +83,9 @@ def test_comp_values_iteration(): ...@@ -83,9 +83,9 @@ def test_comp_values_iteration():
return [value.value for value in d.values()] return [value.value for value in d.values()]
def test_nogil_values_iteration(): def test_nogil_setitem_and_values_iteration():
""" """
>>> test_nogil_values_iteration() >>> test_nogil_setitem_and_values_iteration()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
""" """
values = [] values = []
...@@ -101,9 +101,9 @@ def test_nogil_values_iteration(): ...@@ -101,9 +101,9 @@ def test_nogil_values_iteration():
return values return values
def test_comp_items_iteration(): def test_setitem_and_items_iteration():
""" """
>>> test_comp_items_iteration() >>> test_setitem_and_items_iteration()
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9)] [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9)]
""" """
d = cypdict[Index, Value]() d = cypdict[Index, Value]()
...@@ -112,9 +112,9 @@ def test_comp_items_iteration(): ...@@ -112,9 +112,9 @@ def test_comp_items_iteration():
return [(key.index, value.value) for (key, value) in d.items()] return [(key.index, value.value) for (key, value) in d.items()]
def test_nogil_items_iteration(): def test_nogil_setitem_and_items_iteration():
""" """
>>> test_nogil_items_iteration() >>> test_nogil_setitem_and_items_iteration()
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9)] [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9)]
""" """
items = [] items = []
...@@ -130,9 +130,62 @@ def test_nogil_items_iteration(): ...@@ -130,9 +130,62 @@ def test_nogil_items_iteration():
return items return items
def test_getitem_exception(): def test_len():
"""
>>> test_len()
1
"""
d = cypdict[Index, Value]()
cdef long unsigned int nb_elements = 0
for i in range(10):
d[Index(i)] = Value(i)
for k in d:
nb_elements += 1
if d.__len__() != nb_elements:
return 0
if nb_elements != 10:
return 0
return 1
def test_clear():
""" """
>>> test_getitem_exception() >>> test_clear()
1
"""
d = cypdict[Index, Value]()
for i in range(10):
d[Index(i)] = Value(i)
if d.__len__() != 10:
return -1
d.clear()
if d.__len__() != 0:
return 0
return 1
def test_update():
"""
>>> test_update()
1
"""
d1 = cypdict[Index, Value]()
d2 = cypdict[Index, Value]()
d1[Index(1)] = Value(10)
d1[Index(3)] = Value(30)
d2[Index(4)] = Value(40)
d2[Index(2)] = Value(20)
d1.update(d2)
if d1.__len__() != 4:
return 0
for key in d2:
if not key in d1:
return 0
if d2[key] is not d1[key]:
return 0
return 1
def test_nonexistent_getitem_exception():
"""
>>> test_nonexistent_getitem_exception()
'Getting nonexistent item' 'Getting nonexistent item'
1 1
""" """
...@@ -146,9 +199,9 @@ def test_getitem_exception(): ...@@ -146,9 +199,9 @@ def test_getitem_exception():
print(e) print(e)
return 1 return 1
def test_delitem_exception(): def test_nonexistent_delitem_exception():
""" """
>>> test_delitem_exception() >>> test_nonexistent_delitem_exception()
'Deleting nonexistent item' 'Deleting nonexistent item'
1 1
""" """
...@@ -162,9 +215,9 @@ def test_delitem_exception(): ...@@ -162,9 +215,9 @@ def test_delitem_exception():
print(e) print(e)
return 1 return 1
def test_setitem_exception_dict_iterator(): def test_setitem_iterator_invalidation():
""" """
>>> test_setitem_exception_dict_iterator() >>> test_setitem_iterator_invalidation()
Modifying a dictionary with active iterators Modifying a dictionary with active iterators
1 1
""" """
...@@ -179,9 +232,9 @@ def test_setitem_exception_dict_iterator(): ...@@ -179,9 +232,9 @@ def test_setitem_exception_dict_iterator():
print(e) print(e)
return 1 return 1
def test_setitem_exception_dict_keys_iterator(): def test_setitem_keys_iterator_invalidation():
""" """
>>> test_setitem_exception_dict_keys_iterator() >>> test_setitem_keys_iterator_invalidation()
Modifying a dictionary with active iterators Modifying a dictionary with active iterators
1 1
""" """
...@@ -196,9 +249,9 @@ def test_setitem_exception_dict_keys_iterator(): ...@@ -196,9 +249,9 @@ def test_setitem_exception_dict_keys_iterator():
print(e) print(e)
return 1 return 1
def test_setitem_exception_dict_values_iterator(): def test_setitem_values_iterator_invalidation():
""" """
>>> test_setitem_exception_dict_values_iterator() >>> test_setitem_values_iterator_invalidation()
Modifying a dictionary with active iterators Modifying a dictionary with active iterators
1 1
""" """
...@@ -213,9 +266,9 @@ def test_setitem_exception_dict_values_iterator(): ...@@ -213,9 +266,9 @@ def test_setitem_exception_dict_values_iterator():
print(e) print(e)
return 1 return 1
def test_setitem_exception_dict_items_iterator(): def test_setitem_items_iterator_invalidation():
""" """
>>> test_setitem_exception_dict_items_iterator() >>> test_setitem_items_iterator_invalidation()
Modifying a dictionary with active iterators Modifying a dictionary with active iterators
1 1
""" """
...@@ -230,122 +283,94 @@ def test_setitem_exception_dict_items_iterator(): ...@@ -230,122 +283,94 @@ def test_setitem_exception_dict_items_iterator():
print(e) print(e)
return 1 return 1
def test_setitem_after_dict_iterator(): def test_clear_iterator_invalidation():
""" """
>>> test_setitem_after_dict_iterator() >>> test_clear_iterator_invalidation()
Modifying a dictionary with active iterators
1 1
""" """
d = cypdict[Index, Value]() d = cypdict[Index, Value]()
for key in d: iterator = d.begin()
pass
try: try:
with nogil: with nogil:
d[Index()] = Value() d.clear()
with gil: with gil:
return 1 return 0
except RuntimeError as e: except RuntimeError as e:
print(e) print(e)
return 0 return 1
def test_setitem_after_dict_keys_iterator(): def test_modification_after_dict_iterator():
""" """
>>> test_setitem_after_dict_keys_iterator() >>> test_modification_after_dict_iterator()
1 1
""" """
d = cypdict[Index, Value]() d = cypdict[Index, Value]()
for key in d.keys(): for key in d:
pass pass
try: try:
with nogil: with nogil:
d[Index()] = Value() d[Index()] = Value()
d.clear()
with gil: with gil:
return 1 return 1
except RuntimeError as e: except RuntimeError as e:
print(e) print(e)
return 0 return 0
def test_setitem_after_dict_values_iterator(): def test_modification_after_dict_keys_iterator():
""" """
>>> test_setitem_after_dict_values_iterator() >>> test_modification_after_dict_keys_iterator()
1 1
""" """
d = cypdict[Index, Value]() d = cypdict[Index, Value]()
for value in d.values(): for key in d.keys():
pass pass
try: try:
with nogil: with nogil:
d[Index()] = Value() d[Index()] = Value()
d.clear()
with gil: with gil:
return 1 return 1
except RuntimeError as e: except RuntimeError as e:
print(e) print(e)
return 0 return 0
def test_setitem_after_dict_items_iterator(): def test_modification_after_dict_values_iterator():
""" """
>>> test_setitem_after_dict_items_iterator() >>> test_modification_after_dict_values_iterator()
1 1
""" """
d = cypdict[Index, Value]() d = cypdict[Index, Value]()
for item in d.items(): for value in d.values():
pass pass
try: try:
with nogil: with nogil:
d[Index()] = Value() d[Index()] = Value()
d.clear()
with gil: with gil:
return 1 return 1
except RuntimeError as e: except RuntimeError as e:
print(e) print(e)
return 0 return 0
def test_len(): def test_modification_after_dict_items_iterator():
"""
>>> test_len()
1
"""
d = cypdict[Index, Value]()
cdef long unsigned int nb_elements = 0
for i in range(10):
d[Index(i)] = Value(i)
for k in d:
nb_elements += 1
if d.__len__() != nb_elements:
return 0
if nb_elements != 10:
return 0
return 1
def test_clear():
""" """
>>> test_clear() >>> test_modification_after_dict_items_iterator()
1 1
""" """
d = cypdict[Index, Value]() d = cypdict[Index, Value]()
for i in range(10): for item in d.items():
d[Index(i)] = Value(i) pass
if d.__len__() != 10:
return -1
d.clear()
if d.__len__() != 0:
return 0
return 1
def test_clear_exception_dict_iterator():
"""
>>> test_clear_exception_dict_iterator()
Modifying a dictionary with active iterators
1
"""
d = cypdict[Index, Value]()
iterator = d.begin()
try: try:
with nogil: with nogil:
d[Index()] = Value()
d.clear() d.clear()
with gil: with gil:
return 0 return 1
except RuntimeError as e: except RuntimeError as e:
print(e) print(e)
return 1 return 0
def test_scalar_types_dict(): def test_scalar_types_dict():
""" """
...@@ -439,27 +464,6 @@ def test_items_refcount(): ...@@ -439,27 +464,6 @@ def test_items_refcount():
return 0 return 0
return 1 return 1
def test_update():
"""
>>> test_update()
1
"""
d1 = cypdict[Index, Value]()
d2 = cypdict[Index, Value]()
d1[Index(1)] = Value(10)
d1[Index(3)] = Value(30)
d2[Index(4)] = Value(40)
d2[Index(2)] = Value(20)
d1.update(d2)
if d1.__len__() != 4:
return 0
for key in d2:
if not key in d1:
return 0
if d2[key] is not d1[key]:
return 0
return 1
def test_update_refcount(): def test_update_refcount():
""" """
>>> test_update_refcount() >>> test_update_refcount()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment