def test_records(self): d1 = np.dtype([('m', np.int32), ('n', np.int64)]) d2 = np.dtype([('m', np.int32), ('n', np.int16)]) v1 = np.empty(1, dtype=d1)[0] v2 = np.empty(1, dtype=d2)[0] self.assertNotEqual(compute_fingerprint(v1), compute_fingerprint(v2))
def test_datetime(self): a = np.datetime64(1, 'Y') b = np.datetime64(2, 'Y') c = np.datetime64(2, 's') d = np.timedelta64(2, 's') self.assertEqual(compute_fingerprint(a), compute_fingerprint(b)) distinct = set(compute_fingerprint(x) for x in (a, c, d)) self.assertEqual(len(distinct), 3, distinct)
def test_datetime(self): a = np.datetime64(1, "Y") b = np.datetime64(2, "Y") c = np.datetime64(2, "s") d = np.timedelta64(2, "s") self.assertEqual(compute_fingerprint(a), compute_fingerprint(b)) distinct = set(compute_fingerprint(x) for x in (a, c, d)) self.assertEqual(len(distinct), 3, distinct)
def test_ints(self): s = compute_fingerprint(1) for v in (-1, 2 ** 60): self.assertEqual(compute_fingerprint(v), s) # Different int widths resolve to different fingerprints distinct = set() for tp in ("int8", "int16", "int32", "int64", "uint8", "uint16", "uint32", "uint64"): tp = getattr(np, tp) distinct.add(compute_fingerprint(tp())) self.assertEqual(len(distinct), 8, distinct)
def test_ints(self): s = compute_fingerprint(1) for v in (-1, 2**60): self.assertEqual(compute_fingerprint(v), s) # Different int widths resolve to different fingerprints distinct = set() for tp in ('int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', 'uint64'): tp = getattr(np, tp) distinct.add(compute_fingerprint(tp())) self.assertEqual(len(distinct), 8, distinct)
def test_omitted_args(self): distinct = DistinctChecker() v0 = OmittedArg(0.0) v1 = OmittedArg(1.0) v2 = OmittedArg(1) s = compute_fingerprint(v0) self.assertEqual(compute_fingerprint(v1), s) distinct.add(s) distinct.add(compute_fingerprint(v2)) distinct.add(compute_fingerprint(0.0)) distinct.add(compute_fingerprint(1))
def test_sets(self): distinct = DistinctChecker() s = compute_fingerprint(set([1])) self.assertEqual(compute_fingerprint(set([2, 3])), s) distinct.add(s) distinct.add(compute_fingerprint([1])) distinct.add(compute_fingerprint(set([1j]))) distinct.add(compute_fingerprint(set([4.5, 6.7]))) distinct.add(compute_fingerprint(set([(1,)]))) with self.assertRaises(ValueError): compute_fingerprint(set()) with self.assertRaises(NotImplementedError): compute_fingerprint(frozenset([2, 3]))
def test_sets(self): distinct = DistinctChecker() s = compute_fingerprint(set([1])) self.assertEqual(compute_fingerprint(set([2, 3])), s) distinct.add(s) distinct.add(compute_fingerprint([1])) distinct.add(compute_fingerprint(set([1j]))) distinct.add(compute_fingerprint(set([4.5, 6.7]))) distinct.add(compute_fingerprint(set([(1, )]))) with self.assertRaises(ValueError): compute_fingerprint(set()) with self.assertRaises(NotImplementedError): compute_fingerprint(frozenset([2, 3]))
def test_dtype(self): distinct = DistinctChecker() s = compute_fingerprint(np.dtype("int64")) self.assertEqual(compute_fingerprint(np.dtype("int64")), s) distinct.add(s) for descr in ("int32", "m8[s]", "m8[W]", "M8[s]"): distinct.add(np.dtype(descr)) distinct.add(recordtype) distinct.add(recordtype2) # np.recarray() is peculiar: it creates a new dtype instance in # its constructor; check that the fingerprint remains efficient a = np.recarray(1, dtype=recordtype) b = np.recarray(1, dtype=recordtype) self.assertEqual(compute_fingerprint(a.dtype), compute_fingerprint(b.dtype))
def test_tuples(self): distinct = DistinctChecker() s = compute_fingerprint((1,)) self.assertEqual(compute_fingerprint((2,)), s) distinct.add(s) distinct.add(compute_fingerprint(())) distinct.add(compute_fingerprint((1, 2, 3))) distinct.add(compute_fingerprint((1j, 2, 3))) distinct.add(compute_fingerprint((1, (), np.empty(5)))) distinct.add(compute_fingerprint((1, (), np.empty((5, 1)))))
def test_tuples(self): distinct = DistinctChecker() s = compute_fingerprint((1, )) self.assertEqual(compute_fingerprint((2, )), s) distinct.add(s) distinct.add(compute_fingerprint(())) distinct.add(compute_fingerprint((1, 2, 3))) distinct.add(compute_fingerprint((1j, 2, 3))) distinct.add(compute_fingerprint((1, (), np.empty(5)))) distinct.add(compute_fingerprint((1, (), np.empty((5, 1)))))
def test_lists(self): distinct = DistinctChecker() s = compute_fingerprint([1]) self.assertEqual(compute_fingerprint([2, 3]), s) distinct.add(s) distinct.add(compute_fingerprint([1j])) distinct.add(compute_fingerprint([4.5, 6.7])) distinct.add(compute_fingerprint([(1, )])) with self.assertRaises(ValueError): compute_fingerprint([])
def test_lists(self): distinct = DistinctChecker() s = compute_fingerprint([1]) self.assertEqual(compute_fingerprint([2, 3]), s) distinct.add(s) distinct.add(compute_fingerprint([1j])) distinct.add(compute_fingerprint([4.5, 6.7])) distinct.add(compute_fingerprint([(1,)])) with self.assertRaises(ValueError): compute_fingerprint([])
def test_fingerprint_failure(self): """ Failure in computing the fingerprint cannot affect a nopython=False function. On the other hand, with nopython=True, a ValueError should be raised to report the failure with fingerprint. """ @jit def foo(x): return x # Empty list will trigger failure in compile_fingerprint errmsg = 'cannot compute fingerprint of empty list' with self.assertRaises(ValueError) as raises: _dispatcher.compute_fingerprint([]) self.assertIn(errmsg, str(raises.exception)) # It should work in fallback self.assertEqual(foo([]), []) # But, not in nopython=True strict_foo = jit(nopython=True)(foo.py_func) with self.assertRaises(ValueError) as raises: strict_foo([]) self.assertIn(errmsg, str(raises.exception)) # Test in loop lifting context @jit def bar(): object() # force looplifting x = [] for i in range(10): x = foo(x) return x self.assertEqual(bar(), []) # Make sure it was looplifted [cr] = bar.overloads.values() self.assertEqual(len(cr.lifted), 1)
def test_arrays(self): distinct = DistinctChecker() # 1D arr = np.empty(4, dtype=np.float64) s = compute_fingerprint(arr) distinct.add(s) self.assertEqual(compute_fingerprint(arr[:1]), s) # Non-contiguous distinct.add(compute_fingerprint(arr[::2])) # Other type distinct.add(compute_fingerprint(arr.astype(np.complex64))) # Readonly arr.setflags(write=False) distinct.add(compute_fingerprint(arr)) # 2D arr = np.empty((4, 4), dtype=np.float64) distinct.add(compute_fingerprint(arr)) # F-contiguous distinct.add(compute_fingerprint(arr.T)) # Non-contiguous distinct.add(compute_fingerprint(arr[::2])) # 0D arr = np.empty((), dtype=np.float64) distinct.add(compute_fingerprint(arr)) # Structured arrays arr = np.empty(5, dtype=recordtype) s = compute_fingerprint(arr) distinct.add(s) self.assertEqual(compute_fingerprint(arr[:1]), s) arr = np.empty(5, dtype=recordtype2) distinct.add(compute_fingerprint(arr)) arr = np.empty(5, dtype=recordtype3) distinct.add(compute_fingerprint(arr)) # np.recarray() is peculiar: it creates a new dtype instance in # its constructor; check that the fingerprint remains efficient a = np.recarray(1, dtype=recordtype) b = np.recarray(1, dtype=recordtype) self.assertEqual(compute_fingerprint(a), compute_fingerprint(b))
def test_complex(self): s = compute_fingerprint(1j) self.assertEqual(s, compute_fingerprint(1+0j)) s = compute_fingerprint(np.complex64()) self.assertEqual(compute_fingerprint(np.complex64(2.0)), s) self.assertNotEqual(compute_fingerprint(np.complex128()), s)
def test_enums(self): # Enums should fail fingerprinting, even IntEnums with self.assertRaises(NotImplementedError): compute_fingerprint(Color.red) with self.assertRaises(NotImplementedError): compute_fingerprint(RequestError.not_found)
def test_none(self): compute_fingerprint(None)
def test_buffers(self): distinct = DistinctChecker() s = compute_fingerprint(b'') self.assertEqual(compute_fingerprint(b'xx'), s) distinct.add(s) distinct.add(compute_fingerprint(bytearray())) distinct.add(compute_fingerprint(memoryview(b''))) m_uint8_1d = compute_fingerprint(memoryview(bytearray())) distinct.add(m_uint8_1d) if sys.version_info >= (3,): arr = array.array('B', [42]) distinct.add(compute_fingerprint(arr)) self.assertEqual(compute_fingerprint(memoryview(arr)), m_uint8_1d) for array_code in 'bi': arr = array.array(array_code, [0, 1, 2]) distinct.add(compute_fingerprint(arr)) distinct.add(compute_fingerprint(memoryview(arr))) arr = np.empty(16, dtype=np.uint8) distinct.add(compute_fingerprint(arr)) self.assertEqual(compute_fingerprint(memoryview(arr)), m_uint8_1d) arr = arr.reshape((4, 4)) distinct.add(compute_fingerprint(arr)) distinct.add(compute_fingerprint(memoryview(arr))) arr = arr.T distinct.add(compute_fingerprint(arr)) distinct.add(compute_fingerprint(memoryview(arr))) arr = arr[::2] distinct.add(compute_fingerprint(arr)) distinct.add(compute_fingerprint(memoryview(arr))) if sys.version_info >= (3,): m = mmap.mmap(-1, 16384) distinct.add(compute_fingerprint(m)) self.assertEqual(compute_fingerprint(memoryview(m)), m_uint8_1d)
def test_floats(self): s = compute_fingerprint(1.0) self.assertEqual(compute_fingerprint(2.0), s) s = compute_fingerprint(np.float32()) self.assertEqual(compute_fingerprint(np.float32(2.0)), s) self.assertNotEqual(compute_fingerprint(np.float64()), s)
def test_complicated_type(self): # Generating a large fingerprint t = None for i in range(1000): t = (t,) s = compute_fingerprint(t)
def test_buffers(self): distinct = DistinctChecker() s = compute_fingerprint(b'') self.assertEqual(compute_fingerprint(b'xx'), s) distinct.add(s) distinct.add(compute_fingerprint(bytearray())) distinct.add(compute_fingerprint(memoryview(b''))) m_uint8_1d = compute_fingerprint(memoryview(bytearray())) distinct.add(m_uint8_1d) arr = array.array('B', [42]) distinct.add(compute_fingerprint(arr)) self.assertEqual(compute_fingerprint(memoryview(arr)), m_uint8_1d) for array_code in 'bi': arr = array.array(array_code, [0, 1, 2]) distinct.add(compute_fingerprint(arr)) distinct.add(compute_fingerprint(memoryview(arr))) arr = np.empty(16, dtype=np.uint8) distinct.add(compute_fingerprint(arr)) self.assertEqual(compute_fingerprint(memoryview(arr)), m_uint8_1d) arr = arr.reshape((4, 4)) distinct.add(compute_fingerprint(arr)) distinct.add(compute_fingerprint(memoryview(arr))) arr = arr.T distinct.add(compute_fingerprint(arr)) distinct.add(compute_fingerprint(memoryview(arr))) arr = arr[::2] distinct.add(compute_fingerprint(arr)) distinct.add(compute_fingerprint(memoryview(arr))) m = mmap.mmap(-1, 16384) distinct.add(compute_fingerprint(m)) self.assertEqual(compute_fingerprint(memoryview(m)), m_uint8_1d)
def test_bool(self): s = compute_fingerprint(True) self.assertEqual(compute_fingerprint(False), s) self.assertNotEqual(compute_fingerprint(1), s)
def test_complex(self): s = compute_fingerprint(1j) self.assertEqual(s, compute_fingerprint(1 + 0j)) s = compute_fingerprint(np.complex64()) self.assertEqual(compute_fingerprint(np.complex64(2.0)), s) self.assertNotEqual(compute_fingerprint(np.complex128()), s)
def test_complicated_type(self): # Generating a large fingerprint t = None for i in range(1000): t = (t, ) s = compute_fingerprint(t)