Skip to content

Commit

Permalink
Again issue three merger is vexing me. Hope this gets somewhere!
Browse files Browse the repository at this point in the history
  • Loading branch information
cpsusie committed Nov 27, 2020
1 parent ade387f commit 7306069
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 17 deletions.
13 changes: 6 additions & 7 deletions Duration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ public double TotalMilliseconds
{
get
{
double temp = (double)_ticks / TicksPerMillisecond;
double temp = (double) _ticks / TicksPerSecond * 1_000;

if (temp > (double) MaxMilliseconds)
return (double)MaxMilliseconds;

Expand Down Expand Up @@ -644,9 +645,7 @@ private static Duration IntervalFromDoubleTicks(double ticks)
{
if ((ticks > (double) TickInt.MaxValue) || (ticks < (double) TickInt.MinValue) || double.IsNaN(ticks))
throw new OverflowException("Value cannot fit in a TimeSpan.");
if (ticks >= long.MaxValue)
return MaxValue;
return new Duration((long)ticks);
return new Duration((TickInt)ticks);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -673,10 +672,10 @@ internal static bool AreValuesCloseEnoughAfterConversionToTimeSpan(in Duration d

internal static bool AreValuesCloseEnough(in Duration d, TimeSpan t)
{
const double diffMustBeLessThan = 1.0;
if (ConvertStopwatchTicksToTimespanTicks(d._ticks) == t.Ticks) return true;
long wholeMillisecondsDuration = Convert.ToInt64(Math.Truncate(d.TotalMilliseconds));
long wholeMillisecondsTimespan = Convert.ToInt64(Math.Truncate(t.TotalMilliseconds));
return wholeMillisecondsDuration == wholeMillisecondsTimespan;
double diff = Math.Abs(d.TotalMilliseconds - t.TotalMilliseconds);
return diff < diffMustBeLessThan;
}

internal static bool AreValuesCloseEnough(in Duration d, in PortableDuration pd)
Expand Down
11 changes: 1 addition & 10 deletions UnitTests/UnitTests/MonotonicStampTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,12 @@ public void PortableDurationConversionFailureTwoElaborationOne()
TimeSpan roundTripped = (TimeSpan) pd;
Assert.True(roundTripped.Ticks == timespanTicks);
}

[Fact]
public void TestPortableDurationConversionFailureCaseFour()
{
const long failingVal = -6_433_771_731_613_161_268;
TestPortableDurationDurationConversions(1, failingVal);
}

private IEnumerable<TimeSpan> GetNRandomTimespans(int numSpans)
{
if (numSpans < 0) throw new ArgumentOutOfRangeException(nameof(numSpans), numSpans, "Value may not be negative.");
Expand Down Expand Up @@ -274,7 +272,7 @@ private void AssertPortableDoubleCloseEnough(double portable, double notPortable
{
double epsilon = PortableDuration.TicksPerSecond == MonotonicStampFixture.StampContext.TicksPerSecond
? 0.25
: CalculateEpsilon(MonotonicStampFixture.StampContext.TicksPerSecond, PortableDuration.TicksPerSecond);//((double) MonotonicStampFixture.StampContext.TicksPerSecond / PortableDuration.TicksPerSecond * 75.0) ;
: 1.0 ;
Assert.False(double.IsNaN(portable));
Assert.False(double.IsNaN(notPortable));
Assert.False(double.IsPositiveInfinity(portable) || double.IsNegativeInfinity(portable) || double.IsPositiveInfinity(notPortable) || double.IsNegativeInfinity(notPortable));
Expand All @@ -285,13 +283,6 @@ private void AssertPortableDoubleCloseEnough(double portable, double notPortable
Assert.False(true,
$"The absolute distance between {portable:N} and {notPortable:N} is {absVOfDiff:N} which is greater than {epsilon:N}.");
}

double CalculateEpsilon(double monotonicTicksPerSecond, double portableTicksPerSecond)
{
double greater = Math.Max(monotonicTicksPerSecond, portableTicksPerSecond);
double lesser = Math.Min(monotonicTicksPerSecond, portableTicksPerSecond);
return (1.0 - (lesser / greater));
}
}

private void AssertDoubleEqual(double expected, double actual, double epsilon = 0.5)
Expand Down

0 comments on commit 7306069

Please sign in to comment.