<s> from __future__ import print_function , absolute_import , division <EOL> import sys <EOL> import numpy as np <EOL> import threading <EOL> import random <EOL> from numba import unittest_support as unittest <EOL> from numba import njit <EOL> from numba import utils <EOL> from numba . numpy_support import version as numpy_version <EOL> from . support import MemoryLeakMixin , TestCase , tag <EOL> nrtjit = njit ( _nrt = True , nogil = True ) <EOL> class BaseTest ( TestCase ) : <EOL> def check_outputs ( self , pyfunc , argslist , exact = True ) : <EOL> cfunc = nrtjit ( pyfunc ) <EOL> for args in argslist : <EOL> expected = pyfunc ( * args ) <EOL> ret = cfunc ( * args ) <EOL> self . assertEqual ( ret . size , expected . size ) <EOL> self . assertEqual ( ret . dtype , expected . dtype ) <EOL> self . assertStridesEqual ( ret , expected ) <EOL> if exact : <EOL> np . testing . assert_equal ( expected , ret ) <EOL> else : <EOL> np . testing . assert_allclose ( expected , ret ) <EOL> class NrtRefCtTest ( MemoryLeakMixin ) : <EOL> def assert_array_nrt_refct ( self , arr , expect ) : <EOL> self . assertEqual ( arr . base . refcount , expect ) <EOL> class TestDynArray ( NrtRefCtTest , TestCase ) : <EOL> def test_empty_0d ( self ) : <EOL> @ nrtjit <EOL> def foo ( ) : <EOL> arr = np . empty ( ( ) ) <EOL> arr [ ( ) ] = <NUM_LIT> <EOL> return arr <EOL> arr = foo ( ) <EOL> self . assert_array_nrt_refct ( arr , <NUM_LIT:1> ) <EOL> np . testing . assert_equal ( <NUM_LIT> , arr ) <EOL> self . assertEqual ( arr . size , <NUM_LIT:1> ) <EOL> self . assertEqual ( arr . shape , ( ) ) <EOL> self . assertEqual ( arr . dtype , np . dtype ( np . float64 ) ) <EOL> self . assertEqual ( arr . strides , ( ) ) <EOL> arr . fill ( <NUM_LIT> ) <EOL> np . testing . assert_equal ( <NUM_LIT> , arr ) <EOL> del arr <EOL> def test_empty_1d ( self ) : <EOL> @ nrtjit <EOL> def foo ( n ) : <EOL> arr = np . empty ( n ) <EOL> for i in range ( n ) : <EOL> arr [ i ] = i <EOL> return arr <EOL> n = <NUM_LIT:3> <EOL> arr = foo ( n ) <EOL> self . assert_array_nrt_refct ( arr , <NUM_LIT:1> ) <EOL> np . testing . assert_equal ( np . arange ( n ) , arr ) <EOL> self . assertEqual ( arr . size , n ) <EOL> self . assertEqual ( arr . shape , ( n , ) ) <EOL> self . assertEqual ( arr . dtype , np . dtype ( np . float64 ) ) <EOL> self . assertEqual ( arr . strides , ( np . dtype ( np . float64 ) . itemsize , ) ) <EOL> arr . fill ( <NUM_LIT> ) <EOL> np . testing . assert_equal ( <NUM_LIT> , arr ) <EOL> del arr <EOL> def test_empty_2d ( self ) : <EOL> def pyfunc ( m , n ) : <EOL> arr = np . empty ( ( m , n ) , np . int32 ) <EOL> for i in range ( m ) : <EOL> for j in range ( n ) : <EOL> arr [ i , j ] = i + j <EOL> return arr <EOL> cfunc = nrtjit ( pyfunc ) <EOL> m = <NUM_LIT:4> <EOL> n = <NUM_LIT:3> <EOL> expected_arr = pyfunc ( m , n ) <EOL> got_arr = cfunc ( m , n ) <EOL> self . assert_array_nrt_refct ( got_arr , <NUM_LIT:1> ) <EOL> np . testing . assert_equal ( expected_arr , got_arr ) <EOL> self . assertEqual ( expected_arr . size , got_arr . size ) <EOL> self . assertEqual ( expected_arr . shape , got_arr . shape ) <EOL> self . assertEqual ( expected_arr . strides , got_arr . strides ) <EOL> del got_arr <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_empty_3d ( self ) : <EOL> def pyfunc ( m , n , p ) : <EOL> arr = np . empty ( ( m , n , p ) , np . int32 ) <EOL> for i in range ( m ) : <EOL> for j in range ( n ) : <EOL> for k in range ( p ) : <EOL> arr [ i , j , k ] = i + j + k <EOL> return arr <EOL> cfunc = nrtjit ( pyfunc ) <EOL> m = <NUM_LIT:4> <EOL> n = <NUM_LIT:3> <EOL> p = <NUM_LIT:2> <EOL> expected_arr = pyfunc ( m , n , p ) <EOL> got_arr = cfunc ( m , n , p ) <EOL> self . assert_array_nrt_refct ( got_arr , <NUM_LIT:1> ) <EOL> np . testing . assert_equal ( expected_arr , got_arr ) <EOL> self . assertEqual ( expected_arr . size , got_arr . size ) <EOL> self . assertEqual ( expected_arr . shape , got_arr . shape ) <EOL> self . assertEqual ( expected_arr . strides , got_arr . strides ) <EOL> del got_arr <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_empty_2d_sliced ( self ) : <EOL> def pyfunc ( m , n , p ) : <EOL> arr = np . empty ( ( m , n ) , np . int32 ) <EOL> for i in range ( m ) : <EOL> for j in range ( n ) : <EOL> arr [ i , j ] = i + j <EOL> return arr [ p ] <EOL> cfunc = nrtjit ( pyfunc ) <EOL> m = <NUM_LIT:4> <EOL> n = <NUM_LIT:3> <EOL> p = <NUM_LIT:2> <EOL> expected_arr = pyfunc ( m , n , p ) <EOL> got_arr = cfunc ( m , n , p ) <EOL> self . assert_array_nrt_refct ( got_arr , <NUM_LIT:1> ) <EOL> np . testing . assert_equal ( expected_arr , got_arr ) <EOL> self . assertEqual ( expected_arr . size , got_arr . size ) <EOL> self . assertEqual ( expected_arr . shape , got_arr . shape ) <EOL> self . assertEqual ( expected_arr . strides , got_arr . strides ) <EOL> del got_arr <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_return_global_array ( self ) : <EOL> y = np . ones ( <NUM_LIT:4> , dtype = np . float32 ) <EOL> initrefct = sys . getrefcount ( y ) <EOL> def return_external_array ( ) : <EOL> return y <EOL> cfunc = nrtjit ( return_external_array ) <EOL> out = cfunc ( ) <EOL> self . assertEqual ( initrefct + <NUM_LIT:1> , sys . getrefcount ( y ) ) <EOL> np . testing . assert_equal ( y , out ) <EOL> np . testing . assert_equal ( y , np . ones ( <NUM_LIT:4> , dtype = np . float32 ) ) <EOL> np . testing . assert_equal ( out , np . ones ( <NUM_LIT:4> , dtype = np . float32 ) ) <EOL> del out <EOL> self . assertEqual ( initrefct + <NUM_LIT:1> , sys . getrefcount ( y ) ) <EOL> del cfunc <EOL> self . assertEqual ( initrefct , sys . getrefcount ( y ) ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_return_global_array_sliced ( self ) : <EOL> y = np . ones ( <NUM_LIT:4> , dtype = np . float32 ) <EOL> def return_external_array ( ) : <EOL> return y [ <NUM_LIT:2> : ] <EOL> cfunc = nrtjit ( return_external_array ) <EOL> out = cfunc ( ) <EOL> self . assertIsNone ( out . base ) <EOL> yy = y [ <NUM_LIT:2> : ] <EOL> np . testing . assert_equal ( yy , out ) <EOL> np . testing . assert_equal ( yy , np . ones ( <NUM_LIT:2> , dtype = np . float32 ) ) <EOL> np . testing . assert_equal ( out , np . ones ( <NUM_LIT:2> , dtype = np . float32 ) ) <EOL> def test_array_pass_through ( self ) : <EOL> def pyfunc ( y ) : <EOL> return y <EOL> arr = np . ones ( <NUM_LIT:4> , dtype = np . float32 ) <EOL> cfunc = nrtjit ( pyfunc ) <EOL> expected = cfunc ( arr ) <EOL> got = pyfunc ( arr ) <EOL> np . testing . assert_equal ( expected , arr ) <EOL> np . testing . assert_equal ( expected , got ) <EOL> self . assertIs ( expected , arr ) <EOL> self . assertIs ( expected , got ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_array_pass_through_sliced ( self ) : <EOL> def pyfunc ( y ) : <EOL> return y [ y . size // <NUM_LIT:2> : ] <EOL> arr = np . ones ( <NUM_LIT:4> , dtype = np . float32 ) <EOL> initrefct = sys . getrefcount ( arr ) <EOL> cfunc = nrtjit ( pyfunc ) <EOL> got = cfunc ( arr ) <EOL> self . assertEqual ( initrefct + <NUM_LIT:1> , sys . getrefcount ( arr ) ) <EOL> expected = pyfunc ( arr ) <EOL> self . assertEqual ( initrefct + <NUM_LIT:2> , sys . getrefcount ( arr ) ) <EOL> np . testing . assert_equal ( expected , arr [ arr . size // <NUM_LIT:2> ] ) <EOL> np . testing . assert_equal ( expected , got ) <EOL> del expected <EOL> self . assertEqual ( initrefct + <NUM_LIT:1> , sys . getrefcount ( arr ) ) <EOL> del got <EOL> self . assertEqual ( initrefct , sys . getrefcount ( arr ) ) <EOL> def test_ufunc_with_allocated_output ( self ) : <EOL> def pyfunc ( a , b ) : <EOL> out = np . empty ( a . shape ) <EOL> np . add ( a , b , out ) <EOL> return out <EOL> cfunc = nrtjit ( pyfunc ) <EOL> arr_a = np . random . random ( <NUM_LIT:10> ) <EOL> arr_b = np . random . random ( <NUM_LIT:10> ) <EOL> np . testing . assert_equal ( pyfunc ( arr_a , arr_b ) , <EOL> cfunc ( arr_a , arr_b ) ) <EOL> self . assert_array_nrt_refct ( cfunc ( arr_a , arr_b ) , <NUM_LIT:1> ) <EOL> arr_a = np . random . random ( <NUM_LIT:10> ) . reshape ( <NUM_LIT:2> , <NUM_LIT:5> ) <EOL> arr_b = np . random . random ( <NUM_LIT:10> ) . reshape ( <NUM_LIT:2> , <NUM_LIT:5> ) <EOL> np . testing . assert_equal ( pyfunc ( arr_a , arr_b ) , <EOL> cfunc ( arr_a , arr_b ) ) <EOL> self . assert_array_nrt_refct ( cfunc ( arr_a , arr_b ) , <NUM_LIT:1> ) <EOL> arr_a = np . random . random ( <NUM_LIT> ) . reshape ( <NUM_LIT:2> , <NUM_LIT:5> , <NUM_LIT:7> ) <EOL> arr_b = np . random . random ( <NUM_LIT> ) . reshape ( <NUM_LIT:2> , <NUM_LIT:5> , <NUM_LIT:7> ) <EOL> np . testing . assert_equal ( pyfunc ( arr_a , arr_b ) , <EOL> cfunc ( arr_a , arr_b ) ) <EOL> self . assert_array_nrt_refct ( cfunc ( arr_a , arr_b ) , <NUM_LIT:1> ) <EOL> def test_allocation_mt ( self ) : <EOL> """<STR_LIT>""" <EOL> def pyfunc ( inp ) : <EOL> out = np . empty ( inp . size ) <EOL> for i in range ( out . size ) : <EOL> out [ i ] = <NUM_LIT:0> <EOL> for i in range ( inp [ <NUM_LIT:0> ] ) : <EOL> tmp = np . empty ( inp . size ) <EOL> for j in range ( tmp . size ) : <EOL> tmp [ j ] = inp [ j ] <EOL> for j in range ( tmp . size ) : <EOL> out [ j ] += tmp [ j ] + i <EOL> return out <EOL> cfunc = nrtjit ( pyfunc ) <EOL> size = <NUM_LIT:10> <EOL> arr = np . random . randint ( <NUM_LIT:1> , <NUM_LIT:10> , size ) <EOL> frozen_arr = arr . copy ( ) <EOL> np . testing . assert_equal ( pyfunc ( arr ) , cfunc ( arr ) ) <EOL> np . testing . assert_equal ( frozen_arr , arr ) <EOL> workers = [ ] <EOL> inputs = [ ] <EOL> outputs = [ ] <EOL> def wrapped ( inp , out ) : <EOL> out [ : ] = cfunc ( inp ) <EOL> for i in range ( <NUM_LIT:100> ) : <EOL> arr = np . random . randint ( <NUM_LIT:1> , <NUM_LIT:10> , size ) <EOL> out = np . empty_like ( arr ) <EOL> thread = threading . Thread ( target = wrapped , <EOL> args = ( arr , out ) , <EOL> name = "<STR_LIT>" . format ( i ) ) <EOL> workers . append ( thread ) <EOL> inputs . append ( arr ) <EOL> outputs . append ( out ) <EOL> for thread in workers : <EOL> thread . start ( ) <EOL> for thread in workers : <EOL> thread . join ( ) <EOL> for inp , out in zip ( inputs , outputs ) : <EOL> np . testing . assert_equal ( pyfunc ( inp ) , out ) <EOL> def test_refct_mt ( self ) : <EOL> """<STR_LIT>""" <EOL> def pyfunc ( n , inp ) : <EOL> out = np . empty ( inp . size ) <EOL> for i in range ( out . size ) : <EOL> out [ i ] = inp [ i ] + <NUM_LIT:1> <EOL> for i in range ( n ) : <EOL> out , inp = inp , out <EOL> return out <EOL> cfunc = nrtjit ( pyfunc ) <EOL> size = <NUM_LIT:10> <EOL> input = np . arange ( size , dtype = np . float ) <EOL> expected_refct = sys . getrefcount ( input ) <EOL> swapct = random . randrange ( <NUM_LIT:1000> ) <EOL> expected = pyfunc ( swapct , input ) <EOL> np . testing . assert_equal ( expected , cfunc ( swapct , input ) ) <EOL> del expected <EOL> self . assertEqual ( expected_refct , sys . getrefcount ( input ) ) <EOL> workers = [ ] <EOL> outputs = [ ] <EOL> swapcts = [ ] <EOL> def wrapped ( n , input , out ) : <EOL> out [ : ] = cfunc ( n , input ) <EOL> for i in range ( <NUM_LIT:100> ) : <EOL> out = np . empty ( size ) <EOL> swapct = random . randrange ( <NUM_LIT:1000> ) <EOL> thread = threading . Thread ( target = wrapped , <EOL> args = ( swapct , input , out ) , <EOL> name = "<STR_LIT>" . format ( i ) ) <EOL> workers . append ( thread ) <EOL> outputs . append ( out ) <EOL> swapcts . append ( swapct ) <EOL> for thread in workers : <EOL> thread . start ( ) <EOL> for thread in workers : <EOL> thread . join ( ) <EOL> for swapct , out in zip ( swapcts , outputs ) : <EOL> np . testing . assert_equal ( pyfunc ( swapct , input ) , out ) <EOL> del outputs , workers <EOL> self . assertEqual ( expected_refct , sys . getrefcount ( input ) ) <EOL> def test_swap ( self ) : <EOL> def pyfunc ( x , y , t ) : <EOL> """<STR_LIT>""" <EOL> for i in range ( t ) : <EOL> x , y = y , x <EOL> return x , y <EOL> cfunc = nrtjit ( pyfunc ) <EOL> x = np . random . random ( <NUM_LIT:100> ) <EOL> y = np . random . random ( <NUM_LIT:100> ) <EOL> t = <NUM_LIT:100> <EOL> initrefct = sys . getrefcount ( x ) , sys . getrefcount ( y ) <EOL> expect , got = pyfunc ( x , y , t ) , cfunc ( x , y , t ) <EOL> self . assertIsNone ( got [ <NUM_LIT:0> ] . base ) <EOL> self . assertIsNone ( got [ <NUM_LIT:1> ] . base ) <EOL> np . testing . assert_equal ( expect , got ) <EOL> del expect , got <EOL> self . assertEqual ( initrefct , ( sys . getrefcount ( x ) , sys . getrefcount ( y ) ) ) <EOL> def test_return_tuple_of_array ( self ) : <EOL> def pyfunc ( x ) : <EOL> y = np . empty ( x . size ) <EOL> for i in range ( y . size ) : <EOL> y [ i ] = x [ i ] + <NUM_LIT:1> <EOL> return x , y <EOL> cfunc = nrtjit ( pyfunc ) <EOL> x = np . random . random ( <NUM_LIT:5> ) <EOL> initrefct = sys . getrefcount ( x ) <EOL> expected_x , expected_y = pyfunc ( x ) <EOL> got_x , got_y = cfunc ( x ) <EOL> self . assertIs ( x , expected_x ) <EOL> self . assertIs ( x , got_x ) <EOL> np . testing . assert_equal ( expected_x , got_x ) <EOL> np . testing . assert_equal ( expected_y , got_y ) <EOL> del expected_x , got_x <EOL> self . assertEqual ( initrefct , sys . getrefcount ( x ) ) <EOL> self . assertEqual ( sys . getrefcount ( expected_y ) , sys . getrefcount ( got_y ) ) <EOL> def test_return_tuple_of_array_created ( self ) : <EOL> def pyfunc ( x ) : <EOL> y = np . empty ( x . size ) <EOL> for i in range ( y . size ) : <EOL> y [ i ] = x [ i ] + <NUM_LIT:1> <EOL> out = y , y <EOL> return out <EOL> cfunc = nrtjit ( pyfunc ) <EOL> x = np . random . random ( <NUM_LIT:5> ) <EOL> expected_x , expected_y = pyfunc ( x ) <EOL> got_x , got_y = cfunc ( x ) <EOL> np . testing . assert_equal ( expected_x , got_x ) <EOL> np . testing . assert_equal ( expected_y , got_y ) <EOL> self . assertEqual ( <NUM_LIT:2> , sys . getrefcount ( got_y ) ) <EOL> self . assertEqual ( <NUM_LIT:2> , sys . getrefcount ( got_y ) ) <EOL> def test_issue_with_return_leak ( self ) : <EOL> """<STR_LIT>""" <EOL> @ nrtjit <EOL> def inner ( out ) : <EOL> return out <EOL> def pyfunc ( x ) : <EOL> return inner ( x ) <EOL> cfunc = nrtjit ( pyfunc ) <EOL> arr = np . arange ( <NUM_LIT:10> ) <EOL> old_refct = sys . getrefcount ( arr ) <EOL> self . assertEqual ( old_refct , sys . getrefcount ( pyfunc ( arr ) ) ) <EOL> self . assertEqual ( old_refct , sys . getrefcount ( cfunc ( arr ) ) ) <EOL> self . assertEqual ( old_refct , sys . getrefcount ( arr ) ) <EOL> class ConstructorBaseTest ( NrtRefCtTest ) : <EOL> def check_0d ( self , pyfunc ) : <EOL> cfunc = nrtjit ( pyfunc ) <EOL> expected = pyfunc ( ) <EOL> ret = cfunc ( ) <EOL> self . assert_array_nrt_refct ( ret , <NUM_LIT:1> ) <EOL> self . assertEqual ( ret . size , expected . size ) <EOL> self . assertEqual ( ret . shape , expected . shape ) <EOL> self . assertEqual ( ret . dtype , expected . dtype ) <EOL> self . assertEqual ( ret . strides , expected . strides ) <EOL> self . check_result_value ( ret , expected ) <EOL> expected = np . empty_like ( ret ) <EOL> expected . fill ( <NUM_LIT> ) <EOL> ret . fill ( <NUM_LIT> ) <EOL> np . testing . assert_equal ( ret , expected ) <EOL> def check_1d ( self , pyfunc ) : <EOL> cfunc = nrtjit ( pyfunc ) <EOL> n = <NUM_LIT:3> <EOL> expected = pyfunc ( n ) <EOL> ret = cfunc ( n ) <EOL> self . assert_array_nrt_refct ( ret , <NUM_LIT:1> ) <EOL> self . assertEqual ( ret . size , expected . size ) <EOL> self . assertEqual ( ret . shape , expected . shape ) <EOL> self . assertEqual ( ret . dtype , expected . dtype ) <EOL> self . assertEqual ( ret . strides , expected . strides ) <EOL> self . check_result_value ( ret , expected ) <EOL> expected = np . empty_like ( ret ) <EOL> expected . fill ( <NUM_LIT> ) <EOL> ret . fill ( <NUM_LIT> ) <EOL> np . testing . assert_equal ( ret , expected ) <EOL> with self . assertRaises ( ValueError ) as cm : <EOL> cfunc ( - <NUM_LIT:1> ) <EOL> self . assertEqual ( str ( cm . exception ) , "<STR_LIT>" ) <EOL> def check_2d ( self , pyfunc ) : <EOL> cfunc = nrtjit ( pyfunc ) <EOL> m , n = <NUM_LIT:2> , <NUM_LIT:3> <EOL> expected = pyfunc ( m , n ) <EOL> ret = cfunc ( m , n ) <EOL> self . assert_array_nrt_refct ( ret , <NUM_LIT:1> ) <EOL> self . assertEqual ( ret . size , expected . size ) <EOL> self . assertEqual ( ret . shape , expected . shape ) <EOL> self . assertEqual ( ret . dtype , expected . dtype ) <EOL> self . assertEqual ( ret . strides , expected . strides ) <EOL> self . check_result_value ( ret , expected ) <EOL> expected = np . empty_like ( ret ) <EOL> expected . fill ( <NUM_LIT> ) <EOL> ret . fill ( <NUM_LIT> ) <EOL> np . testing . assert_equal ( ret , expected ) <EOL> with self . assertRaises ( ValueError ) as cm : <EOL> cfunc ( <NUM_LIT:2> , - <NUM_LIT:1> ) <EOL> self . assertEqual ( str ( cm . exception ) , "<STR_LIT>" ) <EOL> class TestNdZeros ( ConstructorBaseTest , TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestNdZeros , self ) . setUp ( ) <EOL> self . pyfunc = np . zeros <EOL> def check_result_value ( self , ret , expected ) : <EOL> np . testing . assert_equal ( ret , expected ) <EOL> def test_0d ( self ) : <EOL> pyfunc = self . pyfunc <EOL> def func ( ) : <EOL> return pyfunc ( ( ) ) <EOL> self . check_0d ( func ) <EOL> def test_1d ( self ) : <EOL> pyfunc = self . pyfunc <EOL> def func ( n ) : <EOL> return pyfunc ( n ) <EOL> self . check_1d ( func ) <EOL> def test_1d_dtype ( self ) : <EOL> pyfunc = self . pyfunc <EOL> def func ( n ) : <EOL> return pyfunc ( n , np . int32 ) <EOL> self . check_1d ( func ) <EOL> def test_1d_dtype_instance ( self ) : <EOL> pyfunc = self . pyfunc <EOL> _dtype = np . dtype ( '<STR_LIT>' ) <EOL> def func ( n ) : <EOL> return pyfunc ( n , _dtype ) <EOL> self . check_1d ( func ) <EOL> def test_2d ( self ) : <EOL> pyfunc = self . pyfunc <EOL> def func ( m , n ) : <EOL> return pyfunc ( ( m , n ) ) <EOL> self . check_2d ( func ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_2d_dtype_kwarg ( self ) : <EOL> pyfunc = self . pyfunc <EOL> def func ( m , n ) : <EOL> return pyfunc ( ( m , n ) , dtype = np . complex64 ) <EOL> self . check_2d ( func ) <EOL> class TestNdOnes ( TestNdZeros ) : <EOL> def setUp ( self ) : <EOL> super ( TestNdOnes , self ) . setUp ( ) <EOL> self . pyfunc = np . ones <EOL> @ unittest . skipIf ( numpy_version < ( <NUM_LIT:1> , <NUM_LIT:8> ) , "<STR_LIT>" ) <EOL> class TestNdFull ( ConstructorBaseTest , TestCase ) : <EOL> def check_result_value ( self , ret , expected ) : <EOL> np . testing . assert_equal ( ret , expected ) <EOL> def test_0d ( self ) : <EOL> def func ( ) : <EOL> return np . full ( ( ) , <NUM_LIT> ) <EOL> self . check_0d ( func ) <EOL> def test_1d ( self ) : <EOL> def func ( n ) : <EOL> return np . full ( n , <NUM_LIT> ) <EOL> self . check_1d ( func ) <EOL> def test_1d_dtype ( self ) : <EOL> def func ( n ) : <EOL> return np . full ( n , <NUM_LIT> , np . bool_ ) <EOL> self . check_1d ( func ) <EOL> def test_1d_dtype_instance ( self ) : <EOL> dtype = np . dtype ( '<STR_LIT:bool>' ) <EOL> def func ( n ) : <EOL> return np . full ( n , <NUM_LIT> , dtype ) <EOL> self . check_1d ( func ) <EOL> def test_2d ( self ) : <EOL> def func ( m , n ) : <EOL> return np . full ( ( m , n ) , <NUM_LIT> ) <EOL> self . check_2d ( func ) <EOL> def test_2d_dtype_kwarg ( self ) : <EOL> def func ( m , n ) : <EOL> return np . full ( ( m , n ) , <NUM_LIT:1> + <NUM_LIT> , dtype = np . complex64 ) <EOL> self . check_2d ( func ) <EOL> class ConstructorLikeBaseTest ( object ) : <EOL> def mutate_array ( self , arr ) : <EOL> try : <EOL> arr . fill ( <NUM_LIT> ) <EOL> except ( TypeError , ValueError ) : <EOL> fill_value = b'<STR_LIT:x>' * arr . dtype . itemsize <EOL> arr . fill ( fill_value ) <EOL> def check_like ( self , pyfunc , dtype ) : <EOL> def check_arr ( arr ) : <EOL> expected = pyfunc ( arr ) <EOL> ret = cfunc ( arr ) <EOL> self . assertEqual ( ret . size , expected . size ) <EOL> self . assertEqual ( ret . dtype , expected . dtype ) <EOL> self . assertStridesEqual ( ret , expected ) <EOL> self . check_result_value ( ret , expected ) <EOL> self . mutate_array ( ret ) <EOL> self . mutate_array ( expected ) <EOL> np . testing . assert_equal ( ret , expected ) <EOL> orig = np . linspace ( <NUM_LIT:0> , <NUM_LIT:5> , <NUM_LIT:6> ) . astype ( dtype ) <EOL> cfunc = nrtjit ( pyfunc ) <EOL> for shape in ( <NUM_LIT:6> , ( <NUM_LIT:2> , <NUM_LIT:3> ) , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) , ( <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:2> ) , ( ) ) : <EOL> if shape == ( ) : <EOL> arr = orig [ - <NUM_LIT:1> : ] . reshape ( ( ) ) <EOL> else : <EOL> arr = orig . reshape ( shape ) <EOL> check_arr ( arr ) <EOL> if arr . ndim > <NUM_LIT:0> : <EOL> check_arr ( arr [ : : <NUM_LIT:2> ] ) <EOL> check_arr ( orig [ <NUM_LIT:0> ] ) <EOL> class TestNdEmptyLike ( ConstructorLikeBaseTest , TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestNdEmptyLike , self ) . setUp ( ) <EOL> self . pyfunc = np . empty_like <EOL> def check_result_value ( self , ret , expected ) : <EOL> pass <EOL> def test_like ( self ) : <EOL> pyfunc = self . pyfunc <EOL> def func ( arr ) : <EOL> return pyfunc ( arr ) <EOL> self . check_like ( func , np . float64 ) <EOL> def test_like_structured ( self ) : <EOL> dtype = np . dtype ( [ ( '<STR_LIT:a>' , np . int16 ) , ( '<STR_LIT:b>' , np . float32 ) ] ) <EOL> pyfunc = self . pyfunc <EOL> def func ( arr ) : <EOL> return pyfunc ( arr ) <EOL> self . check_like ( func , dtype ) <EOL> def test_like_dtype ( self ) : <EOL> pyfunc = self . pyfunc <EOL> def func ( arr ) : <EOL> return pyfunc ( arr , np . int32 ) <EOL> self . check_like ( func , np . float64 ) <EOL> def test_like_dtype_instance ( self ) : <EOL> dtype = np . dtype ( '<STR_LIT>' ) <EOL> pyfunc = self . pyfunc <EOL> def func ( arr ) : <EOL> return pyfunc ( arr , dtype ) <EOL> self . check_like ( func , np . float64 ) <EOL> def test_like_dtype_structured ( self ) : <EOL> dtype = np . dtype ( [ ( '<STR_LIT:a>' , np . int16 ) , ( '<STR_LIT:b>' , np . float32 ) ] ) <EOL> pyfunc = self . pyfunc <EOL> def func ( arr ) : <EOL> return pyfunc ( arr , dtype ) <EOL> self . check_like ( func , np . float64 ) <EOL> def test_like_dtype_kwarg ( self ) : <EOL> pyfunc = self . pyfunc <EOL> def func ( arr ) : <EOL> return pyfunc ( arr , dtype = np . int32 ) <EOL> self . check_like ( func , np . float64 ) <EOL> class TestNdZerosLike ( TestNdEmptyLike ) : <EOL> def setUp ( self ) : <EOL> super ( TestNdZerosLike , self ) . setUp ( ) <EOL> self . pyfunc = np . zeros_like <EOL> def check_result_value ( self , ret , expected ) : <EOL> np . testing . assert_equal ( ret , expected ) <EOL> def test_like_structured ( self ) : <EOL> super ( TestNdZerosLike , self ) . test_like_structured ( ) <EOL> def test_like_dtype_structured ( self ) : <EOL> super ( TestNdZerosLike , self ) . test_like_dtype_structured ( ) <EOL> class TestNdOnesLike ( TestNdZerosLike ) : <EOL> def setUp ( self ) : <EOL> super ( TestNdOnesLike , self ) . setUp ( ) <EOL> self . pyfunc = np . ones_like <EOL> self . expected_value = <NUM_LIT:1> <EOL> @ unittest . expectedFailure <EOL> def test_like_structured ( self ) : <EOL> super ( TestNdOnesLike , self ) . test_like_structured ( ) <EOL> @ unittest . expectedFailure <EOL> def test_like_dtype_structured ( self ) : <EOL> super ( TestNdOnesLike , self ) . test_like_dtype_structured ( ) <EOL> @ unittest . skipIf ( numpy_version < ( <NUM_LIT:1> , <NUM_LIT:8> ) , "<STR_LIT>" ) <EOL> class TestNdFullLike ( ConstructorLikeBaseTest , TestCase ) : <EOL> def check_result_value ( self , ret , expected ) : <EOL> np . testing . assert_equal ( ret , expected ) <EOL> def test_like ( self ) : <EOL> def func ( arr ) : <EOL> return np . full_like ( arr , <NUM_LIT> ) <EOL> self . check_like ( func , np . float64 ) <EOL> @ unittest . expectedFailure <EOL> def test_like_structured ( self ) : <EOL> dtype = np . dtype ( [ ( '<STR_LIT:a>' , np . int16 ) , ( '<STR_LIT:b>' , np . float32 ) ] ) <EOL> def func ( arr ) : <EOL> return np . full_like ( arr , <NUM_LIT> ) <EOL> self . check_like ( func , dtype ) <EOL> def test_like_dtype ( self ) : <EOL> def func ( arr ) : <EOL> return np . full_like ( arr , <NUM_LIT> , np . bool_ ) <EOL> self . check_like ( func , np . float64 ) <EOL> def test_like_dtype_instance ( self ) : <EOL> dtype = np . dtype ( '<STR_LIT:bool>' ) <EOL> def func ( arr ) : <EOL> return np . full_like ( arr , <NUM_LIT> , dtype ) <EOL> self . check_like ( func , np . float64 ) <EOL> def test_like_dtype_kwarg ( self ) : <EOL> def func ( arr ) : <EOL> return np . full_like ( arr , <NUM_LIT> , dtype = np . bool_ ) <EOL> self . check_like ( func , np . float64 ) <EOL> class TestNdIdentity ( BaseTest ) : <EOL> def check_identity ( self , pyfunc ) : <EOL> self . check_outputs ( pyfunc , [ ( <NUM_LIT:3> , ) ] ) <EOL> def test_identity ( self ) : <EOL> def func ( n ) : <EOL> return np . identity ( n ) <EOL> self . check_identity ( func ) <EOL> def test_identity_dtype ( self ) : <EOL> for dtype in ( np . complex64 , np . int16 , np . bool_ , np . dtype ( '<STR_LIT:bool>' ) ) : <EOL> def func ( n ) : <EOL> return np . identity ( n , dtype ) <EOL> self . check_identity ( func ) <EOL> class TestNdEye ( BaseTest ) : <EOL> def test_eye_n ( self ) : <EOL> def func ( n ) : <EOL> return np . eye ( n ) <EOL> self . check_outputs ( func , [ ( <NUM_LIT:1> , ) , ( <NUM_LIT:3> , ) ] ) <EOL> def test_eye_n_m ( self ) : <EOL> def func ( n , m ) : <EOL> return np . eye ( n , m ) <EOL> self . check_outputs ( func , [ ( <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:3> , <NUM_LIT:2> ) , ( <NUM_LIT:0> , <NUM_LIT:3> ) ] ) <EOL> def check_eye_n_m_k ( self , func ) : <EOL> self . check_outputs ( func , [ ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:1> ) , <EOL> ( <NUM_LIT:3> , <NUM_LIT:4> , - <NUM_LIT:1> ) , <EOL> ( <NUM_LIT:4> , <NUM_LIT:3> , - <NUM_LIT:2> ) , <EOL> ( <NUM_LIT:4> , <NUM_LIT:3> , - <NUM_LIT:5> ) , <EOL> ( <NUM_LIT:4> , <NUM_LIT:3> , <NUM_LIT:5> ) ] ) <EOL> def test_eye_n_m_k ( self ) : <EOL> def func ( n , m , k ) : <EOL> return np . eye ( n , m , k ) <EOL> self . check_eye_n_m_k ( func ) <EOL> def test_eye_n_m_k_dtype ( self ) : <EOL> def func ( n , m , k ) : <EOL> return np . eye ( N = n , M = m , k = k , dtype = np . int16 ) <EOL> self . check_eye_n_m_k ( func ) <EOL> def test_eye_n_m_k_dtype_instance ( self ) : <EOL> dtype = np . dtype ( '<STR_LIT>' ) <EOL> def func ( n , m , k ) : <EOL> return np . eye ( N = n , M = m , k = k , dtype = dtype ) <EOL> self . check_eye_n_m_k ( func ) <EOL> class TestNdDiag ( TestCase ) : <EOL> def setUp ( self ) : <EOL> v = np . array ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> hv = np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ] ) <EOL> vv = np . transpose ( hv ) <EOL> self . vectors = [ v , hv , vv ] <EOL> a3x4 = np . arange ( <NUM_LIT:12> ) . reshape ( <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> a4x3 = np . arange ( <NUM_LIT:12> ) . reshape ( <NUM_LIT:4> , <NUM_LIT:3> ) <EOL> self . matricies = [ a3x4 , a4x3 ] <EOL> def func ( q ) : <EOL> return np . diag ( q ) <EOL> self . py = func <EOL> self . jit = nrtjit ( func ) <EOL> def func_kwarg ( q , k = <NUM_LIT:0> ) : <EOL> return np . diag ( q , k = k ) <EOL> self . py_kw = func_kwarg <EOL> self . jit_kw = nrtjit ( func_kwarg ) <EOL> def check_diag ( self , pyfunc , nrtfunc , * args , ** kwargs ) : <EOL> expected = pyfunc ( * args , ** kwargs ) <EOL> computed = nrtfunc ( * args , ** kwargs ) <EOL> self . assertEqual ( computed . size , expected . size ) <EOL> self . assertEqual ( computed . dtype , expected . dtype ) <EOL> np . testing . assert_equal ( expected , computed ) <EOL> def test_diag_vect_create ( self ) : <EOL> for d in self . vectors : <EOL> self . check_diag ( self . py , self . jit , d ) <EOL> def test_diag_vect_create_kwarg ( self ) : <EOL> for k in range ( - <NUM_LIT:10> , <NUM_LIT:10> ) : <EOL> for d in self . vectors : <EOL> self . check_diag ( self . py_kw , self . jit_kw , d , k = k ) <EOL> def test_diag_extract ( self ) : <EOL> for d in self . matricies : <EOL> self . check_diag ( self . py , self . jit , d ) <EOL> def test_diag_extract_kwarg ( self ) : <EOL> for k in range ( - <NUM_LIT:4> , <NUM_LIT:4> ) : <EOL> for d in self . matricies : <EOL> self . check_diag ( self . py_kw , self . jit_kw , d , k = k ) <EOL> def test_error_handling ( self ) : <EOL> from numba . errors import TypingError <EOL> d = np . array ( [ [ [ <NUM_LIT:1.> ] ] ] ) <EOL> cfunc = nrtjit ( self . py ) <EOL> with self . assertRaises ( TypeError ) : <EOL> cfunc ( ) <EOL> with self . assertRaises ( TypingError ) : <EOL> cfunc ( d ) <EOL> with self . assertRaises ( TypingError ) : <EOL> dfunc = nrtjit ( self . py_kw ) <EOL> dfunc ( d , k = <NUM_LIT:3> ) <EOL> class TestNdArange ( BaseTest ) : <EOL> def test_linspace_2 ( self ) : <EOL> def pyfunc ( n , m ) : <EOL> return np . linspace ( n , m ) <EOL> self . check_outputs ( pyfunc , <EOL> [ ( <NUM_LIT:0> , <NUM_LIT:4> ) , ( <NUM_LIT:1> , <NUM_LIT:100> ) , ( - <NUM_LIT> , <NUM_LIT> ) , ( - <NUM_LIT> , <NUM_LIT:2> + <NUM_LIT> ) , <EOL> ( <NUM_LIT:2> , <NUM_LIT:1> ) , ( <NUM_LIT:1> + <NUM_LIT> , <NUM_LIT> ) ] , exact = False ) <EOL> def test_linspace_3 ( self ) : <EOL> def pyfunc ( n , m , p ) : <EOL> return np . linspace ( n , m , p ) <EOL> self . check_outputs ( pyfunc , <EOL> [ ( <NUM_LIT:0> , <NUM_LIT:4> , <NUM_LIT:9> ) , ( <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:3> ) , ( - <NUM_LIT> , <NUM_LIT> , <NUM_LIT:8> ) , <EOL> ( - <NUM_LIT> , <NUM_LIT:2> + <NUM_LIT> , <NUM_LIT:7> ) , ( <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:0> ) , <EOL> ( <NUM_LIT:1> + <NUM_LIT> , <NUM_LIT> , <NUM_LIT:5> ) , ( <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT:1> ) ] , <EOL> exact = False ) <EOL> class TestNpyEmptyKeyword ( TestCase ) : <EOL> def _test_with_dtype_kw ( self , dtype ) : <EOL> def pyfunc ( shape ) : <EOL> return np . empty ( shape , dtype = dtype ) <EOL> shapes = [ <NUM_LIT:1> , <NUM_LIT:5> , <NUM_LIT:9> ] <EOL> cfunc = nrtjit ( pyfunc ) <EOL> for s in shapes : <EOL> expected = pyfunc ( s ) <EOL> got = cfunc ( s ) <EOL> self . assertEqual ( expected . dtype , got . dtype ) <EOL> self . assertEqual ( expected . shape , got . shape ) <EOL> def test_with_dtype_kws ( self ) : <EOL> for dtype in [ np . int32 , np . float32 , np . complex64 , np . dtype ( '<STR_LIT>' ) ] : <EOL> self . _test_with_dtype_kw ( dtype ) <EOL> def _test_with_shape_and_dtype_kw ( self , dtype ) : <EOL> def pyfunc ( shape ) : <EOL> return np . empty ( shape = shape , dtype = dtype ) <EOL> shapes = [ <NUM_LIT:1> , <NUM_LIT:5> , <NUM_LIT:9> ] <EOL> cfunc = nrtjit ( pyfunc ) <EOL> for s in shapes : <EOL> expected = pyfunc ( s ) <EOL> got = cfunc ( s ) <EOL> self . assertEqual ( expected . dtype , got . dtype ) <EOL> self . assertEqual ( expected . shape , got . shape ) <EOL> def test_with_shape_and_dtype_kws ( self ) : <EOL> for dtype in [ np . int32 , np . float32 , np . complex64 , np . dtype ( '<STR_LIT>' ) ] : <EOL> self . _test_with_shape_and_dtype_kw ( dtype ) <EOL> def test_empty_no_args ( self ) : <EOL> from numba . errors import TypingError <EOL> def pyfunc ( ) : <EOL> return np . empty ( ) <EOL> cfunc = nrtjit ( pyfunc ) <EOL> with self . assertRaises ( TypingError ) : <EOL> cfunc ( ) <EOL> class TestNpArray ( MemoryLeakMixin , BaseTest ) : <EOL> def test_0d ( self ) : <EOL> def pyfunc ( arg ) : <EOL> return np . array ( arg ) <EOL> cfunc = nrtjit ( pyfunc ) <EOL> got = cfunc ( <NUM_LIT> ) <EOL> self . assertPreciseEqual ( got , np . array ( <NUM_LIT> , dtype = np . intp ) ) <EOL> got = cfunc ( <NUM_LIT> ) <EOL> self . assertPreciseEqual ( got , np . array ( <NUM_LIT> ) ) <EOL> def test_0d_with_dtype ( self ) : <EOL> def pyfunc ( arg ) : <EOL> return np . array ( arg , dtype = np . int16 ) <EOL> self . check_outputs ( pyfunc , [ ( <NUM_LIT> , ) , ( <NUM_LIT> , ) ] ) <EOL> def test_1d ( self ) : <EOL> def pyfunc ( arg ) : <EOL> return np . array ( arg ) <EOL> cfunc = nrtjit ( pyfunc ) <EOL> got = cfunc ( [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT> ] ) <EOL> self . assertPreciseEqual ( got , np . intp ( [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT> ] ) ) <EOL> got = cfunc ( ( <NUM_LIT:1.0> , <NUM_LIT> , <NUM_LIT> ) ) <EOL> self . assertPreciseEqual ( got , np . array ( [ <NUM_LIT:1.0> , <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> got = cfunc ( ( ) ) <EOL> self . assertPreciseEqual ( got , np . float64 ( ( ) ) ) <EOL> def test_1d_with_dtype ( self ) : <EOL> def pyfunc ( arg ) : <EOL> return np . array ( arg , dtype = np . float32 ) <EOL> self . check_outputs ( pyfunc , <EOL> [ ( [ <NUM_LIT:2> , <NUM_LIT> ] , ) , <EOL> ( [ <NUM_LIT> , <NUM_LIT:1.0> ] , ) , <EOL> ( ( <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT> ) , ) , <EOL> ( ( ) , ) , <EOL> ] ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_2d ( self ) : <EOL> def pyfunc ( arg ) : <EOL> return np . array ( arg ) <EOL> cfunc = nrtjit ( pyfunc ) <EOL> got = cfunc ( [ ( <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:3> , <NUM_LIT:4> ) ] ) <EOL> self . assertPreciseEqual ( got , np . intp ( [ [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:4> ] ] ) ) <EOL> got = cfunc ( [ ( <NUM_LIT:1> , <NUM_LIT> ) , ( <NUM_LIT:3> , <NUM_LIT> ) ] ) <EOL> self . assertPreciseEqual ( got , np . float64 ( [ [ <NUM_LIT:1> , <NUM_LIT> ] , [ <NUM_LIT:3> , <NUM_LIT> ] ] ) ) <EOL> got = cfunc ( ( [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:4> ] ) ) <EOL> self . assertPreciseEqual ( got , np . intp ( [ [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:4> ] ] ) ) <EOL> got = cfunc ( ( [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT> , <NUM_LIT> ] ) ) <EOL> self . assertPreciseEqual ( got , np . float64 ( [ [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT> , <NUM_LIT> ] ] ) ) <EOL> got = cfunc ( ( ( <NUM_LIT> , <NUM_LIT:2> ) , ( <NUM_LIT> , <NUM_LIT> ) ) ) <EOL> self . assertPreciseEqual ( got , np . float64 ( [ [ <NUM_LIT> , <NUM_LIT:2> ] , [ <NUM_LIT> , <NUM_LIT> ] ] ) ) <EOL> got = cfunc ( ( ( ) , ( ) ) ) <EOL> self . assertPreciseEqual ( got , np . float64 ( ( ( ) , ( ) ) ) ) <EOL> def test_2d_with_dtype ( self ) : <EOL> def pyfunc ( arg ) : <EOL> return np . array ( arg , dtype = np . int32 ) <EOL> cfunc = nrtjit ( pyfunc ) <EOL> got = cfunc ( [ ( <NUM_LIT:1> , <NUM_LIT> ) , ( <NUM_LIT:3> , <NUM_LIT> ) ] ) <EOL> self . assertPreciseEqual ( got , np . int32 ( [ [ <NUM_LIT:1> , <NUM_LIT:2> ] , [ <NUM_LIT:3> , <NUM_LIT:4> ] ] ) ) <EOL> def benchmark_refct_speed ( ) : <EOL> def pyfunc ( x , y , t ) : <EOL> """<STR_LIT>""" <EOL> for i in range ( t ) : <EOL> x , y = y , x <EOL> return x , y <EOL> cfunc = nrtjit ( pyfunc ) <EOL> x = np . random . random ( <NUM_LIT:100> ) <EOL> y = np . random . random ( <NUM_LIT:100> ) <EOL> t = <NUM_LIT> <EOL> def bench_pyfunc ( ) : <EOL> pyfunc ( x , y , t ) <EOL> def bench_cfunc ( ) : <EOL> cfunc ( x , y , t ) <EOL> python_time = utils . benchmark ( bench_pyfunc ) <EOL> numba_time = utils . benchmark ( bench_cfunc ) <EOL> print ( python_time ) <EOL> print ( numba_time ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
