MCPcopy Create free account
hub / github.com/numpy/numpy / test_argsort

Method test_argsort

numpy/core/tests/test_multiarray.py:2341–2476  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

2339 dtype=mydtype))
2340
2341 def test_argsort(self):
2342 # all c scalar argsorts use the same code with different types
2343 # so it suffices to run a quick check with one type. The number
2344 # of sorted items must be greater than ~50 to check the actual
2345 # algorithm because quick and merge sort fall over to insertion
2346 # sort for small arrays.
2347
2348 for dtype in [np.int32, np.uint32, np.float32]:
2349 a = np.arange(101, dtype=dtype)
2350 b = a[::-1].copy()
2351 for kind in self.sort_kinds:
2352 msg = "scalar argsort, kind=%s, dtype=%s" % (kind, dtype)
2353 assert_equal(a.copy().argsort(kind=kind), a, msg)
2354 assert_equal(b.copy().argsort(kind=kind), b, msg)
2355
2356 # test complex argsorts. These use the same code as the scalars
2357 # but the compare function differs.
2358 ai = a*1j + 1
2359 bi = b*1j + 1
2360 for kind in self.sort_kinds:
2361 msg = "complex argsort, kind=%s" % kind
2362 assert_equal(ai.copy().argsort(kind=kind), a, msg)
2363 assert_equal(bi.copy().argsort(kind=kind), b, msg)
2364 ai = a + 1j
2365 bi = b + 1j
2366 for kind in self.sort_kinds:
2367 msg = "complex argsort, kind=%s" % kind
2368 assert_equal(ai.copy().argsort(kind=kind), a, msg)
2369 assert_equal(bi.copy().argsort(kind=kind), b, msg)
2370
2371 # test argsort of complex arrays requiring byte-swapping, gh-5441
2372 for endianness in '<>':
2373 for dt in np.typecodes['Complex']:
2374 arr = np.array([1+3.j, 2+2.j, 3+1.j], dtype=endianness + dt)
2375 msg = 'byte-swapped complex argsort, dtype={0}'.format(dt)
2376 assert_equal(arr.argsort(),
2377 np.arange(len(arr), dtype=np.intp), msg)
2378
2379 # test string argsorts.
2380 s = 'aaaaaaaa'
2381 a = np.array([s + chr(i) for i in range(101)])
2382 b = a[::-1].copy()
2383 r = np.arange(101)
2384 rr = r[::-1]
2385 for kind in self.sort_kinds:
2386 msg = "string argsort, kind=%s" % kind
2387 assert_equal(a.copy().argsort(kind=kind), r, msg)
2388 assert_equal(b.copy().argsort(kind=kind), rr, msg)
2389
2390 # test unicode argsorts.
2391 s = 'aaaaaaaa'
2392 a = np.array([s + chr(i) for i in range(101)], dtype=np.str_)
2393 b = a[::-1]
2394 r = np.arange(101)
2395 rr = r[::-1]
2396 for kind in self.sort_kinds:
2397 msg = "unicode argsort, kind=%s" % kind
2398 assert_equal(a.copy().argsort(kind=kind), r, msg)

Callers

nothing calls this directly

Calls 5

assert_equalFunction · 0.90
copyMethod · 0.45
argsortMethod · 0.45
dtypeMethod · 0.45
ravelMethod · 0.45

Tested by

no test coverage detected