<s> from __future__ import print_function , division , absolute_import <EOL> import sys <EOL> import numpy as np <EOL> from numba import jit , numpy_support , types <EOL> from numba import unittest_support as unittest <EOL> from numba . compiler import compile_isolated <EOL> from numba . funcdesc import transform_arg_name <EOL> from numba . utils import IS_PY3 <EOL> from . support import tag <EOL> def get_a ( ary , i ) : <EOL> return ary [ i ] . a <EOL> def get_b ( ary , i ) : <EOL> return ary [ i ] . b <EOL> def get_c ( ary , i ) : <EOL> return ary [ i ] . c <EOL> def make_getitem ( item ) : <EOL> def get_xx ( ary , i ) : <EOL> return ary [ i ] [ item ] <EOL> return get_xx <EOL> def get_zero_a ( ary , _unused ) : <EOL> return ary [ <NUM_LIT:0> ] . a <EOL> getitem_a = make_getitem ( '<STR_LIT:a>' ) <EOL> getitem_b = make_getitem ( '<STR_LIT:b>' ) <EOL> getitem_c = make_getitem ( '<STR_LIT:c>' ) <EOL> def get_a_subarray ( ary , i ) : <EOL> return ary . a [ i ] <EOL> def get_b_subarray ( ary , i ) : <EOL> return ary . b [ i ] <EOL> def get_c_subarray ( ary , i ) : <EOL> return ary . c [ i ] <EOL> def get_a_zero ( ary , _unused ) : <EOL> return ary . a [ <NUM_LIT:0> ] <EOL> def make_getitem_subarray ( item ) : <EOL> def get_xx_subarray ( ary , i ) : <EOL> return ary [ item ] [ i ] <EOL> return get_xx_subarray <EOL> getitem_a_subarray = make_getitem_subarray ( '<STR_LIT:a>' ) <EOL> getitem_b_subarray = make_getitem_subarray ( '<STR_LIT:b>' ) <EOL> getitem_c_subarray = make_getitem_subarray ( '<STR_LIT:c>' ) <EOL> def get_two_arrays_a ( ary1 , ary2 , i ) : <EOL> return ary1 [ i ] . a + ary2 [ i ] . a <EOL> def get_two_arrays_b ( ary1 , ary2 , i ) : <EOL> return ary1 [ i ] . b + ary2 [ i ] . b <EOL> def get_two_arrays_c ( ary1 , ary2 , i ) : <EOL> return ary1 [ i ] . c + ary2 [ i ] . c <EOL> def get_two_arrays_distinct ( ary1 , ary2 , i ) : <EOL> return ary1 [ i ] . a + ary2 [ i ] . f <EOL> def set_a ( ary , i , v ) : <EOL> ary [ i ] . a = v <EOL> def set_b ( ary , i , v ) : <EOL> ary [ i ] . b = v <EOL> def set_c ( ary , i , v ) : <EOL> ary [ i ] . c = v <EOL> def make_setitem ( item ) : <EOL> def set_xx ( ary , i , v ) : <EOL> ary [ i ] [ item ] = v <EOL> return set_xx <EOL> setitem_a = make_setitem ( '<STR_LIT:a>' ) <EOL> setitem_b = make_setitem ( '<STR_LIT:b>' ) <EOL> setitem_c = make_setitem ( '<STR_LIT:c>' ) <EOL> def set_a_subarray ( ary , i , v ) : <EOL> ary . a [ i ] = v <EOL> def set_b_subarray ( ary , i , v ) : <EOL> ary . b [ i ] = v <EOL> def set_c_subarray ( ary , i , v ) : <EOL> ary . c [ i ] = v <EOL> def make_setitem_subarray ( item ) : <EOL> def set_xx_subarray ( ary , i , v ) : <EOL> ary [ item ] [ i ] = v <EOL> return set_xx_subarray <EOL> setitem_a_subarray = make_setitem ( '<STR_LIT:a>' ) <EOL> setitem_b_subarray = make_setitem ( '<STR_LIT:b>' ) <EOL> setitem_c_subarray = make_setitem ( '<STR_LIT:c>' ) <EOL> def set_record ( ary , i , j ) : <EOL> ary [ i ] = ary [ j ] <EOL> def get_record_a ( rec , val ) : <EOL> x = rec . a <EOL> rec . a = val <EOL> return x <EOL> def get_record_b ( rec , val ) : <EOL> x = rec . b <EOL> rec . b = val <EOL> return x <EOL> def get_record_c ( rec , val ) : <EOL> x = rec . c <EOL> rec . c = val <EOL> return x <EOL> def get_record_rev_a ( val , rec ) : <EOL> x = rec . a <EOL> rec . a = val <EOL> return x <EOL> def get_record_rev_b ( val , rec ) : <EOL> x = rec . b <EOL> rec . b = val <EOL> return x <EOL> def get_record_rev_c ( val , rec ) : <EOL> x = rec . c <EOL> rec . c = val <EOL> return x <EOL> def get_two_records_a ( rec1 , rec2 ) : <EOL> x = rec1 . a + rec2 . a <EOL> return x <EOL> def get_two_records_b ( rec1 , rec2 ) : <EOL> x = rec1 . b + rec2 . b <EOL> return x <EOL> def get_two_records_c ( rec1 , rec2 ) : <EOL> x = rec1 . c + rec2 . c <EOL> return x <EOL> def get_two_records_distinct ( rec1 , rec2 ) : <EOL> x = rec1 . a + rec2 . f <EOL> return x <EOL> def record_return ( ary , i ) : <EOL> return ary [ i ] <EOL> def record_write_array ( ary ) : <EOL> ary . g = <NUM_LIT:2> <EOL> ary . h [ <NUM_LIT:0> ] = <NUM_LIT> <EOL> ary . h [ <NUM_LIT:1> ] = <NUM_LIT> <EOL> def record_write_2d_array ( ary ) : <EOL> ary . i = <NUM_LIT:3> <EOL> ary . j [ <NUM_LIT:0> , <NUM_LIT:0> ] = <NUM_LIT> <EOL> ary . j [ <NUM_LIT:0> , <NUM_LIT:1> ] = <NUM_LIT> <EOL> ary . j [ <NUM_LIT:1> , <NUM_LIT:0> ] = <NUM_LIT> <EOL> ary . j [ <NUM_LIT:1> , <NUM_LIT:1> ] = <NUM_LIT> <EOL> ary . j [ <NUM_LIT:2> , <NUM_LIT:0> ] = <NUM_LIT> <EOL> ary . j [ <NUM_LIT:2> , <NUM_LIT:1> ] = <NUM_LIT> <EOL> def record_read_array0 ( ary ) : <EOL> return ary . h [ <NUM_LIT:0> ] <EOL> def record_read_array1 ( ary ) : <EOL> return ary . h [ <NUM_LIT:1> ] <EOL> def record_read_2d_array00 ( ary ) : <EOL> return ary . j [ <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> def record_read_2d_array10 ( ary ) : <EOL> return ary . j [ <NUM_LIT:1> , <NUM_LIT:0> ] <EOL> def record_read_2d_array01 ( ary ) : <EOL> return ary . j [ <NUM_LIT:0> , <NUM_LIT:1> ] <EOL> def record_read_first_arr ( ary ) : <EOL> return ary . k [ <NUM_LIT:2> , <NUM_LIT:2> ] <EOL> def record_read_second_arr ( ary ) : <EOL> return ary . l [ <NUM_LIT:2> , <NUM_LIT:2> ] <EOL> def get_charseq ( ary , i ) : <EOL> return ary [ i ] . n <EOL> def set_charseq ( ary , i , cs ) : <EOL> ary [ i ] . n = cs <EOL> def get_charseq_tuple ( ary , i ) : <EOL> return ary [ i ] . m , ary [ i ] . n <EOL> recordtype = np . dtype ( [ ( '<STR_LIT:a>' , np . float64 ) , <EOL> ( '<STR_LIT:b>' , np . int16 ) , <EOL> ( '<STR_LIT:c>' , np . complex64 ) , <EOL> ( '<STR_LIT:d>' , ( np . str , <NUM_LIT:5> ) ) ] ) <EOL> recordtype2 = np . dtype ( [ ( '<STR_LIT:e>' , np . int32 ) , <EOL> ( '<STR_LIT:f>' , np . float64 ) ] , align = True ) <EOL> recordtype3 = np . dtype ( [ ( '<STR_LIT>' , np . float32 ) , <EOL> ( '<STR_LIT>' , np . float64 ) ] ) <EOL> recordwitharray = np . dtype ( [ ( '<STR_LIT:g>' , np . int32 ) , <EOL> ( '<STR_LIT:h>' , np . float32 , <NUM_LIT:2> ) ] ) <EOL> recordwith2darray = np . dtype ( [ ( '<STR_LIT:i>' , np . int32 ) , <EOL> ( '<STR_LIT>' , np . float32 , ( <NUM_LIT:3> , <NUM_LIT:2> ) ) ] ) <EOL> recordwith2arrays = np . dtype ( [ ( '<STR_LIT:k>' , np . int32 , ( <NUM_LIT:10> , <NUM_LIT:20> ) ) , <EOL> ( '<STR_LIT:l>' , np . int32 , ( <NUM_LIT:6> , <NUM_LIT:12> ) ) ] ) <EOL> recordwithcharseq = np . dtype ( [ ( '<STR_LIT:m>' , np . int32 ) , <EOL> ( '<STR_LIT:n>' , '<STR_LIT>' ) ] ) <EOL> class TestRecordDtype ( unittest . TestCase ) : <EOL> def _createSampleArrays ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . refsample1d = np . recarray ( <NUM_LIT:3> , dtype = recordtype ) <EOL> self . refsample1d2 = np . recarray ( <NUM_LIT:3> , dtype = recordtype2 ) <EOL> self . refsample1d3 = np . recarray ( <NUM_LIT:3> , dtype = recordtype ) <EOL> self . nbsample1d = np . recarray ( <NUM_LIT:3> , dtype = recordtype ) <EOL> self . nbsample1d2 = np . recarray ( <NUM_LIT:3> , dtype = recordtype2 ) <EOL> self . nbsample1d3 = np . recarray ( <NUM_LIT:3> , dtype = recordtype ) <EOL> def setUp ( self ) : <EOL> self . _createSampleArrays ( ) <EOL> for ary in ( self . refsample1d , self . nbsample1d ) : <EOL> for i in range ( ary . size ) : <EOL> x = i + <NUM_LIT:1> <EOL> ary [ i ] [ '<STR_LIT:a>' ] = x / <NUM_LIT:2> <EOL> ary [ i ] [ '<STR_LIT:b>' ] = x <EOL> ary [ i ] [ '<STR_LIT:c>' ] = x * <NUM_LIT> <EOL> ary [ i ] [ '<STR_LIT:d>' ] = "<STR_LIT>" % x <EOL> for ary2 in ( self . refsample1d2 , self . nbsample1d2 ) : <EOL> for i in range ( ary2 . size ) : <EOL> x = i + <NUM_LIT:5> <EOL> ary2 [ i ] [ '<STR_LIT:e>' ] = x <EOL> ary2 [ i ] [ '<STR_LIT:f>' ] = x / <NUM_LIT:2> <EOL> for ary3 in ( self . refsample1d3 , self . nbsample1d3 ) : <EOL> for i in range ( ary3 . size ) : <EOL> x = i + <NUM_LIT:10> <EOL> ary3 [ i ] [ '<STR_LIT:a>' ] = x / <NUM_LIT:2> <EOL> ary3 [ i ] [ '<STR_LIT:b>' ] = x <EOL> ary3 [ i ] [ '<STR_LIT:c>' ] = x * <NUM_LIT> <EOL> ary3 [ i ] [ '<STR_LIT:d>' ] = "<STR_LIT>" % x <EOL> def get_cfunc ( self , pyfunc , argspec ) : <EOL> cres = compile_isolated ( pyfunc , argspec ) <EOL> return cres . entry_point <EOL> def test_from_dtype ( self ) : <EOL> rec = numpy_support . from_dtype ( recordtype ) <EOL> self . assertEqual ( rec . typeof ( '<STR_LIT:a>' ) , types . float64 ) <EOL> self . assertEqual ( rec . typeof ( '<STR_LIT:b>' ) , types . int16 ) <EOL> self . assertEqual ( rec . typeof ( '<STR_LIT:c>' ) , types . complex64 ) <EOL> if IS_PY3 : <EOL> self . assertEqual ( rec . typeof ( '<STR_LIT:d>' ) , types . UnicodeCharSeq ( <NUM_LIT:5> ) ) <EOL> else : <EOL> self . assertEqual ( rec . typeof ( '<STR_LIT:d>' ) , types . CharSeq ( <NUM_LIT:5> ) ) <EOL> self . assertEqual ( rec . offset ( '<STR_LIT:a>' ) , recordtype . fields [ '<STR_LIT:a>' ] [ <NUM_LIT:1> ] ) <EOL> self . assertEqual ( rec . offset ( '<STR_LIT:b>' ) , recordtype . fields [ '<STR_LIT:b>' ] [ <NUM_LIT:1> ] ) <EOL> self . assertEqual ( rec . offset ( '<STR_LIT:c>' ) , recordtype . fields [ '<STR_LIT:c>' ] [ <NUM_LIT:1> ] ) <EOL> self . assertEqual ( rec . offset ( '<STR_LIT:d>' ) , recordtype . fields [ '<STR_LIT:d>' ] [ <NUM_LIT:1> ] ) <EOL> self . assertEqual ( recordtype . itemsize , rec . size ) <EOL> def _test_get_equal ( self , pyfunc ) : <EOL> rec = numpy_support . from_dtype ( recordtype ) <EOL> cfunc = self . get_cfunc ( pyfunc , ( rec [ : ] , types . intp ) ) <EOL> for i in range ( self . refsample1d . size ) : <EOL> self . assertEqual ( pyfunc ( self . refsample1d , i ) , cfunc ( self . nbsample1d , i ) ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_get_a ( self ) : <EOL> self . _test_get_equal ( get_a ) <EOL> self . _test_get_equal ( get_a_subarray ) <EOL> self . _test_get_equal ( getitem_a ) <EOL> self . _test_get_equal ( getitem_a_subarray ) <EOL> self . _test_get_equal ( get_a_zero ) <EOL> self . _test_get_equal ( get_zero_a ) <EOL> def test_get_b ( self ) : <EOL> self . _test_get_equal ( get_b ) <EOL> self . _test_get_equal ( get_b_subarray ) <EOL> self . _test_get_equal ( getitem_b ) <EOL> self . _test_get_equal ( getitem_b_subarray ) <EOL> def test_get_c ( self ) : <EOL> self . _test_get_equal ( get_c ) <EOL> self . _test_get_equal ( get_c_subarray ) <EOL> self . _test_get_equal ( getitem_c ) <EOL> self . _test_get_equal ( getitem_c_subarray ) <EOL> def _test_get_two_equal ( self , pyfunc ) : <EOL> '''<STR_LIT>''' <EOL> rec = numpy_support . from_dtype ( recordtype ) <EOL> cfunc = self . get_cfunc ( pyfunc , ( rec [ : ] , rec [ : ] , types . intp ) ) <EOL> for i in range ( self . refsample1d . size ) : <EOL> self . assertEqual ( pyfunc ( self . refsample1d , self . refsample1d3 , i ) , <EOL> cfunc ( self . nbsample1d , self . nbsample1d3 , i ) ) <EOL> def test_two_distinct_arrays ( self ) : <EOL> '''<STR_LIT>''' <EOL> pyfunc = get_two_arrays_distinct <EOL> rec1 = numpy_support . from_dtype ( recordtype ) <EOL> rec2 = numpy_support . from_dtype ( recordtype2 ) <EOL> cfunc = self . get_cfunc ( pyfunc , ( rec1 [ : ] , rec2 [ : ] , types . intp ) ) <EOL> for i in range ( self . refsample1d . size ) : <EOL> pres = pyfunc ( self . refsample1d , self . refsample1d2 , i ) <EOL> cres = cfunc ( self . nbsample1d , self . nbsample1d2 , i ) <EOL> self . assertEqual ( pres , cres ) <EOL> def test_get_two_a ( self ) : <EOL> self . _test_get_two_equal ( get_two_arrays_a ) <EOL> def test_get_two_b ( self ) : <EOL> self . _test_get_two_equal ( get_two_arrays_b ) <EOL> def test_get_two_c ( self ) : <EOL> self . _test_get_two_equal ( get_two_arrays_c ) <EOL> def _test_set_equal ( self , pyfunc , value , valuetype ) : <EOL> rec = numpy_support . from_dtype ( recordtype ) <EOL> cfunc = self . get_cfunc ( pyfunc , ( rec [ : ] , types . intp , valuetype ) ) <EOL> for i in range ( self . refsample1d . size ) : <EOL> expect = self . refsample1d . copy ( ) <EOL> pyfunc ( expect , i , value ) <EOL> got = self . nbsample1d . copy ( ) <EOL> cfunc ( got , i , value ) <EOL> self . assertTrue ( np . all ( expect == got ) ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_set_a ( self ) : <EOL> def check ( pyfunc ) : <EOL> self . _test_set_equal ( pyfunc , <NUM_LIT> , types . float64 ) <EOL> self . _test_set_equal ( pyfunc , <NUM_LIT> , types . float32 ) <EOL> check ( set_a ) <EOL> check ( set_a_subarray ) <EOL> check ( setitem_a ) <EOL> check ( setitem_a_subarray ) <EOL> def test_set_b ( self ) : <EOL> def check ( pyfunc ) : <EOL> self . _test_set_equal ( pyfunc , <NUM_LIT> , types . int32 ) <EOL> self . _test_set_equal ( pyfunc , <NUM_LIT> , types . float64 ) <EOL> check ( set_b ) <EOL> check ( set_b_subarray ) <EOL> check ( setitem_b ) <EOL> check ( setitem_b_subarray ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_set_c ( self ) : <EOL> def check ( pyfunc ) : <EOL> self . _test_set_equal ( pyfunc , <NUM_LIT> , types . complex64 ) <EOL> self . _test_set_equal ( pyfunc , <NUM_LIT> , types . complex128 ) <EOL> check ( set_c ) <EOL> check ( set_c_subarray ) <EOL> check ( setitem_c ) <EOL> check ( setitem_c_subarray ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_set_record ( self ) : <EOL> pyfunc = set_record <EOL> rec = numpy_support . from_dtype ( recordtype ) <EOL> cfunc = self . get_cfunc ( pyfunc , ( rec [ : ] , types . intp , types . intp ) ) <EOL> test_indices = [ ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:0> , <NUM_LIT:2> ) ] <EOL> for i , j in test_indices : <EOL> expect = self . refsample1d . copy ( ) <EOL> pyfunc ( expect , i , j ) <EOL> got = self . nbsample1d . copy ( ) <EOL> cfunc ( got , i , j ) <EOL> self . assertEqual ( expect [ i ] , expect [ j ] ) <EOL> self . assertEqual ( got [ i ] , got [ j ] ) <EOL> self . assertTrue ( np . all ( expect == got ) ) <EOL> def _test_record_args ( self , revargs ) : <EOL> """<STR_LIT>""" <EOL> npval = self . refsample1d . copy ( ) [ <NUM_LIT:0> ] <EOL> nbval = self . nbsample1d . copy ( ) [ <NUM_LIT:0> ] <EOL> attrs = '<STR_LIT:abc>' <EOL> valtypes = types . float64 , types . int16 , types . complex64 <EOL> values = <NUM_LIT> , <NUM_LIT> , <NUM_LIT> + <NUM_LIT> <EOL> old_refcnt = sys . getrefcount ( nbval ) <EOL> for attr , valtyp , val in zip ( attrs , valtypes , values ) : <EOL> expected = getattr ( npval , attr ) <EOL> nbrecord = numpy_support . from_dtype ( recordtype ) <EOL> if revargs : <EOL> prefix = '<STR_LIT>' <EOL> argtypes = ( valtyp , nbrecord ) <EOL> args = ( val , nbval ) <EOL> else : <EOL> prefix = '<STR_LIT>' <EOL> argtypes = ( nbrecord , valtyp ) <EOL> args = ( nbval , val ) <EOL> pyfunc = globals ( ) [ prefix + attr ] <EOL> cfunc = self . get_cfunc ( pyfunc , argtypes ) <EOL> got = cfunc ( * args ) <EOL> try : <EOL> self . assertEqual ( expected , got ) <EOL> except AssertionError as e : <EOL> import llvmlite . binding as ll <EOL> if attr != '<STR_LIT:c>' : <EOL> raise <EOL> if ll . get_default_triple ( ) != '<STR_LIT>' : <EOL> raise <EOL> self . assertEqual ( val , got ) <EOL> else : <EOL> self . assertEqual ( nbval [ attr ] , val ) <EOL> del got , expected , args <EOL> self . assertEqual ( sys . getrefcount ( nbval ) , old_refcnt ) <EOL> def test_record_args ( self ) : <EOL> self . _test_record_args ( False ) <EOL> def test_record_args_reverse ( self ) : <EOL> self . _test_record_args ( True ) <EOL> def test_two_records ( self ) : <EOL> '''<STR_LIT>''' <EOL> npval1 = self . refsample1d . copy ( ) [ <NUM_LIT:0> ] <EOL> npval2 = self . refsample1d . copy ( ) [ <NUM_LIT:1> ] <EOL> nbval1 = self . nbsample1d . copy ( ) [ <NUM_LIT:0> ] <EOL> nbval2 = self . nbsample1d . copy ( ) [ <NUM_LIT:1> ] <EOL> attrs = '<STR_LIT:abc>' <EOL> valtypes = types . float64 , types . int32 , types . complex64 <EOL> for attr , valtyp in zip ( attrs , valtypes ) : <EOL> expected = getattr ( npval1 , attr ) + getattr ( npval2 , attr ) <EOL> nbrecord = numpy_support . from_dtype ( recordtype ) <EOL> pyfunc = globals ( ) [ '<STR_LIT>' + attr ] <EOL> cfunc = self . get_cfunc ( pyfunc , ( nbrecord , nbrecord ) ) <EOL> got = cfunc ( nbval1 , nbval2 ) <EOL> self . assertEqual ( expected , got ) <EOL> def test_two_distinct_records ( self ) : <EOL> '''<STR_LIT>''' <EOL> nbval1 = self . nbsample1d . copy ( ) [ <NUM_LIT:0> ] <EOL> nbval2 = self . refsample1d2 . copy ( ) [ <NUM_LIT:0> ] <EOL> expected = nbval1 [ '<STR_LIT:a>' ] + nbval2 [ '<STR_LIT:f>' ] <EOL> nbrecord1 = numpy_support . from_dtype ( recordtype ) <EOL> nbrecord2 = numpy_support . from_dtype ( recordtype2 ) <EOL> cfunc = self . get_cfunc ( get_two_records_distinct , ( nbrecord1 , nbrecord2 ) ) <EOL> got = cfunc ( nbval1 , nbval2 ) <EOL> self . assertEqual ( expected , got ) <EOL> def test_record_write_array ( self ) : <EOL> '''<STR_LIT>''' <EOL> nbval = np . recarray ( <NUM_LIT:1> , dtype = recordwitharray ) <EOL> nbrecord = numpy_support . from_dtype ( recordwitharray ) <EOL> cfunc = self . get_cfunc ( record_write_array , ( nbrecord , ) ) <EOL> cfunc ( nbval [ <NUM_LIT:0> ] ) <EOL> expected = np . recarray ( <NUM_LIT:1> , dtype = recordwitharray ) <EOL> expected [ <NUM_LIT:0> ] . g = <NUM_LIT:2> <EOL> expected [ <NUM_LIT:0> ] . h [ <NUM_LIT:0> ] = <NUM_LIT> <EOL> expected [ <NUM_LIT:0> ] . h [ <NUM_LIT:1> ] = <NUM_LIT> <EOL> np . testing . assert_equal ( expected , nbval ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_record_write_2d_array ( self ) : <EOL> '''<STR_LIT>''' <EOL> nbval = np . recarray ( <NUM_LIT:1> , dtype = recordwith2darray ) <EOL> nbrecord = numpy_support . from_dtype ( recordwith2darray ) <EOL> cfunc = self . get_cfunc ( record_write_2d_array , ( nbrecord , ) ) <EOL> cfunc ( nbval [ <NUM_LIT:0> ] ) <EOL> expected = np . recarray ( <NUM_LIT:1> , dtype = recordwith2darray ) <EOL> expected [ <NUM_LIT:0> ] . i = <NUM_LIT:3> <EOL> expected [ <NUM_LIT:0> ] . j [ : ] = np . asarray ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> np . float32 ) . reshape ( <NUM_LIT:3> , <NUM_LIT:2> ) <EOL> np . testing . assert_equal ( expected , nbval ) <EOL> def test_record_read_array ( self ) : <EOL> '''<STR_LIT>''' <EOL> nbval = np . recarray ( <NUM_LIT:1> , dtype = recordwitharray ) <EOL> nbval [ <NUM_LIT:0> ] . h [ <NUM_LIT:0> ] = <NUM_LIT> <EOL> nbval [ <NUM_LIT:0> ] . h [ <NUM_LIT:1> ] = <NUM_LIT> <EOL> nbrecord = numpy_support . from_dtype ( recordwitharray ) <EOL> cfunc = self . get_cfunc ( record_read_array0 , ( nbrecord , ) ) <EOL> res = cfunc ( nbval [ <NUM_LIT:0> ] ) <EOL> np . testing . assert_equal ( res , nbval [ <NUM_LIT:0> ] . h [ <NUM_LIT:0> ] ) <EOL> cfunc = self . get_cfunc ( record_read_array1 , ( nbrecord , ) ) <EOL> res = cfunc ( nbval [ <NUM_LIT:0> ] ) <EOL> np . testing . assert_equal ( res , nbval [ <NUM_LIT:0> ] . h [ <NUM_LIT:1> ] ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_record_read_2d_array ( self ) : <EOL> '''<STR_LIT>''' <EOL> nbval = np . recarray ( <NUM_LIT:1> , dtype = recordwith2darray ) <EOL> nbval [ <NUM_LIT:0> ] . j = np . asarray ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> np . float32 ) . reshape ( <NUM_LIT:3> , <NUM_LIT:2> ) <EOL> nbrecord = numpy_support . from_dtype ( recordwith2darray ) <EOL> cfunc = self . get_cfunc ( record_read_2d_array00 , ( nbrecord , ) ) <EOL> res = cfunc ( nbval [ <NUM_LIT:0> ] ) <EOL> np . testing . assert_equal ( res , nbval [ <NUM_LIT:0> ] . j [ <NUM_LIT:0> , <NUM_LIT:0> ] ) <EOL> cfunc = self . get_cfunc ( record_read_2d_array01 , ( nbrecord , ) ) <EOL> res = cfunc ( nbval [ <NUM_LIT:0> ] ) <EOL> np . testing . assert_equal ( res , nbval [ <NUM_LIT:0> ] . j [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> cfunc = self . get_cfunc ( record_read_2d_array10 , ( nbrecord , ) ) <EOL> res = cfunc ( nbval [ <NUM_LIT:0> ] ) <EOL> np . testing . assert_equal ( res , nbval [ <NUM_LIT:0> ] . j [ <NUM_LIT:1> , <NUM_LIT:0> ] ) <EOL> @ tag ( '<STR_LIT>' ) <EOL> def test_record_return ( self ) : <EOL> """<STR_LIT>""" <EOL> pyfunc = record_return <EOL> recty = numpy_support . from_dtype ( recordtype ) <EOL> cfunc = self . get_cfunc ( pyfunc , ( recty [ : ] , types . intp ) ) <EOL> attrs = '<STR_LIT:abc>' <EOL> indices = [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] <EOL> for index , attr in zip ( indices , attrs ) : <EOL> nbary = self . nbsample1d . copy ( ) <EOL> old_refcnt = sys . getrefcount ( nbary ) <EOL> res = cfunc ( nbary , index ) <EOL> self . assertEqual ( nbary [ index ] , res ) <EOL> setattr ( res , attr , <NUM_LIT:0> ) <EOL> self . assertNotEqual ( nbary [ index ] , res ) <EOL> del res <EOL> self . assertEqual ( sys . getrefcount ( nbary ) , old_refcnt ) <EOL> def test_record_arg_transform ( self ) : <EOL> """<STR_LIT>""" <EOL> rec = numpy_support . from_dtype ( recordtype3 ) <EOL> transformed = transform_arg_name ( rec ) <EOL> self . assertNotIn ( '<STR_LIT>' , transformed ) <EOL> self . assertNotIn ( '<STR_LIT>' , transformed ) <EOL> self . assertLess ( len ( transformed ) , <NUM_LIT:16> ) <EOL> struct_arr = types . Array ( rec , <NUM_LIT:1> , '<STR_LIT:C>' ) <EOL> transformed = transform_arg_name ( struct_arr ) <EOL> self . assertIn ( '<STR_LIT>' , transformed ) <EOL> self . assertNotIn ( '<STR_LIT>' , transformed ) <EOL> self . assertNotIn ( '<STR_LIT>' , transformed ) <EOL> self . assertLess ( len ( transformed ) , <NUM_LIT> ) <EOL> def test_record_two_arrays ( self ) : <EOL> """<STR_LIT>""" <EOL> nbrecord = numpy_support . from_dtype ( recordwith2arrays ) <EOL> rec = np . recarray ( <NUM_LIT:1> , dtype = recordwith2arrays ) [ <NUM_LIT:0> ] <EOL> rec . k [ : ] = np . arange ( <NUM_LIT:200> ) . reshape ( <NUM_LIT:10> , <NUM_LIT:20> ) <EOL> rec . l [ : ] = np . arange ( <NUM_LIT> ) . reshape ( <NUM_LIT:6> , <NUM_LIT:12> ) <EOL> pyfunc = record_read_first_arr <EOL> cfunc = self . get_cfunc ( pyfunc , ( nbrecord , ) ) <EOL> self . assertEqual ( cfunc ( rec ) , pyfunc ( rec ) ) <EOL> pyfunc = record_read_second_arr <EOL> cfunc = self . get_cfunc ( pyfunc , ( nbrecord , ) ) <EOL> self . assertEqual ( cfunc ( rec ) , pyfunc ( rec ) ) <EOL> def _get_cfunc_nopython ( pyfunc , argspec ) : <EOL> return jit ( argspec , nopython = True ) ( pyfunc ) <EOL> class TestRecordDtypeWithDispatcher ( TestRecordDtype ) : <EOL> '''<STR_LIT>''' <EOL> def get_cfunc ( self , pyfunc , argspec ) : <EOL> return _get_cfunc_nopython ( pyfunc , argspec ) <EOL> class TestRecordDtypeWithStructArrays ( TestRecordDtype ) : <EOL> '''<STR_LIT>''' <EOL> def _createSampleArrays ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . refsample1d = np . recarray ( <NUM_LIT:3> , dtype = recordtype ) <EOL> self . refsample1d2 = np . recarray ( <NUM_LIT:3> , dtype = recordtype2 ) <EOL> self . refsample1d3 = np . recarray ( <NUM_LIT:3> , dtype = recordtype ) <EOL> self . nbsample1d = np . zeros ( <NUM_LIT:3> , dtype = recordtype ) <EOL> self . nbsample1d2 = np . zeros ( <NUM_LIT:3> , dtype = recordtype2 ) <EOL> self . nbsample1d3 = np . zeros ( <NUM_LIT:3> , dtype = recordtype ) <EOL> class TestRecordDtypeWithStructArraysAndDispatcher ( TestRecordDtypeWithStructArrays ) : <EOL> '''<STR_LIT>''' <EOL> def get_cfunc ( self , pyfunc , argspec ) : <EOL> return _get_cfunc_nopython ( pyfunc , argspec ) <EOL> class TestRecordDtypeWithCharSeq ( unittest . TestCase ) : <EOL> def _createSampleaArray ( self ) : <EOL> self . refsample1d = np . recarray ( <NUM_LIT:3> , dtype = recordwithcharseq ) <EOL> self . nbsample1d = np . zeros ( <NUM_LIT:3> , dtype = recordwithcharseq ) <EOL> def _fillData ( self , arr ) : <EOL> for i in range ( arr . size ) : <EOL> arr [ i ] [ '<STR_LIT:m>' ] = i <EOL> arr [ i ] [ '<STR_LIT:n>' ] = "<STR_LIT>" % i <EOL> arr [ <NUM_LIT:0> ] [ '<STR_LIT:n>' ] = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> self . _createSampleaArray ( ) <EOL> self . _fillData ( self . refsample1d ) <EOL> self . _fillData ( self . nbsample1d ) <EOL> def get_cfunc ( self , pyfunc ) : <EOL> rectype = numpy_support . from_dtype ( recordwithcharseq ) <EOL> cres = compile_isolated ( pyfunc , ( rectype [ : ] , types . intp ) ) <EOL> return cres . entry_point <EOL> def test_return_charseq ( self ) : <EOL> pyfunc = get_charseq <EOL> cfunc = self . get_cfunc ( pyfunc ) <EOL> for i in range ( self . refsample1d . size ) : <EOL> expected = pyfunc ( self . refsample1d , i ) <EOL> got = cfunc ( self . nbsample1d , i ) <EOL> self . assertEqual ( expected , got ) <EOL> def test_npm_argument_charseq ( self ) : <EOL> """<STR_LIT>""" <EOL> def pyfunc ( arr , i ) : <EOL> return arr [ i ] . n <EOL> identity = jit ( lambda x : x ) <EOL> @ jit ( nopython = True ) <EOL> def cfunc ( arr , i ) : <EOL> return identity ( arr [ i ] . n ) <EOL> for i in range ( self . refsample1d . size ) : <EOL> expected = pyfunc ( self . refsample1d , i ) <EOL> got = cfunc ( self . nbsample1d , i ) <EOL> self . assertEqual ( expected , got ) <EOL> def test_py_argument_charseq ( self ) : <EOL> """<STR_LIT>""" <EOL> pyfunc = set_charseq <EOL> rectype = numpy_support . from_dtype ( recordwithcharseq ) <EOL> cres = compile_isolated ( pyfunc , ( rectype [ : ] , types . intp , <EOL> rectype . typeof ( '<STR_LIT:n>' ) ) ) <EOL> cfunc = cres . entry_point <EOL> for i in range ( self . refsample1d . size ) : <EOL> chars = "<STR_LIT>" . format ( hex ( i + <NUM_LIT:10> ) ) <EOL> expected = pyfunc ( self . refsample1d , i , chars ) <EOL> got = cfunc ( self . nbsample1d , i , chars ) <EOL> np . testing . assert_equal ( self . refsample1d , self . nbsample1d ) <EOL> def test_py_argument_char_seq_near_overflow ( self ) : <EOL> """<STR_LIT>""" <EOL> pyfunc = set_charseq <EOL> rectype = numpy_support . from_dtype ( recordwithcharseq ) <EOL> cres = compile_isolated ( pyfunc , ( rectype [ : ] , types . intp , <EOL> rectype . typeof ( '<STR_LIT:n>' ) ) ) <EOL> cfunc = cres . entry_point <EOL> cs_near_overflow = "<STR_LIT>" <EOL> self . assertEqual ( len ( cs_near_overflow ) , <EOL> recordwithcharseq [ '<STR_LIT:n>' ] . itemsize ) <EOL> cfunc ( self . nbsample1d , <NUM_LIT:0> , cs_near_overflow ) <EOL> self . assertEqual ( self . nbsample1d [ <NUM_LIT:0> ] [ '<STR_LIT:n>' ] . decode ( '<STR_LIT:ascii>' ) , cs_near_overflow ) <EOL> np . testing . assert_equal ( self . refsample1d [ <NUM_LIT:1> : ] , self . nbsample1d [ <NUM_LIT:1> : ] ) <EOL> def test_py_argument_char_seq_truncate ( self ) : <EOL> """<STR_LIT>""" <EOL> pyfunc = set_charseq <EOL> rectype = numpy_support . from_dtype ( recordwithcharseq ) <EOL> cres = compile_isolated ( pyfunc , ( rectype [ : ] , types . intp , <EOL> rectype . typeof ( '<STR_LIT:n>' ) ) ) <EOL> cfunc = cres . entry_point <EOL> cs_overflowed = "<STR_LIT>" <EOL> pyfunc ( self . refsample1d , <NUM_LIT:1> , cs_overflowed ) <EOL> cfunc ( self . nbsample1d , <NUM_LIT:1> , cs_overflowed ) <EOL> np . testing . assert_equal ( self . refsample1d , self . nbsample1d ) <EOL> self . assertEqual ( self . refsample1d [ <NUM_LIT:1> ] . n , <EOL> cs_overflowed [ : - <NUM_LIT:1> ] . encode ( "<STR_LIT:ascii>" ) ) <EOL> def test_return_charseq_tuple ( self ) : <EOL> pyfunc = get_charseq_tuple <EOL> cfunc = self . get_cfunc ( pyfunc ) <EOL> for i in range ( self . refsample1d . size ) : <EOL> expected = pyfunc ( self . refsample1d , i ) <EOL> got = cfunc ( self . nbsample1d , i ) <EOL> self . assertEqual ( expected , got ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
