当前位置: 首页>>代码示例>>C#>>正文


C# IntPtr.GetHashCode方法代码示例

本文整理汇总了C#中IntPtr.GetHashCode方法的典型用法代码示例。如果您正苦于以下问题:C# IntPtr.GetHashCode方法的具体用法?C# IntPtr.GetHashCode怎么用?C# IntPtr.GetHashCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IntPtr的用法示例。


在下文中一共展示了IntPtr.GetHashCode方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: PosTest2

 unsafe public bool PosTest2()
 {
     bool retVal = true;
     try
     {
         byte* mem = stackalloc byte[1024];
         System.IntPtr ip = new IntPtr((void*)mem);
         if (System.IntPtr.Size == 4)
         {
             if (ip.GetHashCode() != (int)mem)
             {
                 TestLibrary.TestFramework.LogError("002", "expect IntPtr.GetHashCode() equals the address");
                 retVal = false;
             }
         }
         else if (System.IntPtr.Size == 8)
         {
             if (ip.GetHashCode() != ((int)mem ^ (int)((long)mem >> 32)))
             {
                 TestLibrary.TestFramework.LogError("002", "expect IntPtr.GetHashCode() equals the address xor halves");
                 retVal = false;
             }
         }
         else
         {
             TestLibrary.TestFramework.LogError("002", "Unexpected IntPtr.Size: " + System.IntPtr.Size);
             retVal = false;
         }
     }
     catch (Exception e)
     {
         TestLibrary.TestFramework.LogError("002", "Unexpected exception: " + e);
         retVal = false;
     }
     return retVal;
 }
开发者ID:CheneyWu,项目名称:coreclr,代码行数:36,代码来源:intptrgethashcode.cs

示例2: PosTest1

 public bool PosTest1()
 {
     bool retVal = true;
     try
     {
         System.IntPtr ip = new IntPtr(0);
         if (ip.GetHashCode() != 0)
         {
             TestLibrary.TestFramework.LogError("001", "expect IntPtr(0).GetHashCode() == 0");
             retVal = false;
         }
     }
     catch (Exception e)
     {
         TestLibrary.TestFramework.LogError("001", "Unexpected exception: " + e);
         retVal = false;
     }
     return retVal;
 }
开发者ID:CheneyWu,项目名称:coreclr,代码行数:19,代码来源:intptrgethashcode.cs

示例3: runTest

 public virtual bool runTest()
   {
   Console.Error.WriteLine(s_strTFPath + " " + s_strTFName + " , for " + s_strClassMethod + " , Source ver " + s_strDtTmVer);
   int iCountErrors = 0;
   int iCountTestcases = 0;
   String strLoc = "Loc_000oo";
   IntPtr ip1;
   Int32 iValue;
   try {
   strLoc = "Loc_743wg";
   iValue = 16;
   ip1 = new IntPtr(iValue);
   iCountTestcases++;
   if(ip1.GetHashCode() != iValue){
   iCountErrors++;
   Console.WriteLine("Err_865sg! Wrong value returned");
   }             
   strLoc = "Loc_87453sg";
   iValue = -25;
   ip1 = new IntPtr(iValue);
   iCountTestcases++;
   if(ip1.GetHashCode() != iValue){
   iCountErrors++;
   Console.WriteLine("Err_97356sg! Wrong value returned");
   }             
   strLoc = "Loc_87453sg";
   iValue = 0;
   ip1 = new IntPtr(iValue);
   iCountTestcases++;
   if(ip1.GetHashCode() != iValue){
   iCountErrors++;
   Console.WriteLine("Err_97sdg! Wrong value returned");
   }             
   strLoc = "Loc_87453sg";
   iValue = Int32.MaxValue;
   ip1 = new IntPtr(iValue);
   iCountTestcases++;
   if(ip1.GetHashCode() != iValue){
   iCountErrors++;
   Console.WriteLine("Err_97sdg! Wrong value returned");
   }             
   strLoc = "Loc_87453sg";
   iValue = Int32.MinValue;
   ip1 = new IntPtr(iValue);
   iCountTestcases++;
   if(ip1.GetHashCode() != iValue){
   iCountErrors++;
   Console.WriteLine("Err_97sdg! Wrong value returned");
   }             
   } catch (Exception exc_general ) {
   ++iCountErrors;
   Console.WriteLine(s_strTFAbbrev +" Error Err_8888yyy!  strLoc=="+ strLoc +", exc_general=="+exc_general);
   }
   if ( iCountErrors == 0 )
     {
     Console.Error.WriteLine( "paSs.   "+s_strTFPath +" "+s_strTFName+" ,iCountTestcases=="+iCountTestcases);
     return true;
     }
   else
     {
     Console.Error.WriteLine("FAiL!   "+s_strTFPath+" "+s_strTFName+" ,iCountErrors=="+iCountErrors+" , BugNums?: "+s_strActiveBugNums );
     return false;
     }
   }
开发者ID:gbarnett,项目名称:shared-source-cli-2.0,代码行数:64,代码来源:co8527gethashcode.cs

