Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions src/System.Runtime/tests/System/TimeSpanTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,14 @@ public static IEnumerable<object[]> Parse_Valid_TestData()
yield return new object[] { "1:1:1.00001", CultureInfo.InvariantCulture, new TimeSpan(36610000100) };
yield return new object[] { "1:1:1.000001", CultureInfo.InvariantCulture, new TimeSpan(36610000010) };
yield return new object[] { "1:1:1.0000001", CultureInfo.InvariantCulture, new TimeSpan(36610000001) };
yield return new object[] { "1:1:1.00000001", CultureInfo.InvariantCulture, new TimeSpan(36610000001) };

if (PlatformDetection.IsFullFramework)
{
// Full framework can produce some incorrect results in some cases involving leading zeros when
// parsing fraction more than 7 digits. we test the expected full framework results here and we have
// have more net core tests to validate the correct the results.
yield return new object[] { "1:1:1.00000001", CultureInfo.InvariantCulture, new TimeSpan(36610000001) };
}

// DD.HH:MM:SS
yield return new object[] { "1.12:24:02", null, new TimeSpan(1, 12, 24, 2, 0) };
Expand All @@ -560,7 +567,14 @@ public static IEnumerable<object[]> Parse_Valid_TestData()
yield return new object[] { "1:1:.00001", CultureInfo.InvariantCulture, new TimeSpan(36600000100) };
yield return new object[] { "1:1:.000001", CultureInfo.InvariantCulture, new TimeSpan(36600000010) };
yield return new object[] { "1:1:.0000001", CultureInfo.InvariantCulture, new TimeSpan(36600000001) };
yield return new object[] { "1:1:.00000001", CultureInfo.InvariantCulture, new TimeSpan(36600000001) };

if (PlatformDetection.IsFullFramework)
{
// Full framework can produce some incorrect results in some cases involving leading zeros when
// parsing fraction more than 7 digits. we test the expected full framework results here and we have
// have more net core tests to validate the correct the results.
yield return new object[] { "1:1:.00000001", CultureInfo.InvariantCulture, new TimeSpan(36600000001) };
}

// Just below overflow on various components
yield return new object[] { "10675199", null, new TimeSpan(9223371936000000000) };
Expand Down Expand Up @@ -625,7 +639,16 @@ public static IEnumerable<object[]> Parse_Invalid_TestData()

// OverflowExceptions
yield return new object[] { "1:1:1.99999999", null, typeof(OverflowException) }; // overflowing fraction
yield return new object[] { "1:1:1.000000001", null, typeof(OverflowException) }; // too many leading zeroes in fraction

if (PlatformDetection.IsFullFramework)
{
// on non full framework we now succeed parsing the fraction .000000001
// Full framework can produce some incorrect results in some cases involving leading zeros when
// parsing fraction more than 7 digits. we test the expected full framework results here and we have
// have more net core tests to validate the correct the results.
yield return new object[] { "1:1:1.000000001", null, typeof(OverflowException) }; // too many leading zeroes in fraction
}

yield return new object[] { "2147483647", null, typeof(OverflowException) }; // overflowing value == int.MaxValue
yield return new object[] { "2147483648", null, typeof(OverflowException) }; // overflowing value == int.MaxValue + 1
yield return new object[] { "10675200", null, typeof(OverflowException) }; // overflowing number of days
Expand Down Expand Up @@ -809,15 +832,15 @@ public static void ParseExactTest_Invalid(string input, string format, Type exce
{
Assert.Throws(exceptionType, () => TimeSpan.ParseExact(input, format, new CultureInfo("en-US")));
Assert.Throws(exceptionType, () => TimeSpan.ParseExact(input, format, new CultureInfo("en-US"), TimeSpanStyles.None));

Type exceptionTypeMultiple = exceptionType == typeof(OverflowException) || string.IsNullOrEmpty(format) ? typeof(FormatException) : exceptionType;
Assert.Throws(exceptionTypeMultiple, () => TimeSpan.ParseExact(input, new string[] { format }, new CultureInfo("en-US")));
Assert.Throws(exceptionTypeMultiple, () => TimeSpan.ParseExact(input, new string[] { format }, new CultureInfo("en-US"), TimeSpanStyles.None));

TimeSpan result;
Assert.False(TimeSpan.TryParseExact(input, format, new CultureInfo("en-US"), out result));
Assert.Equal(TimeSpan.Zero, result);

Assert.False(TimeSpan.TryParseExact(input, format, new CultureInfo("en-US"), TimeSpanStyles.None, out result));
Assert.Equal(TimeSpan.Zero, result);

Expand Down
24 changes: 24 additions & 0 deletions src/System.Runtime/tests/System/TimeSpanTests.netcoreapp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,30 @@ private static IEnumerable<object[]> MultiplicationTestData()
yield return new object[] {TimeSpan.MaxValue, 0.5, TimeSpan.FromTicks((long)(long.MaxValue * 0.5))};
}

// ParseDifferentLengthFractionWithLeadingZerosData mainly testing the behavior we have fixed in net core
// which is the way we normalize the parsed fraction and possibly rounding it.
private static IEnumerable<object[]> ParseDifferentLengthFractionWithLeadingZerosData()
{
yield return new object[] {"00:00:00.00000001", new TimeSpan(0)};
yield return new object[] {"00:00:00.00000005", new TimeSpan(1)};
yield return new object[] {"00:00:00.09999999", new TimeSpan(1_000_000)};
yield return new object[] {"00:00:00.0268435455", new TimeSpan(268435)};
yield return new object[] {"00:00:00.01", new TimeSpan(1_00_000)};
yield return new object[] {"0:00:00.01000000", new TimeSpan(100_000)};
yield return new object[] {"0:00:00.010000000", new TimeSpan(100_000)};
yield return new object[] {"0:00:00.0123456", new TimeSpan(123456)};
yield return new object[] {"0:00:00.00123456", new TimeSpan(12346)};
yield return new object[] {"0:00:00.00000098", new TimeSpan(10)};
yield return new object[] {"0:00:00.00000099", new TimeSpan(10)};
}

[Theory, MemberData(nameof(ParseDifferentLengthFractionWithLeadingZerosData))]
public static void Multiplication(string input, TimeSpan expected)
{
Assert.Equal(expected, TimeSpan.Parse(input, CultureInfo.InvariantCulture));
Assert.Equal(expected, TimeSpan.ParseExact(input, "g", CultureInfo.InvariantCulture));
}

[Theory, MemberData(nameof(MultiplicationTestData))]
public static void Multiplication(TimeSpan timeSpan, double factor, TimeSpan expected)
{
Expand Down