Add some DateMath tests to TestWebKitAPI
authorbfulgham@apple.com <bfulgham@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 11 Mar 2015 17:49:04 +0000 (17:49 +0000)
committerbfulgham@apple.com <bfulgham@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 11 Mar 2015 17:49:04 +0000 (17:49 +0000)
https://bugs.webkit.org/show_bug.cgi?id=142565

Reviewed by Alexey Proskuryakov.

* TestWebKitAPI/CMakeLists.txt: Add new DateMath.cpp file.
* TestWebKitAPI/TestWebKitAPI.vcxproj/TestWebKitAPI.vcxproj: Ditto.
* TestWebKitAPI/TestWebKitAPI.vcxproj/TestWebKitAPI.vcxproj.filters: Ditto.
* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj: Ditto."
* TestWebKitAPI/Tests/WTF/DateMath.cpp: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@181393 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Tools/ChangeLog
Tools/TestWebKitAPI/CMakeLists.txt
Tools/TestWebKitAPI/TestWebKitAPI.vcxproj/TestWebKitAPI.vcxproj
Tools/TestWebKitAPI/TestWebKitAPI.vcxproj/TestWebKitAPI.vcxproj.filters
Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Tools/TestWebKitAPI/Tests/WTF/DateMath.cpp [new file with mode: 0644]

index 85bffd2..102b847 100644 (file)
@@ -1,3 +1,16 @@
+2015-03-11  Brent Fulgham  <bfulgham@apple.com>
+
+        Add some DateMath tests to TestWebKitAPI
+        https://bugs.webkit.org/show_bug.cgi?id=142565
+
+        Reviewed by Alexey Proskuryakov.
+
+        * TestWebKitAPI/CMakeLists.txt: Add new DateMath.cpp file.
+        * TestWebKitAPI/TestWebKitAPI.vcxproj/TestWebKitAPI.vcxproj: Ditto.
+        * TestWebKitAPI/TestWebKitAPI.vcxproj/TestWebKitAPI.vcxproj.filters: Ditto.
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj: Ditto."
+        * TestWebKitAPI/Tests/WTF/DateMath.cpp: Added.
+
 2015-03-10  Brent Fulgham  <bfulgham@apple.com>
 
         Regression(r181257): WTF_WorkQueue.Simple is flaky
index c2859bc..e4b52e9 100644 (file)
@@ -77,6 +77,7 @@ set(TestWTF_SOURCES
     ${TESTWEBKITAPI_DIR}/Tests/WTF/AtomicString.cpp
     ${TESTWEBKITAPI_DIR}/Tests/WTF/CString.cpp
     ${TESTWEBKITAPI_DIR}/Tests/WTF/CheckedArithmeticOperations.cpp
+    ${TESTWEBKITAPI_DIR}/Tests/WTF/DateMath.cpp
     ${TESTWEBKITAPI_DIR}/Tests/WTF/Deque.cpp
     ${TESTWEBKITAPI_DIR}/Tests/WTF/Functional.cpp
     ${TESTWEBKITAPI_DIR}/Tests/WTF/HashMap.cpp
index cfa48b4..ff430a5 100644 (file)
     <ClCompile Include="..\Tests\WTF\cf\RetainPtrHashing.cpp" />
     <ClCompile Include="..\Tests\WTF\CheckedArithmeticOperations.cpp" />
     <ClCompile Include="..\Tests\WTF\CString.cpp" />
+    <ClCompile Include="..\Tests\WTF\DateMath.cpp" />
     <ClCompile Include="..\Tests\WTF\Deque.cpp" />
     <ClCompile Include="..\Tests\WTF\Functional.cpp" />
     <ClCompile Include="..\Tests\WTF\HashMap.cpp" />
index fb96610..bae5733 100644 (file)
     <ClCompile Include="..\Tests\WTF\WorkQueue.cpp">
       <Filter>Tests\WTF</Filter>
     </ClCompile>
+    <ClCompile Include="..\Tests\WTF\DateMath.cpp">
+      <Filter>Tests\WTF</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <None Include="TestWebKitAPIPostBuild.cmd" />
index 7801983..3bcf2dd 100644 (file)
@@ -56,6 +56,7 @@
                7673499D1930C5BB00E44DF9 /* StopLoadingDuringDidFailProvisionalLoad_bundle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7673499A1930182E00E44DF9 /* StopLoadingDuringDidFailProvisionalLoad_bundle.cpp */; };
                76E182DD1547569100F1FADD /* WillSendSubmitEvent_Bundle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 76E182DC1547569100F1FADD /* WillSendSubmitEvent_Bundle.cpp */; };
                76E182DF154767E600F1FADD /* auto-submitting-form.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 76E182DE15475A8300F1FADD /* auto-submitting-form.html */; };
