當前位置: 首頁>>代碼示例>>C#>>正文


C# TimeSpan.GetHashCode方法代碼示例

本文整理匯總了C#中System.TimeSpan.GetHashCode方法的典型用法代碼示例。如果您正苦於以下問題:C# TimeSpan.GetHashCode方法的具體用法?C# TimeSpan.GetHashCode怎麽用?C# TimeSpan.GetHashCode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.TimeSpan的用法示例。


在下文中一共展示了TimeSpan.GetHashCode方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: PosTest1

    public bool PosTest1()
    {
        bool retVal = true;

        TestLibrary.TestFramework.BeginScenario("PosTest1: GetHashCode should always return the same value for same TimeSpan instance");

        try
        {
            long randValue = TestLibrary.Generator.GetInt64(-55);
            TimeSpan ts = new TimeSpan(randValue);

            int hash1 = ts.GetHashCode();
            int hash2 = ts.GetHashCode();

            if (hash1 != hash2)
            {
                TestLibrary.TestFramework.LogError("001.1", "GetHashCode not always return the same value for same TimeSpan instance");
                TestLibrary.TestFramework.LogInformation("WARNING [LOCAL VARIABLE] hash1 = " + hash1 + ", hash2 = " + hash2 + ", randValue = " + randValue);
                retVal = false;
            }
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("001.0", "Unexpected exception: " + e);
            TestLibrary.TestFramework.LogInformation(e.StackTrace);
            retVal = false;
        }

        return retVal;
    }
開發者ID:l1183479157,項目名稱:coreclr,代碼行數:30,代碼來源:timespangethashcode.cs

示例2: runTest

 public virtual bool runTest()
   {
   int iCountErrors = 0;
   int iCountTestcases = 0;
   Console.Error.WriteLine( strName + ": " + strTest + " runTest started..." );
   TimeSpan	ts1;
   TimeSpan	ts2;
   TimeSpan	tsResult;
   do
     {
     ++iCountTestcases;
     Console.Error.WriteLine( "[]  Negate positive time to yield negative time" );
     try
       {
       ts1 = new TimeSpan(10*TimeSpan.TicksPerHour);
       ts2 = new TimeSpan(10*TimeSpan.TicksPerHour);
       if ( ts1.GetHashCode() != ts2.GetHashCode() )
	 {
	 String strInfo = strTest + " error: ";
	 strInfo = strInfo + "Expected Ticks <" + ts1.GetHashCode() + "> ";
	 strInfo = strInfo + "Returned Ticks <" + ts2.GetHashCode() + "> ";
	 Console.WriteLine( strTest+ "E_101a: " + strInfo );
	 ++iCountErrors;
	 break;
	 }
       }
     catch (Exception ex)
       {
       Console.WriteLine( strTest+ "E_10001: Unexpected Exception: " + ex.ToString() );
       ++iCountErrors;
       break;
       }
     }
   while ( false );
   Console.Error.Write( strName );
   Console.Error.Write( ": " );
   if ( iCountErrors == 0 )
     {
     Console.Error.WriteLine( strTest + " iCountTestcases==" + iCountTestcases + " paSs" );
     return true;
     }
   else
     {
     System.String strFailMsg = null;
     Console.WriteLine( strTest+ strPath );
     Console.WriteLine( strTest+ "FAiL" );
     Console.Error.WriteLine( strTest + " iCountErrors==" + iCountErrors );
     return false;
     }
   }
開發者ID:ArildF,項目名稱:masters,代碼行數:50,代碼來源:co2926gethashcode.cs

示例3: TestEquals

    public static void TestEquals(TimeSpan timeSpan1, object obj, bool expected)
    {
        if (obj is TimeSpan)
        {
            TimeSpan timeSpan2 = (TimeSpan)obj;
            Assert.Equal(expected, TimeSpan.Equals(timeSpan1, timeSpan2));
            Assert.Equal(expected, timeSpan1.Equals(timeSpan2));
            Assert.Equal(expected, timeSpan1 == timeSpan2);
            Assert.Equal(!expected, timeSpan1 != timeSpan2);

            Assert.Equal(expected, timeSpan1.GetHashCode().Equals(timeSpan2.GetHashCode()));
        }
        Assert.Equal(expected, timeSpan1.Equals(obj));
    }
開發者ID:eerhardt,項目名稱:corefx,代碼行數:14,代碼來源:TimeSpan.cs

示例4: GetHashCode_Test5

		public MFTestResults GetHashCode_Test5(  )
		{
            /// <summary>
            /// 1. Test that GetHashCode returns the same for the same TimeSpan
            /// 2. Test that GetHashCode returns differently for different TimeSpans
            /// </summary>
            ///
            Log.Comment("Testing the GetHashCode method");
			bool testResult = true;
            Random random = new Random();
            Log.Comment("Test that GetHashCode returns the same for the same TimeSpan");
			for( int i = 0; i<30; i++ )
			{
				int hours = random.Next(23);
				int minutes = random.Next(59);
				int seconds = random.Next(59);
				TimeSpan ts01 = new TimeSpan( hours, minutes, seconds );
				TimeSpan ts02 = new TimeSpan( hours, minutes, seconds );
				Log.Comment( ts01.GetHashCode().ToString() + " == " + 
                    ts02.GetHashCode().ToString() );
				testResult &= (ts01.GetHashCode() == ts02.GetHashCode());
			}

			TimeSpan ts1 = new TimeSpan( 1,1,1 );
            Log.Comment("Test that GetHashCode returns differently for different TimeSpans");
            Log.Comment("This may fail erroneously.");  
            Log.Comment("From the docs two different TimeSpans may have same hashcode" );
			Log.Comment( "But, for the most part the values should be different." );
			for( int i = 0; i < 5; i++ )
			{
				TimeSpan ts2 = new TimeSpan( random.Next(23),
                    random.Next(59),random.Next(59) );
				Log.Comment( ts1.GetHashCode().ToString() + " Does Not Equal " + 
                    ts2.GetHashCode().ToString() );
				if( ts1 != ts2 )
					testResult &= (ts1.GetHashCode() != ts2.GetHashCode());
				else
					testResult &= (ts1.GetHashCode() == ts2.GetHashCode());
			}

			return (testResult? MFTestResults.Pass: MFTestResults.Fail);
		}
開發者ID:aura1213,項目名稱:netmf-interpreter,代碼行數:42,代碼來源:SystemTimeSpanTests.cs


注:本文中的System.TimeSpan.GetHashCode方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。