Update TimeSpan Parsing Test
authorTarek Mahmoud Sayed <tarekms@microsoft.com>
Sat, 12 Jan 2019 00:26:11 +0000 (16:26 -0800)
committerStephen Toub <stoub@microsoft.com>
Tue, 15 Jan 2019 17:35:11 +0000 (12:35 -0500)
Commit migrated from https://github.com/dotnet/corefx/commit/a58777883c9102f9586dd723e2d4ba35aeedf0ef

src/libraries/System.Runtime/tests/System/TimeSpanTests.cs
src/libraries/System.Runtime/tests/System/TimeSpanTests.netcoreapp.cs

index 031caf3..a81e4a0 100644 (file)
@@ -538,7 +538,14 @@ namespace System.Tests
             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) };
@@ -560,7 +567,14 @@ namespace System.Tests
             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) };
@@ -625,7 +639,16 @@ namespace System.Tests
 
             // 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
@@ -809,15 +832,15 @@ namespace System.Tests
         {
             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);
 
index e44e53d..d89791d 100644 (file)
@@ -22,6 +22,30 @@ namespace System.Tests
             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)
         {