+               7AA021BB1AB09EA70052953F /* DateMath.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7AA021BA1AB09EA70052953F /* DateMath.cpp */; };
                7AA6A1521AAC0B31002B2ED3 /* WorkQueue.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7AA6A1511AAC0B31002B2ED3 /* WorkQueue.cpp */; };
                7C486BA11AA12567003F6F9B /* bundle-file.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 7C486BA01AA1254B003F6F9B /* bundle-file.html */; };
                7C54A4BE1AA11CCA00380F78 /* WKBundleFileHandle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7C54A4BC1AA11CCA00380F78 /* WKBundleFileHandle.cpp */; };
                76E182D91547550100F1FADD /* WillSendSubmitEvent.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WillSendSubmitEvent.cpp; sourceTree = "<group>"; };
                76E182DC1547569100F1FADD /* WillSendSubmitEvent_Bundle.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WillSendSubmitEvent_Bundle.cpp; sourceTree = "<group>"; };
                76E182DE15475A8300F1FADD /* auto-submitting-form.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "auto-submitting-form.html"; sourceTree = "<group>"; };
+               7AA021BA1AB09EA70052953F /* DateMath.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DateMath.cpp; sourceTree = "<group>"; };
                7AA6A1511AAC0B31002B2ED3 /* WorkQueue.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WorkQueue.cpp; sourceTree = "<group>"; };
                7C486BA01AA1254B003F6F9B /* bundle-file.html */ = {isa = PBXFileReference; lastKnownFileType = text.html; path = "bundle-file.html"; sourceTree = "<group>"; };
                7C54A4BC1AA11CCA00380F78 /* WKBundleFileHandle.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WKBundleFileHandle.cpp; sourceTree = "<group>"; };
                                26F1B44215CA434F00D1E4BF /* AtomicString.cpp */,
                                A7A966DA140ECCC8005EF9B4 /* CheckedArithmeticOperations.cpp */,
                                26A2C72E15E2E73C005B1A14 /* CString.cpp */,
