From 740489d5036936ec819bcebf6b39cb30ec9468d1 Mon Sep 17 00:00:00 2001
From: Frank Sauerburger <frank@sauerburger.com>
Date: Mon, 15 Apr 2019 21:16:12 +0200
Subject: [PATCH] Check for misuse of non-c-contiguous arrays

Implement test cases to check for improper reference count handling in
connection with non-c-contiguous arrays. See #22.
---
 src/sortednp/tests/test_intersect.py | 55 ++++++++++++++++++++++++++++
 src/sortednp/tests/test_merge.py     | 55 ++++++++++++++++++++++++++++
 2 files changed, 110 insertions(+)

diff --git a/src/sortednp/tests/test_intersect.py b/src/sortednp/tests/test_intersect.py
index 6bcaefc..2c98df8 100644
--- a/src/sortednp/tests/test_intersect.py
+++ b/src/sortednp/tests/test_intersect.py
@@ -651,3 +651,58 @@ class ITC_Default_TypeError(ITC_Default, ITC_TypeError, TC): pass
 class ITC_Simple_TypeError(ITC_Simple, ITC_TypeError, TC): pass
 class ITC_Binary_TypeError(ITC_Binary, ITC_TypeError, TC): pass
 class ITC_Galloping_TypeError(ITC_Galloping, ITC_TypeError, TC): pass
+
+class IntersectNonCContiguousTestCase(unittest.TestCase):
+    """
+    Check that intersect works correctly with the issues of non-c-contiguous
+    arrays. See Issue 22,
+    https://gitlab.sauerburger.com/frank/sortednp/issues/22.
+    """
+
+    def test_non_cc_second(self):
+        """
+        Check that using a non-c-contiguous array as the second argument
+        returns the correct value.
+
+        Repeat the test 1000 times because memory issues might be flaky.
+        """
+        for i in range(1000):
+            a = np.array([[1, 2, 3], [3, 4, 5], [7, 9, 10]]) 
+            nonzero_row, nonzero_col = np.nonzero(a)
+            
+            x = np.array([0, 1, 5])
+            y = nonzero_col[0:3]
+            
+            self.assertEqual(list(snp.merge(x, y)), [0, 0, 1, 1, 2, 5])
+
+    def test_non_cc_first(self):
+        """
+        Check that using a non-c-contiguous array as the first argument
+        returns the correct value.
+
+        Repeat the test 1000 times because memory issues might be flaky.
+        """
+        for i in range(1000):
+            a = np.array([[1, 2, 3], [3, 4, 5], [7, 9, 10]]) 
+            nonzero_row, nonzero_col = np.nonzero(a)
+            
+            x = nonzero_col[0:3]
+            y = np.array([0, 1, 5])
+            
+            self.assertEqual(list(snp.merge(x, y)), [0, 0, 1, 1, 2, 5])
+
+    def test_non_cc_both(self):
+        """
+        Check that using a non-c-contiguous array as the both arguments
+        returns the correct value.
+
+        Repeat the test 1000 times because memory issues might be flaky.
+        """
+        for i in range(1000):
+            a = np.array([[1, 2, 3], [3, 4, 5], [7, 9, 10]]) 
+            nonzero_row, nonzero_col = np.nonzero(a)
+            
+            x = nonzero_col[0:3]
+            y = nonzero_col[0:3]
+            
+            self.assertEqual(list(snp.merge(x, y)), [0, 0, 1, 1, 2, 2])
diff --git a/src/sortednp/tests/test_merge.py b/src/sortednp/tests/test_merge.py
index 9b979d5..ef1c317 100644
--- a/src/sortednp/tests/test_merge.py
+++ b/src/sortednp/tests/test_merge.py
@@ -465,3 +465,58 @@ class MergeTestCase_TypeError(unittest.TestCase):
         b = np.array([2, 5, 6], dtype='float64')
 
         self.assertRaises(ValueError, snp.merge, a, b)
+
+class MergeNonCContiguousTestCase(unittest.TestCase):
+    """
+    Check that merge works correctly with the issues of non-c-contiguous
+    arrays. See Issue 22,
+    https://gitlab.sauerburger.com/frank/sortednp/issues/22.
+    """
+
+    def test_non_cc_second(self):
+        """
+        Check that using a non-c-contiguous array as the second argument
+        returns the correct value.
+
+        Repeat the test 1000 times because memory issues might be flaky.
+        """
+        for i in range(1000):
+            a = np.array([[1, 2, 3], [3, 4, 5], [7, 9, 10]]) 
+            nonzero_row, nonzero_col = np.nonzero(a)
+            
+            x = np.array([0, 1, 5])
+            y = nonzero_col[0:3]
+            
+            self.assertEqual(list(snp.merge(x, y)), [0, 0, 1, 1, 2, 5])
+
+    def test_non_cc_first(self):
+        """
+        Check that using a non-c-contiguous array as the first argument
+        returns the correct value.
+
+        Repeat the test 1000 times because memory issues might be flaky.
+        """
+        for i in range(1000):
+            a = np.array([[1, 2, 3], [3, 4, 5], [7, 9, 10]]) 
+            nonzero_row, nonzero_col = np.nonzero(a)
+            
+            x = nonzero_col[0:3]
+            y = np.array([0, 1, 5])
+            
+            self.assertEqual(list(snp.merge(x, y)), [0, 0, 1, 1, 2, 5])
+
+    def test_non_cc_both(self):
+        """
+        Check that using a non-c-contiguous array as the both arguments
+        returns the correct value.
+
+        Repeat the test 1000 times because memory issues might be flaky.
+        """
+        for i in range(1000):
+            a = np.array([[1, 2, 3], [3, 4, 5], [7, 9, 10]]) 
+            nonzero_row, nonzero_col = np.nonzero(a)
+            
+            x = nonzero_col[0:3]
+            y = nonzero_col[0:3]
+
+            self.assertEqual(list(snp.merge(x, y)), [0, 0, 1, 1, 2, 2])
-- 
GitLab