<s> from __future__ import print_function <EOL> from functools import partial <EOL> import numba . unittest_support as unittest <EOL> import numpy as np <EOL> from numba . compiler import compile_isolated , Flags <EOL> from numba import types , from_dtype , errors , typeof <EOL> from . support import TestCase , MemoryLeakMixin , CompilationCache , tag <EOL> enable_pyobj_flags = Flags ( ) <EOL> enable_pyobj_flags . set ( "<STR_LIT>" ) <EOL> no_pyobj_flags = Flags ( ) <EOL> no_pyobj_flags . set ( '<STR_LIT>' ) <EOL> def array_reshape ( arr , newshape ) : <EOL> return arr . reshape ( newshape ) <EOL> def flatten_array ( a ) : <EOL> return a . flatten ( ) <EOL> def ravel_array ( a ) : <EOL> return a . ravel ( ) <EOL> def ravel_array_size ( a ) : <EOL> return a . ravel ( ) . size <EOL> def numpy_ravel_array ( a ) : <EOL> return np . ravel ( a ) <EOL> def transpose_array ( a ) : <EOL> return a . transpose ( ) <EOL> def squeeze_array ( a ) : <EOL> return a . squeeze ( ) <EOL> def add_axis1 ( a ) : <EOL> return np . expand_dims ( a , axis = <NUM_LIT:0> ) <EOL> def add_axis2 ( a ) : <EOL> return a [ np . newaxis , : ] <EOL> def bad_index ( arr , arr2d ) : <EOL> x = arr . x , <EOL> y = arr . y <EOL> arr2d [ x , y ] = <NUM_LIT:1.0> <EOL> def bad_float_index ( arr ) : <EOL> return arr [ <NUM_LIT:1> , <NUM_LIT> ] <EOL> class TestArrayManipulation ( MemoryLeakMixin , TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> super ( TestArrayManipulation , self ) . setUp ( ) <EOL> self . ccache = CompilationCache ( ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_array_reshape ( self ) : <EOL> pyfunc = array_reshape <EOL> def run ( arr , shape ) : <EOL> cres = self . ccache . compile ( pyfunc , ( typeof ( arr ) , typeof ( shape ) ) ) <EOL> return cres . entry_point ( arr , shape ) <EOL> def check ( arr , shape ) : <EOL> expected = pyfunc ( arr , shape ) <EOL> self . memory_leak_setup ( ) <EOL> got = run ( arr , shape ) <EOL> self . assertPreciseEqual ( got , expected ) <EOL> del got <EOL> self . memory_leak_teardown ( ) <EOL> def check_err_shape ( arr , shape ) : <EOL> with self . assertRaises ( NotImplementedError ) as raises : <EOL> run ( arr , shape ) <EOL> self . assertEqual ( str ( raises . exception ) , <EOL> "<STR_LIT>" ) <EOL> def check_err_size ( arr , shape ) : <EOL> with self . assertRaises ( ValueError ) as raises : <EOL> run ( arr , shape ) <EOL> self . assertEqual ( str ( raises . exception ) , <EOL> "<STR_LIT>" ) <EOL> def check_err_multiple_negative ( arr , shape ) : <EOL> with self . assertRaises ( ValueError ) as raises : <EOL> run ( arr , shape ) <EOL> self . assertEqual ( str ( raises . exception ) , <EOL> "<STR_LIT>" ) <EOL> arr = np . arange ( <NUM_LIT> ) <EOL> check ( arr , ( <NUM_LIT> , ) ) <EOL> check ( arr , ( <NUM_LIT:4> , <NUM_LIT:6> ) ) <EOL> check ( arr , ( <NUM_LIT:8> , <NUM_LIT:3> ) ) <EOL> check ( arr , ( <NUM_LIT:8> , <NUM_LIT:1> , <NUM_LIT:3> ) ) <EOL> check ( arr , ( <NUM_LIT:1> , <NUM_LIT:8> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:1> ) ) <EOL> arr = np . arange ( <NUM_LIT> ) . reshape ( ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) <EOL> check ( arr , ( <NUM_LIT> , ) ) <EOL> check ( arr , ( <NUM_LIT:4> , <NUM_LIT:6> ) ) <EOL> check ( arr , ( <NUM_LIT:8> , <NUM_LIT:3> ) ) <EOL> check ( arr , ( <NUM_LIT:8> , <NUM_LIT:1> , <NUM_LIT:3> ) ) <EOL> check ( arr , ( <NUM_LIT:1> , <NUM_LIT:8> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:1> ) ) <EOL> check_err_size ( arr , ( <NUM_LIT> , ) ) <EOL> check_err_size ( arr , ( <NUM_LIT:8> , <NUM_LIT:4> ) ) <EOL> arr = np . arange ( <NUM_LIT> ) . reshape ( ( <NUM_LIT:1> , <NUM_LIT:8> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:1> ) ) <EOL> check ( arr , ( <NUM_LIT> , ) ) <EOL> check ( arr , ( <NUM_LIT:4> , <NUM_LIT:6> ) ) <EOL> check ( arr , ( <NUM_LIT:8> , <NUM_LIT:3> ) ) <EOL> check ( arr , ( <NUM_LIT:8> , <NUM_LIT:1> , <NUM_LIT:3> ) ) <EOL> arr = np . arange ( <NUM_LIT> ) . reshape ( ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) . T <EOL> check ( arr , ( <NUM_LIT:4> , <NUM_LIT:3> , <NUM_LIT:2> ) ) <EOL> check ( arr , ( <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:1> ) ) <EOL> check_err_shape ( arr , ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) <EOL> check_err_shape ( arr , ( <NUM_LIT:6> , <NUM_LIT:4> ) ) <EOL> check_err_shape ( arr , ( <NUM_LIT:2> , <NUM_LIT:12> ) ) <EOL> arr = np . arange ( <NUM_LIT> ) . reshape ( <NUM_LIT:5> , <NUM_LIT:5> ) <EOL> check ( arr , - <NUM_LIT:1> ) <EOL> check ( arr , ( - <NUM_LIT:1> , ) ) <EOL> check ( arr , ( - <NUM_LIT:1> , <NUM_LIT:5> ) ) <EOL> check ( arr , ( <NUM_LIT:5> , - <NUM_LIT:1> , <NUM_LIT:5> ) ) <EOL> check ( arr , ( <NUM_LIT:5> , <NUM_LIT:5> , - <NUM_LIT:1> ) ) <EOL> check_err_size ( arr , ( - <NUM_LIT:1> , <NUM_LIT:4> ) ) <EOL> check_err_multiple_negative ( arr , ( - <NUM_LIT:1> , - <NUM_LIT:2> , <NUM_LIT:5> , <NUM_LIT:5> ) ) <EOL> check_err_multiple_negative ( arr , ( <NUM_LIT:5> , <NUM_LIT:5> , - <NUM_LIT:1> , - <NUM_LIT:1> ) ) <EOL> self . disable_leak_check ( ) <EOL> def test_flatten_array ( self , flags = enable_pyobj_flags , layout = '<STR_LIT:C>' ) : <EOL> a = np . arange ( <NUM_LIT:9> ) . reshape ( <NUM_LIT:3> , <NUM_LIT:3> ) <EOL> if layout == '<STR_LIT:F>' : <EOL> a = a . T <EOL> pyfunc = flatten_array <EOL> arraytype1 = typeof ( a ) <EOL> if layout == '<STR_LIT:A>' : <EOL> arraytype1 = arraytype1 . copy ( layout = '<STR_LIT:A>' ) <EOL> self . assertEqual ( arraytype1 . layout , layout ) <EOL> cr = compile_isolated ( pyfunc , ( arraytype1 , ) , flags = flags ) <EOL> cfunc = cr . entry_point <EOL> expected = pyfunc ( a ) <EOL> got = cfunc ( a ) <EOL> np . testing . assert_equal ( expected , got ) <EOL> def test_flatten_array_npm ( self ) : <EOL> self . test_flatten_array ( flags = no_pyobj_flags ) <EOL> self . test_flatten_array ( flags = no_pyobj_flags , layout = '<STR_LIT:F>' ) <EOL> self . test_flatten_array ( flags = no_pyobj_flags , layout = '<STR_LIT:A>' ) <EOL> def test_ravel_array ( self , flags = enable_pyobj_flags ) : <EOL> def generic_check ( pyfunc , a , assume_layout ) : <EOL> arraytype1 = typeof ( a ) <EOL> self . assertEqual ( arraytype1 . layout , assume_layout ) <EOL> cr = compile_isolated ( pyfunc , ( arraytype1 , ) , flags = flags ) <EOL> cfunc = cr . entry_point <EOL> expected = pyfunc ( a ) <EOL> got = cfunc ( a ) <EOL> np . testing . assert_equal ( expected , got ) <EOL> py_copied = ( a . ctypes . data != expected . ctypes . data ) <EOL> nb_copied = ( a . ctypes . data != got . ctypes . data ) <EOL> self . assertEqual ( py_copied , assume_layout != '<STR_LIT:C>' ) <EOL> self . assertEqual ( py_copied , nb_copied ) <EOL> check_method = partial ( generic_check , ravel_array ) <EOL> check_function = partial ( generic_check , numpy_ravel_array ) <EOL> def check ( * args , ** kwargs ) : <EOL> check_method ( * args , ** kwargs ) <EOL> check_function ( * args , ** kwargs ) <EOL> check ( np . arange ( <NUM_LIT:9> ) . reshape ( <NUM_LIT:3> , <NUM_LIT:3> ) , assume_layout = '<STR_LIT:C>' ) <EOL> check ( np . arange ( <NUM_LIT:9> ) . reshape ( <NUM_LIT:3> , <NUM_LIT:3> , order = '<STR_LIT:F>' ) , assume_layout = '<STR_LIT:F>' ) <EOL> check ( np . arange ( <NUM_LIT> ) . reshape ( <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:2> ) [ : , : , <NUM_LIT:0> ] , assume_layout = '<STR_LIT:A>' ) <EOL> check ( np . arange ( <NUM_LIT> ) . reshape ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:3> ) , assume_layout = '<STR_LIT:C>' ) <EOL> check ( np . arange ( <NUM_LIT> ) . reshape ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:3> , order = '<STR_LIT:F>' ) , assume_layout = '<STR_LIT:F>' ) <EOL> check ( np . arange ( <NUM_LIT> ) . reshape ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT:2> ) [ : , : , : , <NUM_LIT:0> ] , assume_layout = '<STR_LIT:A>' ) <EOL> def test_ravel_array_size ( self , flags = enable_pyobj_flags ) : <EOL> a = np . arange ( <NUM_LIT:9> ) . reshape ( <NUM_LIT:3> , <NUM_LIT:3> ) <EOL> pyfunc = ravel_array_size <EOL> arraytype1 = typeof ( a ) <EOL> cr = compile_isolated ( pyfunc , ( arraytype1 , ) , flags = flags ) <EOL> cfunc = cr . entry_point <EOL> expected = pyfunc ( a ) <EOL> got = cfunc ( a ) <EOL> np . testing . assert_equal ( expected , got ) <EOL> def test_ravel_array_npm ( self ) : <EOL> self . test_ravel_array ( flags = no_pyobj_flags ) <EOL> def test_ravel_array_size_npm ( self ) : <EOL> self . test_ravel_array_size ( flags = no_pyobj_flags ) <EOL> def test_transpose_array ( self , flags = enable_pyobj_flags ) : <EOL> a = np . arange ( <NUM_LIT:9> ) . reshape ( <NUM_LIT:3> , <NUM_LIT:3> ) <EOL> pyfunc = transpose_array <EOL> arraytype1 = typeof ( a ) <EOL> cr = compile_isolated ( pyfunc , ( arraytype1 , ) , flags = flags ) <EOL> cfunc = cr . entry_point <EOL> expected = pyfunc ( a ) <EOL> got = cfunc ( a ) <EOL> np . testing . assert_equal ( expected , got ) <EOL> def test_transpose_array_npm ( self ) : <EOL> self . test_transpose_array ( flags = no_pyobj_flags ) <EOL> def test_squeeze_array ( self , flags = enable_pyobj_flags ) : <EOL> a = np . arange ( <NUM_LIT:2> * <NUM_LIT:1> * <NUM_LIT:3> * <NUM_LIT:1> * <NUM_LIT:4> ) . reshape ( <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:4> ) <EOL> pyfunc = squeeze_array <EOL> arraytype1 = typeof ( a ) <EOL> cr = compile_isolated ( pyfunc , ( arraytype1 , ) , flags = flags ) <EOL> cfunc = cr . entry_point <EOL> expected = pyfunc ( a ) <EOL> got = cfunc ( a ) <EOL> np . testing . assert_equal ( expected , got ) <EOL> def test_squeeze_array_npm ( self ) : <EOL> with self . assertRaises ( errors . UntypedAttributeError ) as raises : <EOL> self . test_squeeze_array ( flags = no_pyobj_flags ) <EOL> self . assertIn ( "<STR_LIT>" , str ( raises . exception ) ) <EOL> def test_add_axis1 ( self , flags = enable_pyobj_flags ) : <EOL> a = np . arange ( <NUM_LIT:9> ) . reshape ( <NUM_LIT:3> , <NUM_LIT:3> ) <EOL> pyfunc = add_axis1 <EOL> arraytype1 = typeof ( a ) <EOL> cr = compile_isolated ( pyfunc , ( arraytype1 , ) , flags = flags ) <EOL> cfunc = cr . entry_point <EOL> expected = pyfunc ( a ) <EOL> got = cfunc ( a ) <EOL> np . testing . assert_equal ( expected , got ) <EOL> def test_add_axis1_npm ( self ) : <EOL> with self . assertRaises ( errors . UntypedAttributeError ) as raises : <EOL> self . test_add_axis1 ( flags = no_pyobj_flags ) <EOL> self . assertIn ( "<STR_LIT>" , str ( raises . exception ) ) <EOL> def test_add_axis2 ( self , flags = enable_pyobj_flags ) : <EOL> a = np . arange ( <NUM_LIT:9> ) . reshape ( <NUM_LIT:3> , <NUM_LIT:3> ) <EOL> pyfunc = add_axis2 <EOL> arraytype1 = typeof ( a ) <EOL> cr = compile_isolated ( pyfunc , ( arraytype1 , ) , flags = flags ) <EOL> cfunc = cr . entry_point <EOL> expected = pyfunc ( a ) <EOL> got = cfunc ( a ) <EOL> np . testing . assert_equal ( expected , got ) <EOL> def test_add_axis2_npm ( self ) : <EOL> with self . assertTypingError ( ) as raises : <EOL> self . test_add_axis2 ( flags = no_pyobj_flags ) <EOL> self . assertIn ( "<STR_LIT>" , <EOL> str ( raises . exception ) ) <EOL> def test_bad_index_npm ( self ) : <EOL> with self . assertTypingError ( ) as raises : <EOL> arraytype1 = from_dtype ( np . dtype ( [ ( '<STR_LIT:x>' , np . int32 ) , <EOL> ( '<STR_LIT:y>' , np . int32 ) ] ) ) <EOL> arraytype2 = types . Array ( types . int32 , <NUM_LIT:2> , '<STR_LIT:C>' ) <EOL> compile_isolated ( bad_index , ( arraytype1 , arraytype2 ) , <EOL> flags = no_pyobj_flags ) <EOL> self . assertIn ( '<STR_LIT>' , str ( raises . exception ) ) <EOL> def test_bad_float_index_npm ( self ) : <EOL> with self . assertTypingError ( ) as raises : <EOL> compile_isolated ( bad_float_index , <EOL> ( types . Array ( types . float64 , <NUM_LIT:2> , '<STR_LIT:C>' ) , ) ) <EOL> self . assertIn ( '<STR_LIT>' , <EOL> str ( raises . exception ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