+                               7AA021BA1AB09EA70052953F /* DateMath.cpp */,
                                E4A757D3178AEA5B00B5D7A4 /* Deque.cpp */,
                                1AA9E55714980A9900001A8A /* Functional.cpp */,
                                0BCD833414857CE400EA2003 /* HashMap.cpp */,
                        isa = PBXSourcesBuildPhase;
                        buildActionMask = 2147483647;
                        files = (
+                               7AA021BB1AB09EA70052953F /* DateMath.cpp in Sources */,
                                1CB9BC381A67482300FE5678 /* WeakPtr.cpp in Sources */,
                                2E7765CD16C4D80A00BA2BB1 /* mainIOS.mm in Sources */,
                                7AA6A1521AAC0B31002B2ED3 /* WorkQueue.cpp in Sources */,
diff --git a/Tools/TestWebKitAPI/Tests/WTF/DateMath.cpp b/Tools/TestWebKitAPI/Tests/WTF/DateMath.cpp
new file mode 100644 (file)
index 0000000..463041d
--- /dev/null
@@ -0,0 +1,208 @@
+/*
+ * Copyright (C) 2015 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+
+#include "Test.h"
+#include <wtf/DateMath.h>
+
+namespace TestWebKitAPI {
+
+// Note: The results of these function look weird if you do not understand the following mappings:
+// dayOfWeek: [0, 6] 0 being Monday, day: [1, 31], month: [0, 11], year: ex: 2011,
+// hours: [0, 23], minutes: [0, 59], seconds: [0, 59], utcOffset: [-720,720].
+
+TEST(WTF_DateMath, dateToDaysFrom1970)
+{
+    EXPECT_EQ(0.0, dateToDaysFrom1970(1970, 0, 1));
+    EXPECT_EQ(157.0, dateToDaysFrom1970(1970, 5, 7));
+    EXPECT_EQ(-145.0, dateToDaysFrom1970(1969, 7, 9));
+    EXPECT_EQ(16322, dateToDaysFrom1970(2014, 8, 9));
+}
+
+
+TEST(WTF_DateMath, isLeapYear)
+{
+    EXPECT_TRUE(isLeapYear(1804));
+    EXPECT_FALSE(isLeapYear(1900));
+    EXPECT_TRUE(isLeapYear(1968));
+    EXPECT_TRUE(isLeapYear(1976));
+    EXPECT_TRUE(isLeapYear(2000));
+    EXPECT_FALSE(isLeapYear(2010));
+    EXPECT_TRUE(isLeapYear(2012));
+    EXPECT_FALSE(isLeapYear(2100));
+}
+
+TEST(WTF_DateMath, msToYear)
+{
+    EXPECT_EQ(1962, msToYear(-220953600000));
+    EXPECT_EQ(1970, msToYear(0));
+    EXPECT_EQ(1970, msToYear(100));
+    EXPECT_EQ(1977, msToYear(220953600000));
+    EXPECT_EQ(2013, msToYear(1365318000000));
+}
+
+TEST(WTF_DateMath, msToDays)
+{
+    EXPECT_EQ(0, msToDays(0));
+    EXPECT_EQ(2557, msToDays(220953600000));
+    EXPECT_EQ(255, msToDays(22095360000));
+    EXPECT_EQ(25, msToDays(2209536000));
+    EXPECT_EQ(2, msToDays(220953600));
+    EXPECT_EQ(0, msToDays(22095360));
+    EXPECT_EQ(0, msToDays(2209536));
+}
+
+TEST(WTF_DateMath, msToMinutes)
+{
+    EXPECT_EQ(0, msToMinutes(0));
+    EXPECT_EQ(0, msToMinutes(220953600000));
+    EXPECT_EQ(36, msToMinutes(22095360000));
+    EXPECT_EQ(36, msToMinutes(22095360000));
+    EXPECT_EQ(45, msToMinutes(2209536000));
+    EXPECT_EQ(22, msToMinutes(220953600));
+    EXPECT_EQ(8, msToMinutes(22095360));
+    EXPECT_EQ(36, msToMinutes(2209536));
+}
+
+TEST(WTF_DateMath, msToHours)
+{
+    EXPECT_EQ(0, msToHours(0));
+    EXPECT_EQ(8, msToHours(220953600000));
+    EXPECT_EQ(17, msToHours(22095360000));
+    EXPECT_EQ(13, msToHours(2209536000));
+    EXPECT_EQ(13, msToHours(220953600));
+    EXPECT_EQ(6, msToHours(22095360));
+    EXPECT_EQ(0, msToHours(2209536));
+}
+
+TEST(WTF_DateMath, dayInYear)
+{
+    EXPECT_EQ(59, dayInYear(2015, 2, 1));
+    EXPECT_EQ(60, dayInYear(2012, 2, 1));
+    EXPECT_EQ(0, dayInYear(2015, 0, 1));
+    EXPECT_EQ(31, dayInYear(2015, 1, 1));
+}
+
+TEST(WTF_DateMath, monthFromDayInYear)
+{
+    EXPECT_EQ(2, monthFromDayInYear(59, false));
+    EXPECT_EQ(1, monthFromDayInYear(59, true));
+    EXPECT_EQ(2, monthFromDayInYear(60, true));
+    EXPECT_EQ(0, monthFromDayInYear(0, false));
+    EXPECT_EQ(0, monthFromDayInYear(0, true));
+    EXPECT_EQ(1, monthFromDayInYear(31, true));
+    EXPECT_EQ(1, monthFromDayInYear(31, false));
+}
+
+TEST(WTF_DateMath, dayInMonthFromDayInYear)
+{
+    EXPECT_EQ(1, dayInMonthFromDayInYear(0, false));
+    EXPECT_EQ(1, dayInMonthFromDayInYear(0, true));
+    EXPECT_EQ(1, dayInMonthFromDayInYear(59, false));
+    EXPECT_EQ(29, dayInMonthFromDayInYear(59, true));
+    EXPECT_EQ(1, dayInMonthFromDayInYear(60, true));
+    EXPECT_EQ(1, dayInMonthFromDayInYear(0, false));
+    EXPECT_EQ(1, dayInMonthFromDayInYear(0, true));
+    EXPECT_EQ(31, dayInMonthFromDayInYear(30, true));
+    EXPECT_EQ(31, dayInMonthFromDayInYear(30, false));
+    EXPECT_EQ(31, dayInMonthFromDayInYear(365, true));
+    EXPECT_EQ(32, dayInMonthFromDayInYear(365, false));
+    EXPECT_EQ(32, dayInMonthFromDayInYear(366, true));
+}
+
+TEST(WTF_DateMath, calculateLocalTimeOffset)
+{
+    // DST Start: April 30, 1967 (02:00 am)
+    LocalTimeOffset dstStart1967 = calculateLocalTimeOffset(-84301200000, WTF::LocalTime);
+    EXPECT_TRUE(dstStart1967.isDST);
+    EXPECT_EQ(-25200000, dstStart1967.offset);
+
+    // November 1, 1967 (02:00 am)
+    LocalTimeOffset dstAlmostEnd1967 = calculateLocalTimeOffset(-68317200000, WTF::LocalTime);
+    EXPECT_TRUE(dstAlmostEnd1967.isDST);
+    EXPECT_EQ(-25200000, dstAlmostEnd1967.offset);
+
+    // DST End: November 11, 1967 (02:00 am)
+    LocalTimeOffset dstEnd1967 = calculateLocalTimeOffset(-67536000000, WTF::LocalTime);
+    EXPECT_FALSE(dstEnd1967.isDST);
+    EXPECT_EQ(-25200000, dstStart1967.offset);
+
+    // DST Start: April 3, 1988 (02:00 am)
+    LocalTimeOffset dstStart1988 = calculateLocalTimeOffset(576054000000, WTF::LocalTime);
+    EXPECT_TRUE(dstStart1988.isDST);
+    EXPECT_EQ(-25200000, dstStart1988.offset);
+
+    // DST End: November 4, 2012 (02:00 am)
+    LocalTimeOffset dstEnd2012 = calculateLocalTimeOffset(1352012400000, WTF::LocalTime);
+    EXPECT_FALSE(dstEnd2012.isDST);
+    EXPECT_EQ(-28800000, dstEnd2012.offset);
+
+    // DST Begin: March 8, 2015
+    LocalTimeOffset dstBegin2015 = calculateLocalTimeOffset(1425801600000, WTF::LocalTime);
+    EXPECT_TRUE(dstBegin2015.isDST);
+    EXPECT_EQ(-25200000, dstBegin2015.offset);
+
+    LocalTimeOffset dstBegin2015UTC = calculateLocalTimeOffset(1425801600000, WTF::UTCTime);
+    EXPECT_FALSE(dstBegin2015UTC.isDST);
+    EXPECT_EQ(-28800000, dstBegin2015UTC.offset);
+
+    // DST End: November 1, 2015
+    LocalTimeOffset dstEnd2015 = calculateLocalTimeOffset(1446361200000, WTF::LocalTime);
+    EXPECT_FALSE(dstEnd2015.isDST);
+    EXPECT_EQ(-28800000, dstEnd2015.offset);
+
+    // DST Begin: March 13, 2016
+    LocalTimeOffset dstBegin2016 = calculateLocalTimeOffset(1458111600000, WTF::LocalTime);
+    EXPECT_TRUE(dstBegin2016.isDST);
+    EXPECT_EQ(-25200000, dstBegin2016.offset);
+
+    // DST End: November 6, 2016
+    LocalTimeOffset dstEnd2016 = calculateLocalTimeOffset(1478415600000, WTF::LocalTime);
+    EXPECT_FALSE(dstEnd2016.isDST);
+    EXPECT_EQ(-28800000, dstEnd2016.offset);
+
+    // DST Begin: March 12, 2017
+    LocalTimeOffset dstBegin2017 = calculateLocalTimeOffset(1489305600000, WTF::LocalTime);
+    EXPECT_TRUE(dstBegin2017.isDST);
+    EXPECT_EQ(-25200000, dstBegin2017.offset);
+
+    // DST End: November 5, 2017
+    LocalTimeOffset dstEnd2017 = calculateLocalTimeOffset(1509865200000, WTF::LocalTime);
+    EXPECT_FALSE(dstEnd2017.isDST);
+    EXPECT_EQ(-28800000, dstEnd2017.offset);
+
+    // DST Begin: March 11, 2018
+    LocalTimeOffset dstBegin2018 = calculateLocalTimeOffset(1520755200000, WTF::LocalTime);
+    EXPECT_TRUE(dstBegin2018.isDST);
+    EXPECT_EQ(-25200000, dstBegin2018.offset);
+
+    // DST End: November 4, 2018
+    LocalTimeOffset dstEnd2018 = calculateLocalTimeOffset(1541314800000, WTF::LocalTime);
+    EXPECT_FALSE(dstEnd2018.isDST);
+    EXPECT_EQ(-28800000, dstEnd2018.offset);
+}
+
+} // namespace TestWebKitAPI