context: //
  // TimeSpanTest.cs - NUnit Test Cases for the System.TimeSpan struct
  //
  // Authors:
  //	Duco Fijma (duco@lorentz.xs4all.nl)
  //	Sebastien Pouliot  <sebastien@ximian.com>
  //
  // (C) 2001 Duco Fijma
  // Copyright (C) 2004 Novell (http://www.novell.com)
  //
  
  using NUnit.Framework;
  using System;
  using System.Globalization;
  using System.Threading;
  
  namespace MonoTests.System
  {
  
  [TestFixture]
  public class TimeSpanTest {
  
  	private void Debug (TimeSpan ts) 
  	{
  		Console.Out.WriteLine ("Days {0}", ts.Days);
  		Console.Out.WriteLine ("Hours {0}", ts.Hours);
  		Console.Out.WriteLine ("Minutes {0}", ts.Minutes);
  		Console.Out.WriteLine ("Seconds {0}", ts.Seconds);
  		Console.Out.WriteLine ("Milliseconds {0}", ts.Milliseconds);
  		Console.Out.WriteLine ("Ticks {0}", ts.Ticks);
  	}
  
  	public void TestCtors ()
  	{
  		TimeSpan t1 = new TimeSpan (1234567890);
  
  		Assert.AreEqual ("00:02:03.4567890", t1.ToString (), "A1");
  		t1 = new TimeSpan (1,2,3);
  		Assert.AreEqual ("01:02:03", t1.ToString (), "A2");
  		t1 = new TimeSpan (1,2,3,4);
  		Assert.AreEqual ("1.02:03:04", t1.ToString (), "A3");
  		t1 = new TimeSpan (1,2,3,4,5);
  		Assert.AreEqual ("1.02:03:04.0050000", t1.ToString (), "A4");
  		t1 = new TimeSpan (-1,2,-3,4,-5);
  		Assert.AreEqual ("-22:02:56.0050000", t1.ToString (), "A5");
  		t1 = new TimeSpan (0,25,0,0,0);
  		Assert.AreEqual ("1.01:00:00", t1.ToString (), "A6");
          }
  
  	[Test]
  	[ExpectedException (typeof (ArgumentOutOfRangeException))]
  	public void DaysOverflow () 
  	{
  		int days = (int) (Int64.MaxValue / TimeSpan.TicksPerDay) + 1;
  		TimeSpan ts = new TimeSpan (days, 0, 0, 0, 0);
  	}
  
  	[Test]
  #if NET_2_0
  	[ExpectedException (typeof (ArgumentOutOfRangeException))]
  	[Category ("NotWorking")]
  #endif
  	public void TemporaryOverflow () 
  	{
  		// calculating part of this results in overflow (days)
  		// but the negative hours, minutes, seconds & ms correct this
  		int days = (int) (Int64.MaxValue / TimeSpan.TicksPerDay) + 1;
  		TimeSpan ts = new TimeSpan (days, Int32.MinValue, Int32.MinValue, Int32.MinValue, Int32.MinValue);
  		Assert.AreEqual (10650320, ts.Days, "Days");
  		Assert.AreEqual (0, ts.Hours, "Hours");
  		Assert.AreEqual (14, ts.Minutes, "Minutes");
  		Assert.AreEqual (28, ts.Seconds, "Seconds");
  		Assert.AreEqual (352, ts.Milliseconds, "Milliseconds");
  		Assert.AreEqual (9201876488683520000, ts.Ticks, "Ticks");
  	}
  
  	[Test]
  #if NET_2_0
  	[ExpectedException (typeof (ArgumentOutOfRangeException))]
  	[Category ("NotWorking")]
  #endif
  	public void NoOverflowInHoursMinsSecondsMS () 
  	{
  		TimeSpan ts = new TimeSpan (0, Int32.MaxValue, Int32.MaxValue, Int32.MaxValue, Int32.MaxValue);
  		Assert.AreEqual (24879, ts.Days, "Days");
  		Assert.AreEqual (22, ts.Hours, "Hours");
  		Assert.AreEqual (44, ts.Minutes, "Minutes");
  		Assert.AreEqual (30, ts.Seconds, "Seconds");
  		Assert.AreEqual (647, ts.Milliseconds, "Milliseconds");
  		Assert.AreEqual (21496274706470000, ts.Ticks, "Ticks");
  	}
  
  	[Test]
  	[ExpectedException (typeof (ArgumentOutOfRangeException))]
  	public void MaxDays () 
  	{
  		new TimeSpan (Int32.MaxValue, 0, 0, 0, 0);
  	}
  
  	[Test]
  	[ExpectedException (typeof (ArgumentOutOfRangeException))]
  	public void MinDays () 
  	{
  		new TimeSpan (Int32.MinValue, 0, 0, 0, 0);
  	}
  
  	[Test]
  	[Ignore ("too long")]
  	public void MaxHours_TooLong () 
  	{
  		// LAMESPEC: the highest hours are "special"
  		for (int i=0; i < 596523; i++) {
  			TimeSpan ts = new TimeSpan (0, Int32.MaxValue - i, 0, 0, 0);
  			int h = i + 1;
  			string prefix = i.ToString () + '-';
  			Assert.AreEqual (-(h / 24), ts.Days, prefix + "Days");
  			Assert.AreEqual (-(h % 24), ts.Hours, prefix + "Hours");
  			Assert.AreEqual (0, ts.Minutes, prefix + "Minutes");
  			Assert.AreEqual (0, ts.Seconds, prefix + "Seconds");
  			Assert.AreEqual (0, ts.Milliseconds, prefix + "Milliseconds");
  			Assert.AreEqual (-36000000000 * h, ts.Ticks, prefix + "Ticks");
  		}
  	}
  
  	[Test]
  #if NET_2_0
  	[ExpectedException (typeof (ArgumentOutOfRangeException))]
  	[Category ("NotWorking")]
  #endif
  	public void MaxHours () 
  	{
  		// LAMESPEC: the highest hours are "special"
  		TimeSpan ts = new TimeSpan (0, Int32.MaxValue, 0, 0, 0);
  		Assert.AreEqual (0, ts.Days, "Max-Days");
  		Assert.AreEqual (-1, ts.Hours, "Max-Hours");
  		Assert.AreEqual (0, ts.Minutes, "Max-Minutes");
  		Assert.AreEqual (0, ts.Seconds, "Max-Seconds");
  		Assert.AreEqual (0, ts.Milliseconds, "Max-Milliseconds");
  		Assert.AreEqual (-36000000000, ts.Ticks, "Max-Ticks");
  
  		ts = new TimeSpan (0, Int32.MaxValue - 596522, 0, 0, 0);
  		Assert.AreEqual (-24855, ts.Days, "Days");
  		Assert.AreEqual (-3, ts.Hours, "Hours");
  		Assert.AreEqual (0, ts.Minutes, "Minutes");
  		Assert.AreEqual (0, ts.Seconds, "Seconds");
  		Assert.AreEqual (0, ts.Milliseconds, "Milliseconds");
  		Assert.AreEqual (-21474828000000000, ts.Ticks, "Ticks");
  	}
  
  	[Test]
  #if NET_2_0
  	[ExpectedException (typeof (ArgumentOutOfRangeException))]
  	[Category ("NotWorking")]
  #endif
  	public void MaxHours_BreakPoint () 
  	{
  		TimeSpan ts = new TimeSpan (0, Int32.MaxValue - 596523, 0, 0, 0);
  		Assert.AreEqual (24855, ts.Days, "Days");
  		Assert.AreEqual (2, ts.Hours, "Hours");
  		Assert.AreEqual (28, ts.Minutes, "Minutes");
  		Assert.AreEqual (16, ts.Seconds, "Seconds");
  		Assert.AreEqual (0, ts.Milliseconds, "Milliseconds");
  		Assert.AreEqual (21474808960000000, ts.Ticks, "Ticks");
  	}
  
  	[Test]
  	[Ignore ("too long")]
  	public void MinHours_TooLong () 
  	{
  		// LAMESPEC: the lowest hours are "special"
  		for (int i=Int32.MinValue; i < -2146887124; i++) {
  			TimeSpan ts = new TimeSpan (0, i, 0, 0, 0);
  			int h = i + Int32.MaxValue + 1;
  			string prefix = i.ToString () + '-';
  			Assert.AreEqual ((h / 24), ts.Days, prefix + "Days");
  			Assert.AreEqual ((h % 24), ts.Hours, prefix + "Hours");
  			Assert.AreEqual (0, ts.Minutes, prefix + "Minutes");
  			Assert.AreEqual (0, ts.Seconds, prefix + "Seconds");
  			Assert.AreEqual (0, ts.Milliseconds, prefix + "Milliseconds");
  			Assert.AreEqual (36000000000 * h, ts.Ticks, prefix + "Ticks");
  		}
  	}
  
  	[Test]
  #if NET_2_0
  	[Category ("NotWorking")]
  #endif
  	public void MinHours () 
  	{
  #if NET_2_0
  		TimeSpan ts = new TimeSpan (0, -256204778, 0, 0, 0);
  		Assert.AreEqual (-10675199, ts.Days, "Days");
  		Assert.AreEqual (-2, ts.Hours, "Hours");
  		Assert.AreEqual (0, ts.Minutes, "Minutes");
  		Assert.AreEqual (0, ts.Seconds, "Seconds");
  		Assert.AreEqual (0, ts.Milliseconds, "Milliseconds");
  		Assert.AreEqual (-9223372008000000000, ts.Ticks, "Ticks");
  #else
  		// LAMESPEC: the lowest hours are "special"
  		TimeSpan ts = new TimeSpan (0, Int32.MinValue, 0, 0, 0);
  		Assert.AreEqual (0, ts.Days, "Min-Days");
  		Assert.AreEqual (0, ts.Hours, "Min-Hours");
  		Assert.AreEqual (0, ts.Minutes, "Min-Minutes");
  		Assert.AreEqual (0, ts.Seconds, "Min-Seconds");
  		Assert.AreEqual (0, ts.Milliseconds, "Min-Milliseconds");
  		Assert.AreEqual (0, ts.Ticks, "Min-Ticks");
  
  		ts = new TimeSpan (0, -2146887125, 0, 0, 0);
  		Assert.AreEqual (24855, ts.Days, "Days");
  		Assert.AreEqual (3, ts.Hours, "Hours");
  		Assert.AreEqual (0, ts.Minutes, "Minutes");
  		Assert.AreEqual (0, ts.Seconds, "Seconds");
  		Assert.AreEqual (0, ts.Milliseconds, "Milliseconds");
  		Assert.AreEqual (21474828000000000, ts.Ticks, "Ticks");
  #endif
  	}
  
  	[Test]
  #if NET_2_0
  	[ExpectedException (typeof (ArgumentOutOfRangeException))]
  	[Category ("NotWorking")]
  #endif
  	public void MinHours_BreakPoint () 
  	{
  		TimeSpan ts = new TimeSpan (0, -2146887124, 0, 0, 0);
  		Assert.AreEqual (-24855, ts.Days, "Days");
  		Assert.AreEqual (-2, ts.Hours, "Hours");
  		Assert.AreEqual (-28, ts.Minutes, "Minutes");
  		Assert.AreEqual (-16, ts.Seconds, "Seconds");
  		Assert.AreEqual (0, ts.Milliseconds, "Milliseconds");
  		Assert.AreEqual (-21474808960000000, ts.Ticks, "Ticks");
  	}
  
  	[Test]
  	[Ignore ("too long")]
  	public void MaxMinutes_TooLong () 
  	{
  		// LAMESPEC: the highest minutes are "special"
  		for (int i=0; i < 35791394; i++) {
  			TimeSpan ts = new TimeSpan (0, 0, Int32.MaxValue - i, 0, 0);
  			long h = -(i + 1);
  			string prefix = i.ToString () + '-';
  			Assert.AreEqual ((h / 1440), ts.Days, prefix + "Days");
  			Assert.AreEqual (((h / 60) % 24), ts.Hours, prefix + "Hours");
  			Assert.AreEqual ((h % 60), ts.Minutes, prefix + "Minutes");
  			Assert.AreEqual (0, ts.Seconds, prefix + "Seconds");
  			Assert.AreEqual (0, ts.Milliseconds, prefix + "Milliseconds");
  			Assert.AreEqual ((600000000L * h), ts.Ticks, prefix + "Ticks");
  		}
  	}
  
  	[Test]
  #if NET_2_0
  	[Category ("NotWorking")]
  #endif
  	public void MaxMinutes () 
  	{
  		TimeSpan ts;
  #if NET_2_0
  		ts = new TimeSpan (0, 0, 256204778, 0, 0);
  		Assert.AreEqual (177919, ts.Days, "Max-Days");
  		Assert.AreEqual (23, ts.Hours, "Max-Hours");
  		Assert.AreEqual (38, ts.Minutes, "Max-Minutes");
  		Assert.AreEqual (0, ts.Seconds, "Max-Seconds");
  		Assert.AreEqual (0, ts.Milliseconds, "Max-Milliseconds");
  		Assert.AreEqual (153722866800000000, ts.Ticks, "Max-Ticks");
  #else
  		// LAMESPEC: the highest minutes are "special"
  		ts = new TimeSpan (0, 0, Int32.MaxValue, 0, 0);
  		Assert.AreEqual (0, ts.Days, "Max-Days");
  		Assert.AreEqual (0, ts.Hours, "Max-Hours");
  		Assert.AreEqual (-1, ts.Minutes, "Max-Minutes");
  		Assert.AreEqual (0, ts.Seconds, "Max-Seconds");
  		Assert.AreEqual (0, ts.Milliseconds, "Max-Milliseconds");
  		Assert.AreEqual (-600000000, ts.Ticks, "Max-Ticks");
  
  		ts = new TimeSpan (0, 0, Int32.MaxValue - 35791393, 0, 0);
  		Assert.AreEqual (-24855, ts.Days, "Days");
  		Assert.AreEqual (-3, ts.Hours, "Hours");
  		Assert.AreEqual (-14, ts.Minutes, "Minutes");
  		Assert.AreEqual (0, ts.Seconds, "Seconds");
  		Assert.AreEqual (0, ts.Milliseconds, "Milliseconds");
  		Assert.AreEqual (-21474836400000000, ts.Ticks, "Ticks");
  #endif
  	}
  
  	[Test]
  #if NET_2_0
  	[ExpectedException (typeof (ArgumentOutOfRangeException))]
  	[Category ("NotWorking")]
  #endif
  	public void MaxMinutes_BreakPoint () 
  	{
  		TimeSpan ts = new TimeSpan (0, Int32.MaxValue - 35791394, 0, 0, 0);
  		Assert.AreEqual (0, ts.Days, "Days");
  		Assert.AreEqual (0, ts.Hours, "Hours");
  		Assert.AreEqual (-52, ts.Minutes, "Minutes");
  		Assert.AreEqual (0, ts.Seconds, "Seconds");
  		Assert.AreEqual (0, ts.Milliseconds, "Milliseconds");
  		Assert.AreEqual (-31200000000, ts.Ticks, "Ticks");
  	}
  
  	[Test]
  	[Ignore ("too long")]
  	public void MinMinutes_TooLong () 
  	{
  		// LAMESPEC: the highest minutes are "special"
  		for (int i=Int32.MinValue; i < -2111692253; i++) {
  			TimeSpan ts = new TimeSpan (0, 0, i, 0, 0);
  			long h = i + Int32.MaxValue + 1;
  			string prefix = i.ToString () + '-';
  			Assert.AreEqual ((h / 1440), ts.Days, prefix + "Days");
  			Assert.AreEqual (((h / 60) % 24), ts.Hours, prefix + "Hours");
  			Assert.AreEqual ((h % 60), ts.Minutes, prefix + "Minutes");
  			Assert.AreEqual (0, ts.Seconds, prefix + "Seconds");
  			Assert.AreEqual (0, ts.Milliseconds, prefix + "Milliseconds");
  			Assert.AreEqual ((600000000L * h), ts.Ticks, prefix + "Ticks");
  		}
  	}
  
  	[Test]
  #if NET_2_0
  	[Category ("NotWorking")]
  #endif
  	public void MinMinutes () 
  	{
  		TimeSpan ts;
  #if NET_2_0
  		ts = new TimeSpan (0, 0, Int32.MinValue, 0, 0);
  		Assert.AreEqual (-1491308, ts.Days, "Days");
  		Assert.AreEqual (-2, ts.Hours, "Hours");
  		Assert.AreEqual (-8, ts.Minutes, "Minutes");
  		Assert.AreEqual (0, ts.Seconds, "Seconds");
  		Assert.AreEqual (0, ts.Milliseconds, "Milliseconds");
  		Assert.AreEqual (-1288490188800000000, ts.Ticks, "Ticks");
  #else
  		// LAMESPEC: the highest minutes are "special"
  		ts = new TimeSpan (0, 0, Int32.MinValue, 0, 0);
  		Assert.AreEqual (0, ts.Days, "Min-Days");
  		Assert.AreEqual (0, ts.Hours, "Min-Hours");
  		Assert.AreEqual (0, ts.Minutes, "Min-Minutes");
  		Assert.AreEqual (0, ts.Seconds, "Min-Seconds");
  		Assert.AreEqual (0, ts.Milliseconds, "Min-Milliseconds");
  		Assert.AreEqual (0, ts.Ticks, "Min-Ticks");
  
  		ts = new TimeSpan (0, 0, -2111692254, 0, 0);
  		Assert.AreEqual (24855, ts.Days, "Days");
  		Assert.AreEqual (3, ts.Hours, "Hours");
  		Assert.AreEqual (14, ts.Minutes, "Minutes");
  		Assert.AreEqual (0, ts.Seconds, "Seconds");
  		Assert.AreEqual (0, ts.Milliseconds, "Milliseconds");
  		Assert.AreEqual (21474836400000000, ts.Ticks, "Ticks");
  #endif
  	}
  
  	[Test]
  #if NET_2_0
  	[Category ("NotWorking")]
  #endif
  	public void MinMinutes_BreakPoint () 
  	{
  #if NET_2_0
  		TimeSpan ts = new TimeSpan (0, 0, -2111692253, 0, 0);
  		Assert.AreEqual (-1466452, ts.Days, "Days");
  		Assert.AreEqual (-22, ts.Hours, "Hours");
  		Assert.AreEqual (-53, ts.Minutes, "Minutes");
  		Assert.AreEqual (-0, ts.Seconds, "Seconds");
  		Assert.AreEqual (0, ts.Milliseconds, "Milliseconds");
  		Assert.AreEqual (-1267015351800000000, ts.Ticks, "Ticks");
  #else
  		TimeSpan ts = new TimeSpan (0, 0, -2111692253, 0, 0);
  		Assert.AreEqual (-24855, ts.Days, "Days");
  		Assert.AreEqual (-3, ts.Hours, "Hours");
  		Assert.AreEqual (-13, ts.Minutes, "Minutes");
  		Assert.AreEqual (-16, ts.Seconds, "Seconds");
  		Assert.AreEqual (0, ts.Milliseconds, "Milliseconds");
  		Assert.AreEqual (-21474835960000000, ts.Ticks, "Ticks");
  #endif
  	}
  
  	[Test]
  	public void MaxSeconds () 
  	{
  		TimeSpan ts = new TimeSpan (0, 0, 0, Int32.MaxValue, 0);
  		Assert.AreEqual (24855, ts.Days, "Days");
  		Assert.AreEqual (3, ts.Hours, "Hours");
  		Assert.AreEqual (14, ts.Minutes, "Minutes");
  		Assert.AreEqual (7, ts.Seconds, "Seconds");
  		Assert.AreEqual (0, ts.Milliseconds, "Milliseconds");
  		Assert.AreEqual (21474836470000000, ts.Ticks, "Ticks");
  	}
  
  	[Test]
  	public void MinSeconds () 
  	{
  		TimeSpan ts = new TimeSpan (0, 0, 0, Int32.MinValue, 0);
  		Assert.AreEqual (-24855, ts.Days, "Days");
  		Assert.AreEqual (-3, ts.Hours, "Hours");
  		Assert.AreEqual (-14, ts.Minutes, "Minutes");
  		Assert.AreEqual (-8, ts.Seconds, "Seconds");
  		Assert.AreEqual (0, ts.Milliseconds, "Milliseconds");
  		Assert.AreEqual (-21474836480000000, ts.Ticks, "Ticks");
  	}
  
  	[Test]
  	public void MaxMilliseconds () 
  	{
  		TimeSpan ts = new TimeSpan (0, 0, 0, 0, Int32.MaxValue);
  		Assert.AreEqual (24, ts.Days, "Days");
  		Assert.AreEqual (20, ts.Hours, "Hours");
  		Assert.AreEqual (31, ts.Minutes, "Minutes");
  		Assert.AreEqual (23, ts.Seconds, "Seconds");
  		Assert.AreEqual (647, ts.Milliseconds, "Milliseconds");
  		Assert.AreEqual (21474836470000, ts.Ticks, "Ticks");
  	}
  
  	[Test]
  	public void MinMilliseconds () 
  	{
  		TimeSpan ts = new TimeSpan (0, 0, 0, 0, Int32.MinValue);
  		Assert.AreEqual (-24, ts.Days, "Days");
  		Assert.AreEqual (-20, ts.Hours, "Hours");
  		Assert.AreEqual (-31, ts.Minutes, "Minutes");
  		Assert.AreEqual (-23, ts.Seconds, "Seconds");
  		Assert.AreEqual (-648, ts.Milliseconds, "Milliseconds");
  		Assert.AreEqual (-21474836480000, ts.Ticks, "Ticks");
  	}
  
  	[Test]
  	public void NegativeTimeSpan () 
  	{
  		TimeSpan ts = new TimeSpan (-23, -59, -59);
  		Assert.AreEqual (0, ts.Days, "Days");
  		Assert.AreEqual (-23, ts.Hours, "Hours");
  		Assert.AreEqual (-59, ts.Minutes, "Minutes");
  		Assert.AreEqual (-59, ts.Seconds, "Seconds");
  		Assert.AreEqual (0, ts.Milliseconds, "Milliseconds");
  		Assert.AreEqual (-863990000000, ts.Ticks, "Ticks");
  	}
  
  	public void TestProperties ()
  	{
  		TimeSpan t1 = new TimeSpan (1,2,3,4,5);
  		TimeSpan t2 = -t1;
  
  		Assert.AreEqual (1, t1.Days, "A1");
  		Assert.AreEqual (2, t1.Hours, "A2");
  		Assert.AreEqual (3, t1.Minutes, "A3");
  		Assert.AreEqual (4, t1.Seconds, "A4");
  		Assert.AreEqual (5, t1.Milliseconds, "A5");
  		Assert.AreEqual (-1, t2.Days, "A6");
  		Assert.AreEqual (-2, t2.Hours, "A7");
  		Assert.AreEqual (-3, t2.Minutes, "A8");
  		Assert.AreEqual (-4, t2.Seconds, "A9");
  
gt: 		Assert.AreEqual (-5, t2.Milliseconds, "A10");