Python System.Double() Examples

The following are 19 code examples of System.Double(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module System , or try the search function .
Example #1
Source File: test___clrtype.py    From ironpython3 with Apache License 2.0 6 votes vote down vote up
def test_type___clrtype__(self):
        '''
        Tests out type.__clrtype__ directly.
        '''
        import System
        #Make sure it exists
        self.assertTrue(hasattr(type, "__clrtype__"))
        
        #Make sure the documentation is useful
        self.assertTrue("Gets the .NET type which is" in type.__clrtype__.__doc__, type.__clrtype__.__doc__)
        
        self.assertEqual(type.__clrtype__(type),  type)
        self.assertEqual(type.__clrtype__(float), float)
        self.assertEqual(type.__clrtype__(System.Double), float)
        self.assertEqual(type.__clrtype__(float), System.Double)
        self.assertTrue(not type.__clrtype__(float)==type)
        self.assertTrue(type.__clrtype__(float)!=type) 
Example #2
Source File: test___clrtype__.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_type___clrtype__(self):
        '''
        Tests out type.__clrtype__ directly.
        '''
        import System
        #Make sure it exists
        self.assertTrue(hasattr(type, "__clrtype__"))
        
        #Make sure the documentation is useful
        self.assertTrue("Gets the .NET type which is" in type.__clrtype__.__doc__, type.__clrtype__.__doc__)
        
        self.assertEqual(type.__clrtype__(type),  type)
        self.assertEqual(type.__clrtype__(float), float)
        self.assertEqual(type.__clrtype__(System.Double), float)
        self.assertEqual(type.__clrtype__(float), System.Double)
        self.assertTrue(not type.__clrtype__(float)==type)
        self.assertTrue(type.__clrtype__(float)!=type) 
Example #3
Source File: mesh.py    From dhitools with MIT License 6 votes vote down vote up
def _dfsu_element_coordinates(dfsu_object):
    """ Use MIKE SDK method to calc element coords from dfsu_object """

    element_coordinates = np.zeros(shape=(dfsu_object.NumberOfElements, 3))

    # Convert nodes to .NET System double to input to method
    xtemp = Array.CreateInstance(System.Double, 0)
    ytemp = Array.CreateInstance(System.Double, 0)
    ztemp = Array.CreateInstance(System.Double, 0)

    # Get element coords
    elemts_temp = dfs.dfsu.DfsuUtil.CalculateElementCenterCoordinates(dfsu_object, xtemp, ytemp, ztemp)

    # Place in array; need to get from .NET Array to numpy array
    for n in range(3):
        ele_coords_temp = []
        for ele in elemts_temp[n+1]:
            ele_coords_temp.append(ele)
        element_coordinates[:, n] = ele_coords_temp

    return element_coordinates 
Example #4
Source File: test_stdmodules.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_cp13401(self):
        import System
        import copy
        
        #A few special cases
        self.assertEqual(System.Char.MinValue, copy.copy(System.Char.MinValue))
        self.assertTrue(System.Char.MinValue != copy.copy(System.Char.MaxValue))
        self.assertEqual(System.StringSplitOptions.None, copy.copy(System.StringSplitOptions.None))
        self.assertEqual(System.StringSplitOptions.RemoveEmptyEntries, copy.copy(System.StringSplitOptions.RemoveEmptyEntries))
        self.assertTrue(System.StringSplitOptions.None != copy.copy(System.StringSplitOptions.RemoveEmptyEntries))
        
        #Normal cases
        test_dict = {   System.Byte : [System.Byte.MinValue, System.Byte.MinValue+1, System.Byte.MaxValue, System.Byte.MaxValue-1],
                        System.Char : [],
                        System.Boolean : [True, False],
                        System.SByte   : [System.SByte.MinValue, System.SByte.MinValue+1, System.SByte.MaxValue, System.SByte.MaxValue-1],
                        System.UInt32  : [System.UInt32.MinValue, System.UInt32.MinValue+1, System.UInt32.MaxValue, System.UInt32.MaxValue-1],
                        System.Int64   : [System.Int64.MinValue, System.Int64.MinValue+1, System.Int64.MaxValue, System.Int64.MaxValue-1],
                        System.Double  : [0.00, 3.14],
                        }
        
        for key in test_dict.keys():
            temp_type = key
            self.assertTrue(hasattr(temp_type, "__reduce_ex__"), 
                "%s has no attribute '%s'" % (str(temp_type), "__reduce_ex__"))
        
            for temp_value in test_dict[key]:
                x = temp_type(temp_value)
                x_copy = copy.copy(x)
                self.assertEqual(x, x_copy)
                self.assertEqual(x, temp_value) 
Example #5
Source File: __init__.py    From PyESAPI with MIT License 5 votes vote down vote up
def make_dose_for_grid(dose, image=None):
    '''returns a 3D numpy.ndarray of doubles matching dose (default) or image grid indexed like [x,y,z]'''

    if image is not None:
        row_buffer = Array.CreateInstance(Double, image.ZSize)
        dose_array = fill_in_profiles(image, dose.GetDoseProfile, row_buffer, c_double)
    else:
        # default
        dose_array = dose_to_nparray(dose)

    dose_array[np.where(np.isnan(dose_array))] = 0.0
    return dose_array 
Example #6
Source File: __init__.py    From PyESAPI with MIT License 5 votes vote down vote up
def fill_in_profiles(dose_or_image, profile_fxn, row_buffer, dtype, pre_buffer=None):
    '''fills in 3D profile data (dose or segments)'''
    mask_array = np.zeros((dose_or_image.XSize, dose_or_image.YSize, dose_or_image.ZSize))

    # note used ZSize-1 to match zero indexed loops below and compute_voxel_points_matrix(...)
    z_direction = VVector.op_Multiply(Double((dose_or_image.ZSize - 1) * dose_or_image.ZRes), dose_or_image.ZDirection)
    y_step = VVector.op_Multiply(dose_or_image.YRes, dose_or_image.YDirection)

    for x in range(dose_or_image.XSize):  # scan X dimensions
        start_x = VVector.op_Addition(dose_or_image.Origin,
                                      VVector.op_Multiply(Double(x * dose_or_image.XRes), dose_or_image.XDirection))

        for y in range(dose_or_image.YSize):  # scan Y dimension
            stop = VVector.op_Addition(start_x, z_direction)

            # get the profile along Z dimension 
            if pre_buffer is None:
                profile_fxn(start_x, stop, row_buffer)
            else:
                profile_fxn(start_x, stop, pre_buffer)
                pre_buffer.CopyTo(row_buffer, 0)

            # save data
            mask_array[x, y, :] = to_ndarray(row_buffer, dtype)

            # add step for next point
            start_x = VVector.op_Addition(start_x, y_step)

    return mask_array 
Example #7
Source File: test_numtypes.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def mystr(x):
    if isinstance(x, tuple):
        return "(" + ", ".join(mystr(e) for e in x) + ")"
    elif isinstance(x, Single):
        return str(round(float(str(x)), 3))
    elif isinstance(x, Double):
        return str(round(x, 3))
    else:
        s = str(x)
        if s.endswith("L"): return s[:-1]
        else: return s 
Example #8
Source File: test_numtypes.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def get_values(values, itypes, ftypes):
    """
    This will return structure of values converted to variety of types as a list of tuples:
    [ ...
    ( python_value, [ all_values ] ),
    ... ]

    all_values: Byte, UInt16, UInt32, UInt64, SByte, Int16, Int32, Int64, Single, Double, myint, mylong, myfloat
    """
    all = []
    for v in values:
        sv  = str(v)
        py  = int(v)
        clr = get_clr_values(sv, itypes)
        clr.append(long(py))
        clr.append(myint(py))
        clr.append(mylong(py))
        all.append( (py, clr) )

        py  = long(v)
        clr = get_clr_values(sv, itypes)
        clr.append(py)
        clr.append(myint(py))
        clr.append(mylong(py))
        all.append( (py, clr) )

        py  = float(v)
        clr = get_clr_values(sv, ftypes)
        clr.append(myfloat(py))
        all.append( (py, clr) )

        for imag in [0j, 1j, -1j]:
            py = complex(v + imag)
            all.append( (py, [ py, mycomplex(py) ] ) )

    all.append( (True, [ True ] ))
    all.append( (False, [ False ] ))

    return all 
Example #9
Source File: test_stdmodules.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_cp13401(self):
        import System
        import copy
        
        #A few special cases
        StringSplitOptions_None = getattr(System.StringSplitOptions, "None")
        self.assertEqual(System.Char.MinValue, copy.copy(System.Char.MinValue))
        self.assertTrue(System.Char.MinValue != copy.copy(System.Char.MaxValue))
        self.assertEqual(StringSplitOptions_None, copy.copy(StringSplitOptions_None))
        self.assertEqual(System.StringSplitOptions.RemoveEmptyEntries, copy.copy(System.StringSplitOptions.RemoveEmptyEntries))
        self.assertTrue(StringSplitOptions_None != copy.copy(System.StringSplitOptions.RemoveEmptyEntries))
        
        #Normal cases
        test_dict = {   System.Byte : [System.Byte.MinValue, System.Byte.MinValue+1, System.Byte.MaxValue, System.Byte.MaxValue-1],
                        System.Char : [],
                        System.Boolean : [True, False],
                        System.SByte   : [System.SByte.MinValue, System.SByte.MinValue+1, System.SByte.MaxValue, System.SByte.MaxValue-1],
                        System.UInt32  : [System.UInt32.MinValue, System.UInt32.MinValue+1, System.UInt32.MaxValue, System.UInt32.MaxValue-1],
                        System.Int64   : [System.Int64.MinValue, System.Int64.MinValue+1, System.Int64.MaxValue, System.Int64.MaxValue-1],
                        System.Double  : [0.00, 3.14],
                        }
        
        for key in test_dict.keys():
            temp_type = key
            self.assertTrue(hasattr(temp_type, "__reduce_ex__"), 
                "%s has no attribute '%s'" % (str(temp_type), "__reduce_ex__"))
        
            for temp_value in test_dict[key]:
                x = temp_type(temp_value)
                x_copy = copy.copy(x)
                self.assertEqual(x, x_copy)
                self.assertEqual(x, temp_value) 
Example #10
Source File: test_numtypes.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def mystr(x):
    if isinstance(x, tuple):
        return "(" + ", ".join(mystr(e) for e in x) + ")"
    elif isinstance(x, Single):
        return str(round(float(str(x)), 3))
    elif isinstance(x, Double):
        return str(round(x, 3))
    else:
        s = str(x)
        if s.endswith("L"): return s[:-1]
        else: return s 
Example #11
Source File: test_numtypes.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def get_values(values, itypes, ftypes):
    """
    This will return structure of values converted to variety of types as a list of tuples:
    [ ...
    ( python_value, [ all_values ] ),
    ... ]

    all_values: Byte, UInt16, UInt32, UInt64, SByte, Int16, Int32, Int64, Single, Double, myint, mylong, myfloat
    """
    all = []
    for v in values:
        sv  = str(v)
        py  = int(v)
        clr = get_clr_values(sv, itypes)
        clr.append(long(py))
        clr.append(myint(py))
        clr.append(mylong(py))
        all.append( (py, clr) )

        py  = long(v)
        clr = get_clr_values(sv, itypes)
        clr.append(py)
        clr.append(myint(py))
        clr.append(mylong(py))
        all.append( (py, clr) )

        py  = float(v)
        clr = get_clr_values(sv, ftypes)
        clr.append(myfloat(py))
        all.append( (py, clr) )

        for imag in [0j, 1j, -1j]:
            py = complex(v + imag)
            all.append( (py, [ py, mycomplex(py) ] ) )

    all.append( (True, [ True ] ))
    all.append( (False, [ False ] ))

    return all 
Example #12
Source File: cominterop_util.py    From ironpython3 with Apache License 2.0 4 votes vote down vote up
def overflowErrorTrigger(in_type):
    ret_val = {}
    
    ############################################################
    ret_val["VARIANT_BOOL"] =  []
                     
    ############################################################              
    ret_val["BYTE"] = []
    ret_val["BYTE"] += overflow_num_helper(System.Byte)
        
    ############################################################
    #Doesn't seem possible to create a value (w/o 1st overflowing
    #in Python) to pass to the COM method which will overflow.
    ret_val["BSTR"] = [] #["0123456789" * 1234567890]
    
    ############################################################ 
    ret_val["CHAR"] = []
    ret_val["CHAR"] +=  overflow_num_helper(System.SByte)
    
    ############################################################
    ret_val["FLOAT"] = []  
    ret_val["FLOAT"] += overflow_num_helper(System.Double)
    
    #Shouldn't be possible to overflow a double.
    ret_val["DOUBLE"] =  []
    
    
    ############################################################            
    ret_val["USHORT"] =  []
    ret_val["USHORT"] += overflow_num_helper(System.UInt16)
      
    ret_val["ULONG"] =  []
    ret_val["ULONG"] +=  overflow_num_helper(System.UInt32)
               
    ret_val["ULONGLONG"] =  []
    # Dev10 475426
    #ret_val["ULONGLONG"] +=  overflow_num_helper(System.UInt64)
      
    ret_val["SHORT"] =  []
    ret_val["SHORT"] += overflow_num_helper(System.Int16)
      
    ret_val["LONG"] =  []
    # Dev10 475426
    #ret_val["LONG"] += overflow_num_helper(System.Int32)
                
    ret_val["LONGLONG"] =  []
    # Dev10 475426
    #ret_val["LONGLONG"] += overflow_num_helper(System.Int64)
    
    ############################################################
    return ret_val[in_type] 
Example #13
Source File: test___clrtype__.py    From ironpython2 with Apache License 2.0 4 votes vote down vote up
def test_clrtype_returns_existing_clr_types(self):
        '''
        Our implementation of __clrtype__ returns existing .NET types
        instead of subclassing from type or System.Type.
        '''
        global called
        import clr
        import System
        if is_netcoreapp:
            clr.AddReference("System.Collections")
            clr.AddReference("System.Data.Common")
        else:
            clr.AddReference("System.Data")
        
        types = [
                    System.Byte,
                    System.Int16,
                    System.UInt32,
                    System.Int32,
                    System.Int64,
                    System.Double,
                    System.Data.CommandType,
                    System.Data.Common.DataAdapter,
                    System.Boolean,
                    System.Char,
                    System.Decimal,
                    System.IntPtr,
                    System.Object,
                    System.String,
                    System.Collections.BitArray,
                    System.Collections.Generic.List[System.Char],
                    ]

        for x in types:
            called = False
            
            class MyType(type):
                def __clrtype__(self):
                    global called
                    called = True
                    return x
            
            class X(object):
                __metaclass__ = MyType
            
            self.assertEqual(called, True) 
Example #14
Source File: test_clrnuminterop.py    From ironpython3 with Apache License 2.0 4 votes vote down vote up
def test_sanity(self):
        '''Make sure that numbers within the constraints of the numerical types are allowed.'''
        import clr
        import System
        temp_list = [   ["System.Byte", 0, 255],
                        ["System.SByte", -128, 127],
                        ["System.Byte", 0, 255],
                        ["System.Int16", -32768, 32767],
                        ["System.UInt16", 0, 65535],
                        ["System.Int32", -2147483648, 2147483647],
                        ["System.UInt32", 0, 4294967295],
                        ["System.Int64", -9223372036854775808, 9223372036854775807],
                        ["System.UInt64", 0, 18446744073709551615],
                        ["System.Single", -3.40282e+038, 3.40282e+038],
                        ["System.Double", -1.79769313486e+308, 1.79769313486e+308],
                        ["System.Decimal", -79228162514264337593543950335, 79228162514264337593543950335],
                        ["int", -2147483648, 2147483647]
                    ]

        for num_type, small_val, large_val in temp_list:
            self.assertTrue(self.num_ok_for_type(1, num_type))
            self.assertTrue(self.num_ok_for_type(1.0, num_type))

            #Minimum value
            self.assertTrue(self.num_ok_for_type(small_val, num_type))
            self.assertTrue(self.num_ok_for_type(small_val + 1, num_type))
            self.assertTrue(self.num_ok_for_type(small_val + 2, num_type))

            #Maximum value
            self.assertTrue(self.num_ok_for_type(large_val, num_type))
            self.assertTrue(self.num_ok_for_type(large_val - 1, num_type))
            self.assertTrue(self.num_ok_for_type(large_val - 2, num_type))

            #Negative cases
            if num_type!="System.Single" and num_type!="System.Double" and num_type!="System.Decimal":
                self.assertTrue(not self.num_ok_for_type(small_val - 1, num_type))
                self.assertTrue(not self.num_ok_for_type(small_val - 2, num_type))
                self.assertTrue(not self.num_ok_for_type(large_val + 1, num_type))
                self.assertTrue(not self.num_ok_for_type(large_val + 2, num_type))

        #Special cases
        self.assertTrue(self.num_ok_for_type(0, "long"))
        self.assertTrue(self.num_ok_for_type(1, "long"))
        self.assertTrue(self.num_ok_for_type(-1, "long"))
        self.assertTrue(self.num_ok_for_type(5, "long"))
        self.assertTrue(self.num_ok_for_type(-92233720368547758080000, "long"))
        self.assertTrue(self.num_ok_for_type( 18446744073709551615000, "long"))

        self.assertTrue(self.num_ok_for_type(0.0, "float"))
        self.assertTrue(self.num_ok_for_type(1.0, "float"))
        self.assertTrue(self.num_ok_for_type(-1.0, "float"))
        self.assertTrue(self.num_ok_for_type(3.14, "float"))
        self.assertTrue(self.num_ok_for_type(-92233720368547758080000.0, "float"))
        self.assertTrue(self.num_ok_for_type( 18446744073709551615000.0, "float")) 
Example #15
Source File: test_returnvalue.py    From ironpython2 with Apache License 2.0 4 votes vote down vote up
def test_return_numbers(self):
        import clr
        import System
        from Merlin.Testing.Call import Int32RInt32EventHandler
        from Merlin.Testing.TypeSample import EnumInt16, SimpleStruct
        for (f, t, val) in [
            (self.c.ReturnByte, System.Byte, 0), 
            (self.c.ReturnSByte, System.SByte, 1), 
            (self.c.ReturnUInt16, System.UInt16, 2), 
            (self.c.ReturnInt16, System.Int16, 3), 
            (self.c.ReturnUInt32, System.UInt32, 4), 
            (self.c.ReturnInt32, System.Int32, 5), 
            (self.c.ReturnUInt64, System.UInt64, 6), 
            (self.c.ReturnInt64, System.Int64, 7), 
            (self.c.ReturnDouble, System.Double, 8), 
            (self.c.ReturnSingle, System.Single, 9), 
            (self.c.ReturnDecimal, System.Decimal, 10), 
            
            (self.c.ReturnChar, System.Char, 'A'), 
            (self.c.ReturnBoolean, System.Boolean, True), 
            (self.c.ReturnString, System.String, "CLR"), 
            
            (self.c.ReturnEnum, EnumInt16, EnumInt16.C),
        ]:
            x = f()
            self.assertEqual(x.GetType(), clr.GetClrType(t))
            self.assertEqual(x, val)
            
        # return value type
        x= self.c.ReturnStruct()
        self.assertEqual(x.Flag, 100)
        
        x= self.c.ReturnClass()
        self.assertEqual(x.Flag, 200)
        
        x= self.c.ReturnNullableInt1()
        self.assertEqual(x.GetType(), clr.GetClrType(int))
        self.assertEqual(x, 300)
        
        x= self.c.ReturnNullableStruct1()
        self.assertEqual(x.GetType(), clr.GetClrType(SimpleStruct))
        self.assertEqual(x.Flag, 400)
        
        x= self.c.ReturnInterface()
        self.assertEqual(x.Flag, 500)
        
        # return delegate
        x= self.c.ReturnDelegate()
        self.assertEqual(x.GetType(), clr.GetClrType(Int32RInt32EventHandler))
        self.assertEqual(x(3), 6)
        self.assertEqual(x(3.0), 6)
        
        # array
        x= self.c.ReturnInt32Array()
        self.assertEqual(x[0], 1)
        self.assertEqual(x[1], 2)
        
        x= self.c.ReturnStructArray()
        self.assertEqual(x[0].Flag, 1)
        self.assertEqual(x[1].Flag, 2) 
Example #16
Source File: test_returnvalue.py    From ironpython3 with Apache License 2.0 4 votes vote down vote up
def test_return_numbers(self):
        import clr
        import System
        from Merlin.Testing.Call import Int32RInt32EventHandler
        from Merlin.Testing.TypeSample import EnumInt16, SimpleStruct
        for (f, t, val) in [
            (self.c.ReturnByte, System.Byte, 0), 
            (self.c.ReturnSByte, System.SByte, 1), 
            (self.c.ReturnUInt16, System.UInt16, 2), 
            (self.c.ReturnInt16, System.Int16, 3), 
            (self.c.ReturnUInt32, System.UInt32, 4), 
            (self.c.ReturnInt32, System.Int32, 5), 
            (self.c.ReturnUInt64, System.UInt64, 6), 
            (self.c.ReturnInt64, System.Int64, 7), 
            (self.c.ReturnDouble, System.Double, 8), 
            (self.c.ReturnSingle, System.Single, 9), 
            (self.c.ReturnDecimal, System.Decimal, 10), 
            
            (self.c.ReturnChar, System.Char, 'A'), 
            (self.c.ReturnBoolean, System.Boolean, True), 
            (self.c.ReturnString, System.String, "CLR"), 
            
            (self.c.ReturnEnum, EnumInt16, EnumInt16.C),
        ]:
            x = f()
            self.assertEqual(x.GetType(), clr.GetClrType(t))
            self.assertEqual(x, val)
            
        # return value type
        x= self.c.ReturnStruct()
        self.assertEqual(x.Flag, 100)
        
        x= self.c.ReturnClass()
        self.assertEqual(x.Flag, 200)
        
        x= self.c.ReturnNullableInt1()
        self.assertEqual(x.GetType(), clr.GetClrType(int))
        self.assertEqual(x, 300)
        
        x= self.c.ReturnNullableStruct1()
        self.assertEqual(x.GetType(), clr.GetClrType(SimpleStruct))
        self.assertEqual(x.Flag, 400)
        
        x= self.c.ReturnInterface()
        self.assertEqual(x.Flag, 500)
        
        # return delegate
        x= self.c.ReturnDelegate()
        self.assertEqual(x.GetType(), clr.GetClrType(Int32RInt32EventHandler))
        self.assertEqual(x(3), 6)
        
        # array
        x= self.c.ReturnInt32Array()
        self.assertEqual(x[0], 1)
        self.assertEqual(x[1], 2)
        
        x= self.c.ReturnStructArray()
        self.assertEqual(x[0].Flag, 1)
        self.assertEqual(x[1].Flag, 2) 
Example #17
Source File: test___clrtype.py    From ironpython3 with Apache License 2.0 4 votes vote down vote up
def test_clrtype_returns_existing_clr_types(self):
        '''
        Our implementation of __clrtype__ returns existing .NET types
        instead of subclassing from type or System.Type.
        '''
        global called
        import clr
        import System
        if is_netcoreapp:
            clr.AddReference("System.Collections")
            clr.AddReference("System.Data.Common")
        else:
            clr.AddReference("System.Data")
        
        types = [
                    System.Byte,
                    System.Int16,
                    System.UInt32,
                    System.Int32,
                    System.Int64,
                    System.Double,
                    System.Data.CommandType,
                    System.Data.Common.DataAdapter,
                    System.Boolean,
                    System.Char,
                    System.Decimal,
                    System.IntPtr,
                    System.Object,
                    System.String,
                    System.Collections.BitArray,
                    System.Collections.Generic.List[System.Char],
                    ]

        for x in types:
            called = False
            
            class MyType(type):
                def __clrtype__(self):
                    global called
                    called = True
                    return x
            
            class X(object):
                __metaclass__ = MyType
            
            self.assertEqual(called, True) 
Example #18
Source File: test_clrnuminterop.py    From ironpython2 with Apache License 2.0 4 votes vote down vote up
def test_sanity(self):
        '''Make sure that numbers within the constraints of the numerical types are allowed.'''
        import clr
        import System
        temp_list = [   ["System.Byte", 0, 255],
                        ["System.SByte", -128, 127],
                        ["System.Byte", 0, 255],
                        ["System.Int16", -32768, 32767],
                        ["System.UInt16", 0, 65535],
                        ["System.Int32", -2147483648, 2147483647],
                        ["System.UInt32", 0, 4294967295],
                        ["System.Int64", -9223372036854775808, 9223372036854775807],
                        ["System.UInt64", 0, 18446744073709551615],
                        ["System.Single", -3.40282e+038, 3.40282e+038],
                        ["System.Double", -1.79769313486e+308, 1.79769313486e+308],
                        ["System.Decimal", -79228162514264337593543950335, 79228162514264337593543950335],
                        ["int", -2147483648, 2147483647]
                    ]

        for num_type, small_val, large_val in temp_list:
            self.assertTrue(self.num_ok_for_type(1, num_type))
            self.assertTrue(self.num_ok_for_type(1.0, num_type))

            #Minimum value
            self.assertTrue(self.num_ok_for_type(small_val, num_type))
            self.assertTrue(self.num_ok_for_type(small_val + 1, num_type))
            self.assertTrue(self.num_ok_for_type(small_val + 2, num_type))

            #Maximum value
            self.assertTrue(self.num_ok_for_type(large_val, num_type))
            self.assertTrue(self.num_ok_for_type(large_val - 1, num_type))
            self.assertTrue(self.num_ok_for_type(large_val - 2, num_type))

            #Negative cases
            if num_type!="System.Single" and num_type!="System.Double" and num_type!="System.Decimal":
                self.assertTrue(not self.num_ok_for_type(small_val - 1, num_type))
                self.assertTrue(not self.num_ok_for_type(small_val - 2, num_type))
                self.assertTrue(not self.num_ok_for_type(large_val + 1, num_type))
                self.assertTrue(not self.num_ok_for_type(large_val + 2, num_type))

        #Special cases
        self.assertTrue(self.num_ok_for_type(0, "long"))
        self.assertTrue(self.num_ok_for_type(1, "long"))
        self.assertTrue(self.num_ok_for_type(-1, "long"))
        self.assertTrue(self.num_ok_for_type(5, "long"))
        self.assertTrue(self.num_ok_for_type(-92233720368547758080000, "long"))
        self.assertTrue(self.num_ok_for_type( 18446744073709551615000, "long"))

        self.assertTrue(self.num_ok_for_type(0.0, "float"))
        self.assertTrue(self.num_ok_for_type(1.0, "float"))
        self.assertTrue(self.num_ok_for_type(-1.0, "float"))
        self.assertTrue(self.num_ok_for_type(3.14, "float"))
        self.assertTrue(self.num_ok_for_type(-92233720368547758080000.0, "float"))
        self.assertTrue(self.num_ok_for_type( 18446744073709551615000.0, "float")) 
Example #19
Source File: cominterop_util.py    From ironpython2 with Apache License 2.0 4 votes vote down vote up
def overflowErrorTrigger(in_type):
    ret_val = {}
    
    ############################################################
    ret_val["VARIANT_BOOL"] =  []
                     
    ############################################################              
    ret_val["BYTE"] = []
    ret_val["BYTE"] += overflow_num_helper(System.Byte)
        
    ############################################################
    #Doesn't seem possible to create a value (w/o 1st overflowing
    #in Python) to pass to the COM method which will overflow.
    ret_val["BSTR"] = [] #["0123456789" * 1234567890]
    
    ############################################################ 
    ret_val["CHAR"] = []
    ret_val["CHAR"] +=  overflow_num_helper(System.SByte)
    
    ############################################################
    ret_val["FLOAT"] = []  
    ret_val["FLOAT"] += overflow_num_helper(System.Double)
    
    #Shouldn't be possible to overflow a double.
    ret_val["DOUBLE"] =  []
    
    
    ############################################################            
    ret_val["USHORT"] =  []
    ret_val["USHORT"] += overflow_num_helper(System.UInt16)
      
    ret_val["ULONG"] =  []
    ret_val["ULONG"] +=  overflow_num_helper(System.UInt32)
               
    ret_val["ULONGLONG"] =  []
    # Dev10 475426
    #ret_val["ULONGLONG"] +=  overflow_num_helper(System.UInt64)
      
    ret_val["SHORT"] =  []
    ret_val["SHORT"] += overflow_num_helper(System.Int16)
      
    ret_val["LONG"] =  []
    # Dev10 475426
    #ret_val["LONG"] += overflow_num_helper(System.Int32)
                
    ret_val["LONGLONG"] =  []
    # Dev10 475426
    #ret_val["LONGLONG"] += overflow_num_helper(System.Int64)
    
    ############################################################
    return ret_val[in_type]