<s> from __future__ import print_function <EOL> import sys <EOL> import numpy as np <EOL> import numba . unittest_support as unittest <EOL> from numba . compiler import compile_isolated <EOL> from numba . numpy_support import from_dtype <EOL> from numba import types , njit , typeof , numpy_support <EOL> from . support import TestCase , CompilationCache , MemoryLeakMixin , tag <EOL> def array_dtype ( a ) : <EOL> return a . dtype <EOL> def use_dtype ( a , b ) : <EOL> return a . view ( b . dtype ) <EOL> def array_itemsize ( a ) : <EOL> return a . itemsize <EOL> def array_shape ( a , i ) : <EOL> return a . shape [ i ] <EOL> def array_strides ( a , i ) : <EOL> return a . strides [ i ] <EOL> def array_ndim ( a ) : <EOL> return a . ndim <EOL> def array_size ( a ) : <EOL> return a . size <EOL> def array_flags_contiguous ( a ) : <EOL> return a . flags . contiguous <EOL> def array_flags_c_contiguous ( a ) : <EOL> return a . flags . c_contiguous <EOL> def array_flags_f_contiguous ( a ) : <EOL> return a . flags . f_contiguous <EOL> def nested_array_itemsize ( a ) : <EOL> return a . f . itemsize <EOL> def nested_array_shape ( a ) : <EOL> return a . f . shape <EOL> def nested_array_strides ( a ) : <EOL> return a . f . strides <EOL> def nested_array_ndim ( a ) : <EOL> return a . f . ndim <EOL> def nested_array_size ( a ) : <EOL> return a . f . size <EOL> def size_after_slicing_usecase ( buf , i ) : <EOL> sliced = buf [ i ] <EOL> return sliced . size <EOL> def array_ctypes_data ( arr ) : <EOL> return arr . ctypes . data <EOL> class TestArrayAttr ( MemoryLeakMixin , TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestArrayAttr , self ) . setUp ( ) <EOL> self . ccache = CompilationCache ( ) <EOL> self . a = np . arange ( <NUM_LIT:20> , dtype = np . int32 ) . reshape ( <NUM_LIT:4> , <NUM_LIT:5> ) <EOL> def check_unary ( self , pyfunc , arr ) : <EOL> cfunc = self . get_cfunc ( pyfunc , ( typeof ( arr ) , ) ) <EOL> expected = pyfunc ( arr ) <EOL> self . assertPreciseEqual ( cfunc ( arr ) , expected ) <EOL> def check_unary_with_arrays ( self , pyfunc , <EOL> use_reshaped_empty_array = True ) : <EOL> self . check_unary ( pyfunc , self . a ) <EOL> self . check_unary ( pyfunc , self . a . T ) <EOL> self . check_unary ( pyfunc , self . a [ : : <NUM_LIT:2> ] ) <EOL> arr = np . array ( [ <NUM_LIT> ] ) . reshape ( ( ) ) <EOL> self . check_unary ( pyfunc , arr ) <EOL> arr = np . zeros ( <NUM_LIT:0> ) <EOL> self . check_unary ( pyfunc , arr ) <EOL> if use_reshaped_empty_array : <EOL> self . check_unary ( pyfunc , arr . reshape ( ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:2> ) ) ) <EOL> def get_cfunc ( self , pyfunc , argspec ) : <EOL> cres = self . ccache . compile ( pyfunc , argspec ) <EOL> return cres . entry_point <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_shape ( self ) : <EOL> pyfunc = array_shape <EOL> cfunc = self . get_cfunc ( pyfunc , ( types . int32 [ : , : ] , types . int32 ) ) <EOL> for i in range ( self . a . ndim ) : <EOL> self . assertEqual ( pyfunc ( self . a , i ) , cfunc ( self . a , i ) ) <EOL> def test_strides ( self ) : <EOL> pyfunc = array_strides <EOL> cfunc = self . get_cfunc ( pyfunc , ( types . int32 [ : , : ] , types . int32 ) ) <EOL> for i in range ( self . a . ndim ) : <EOL> self . assertEqual ( pyfunc ( self . a , i ) , cfunc ( self . a , i ) ) <EOL> def test_ndim ( self ) : <EOL> self . check_unary_with_arrays ( array_ndim ) <EOL> def test_size ( self ) : <EOL> self . check_unary_with_arrays ( array_size ) <EOL> def test_itemsize ( self ) : <EOL> self . check_unary_with_arrays ( array_itemsize ) <EOL> def test_dtype ( self ) : <EOL> pyfunc = array_dtype <EOL> self . check_unary ( pyfunc , self . a ) <EOL> dtype = np . dtype ( [ ( '<STR_LIT:x>' , np . int8 ) , ( '<STR_LIT:y>' , np . int8 ) ] ) <EOL> arr = np . zeros ( <NUM_LIT:4> , dtype = dtype ) <EOL> self . check_unary ( pyfunc , arr ) <EOL> def test_use_dtype ( self ) : <EOL> b = np . empty ( <NUM_LIT:1> , dtype = np . int16 ) <EOL> pyfunc = use_dtype <EOL> cfunc = self . get_cfunc ( pyfunc , ( typeof ( self . a ) , typeof ( b ) ) ) <EOL> expected = pyfunc ( self . a , b ) <EOL> self . assertPreciseEqual ( cfunc ( self . a , b ) , expected ) <EOL> def test_flags_contiguous ( self ) : <EOL> self . check_unary_with_arrays ( array_flags_contiguous ) <EOL> def test_flags_c_contiguous ( self ) : <EOL> self . check_unary_with_arrays ( array_flags_c_contiguous ) <EOL> def test_flags_f_contiguous ( self ) : <EOL> use_reshaped_empty_array = numpy_support . version < ( <NUM_LIT:1> , <NUM_LIT:10> ) <EOL> self . check_unary_with_arrays ( array_flags_f_contiguous , <EOL> use_reshaped_empty_array = use_reshaped_empty_array ) <EOL> class TestNestedArrayAttr ( MemoryLeakMixin , unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestNestedArrayAttr , self ) . setUp ( ) <EOL> dtype = np . dtype ( [ ( '<STR_LIT:a>' , np . int32 ) , ( '<STR_LIT:f>' , np . int32 , ( <NUM_LIT:2> , <NUM_LIT:5> ) ) ] ) <EOL> self . a = np . recarray ( <NUM_LIT:1> , dtype ) [ <NUM_LIT:0> ] <EOL> self . nbrecord = from_dtype ( self . a . dtype ) <EOL> def get_cfunc ( self , pyfunc ) : <EOL> cres = compile_isolated ( pyfunc , ( self . nbrecord , ) ) <EOL> return cres . entry_point <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_shape ( self ) : <EOL> pyfunc = nested_array_shape <EOL> cfunc = self . get_cfunc ( pyfunc ) <EOL> self . assertEqual ( pyfunc ( self . a ) , cfunc ( self . a ) ) <EOL> def test_strides ( self ) : <EOL> pyfunc = nested_array_strides <EOL> cfunc = self . get_cfunc ( pyfunc ) <EOL> self . assertEqual ( pyfunc ( self . a ) , cfunc ( self . a ) ) <EOL> def test_ndim ( self ) : <EOL> pyfunc = nested_array_ndim <EOL> cfunc = self . get_cfunc ( pyfunc ) <EOL> self . assertEqual ( pyfunc ( self . a ) , cfunc ( self . a ) ) <EOL> def test_size ( self ) : <EOL> pyfunc = nested_array_size <EOL> cfunc = self . get_cfunc ( pyfunc ) <EOL> self . assertEqual ( pyfunc ( self . a ) , cfunc ( self . a ) ) <EOL> def test_itemsize ( self ) : <EOL> pyfunc = nested_array_itemsize <EOL> cfunc = self . get_cfunc ( pyfunc ) <EOL> self . assertEqual ( pyfunc ( self . a ) , cfunc ( self . a ) ) <EOL> class TestSlicedArrayAttr ( MemoryLeakMixin , unittest . TestCase ) : <EOL> def test_size_after_slicing ( self ) : <EOL> pyfunc = size_after_slicing_usecase <EOL> cfunc = njit ( pyfunc ) <EOL> arr = np . arange ( <NUM_LIT:2> * <NUM_LIT:5> ) . reshape ( <NUM_LIT:2> , <NUM_LIT:5> ) <EOL> for i in range ( arr . shape [ <NUM_LIT:0> ] ) : <EOL> self . assertEqual ( pyfunc ( arr , i ) , cfunc ( arr , i ) ) <EOL> arr = np . arange ( <NUM_LIT:2> * <NUM_LIT:5> * <NUM_LIT:3> ) . reshape ( <NUM_LIT:2> , <NUM_LIT:5> , <NUM_LIT:3> ) <EOL> for i in range ( arr . shape [ <NUM_LIT:0> ] ) : <EOL> self . assertEqual ( pyfunc ( arr , i ) , cfunc ( arr , i ) ) <EOL> class TestArrayCTypes ( MemoryLeakMixin , unittest . TestCase ) : <EOL> def test_array_ctypes_data ( self ) : <EOL> pyfunc = array_ctypes_data <EOL> cfunc = njit ( pyfunc ) <EOL> arr = np . arange ( <NUM_LIT:3> ) <EOL> self . assertEqual ( pyfunc ( arr ) , cfunc ( arr ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
