Skip to content

Commit

Permalink
BUG: test_unique needs to test bigger arrays.
Browse files Browse the repository at this point in the history
Small arrays are sorted with insertion sort, which is a stable
sort.  Consequently larger arrays are needed to check that the
sort used is properly stable.

The test was also refactored to make it more compact.
  • Loading branch information
charris committed Jul 11, 2012
1 parent 5a929a4 commit 895ed81
Showing 1 changed file with 31 additions and 37 deletions.
68 changes: 31 additions & 37 deletions numpy/lib/tests/test_arraysetops.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,67 +8,61 @@

import warnings

class TestAso(TestCase):
class TestSetOps(TestCase):


def test_unique( self ):

def check_values(a, b, msg):
def check_all(a, b, i1, i2, dt):
msg = "check values failed for type '%s'" % dt
v = unique(a)
assert_array_equal(v, b, msg)

def check_indexes(a, b, i1, i2, msg):
msg = "check indexes failed for type '%s'" % dt
v, j = unique(a, 1, 0)
assert_array_equal(v, b, msg)
assert_array_equal(j, i1, msg)

msg = "check reverse indexes failed for type '%s'" % dt
v, j = unique(a, 0, 1)
assert_array_equal(v, b, msg)
assert_array_equal(j, i2, msg)

msg = "check with all indexes failed for type '%s'" % dt
v, j1, j2 = unique(a, 1, 1)
assert_array_equal(v, b, msg)
assert_array_equal(j1, i1, msg)
assert_array_equal(j2, i2, msg)

fmt = "Failed for type '%s'"
a = [5, 7, 1, 2, 1, 5, 7]
a = [5, 7, 1, 2, 1, 5, 7]*10
b = [1, 2, 5, 7]
i1 = [2, 3, 0, 1]
i2 = [2, 3, 0, 1, 0, 2, 3]


types = np.typecodes['AllInteger'] + np.typecodes['AllFloat']
i2 = [2, 3, 0, 1, 0, 2, 3]*10

# test for numeric arrays
types = []
types.extend(np.typecodes['AllInteger'])
types.extend(np.typecodes['AllFloat'])
types.append('datetime64[D]')
types.append('timedelta64[D]')
for dt in types:
msg = fmt % dt
aa = np.array(a, dt)
bb = np.array(b, dt)
check_values(aa, bb, msg)
check_indexes(aa, bb, i1, i2, msg)
check_all(aa, bb, i1, i2, dt)

# test for object arrays
msg = fmt % 'O'
aa = np.empty(len(a), 'O')
# test for object arrays
dt = 'O'
aa = np.empty(len(a), dt)
aa[:] = a
bb = np.empty(len(b), 'O')
bb = np.empty(len(b), dt)
bb[:] = b
check_values(aa, bb, msg)
check_indexes(aa, bb, i1, i2, msg)
check_all(aa, bb, i1, i2, dt)

# test for structured arrays
msg = fmt % 'V'
aa = np.array(zip(a,a), [('', 'i'), ('', 'i')])
bb = np.array(zip(b,b), [('', 'i'), ('', 'i')])
check_values(aa, bb, msg)
check_indexes(aa, bb, i1, i2, msg)

# test for datetime64 arrays
msg = fmt % 'M'
aa = np.array(a, 'datetime64[D]')
bb = np.array(b, 'datetime64[D]')
check_values(aa, bb, msg)
check_indexes(aa, bb, i1, i2, msg)

# test for timedelta64 arrays
msg = fmt % 'm'
aa = np.array(a, 'timedelta64[D]')
bb = np.array(b, 'timedelta64[D]')
check_values(aa, bb, msg)
check_indexes(aa, bb, i1, i2, msg)
dt = [('', 'i'), ('', 'i')]
aa = np.array(zip(a,a), dt)
bb = np.array(zip(b,b), dt)
check_all(aa, bb, i1, i2, dt)


def test_intersect1d( self ):
Expand Down

0 comments on commit 895ed81

Please sign in to comment.