示例4: TestEquals

 public static void TestEquals(IntPtr ptr1, object obj, bool expected)
 {
     if (obj is IntPtr)
     {
         IntPtr ptr2 = (IntPtr)obj;
         Assert.Equal(expected, ptr1 == ptr2);
         Assert.Equal(!expected, ptr1 != ptr2);
         Assert.Equal(expected, ptr1.GetHashCode().Equals(ptr2.GetHashCode()));
     }
     Assert.Equal(expected, ptr1.Equals(obj));
     Assert.Equal(ptr1.GetHashCode(), ptr1.GetHashCode());
 }
开发者ID:shiftkey-tester,项目名称:corefx,代码行数:12,代码来源:IntPtr.cs

示例5: PosTest3

 public bool PosTest3()
 {
     bool retVal = true;
     try
     {
         int anyAddr = TestLibrary.Generator.GetInt32(-55);
         System.IntPtr ip = new IntPtr(anyAddr);
         if (ip.GetHashCode() != anyAddr )
         {
             TestLibrary.TestFramework.LogError("003", String.Format("expect IntPtr.GetHashCode() == {0}", anyAddr));
             retVal = false;
         }
     }
     catch (Exception e)
     {
         TestLibrary.TestFramework.LogError("003", "Unexpected exception: " + e);
         retVal = false;
     }
     return retVal;
 }
开发者ID:CheneyWu,项目名称:coreclr,代码行数:20,代码来源:intptrgethashcode.cs

示例6: PosTest4

    public bool PosTest4()
    {
        bool retVal = true;
        try
        {
            long addressOne = 0x123456FFFFFFFFL;
            long addressTwo = 0x654321FFFFFFFFL;
            System.IntPtr ipOne = new IntPtr(addressOne);
            System.IntPtr ipTwo = new IntPtr(addressTwo);
            if (ipOne.GetHashCode() == ipTwo.GetHashCode())
            {
                TestLibrary.TestFramework.LogError("004", "expect different hashcodes.");
                retVal = false;
            }
        }
        catch (System.OverflowException ex)
        {
            if (System.IntPtr.Size == 4)
            {
                // ok, that's what it should be
                return retVal;
            }
            else
		   	{
                TestLibrary.TestFramework.LogError("004", "IntPtr should not have thrown an OverflowException: " + ex.ToString());
                retVal = false;
		   	}
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("004", "Unexpected exception: " + e);
            retVal = false;
        }
        return retVal;
    }
开发者ID:CheneyWu,项目名称:coreclr,代码行数:35,代码来源:intptrgethashcode.cs

示例7: TestBasics

    public static unsafe void TestBasics()
    {
        if (sizeof(void*) == 4)
        {
            // Skip IntPtr tests on 32-bit platforms
            return;
        }

        IntPtr p;
        int i;
        long l;

        int size = IntPtr.Size;
        Assert.Equal(size, sizeof(void*));

        TestPointer(IntPtr.Zero, 0);

        i = 42;
        TestPointer(new IntPtr(i), i);
        TestPointer((IntPtr)i, i);

        i = 42;
        TestPointer(new IntPtr(i), i);

        i = -1;
        TestPointer(new IntPtr(i), i);

        l = 0x0fffffffffffffff;
        TestPointer(new IntPtr(l), l);
        TestPointer((IntPtr)l, l);

        void* pv = new IntPtr(42).ToPointer();
        TestPointer(new IntPtr(pv), 42);
        TestPointer((IntPtr)pv, 42);

        p = IntPtr.Add(new IntPtr(42), 5);
        TestPointer(p, 42 + 5);

        // Add is spected NOT to generate an OverflowException
        p = IntPtr.Add(new IntPtr(0x7fffffffffffffff), 5);
        unchecked
        {
            TestPointer(p, (long)0x8000000000000004);
        }

        p = IntPtr.Subtract(new IntPtr(42), 5);
        TestPointer(p, 42 - 5);

        bool b;
        p = new IntPtr(42);
        b = p.Equals(null);
        Assert.False(b);
        b = p.Equals((object)42);
        Assert.False(b);
        b = p.Equals((object)(new IntPtr(42)));
        Assert.True(b);

        int h = p.GetHashCode();
        int h2 = p.GetHashCode();
        Assert.Equal(h, h2);

        p = new IntPtr(42);
        i = (int)p;
        Assert.Equal(i, 42);
        l = (long)p;
        Assert.Equal(l, 42);
        IntPtr p2;
        p2 = (IntPtr)i;
        Assert.Equal(p, p2);
        p2 = (IntPtr)l;
        Assert.Equal(p, p2);
        p2 = (IntPtr)(p.ToPointer());
        Assert.Equal(p, p2);
        p2 = new IntPtr(40) + 2;
        Assert.Equal(p, p2);
        p2 = new IntPtr(44) - 2;
        Assert.Equal(p, p2);

        p = new IntPtr(0x7fffffffffffffff);
        Assert.Throws<OverflowException>(() => { i = (int)p; });
    }
开发者ID:er0dr1guez,项目名称:corefx,代码行数:81,代码来源:IntPtr.cs

示例8: TestGetHashCodeRespectAllBits

 public static void TestGetHashCodeRespectAllBits()
 {
     var ptr1 = new IntPtr(0x123456FFFFFFFF);
     var ptr2 = new IntPtr(0x654321FFFFFFFF);
     Assert.NotEqual(ptr1.GetHashCode(), ptr2.GetHashCode());
 }
开发者ID:kkurni,项目名称:corefx,代码行数:6,代码来源:IntPtr.cs


注:本文中的IntPtr.GetHashCode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。