// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using System.Globalization; public class DateTimeParseExact2 { private const int c_MIN_STRING_LEN = 1; private const int c_MAX_STRING_LEN = 2048; private const int c_NUM_LOOPS = 100; private static DateTimeStyles[] c_STYLES = new DateTimeStyles[7] {DateTimeStyles.AdjustToUniversal, DateTimeStyles.AllowInnerWhite, DateTimeStyles.AllowLeadingWhite, DateTimeStyles.AllowTrailingWhite , DateTimeStyles.AllowWhiteSpaces, DateTimeStyles.NoCurrentDateDefault, DateTimeStyles.None }; public static int Main() { DateTimeParseExact2 test = new DateTimeParseExact2(); TestLibrary.TestFramework.BeginTestCase("DateTimeParseExact2"); if (test.RunTests()) { TestLibrary.TestFramework.EndTestCase(); TestLibrary.TestFramework.LogInformation("PASS"); return 100; } else { TestLibrary.TestFramework.EndTestCase(); TestLibrary.TestFramework.LogInformation("FAIL"); return 0; } } public bool RunTests() { bool retVal = true; TestLibrary.TestFramework.LogInformation("[Positive]"); retVal = PosTest1() && retVal; retVal = PosTest2() && retVal; TestLibrary.TestFramework.LogInformation("[Negative]"); retVal = NegTest1() && retVal; retVal = NegTest2() && retVal; retVal = NegTest3() && retVal; return retVal; } public bool PosTest1() { bool retVal = true; MyFormater formater = new MyFormater(); string dateBefore = ""; DateTime dateAfter; TestLibrary.TestFramework.BeginScenario("PosTest1: DateTime.ParseExact(DateTime.Now)"); try { for(int i=0; i)"); try { for(int i=-1024; i<1024; i++) { try { // skip the valid values if (0 == (i & (int)DateTimeStyles.AdjustToUniversal) && 0 == (i & (int)DateTimeStyles.AssumeUniversal) && 0 == (i & (int)DateTimeStyles.AllowInnerWhite) && 0 == (i & (int)DateTimeStyles.AllowLeadingWhite) && 0 == (i & (int)DateTimeStyles.AllowTrailingWhite) && 0 == (i & (int)DateTimeStyles.AllowWhiteSpaces) && 0 == (i & (int)DateTimeStyles.NoCurrentDateDefault) && i != (int)DateTimeStyles.None ) { dateBefore = DateTime.Now.ToString(); dateAfter = DateTime.ParseExact( dateBefore, "G", formater, (DateTimeStyles)i); if (!dateBefore.Equals(dateAfter.ToString())) { TestLibrary.TestFramework.LogError("011", "DateTime.ParseExact(" + dateBefore + ", " + (DateTimeStyles)i + ") did not equal " + dateAfter.ToString()); retVal = false; } } } catch (System.ArgumentException) { // } } } catch (Exception e) { TestLibrary.TestFramework.LogError("012", "Failing date: " + dateBefore); TestLibrary.TestFramework.LogError("012", "Unexpected exception: " + e); retVal = false; } return retVal; } public bool NegTest1() { bool retVal = true; MyFormater formater = new MyFormater(); TestLibrary.TestFramework.BeginScenario("NegTest1: DateTime.ParseExact(null)"); try { try { for(int i=0; i)"); try { for (int i=0; i