]> git.saurik.com Git - wxWidgets.git/blobdiff - tests/datetime/datetimetest.cpp
build fix for latest C++ standard
[wxWidgets.git] / tests / datetime / datetimetest.cpp
index 145ba064d2be917a01ce6fd81814b16a8f7ab74d..341d8450bd469e9c64deb04c3fe58c4164780655 100644 (file)
 #ifndef WX_PRECOMP
 #endif // WX_PRECOMP
 
+#if wxUSE_DATETIME
+
 #include "wx/datetime.h"
+#include "wx/ioswrap.h"
+
+// need this to be able to use CPPUNIT_ASSERT_EQUAL with wxDateTime objects
+static wxSTD ostream& operator<<(wxSTD ostream& ostr, const wxDateTime& dt)
+{
+    ostr << dt.Format();
+
+    return ostr;
+}
 
 // to test Today() meaningfully we must be able to change the system date which
 // is not usually the case, but if we're under Win32 we can try it -- define
@@ -177,7 +188,8 @@ private:
         CPPUNIT_TEST( TestTimeDST );
         CPPUNIT_TEST( TestTimeFormat );
         CPPUNIT_TEST( TestTimeTicks );
-        CPPUNIT_TEST( TestTimeParse );
+        CPPUNIT_TEST( TestParceRFC822 );
+        CPPUNIT_TEST( TestDateParse );
         CPPUNIT_TEST( TestTimeArithmetics );
         CPPUNIT_TEST( TestDSTBug );
     CPPUNIT_TEST_SUITE_END();
@@ -190,7 +202,8 @@ private:
     void TestTimeDST();
     void TestTimeFormat();
     void TestTimeTicks();
-    void TestTimeParse();
+    void TestParceRFC822();
+    void TestDateParse();
     void TestTimeArithmetics();
     void TestDSTBug();
 
@@ -678,12 +691,12 @@ void DateTimeTestCase::TestTimeTicks()
     }
 }
 
-// test text -> wxDateTime conversion
-void DateTimeTestCase::TestTimeParse()
+// test parsing dates in RFC822 format
+void DateTimeTestCase::TestParceRFC822()
 {
     static const struct ParseTestData
     {
-        const wxChar *format;
+        const wxChar *rfc822;
         Date date;              // NB: this should be in UTC
         bool good;
     } parseTestDates[] =
@@ -707,15 +720,56 @@ void DateTimeTestCase::TestTimeParse()
 
     for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
     {
-        const wxChar *format = parseTestDates[n].format;
-
         wxDateTime dt;
-        if ( dt.ParseRfc822Date(format) )
+        if ( dt.ParseRfc822Date(parseTestDates[n].rfc822) )
         {
             CPPUNIT_ASSERT( parseTestDates[n].good );
 
             wxDateTime dtReal = parseTestDates[n].date.DT().FromUTC();
-            CPPUNIT_ASSERT( dt == dtReal );
+            CPPUNIT_ASSERT_EQUAL( dtReal, dt );
+        }
+        else // failed to parse
+        {
+            CPPUNIT_ASSERT( !parseTestDates[n].good );
+        }
+    }
+}
+
+// test parsing dates in free format
+void DateTimeTestCase::TestDateParse()
+{
+    static const struct ParseTestData
+    {
+        const wxChar *str;
+        Date date;              // NB: this should be in UTC
+        bool good;
+    } parseTestDates[] =
+    {
+        { _T("21 Mar 2006"), { 21, wxDateTime::Mar, 2006 }, true },
+        { _T("29 Feb 1976"), { 29, wxDateTime::Feb, 1976 }, true },
+        { _T("Feb 29 1976"), { 29, wxDateTime::Feb, 1976 }, true },
+        { _T("31/03/06"),    { 31, wxDateTime::Mar,    6 }, true },
+        { _T("31/03/2006"),  { 31, wxDateTime::Mar, 2006 }, true },
+
+        // some invalid ones too
+        { _T("29 Feb 2006") },
+        { _T("31/04/06") },
+        { _T("bloordyblop") }
+    };
+
+    // special cases
+    wxDateTime dt;
+    CPPUNIT_ASSERT( dt.ParseDate(_T("today")) );
+    CPPUNIT_ASSERT_EQUAL( wxDateTime::Today(), dt );
+
+    for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
+    {
+        wxDateTime dt;
+        if ( dt.ParseDate(parseTestDates[n].str) )
+        {
+            CPPUNIT_ASSERT( parseTestDates[n].good );
+
+            CPPUNIT_ASSERT_EQUAL( parseTestDates[n].date.DT(), dt );
         }
         else // failed to parse
         {
@@ -823,3 +877,4 @@ void DateTimeTestCase::TestDSTBug()
 #endif // CHANGE_SYSTEM_DATE
 }
 
+#endif // wxUSE_DATETIME