Skip to content
Snippets Groups Projects
Unverified Commit e3ca0cb3 authored by Frank Sauerburger's avatar Frank Sauerburger
Browse files

Add unit tests for merge and intersect

The added unit testes fail currently. There is an issue with the exception
handling if invalid arguments are passed to the methods (see #2).

This closes #4.
parent 771cace9
No related branches found
No related tags found
1 merge request!2Resolve "Create unittests"
image: python:3
before_script:
- pip install doxec numpy
- python3 setup.py install
stages:
- test
unittest:
state: test
script:
- python3 setup.py test
README:
stage: test
script:
- pip install doxec numpy
- python3 setup.py install
- doxec README.md
......@@ -11,5 +11,6 @@ module1 = Extension('sortednp', language='c++',
setup (name = 'sortednp',
version = '0.0.0',
install_requires=['numpy'],
test_suite='tests',
description = 'Merge and intersect sorted numpy arrays.',
ext_modules = [module1])
from abc import ABCMeta, abstractmethod
import unittest
import numpy as np
import sortednp as snp
class IntersectBase(metaclass=ABCMeta):
"""
Define general test cases for the intersect method. Sub-classes need to
implement have to overwrite the dtype method.
"""
@abstractmethod
def get_dtype(self):
"""
Returns the numpy data type, which should be used for all tests.
"""
pass
def test_simple_middle(self):
"""
Check that intersect returns only element, which are present in both
non-empty input arrays. The common elements are neither at the
beginning nor at the end of the arrays.
"""
a = np.array([2, 3, 6, 7, 9], dtype=self.get_dtype())
b = np.array([1, 3, 7, 8, 10], dtype=self.get_dtype())
i = snp.intersect(a, b)
self.assertEqual(list(i), [3, 7])
self.assertEqual(i.dtype, self.get_dtype())
def test_simple_end_single(self):
"""
Check that intersect returns only element, which are present in both
non-empty input arrays. One common element is at the end of one array.
"""
a = np.array([2, 3, 6, 7, 9], dtype=self.get_dtype())
b = np.array([1, 3, 7, 9, 10], dtype=self.get_dtype())
i = snp.intersect(a, b)
self.assertEqual(list(i), [3, 7, 9])
self.assertEqual(i.dtype, self.get_dtype())
def test_simple_end_both(self):
"""
Check that intersect returns only element, which are present in both
non-empty input arrays. One common element is at the end of both
arrays.
"""
a = np.array([2, 3, 6, 7, 9], dtype=self.get_dtype())
b = np.array([1, 3, 7, 9], dtype=self.get_dtype())
i = snp.intersect(a, b)
self.assertEqual(list(i), [3, 7, 9])
self.assertEqual(i.dtype, self.get_dtype())
def test_simple_begining_single(self):
"""
Check that intersect returns only element, which are present in both
non-empty input arrays. One common element is at the begining of one array.
"""
a = np.array([2, 3, 6, 7, 8], dtype=self.get_dtype())
b = np.array([1, 2, 3, 7, 9, 10], dtype=self.get_dtype())
i = snp.intersect(a, b)
self.assertEqual(list(i), [2, 3, 7])
self.assertEqual(i.dtype, self.get_dtype())
def test_simple_begining_both(self):
"""
Check that intersect returns only element, which are present in both
non-empty input arrays. One common element is at the end of both
arrays.
"""
a = np.array([2, 3, 6, 7, 8], dtype=self.get_dtype())
b = np.array([2, 3, 7, 9, 10], dtype=self.get_dtype())
i = snp.intersect(a, b)
self.assertEqual(list(i), [2, 3, 7])
self.assertEqual(i.dtype, self.get_dtype())
def test_empty_intersect(self):
"""
Check that intersect returns an empty array, if the non-empty inputs
do not have any elements in common.
"""
a = np.array([1, 3, 5, 10], dtype=self.get_dtype())
b = np.array([2, 4, 7, 8, 20], dtype=self.get_dtype())
i = snp.intersect(a, b)
self.assertEqual(list(i), [])
self.assertEqual(len(i), 0)
self.assertEqual(i.dtype, self.get_dtype())
def test_empty_input_single(self):
"""
Check that intersect returns an empty array, if one of the input arrays
is empty.
"""
a = np.array([], dtype=self.get_dtype())
b = np.array([2, 4, 7, 8, 20], dtype=self.get_dtype())
i = snp.intersect(a, b)
self.assertEqual(list(i), [])
self.assertEqual(len(i), 0)
self.assertEqual(i.dtype, self.get_dtype())
i = snp.intersect(b, a)
self.assertEqual(list(i), [])
self.assertEqual(len(i), 0)
self.assertEqual(i.dtype, self.get_dtype())
def test_empty_input_both(self):
"""
Check that intersect returns an empty array, both input arrays are
empty.
"""
a = np.array([], dtype=self.get_dtype())
b = np.array([], dtype=self.get_dtype())
i = snp.intersect(a, b)
self.assertEqual(list(i), [])
self.assertEqual(len(i), 0)
self.assertEqual(i.dtype, self.get_dtype())
def test_contained(self):
"""
Check that intersect returns the common elements, if one array is
contained in the other.
"""
a = np.array([1, 2, 3, 4, 5, 6, 7, 8], dtype=self.get_dtype())
b = np.array([4, 5, 7], dtype=self.get_dtype())
i = snp.intersect(a, b)
self.assertEqual(list(i), [4, 5, 7])
self.assertEqual(i.dtype, self.get_dtype())
i = snp.intersect(b, a)
self.assertEqual(list(i), [4, 5, 7])
self.assertEqual(i.dtype, self.get_dtype())
def test_identical(self):
"""
Check that intersect returns a copy, if the same array is passed in
twice.
"""
a = np.array([3, 4, 6, 8], dtype=self.get_dtype())
i = snp.intersect(a, a)
self.assertEqual(list(i), [3, 4, 6, 8])
self.assertEqual(list(a), [3, 4, 6, 8])
self.assertEqual(i.dtype, self.get_dtype())
i[0] = 1
self.assertEqual(list(a), [3, 4, 6, 8])
def test_separated(self):
"""
Check that intersect returns an empty array, if all elements are
greater than all elements in the other.
"""
a = np.array([1, 2, 3], dtype=self.get_dtype())
b = np.array([4, 6, 8], dtype=self.get_dtype())
i = snp.intersect(a, b)
self.assertEqual(list(i), [])
self.assertEqual(len(i), 0)
self.assertEqual(i.dtype, self.get_dtype())
def test_duplicates_same(self):
"""
Check that duplicates in the same array are dropped if not present in
the other array.
"""
a = np.array([1, 2, 2, 3], dtype=self.get_dtype())
b = np.array([1, 6, 8], dtype=self.get_dtype())
i = snp.intersect(a, b)
self.assertEqual(list(i), [1])
self.assertEqual(i.dtype, self.get_dtype())
a = np.array([1, 2, 2, 3], dtype=self.get_dtype())
b = np.array([1, 2, 4, 6, 8], dtype=self.get_dtype())
i = snp.intersect(a, b)
self.assertEqual(list(i), [1, 2])
self.assertEqual(i.dtype, self.get_dtype())
def test_duplicates_both(self):
"""
Check that duplicates in the same array are kept if they are also
duplicated in the other array.
"""
a = np.array([1, 2, 2, 3], dtype=self.get_dtype())
b = np.array([2, 2, 4, 6, 8], dtype=self.get_dtype())
i = snp.intersect(a, b)
self.assertEqual(list(i), [2, 2])
self.assertEqual(i.dtype, self.get_dtype())
def test_raise_multi_dim(self):
"""
Check that passing in a multi dimensional array raises an exception.
"""
a = np.zeros((10, 2), dtype=self.get_dtype())
b = np.array([2, 3, 5, 6], dtype=self.get_dtype())
self.assertRaises(ValueError, snp.intersect, a, b)
self.assertRaises(ValueError, snp.intersect, b, a)
self.assertRaises(ValueError, snp.intersect, a, a)
def test_raise_non_array(self):
"""
Check that passing in a non-numpy-array raises an exception.
"""
b = np.array([2, 3, 5, 6], dtype=self.get_dtype())
self.assertRaises(TypeError, snp.intersect, 3, b)
self.assertRaises(TypeError, snp.intersect, b, 2)
self.assertRaises(TypeError, snp.intersect, 3, "a")
class IntersectTestCase_Double(IntersectBase, unittest.TestCase):
def get_dtype(self):
return 'float'
from abc import ABCMeta, abstractmethod
import unittest
import numpy as np
import sortednp as snp
class MergeBase(metaclass=ABCMeta):
"""
Define general test cases for the merge method. Sub-classes need to
implement have to overwrite the dtype method.
"""
@abstractmethod
def get_dtype(self):
"""
Returns the numpy data type, which should be used for all tests.
"""
pass
def test_simple(self):
"""
Check that merging two non-empty arrays returns the union of the two
arrays.
"""
a = np.array([1, 3, 7], dtype=self.get_dtype())
b = np.array([2, 5, 6], dtype=self.get_dtype())
m = snp.merge(a, b)
self.assertEqual(list(m), [1, 2, 3, 5, 6, 7])
self.assertEqual(m.dtype, self.get_dtype())
def test_separated(self):
"""
Check that merging two non-empty arrays returns the union of the two
arrays if all element in on array are greater than all elements in the
other. This tests the copy parts of the implementation.
"""
a = np.array([1, 3, 7], dtype=self.get_dtype())
b = np.array([9, 10, 16], dtype=self.get_dtype())
m = snp.merge(a, b)
self.assertEqual(list(m), [1, 3, 7, 9, 10, 16])
self.assertEqual(m.dtype, self.get_dtype())
def test_empty_single(self):
"""
Check that merging two arrays returns a copy of the first one if
the other is empty.
"""
a = np.array([1, 3, 7], dtype=self.get_dtype())
b = np.array([], dtype=self.get_dtype())
m = snp.merge(a, b)
self.assertEqual(list(m), [1, 3, 7])
self.assertEqual(list(a), [1, 3, 7])
self.assertEqual(m.dtype, self.get_dtype())
m[0] = 0
self.assertEqual(list(a), [1, 3, 7])
m = snp.merge(b, a)
self.assertEqual(list(m), [1, 3, 7])
self.assertEqual(list(a), [1, 3, 7])
self.assertEqual(m.dtype, self.get_dtype())
m[0] = 0
self.assertEqual(list(a), [1, 3, 7])
def test_empty_both(self):
"""
Check that merging two empty arrays returns an empty array.
"""
a = np.array([], dtype=self.get_dtype())
b = np.array([], dtype=self.get_dtype())
m = snp.merge(a, b)
self.assertEqual(list(m), [])
self.assertEqual(len(m), 0)
self.assertEqual(m.dtype, self.get_dtype())
def test_identical(self):
"""
Check that merging two identical arrays returns each element twice.
"""
a = np.array([1, 3, 7], dtype=self.get_dtype())
m = snp.merge(a, a)
self.assertEqual(list(m), [1, 1, 3, 3, 7, 7])
self.assertEqual(m.dtype, self.get_dtype())
def test_duplicates_same(self):
"""
Check that duplications in a single array are passed to the result.
"""
a = np.array([1, 3, 3, 7], dtype=self.get_dtype())
b = np.array([2, 5, 6], dtype=self.get_dtype())
m = snp.merge(a, b)
self.assertEqual(list(m), [1, 2, 3, 3, 5, 6, 7])
self.assertEqual(m.dtype, self.get_dtype())
def test_duplicates_other(self):
"""
Check that duplications in the other array are passed to the result.
"""
a = np.array([1, 3, 7], dtype=self.get_dtype())
b = np.array([2, 3, 5, 6], dtype=self.get_dtype())
m = snp.merge(a, b)
self.assertEqual(list(m), [1, 2, 3, 3, 5, 6, 7])
self.assertEqual(m.dtype, self.get_dtype())
def test_duplicates_both(self):
"""
Check that duplications in a single and the other array are both passed to
the result.
"""
a = np.array([1, 3, 3, 7], dtype=self.get_dtype())
b = np.array([2, 3, 5, 6], dtype=self.get_dtype())
m = snp.merge(a, b)
self.assertEqual(list(m), [1, 2, 3, 3, 3, 5, 6, 7])
self.assertEqual(m.dtype, self.get_dtype())
def test_raise_multi_dim(self):
"""
Check that passing in a multi dimensional array raises an exception.
"""
a = np.zeros((10, 2), dtype=self.get_dtype())
b = np.array([2, 3, 5, 6], dtype=self.get_dtype())
self.assertRaises(ValueError, snp.merge, a, b)
self.assertRaises(ValueError, snp.merge, b, a)
self.assertRaises(ValueError, snp.merge, a, a)
def test_raise_non_array(self):
"""
Check that passing in a non-numpy-array raises an exception.
"""
b = np.array([2, 3, 5, 6], dtype=self.get_dtype())
self.assertRaises(TypeError, snp.merge, 3, b)
self.assertRaises(TypeError, snp.merge, b, 2)
self.assertRaises(TypeError, snp.merge, 3, "a")
class MergeTestCase_Double(MergeBase, unittest.TestCase):
def get_dtype(self):
return 'float'
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment