|
| 1 | +from __future__ import division, absolute_import, print_function |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +from numpy.testing import TestCase |
| 5 | +import sys |
| 6 | + |
| 7 | + |
| 8 | +def is_pypy(): |
| 9 | + return '__pypy__' in sys.builtin_module_names |
| 10 | + |
| 11 | + |
| 12 | +class TestPartition(TestCase): |
| 13 | + pivot = 2 |
| 14 | + pivot_index = 2 |
| 15 | + |
| 16 | + def test_uint16(self): |
| 17 | + arr = np.arange(10, -1, -1, dtype='int16') |
| 18 | + |
| 19 | + if is_pypy(): |
| 20 | + with self.assertRaises(NotImplementedError): |
| 21 | + np.partition(arr, self.pivot_index) |
| 22 | + else: |
| 23 | + partition = np.partition(arr, self.pivot_index) |
| 24 | + self._check_partition(partition, self.pivot_index, self.pivot) |
| 25 | + |
| 26 | + def test_uint32(self): |
| 27 | + arr = np.arange(10, -1, -1, dtype='int32') |
| 28 | + |
| 29 | + partition = np.partition(arr, self.pivot_index) |
| 30 | + |
| 31 | + self._check_partition(partition, self.pivot_index, self.pivot) |
| 32 | + |
| 33 | + def test_uint64(self): |
| 34 | + arr = np.arange(10, -1, -1, dtype='int64') |
| 35 | + |
| 36 | + partition = np.partition(arr, self.pivot_index) |
| 37 | + |
| 38 | + self._check_partition(partition, self.pivot_index, self.pivot) |
| 39 | + |
| 40 | + def _check_partition(self, partition, pivot_index, pivot): |
| 41 | + self.assertTrue(np.all(partition[:pivot_index] < partition[pivot_index])) |
| 42 | + self.assertTrue(np.all(partition[pivot_index:] >= partition[pivot_index])) |
| 43 | + self.assertTrue(partition[pivot_index] == pivot) |
0 commit comments