tests/auto/qdate/tst_qdate.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 #include <QtTest/QtTest>
       
    44 #include <qdatetime.h>
       
    45 #include <qlocale.h>
       
    46 
       
    47 //TESTED_CLASS=
       
    48 //TESTED_FILES=corelib/tools/qdatetime.h corelib/tools/qdatetime.cpp
       
    49 
       
    50 class tst_QDate : public QObject
       
    51 {
       
    52 Q_OBJECT
       
    53 
       
    54 public:
       
    55     tst_QDate();
       
    56     virtual ~tst_QDate();
       
    57 
       
    58 public slots:
       
    59     void init();
       
    60     void cleanup();
       
    61 private slots:
       
    62     void toString();
       
    63     void isValid_data();
       
    64     void isValid();
       
    65     void julianDay_data();
       
    66     void julianDay();
       
    67     void weekNumber_invalid_data();
       
    68     void weekNumber_invalid();
       
    69     void weekNumber_data();
       
    70     void weekNumber();
       
    71     void addDays_data();
       
    72     void addDays();
       
    73     void addMonths_data();
       
    74     void addMonths();
       
    75     void addYears_data();
       
    76     void addYears();
       
    77     void operator_eq_eq();
       
    78     void operator_not_eq();
       
    79     void operator_lt();
       
    80     void operator_gt();
       
    81     void operator_lt_eq();
       
    82     void operator_gt_eq();
       
    83     void fromString_data();
       
    84     void fromString();
       
    85     void fromString_format_data();
       
    86     void fromString_format();
       
    87     void toString_format_data();
       
    88     void toString_format();
       
    89     void isLeapYear();
       
    90     void yearsZeroToNinetyNine();
       
    91     void negativeYear() const;
       
    92     void printNegativeYear() const;
       
    93     void roundtripGermanLocale() const;
       
    94     void shortDayName() const;
       
    95     void standaloneShortDayName() const;
       
    96     void longDayName() const;
       
    97     void standaloneLongDayName() const;
       
    98     void shortMonthName() const;
       
    99     void standaloneShortMonthName() const;
       
   100     void longMonthName() const;
       
   101     void standaloneLongMonthName() const;
       
   102 };
       
   103 
       
   104 Q_DECLARE_METATYPE(QDate)
       
   105 
       
   106 tst_QDate::tst_QDate()
       
   107 {
       
   108 }
       
   109 
       
   110 tst_QDate::~tst_QDate()
       
   111 {
       
   112 
       
   113 }
       
   114 
       
   115 void tst_QDate::init()
       
   116 {
       
   117 // This will be executed immediately before each test is run.
       
   118 // TODO: Add initialization code here.
       
   119 }
       
   120 
       
   121 void tst_QDate::cleanup()
       
   122 {
       
   123 // This will be executed immediately after each test is run.
       
   124 // TODO: Add cleanup code here.
       
   125 }
       
   126 
       
   127 void tst_QDate::isValid_data()
       
   128 {
       
   129     QTest::addColumn<int>("year");
       
   130     QTest::addColumn<int>("month");
       
   131     QTest::addColumn<int>("day");
       
   132     QTest::addColumn<uint>("jd");
       
   133     QTest::addColumn<bool>("valid");
       
   134 
       
   135     QTest::newRow("0-0-0") << 0 << 0 << 0 << 0U << false;
       
   136     QTest::newRow("month 0") << 2000 << 0 << 1 << 0U << false;
       
   137     QTest::newRow("day 0") << 2000 << 1 << 0 << 0U << false;
       
   138 
       
   139     QTest::newRow("month 13") << 2000 << 13 << 1 << 0U << false;
       
   140 
       
   141     // test leap years
       
   142     QTest::newRow("non-leap") << 2006 << 2 << 29 << 0U << false;
       
   143     QTest::newRow("normal leap") << 2004 << 2 << 29 << 2453065U << true;
       
   144     QTest::newRow("century leap") << 1900 << 2 << 29 << 0U << false;
       
   145     QTest::newRow("century leap") << 2100 << 2 << 29 << 0U << false;
       
   146     QTest::newRow("400-years leap") << 2000 << 2 << 29 << 2451604U << true;
       
   147     QTest::newRow("400-years leap 2") << 2400 << 2 << 29 << 2597701U << true;
       
   148     QTest::newRow("400-years leap 3") << 1600 << 2 << 29 << 2305507U << true;
       
   149     QTest::newRow("year 0") << 0 << 2 << 27 << 0U << false;
       
   150 
       
   151     // test the number of days in months:
       
   152     QTest::newRow("jan") << 2000 << 1 << 31 << 2451575U << true;
       
   153     QTest::newRow("feb") << 2000 << 2 << 29 << 2451604U << true; // same data as 400-years leap
       
   154     QTest::newRow("mar") << 2000 << 3 << 31 << 2451635U << true;
       
   155     QTest::newRow("apr") << 2000 << 4 << 30 << 2451665U << true;
       
   156     QTest::newRow("may") << 2000 << 5 << 31 << 2451696U << true;
       
   157     QTest::newRow("jun") << 2000 << 6 << 30 << 2451726U << true;
       
   158     QTest::newRow("jul") << 2000 << 7 << 31 << 2451757U << true;
       
   159     QTest::newRow("aug") << 2000 << 8 << 31 << 2451788U << true;
       
   160     QTest::newRow("sep") << 2000 << 9 << 30 << 2451818U << true;
       
   161     QTest::newRow("oct") << 2000 << 10 << 31 << 2451849U << true;
       
   162     QTest::newRow("nov") << 2000 << 11 << 30 << 2451879U << true;
       
   163     QTest::newRow("dec") << 2000 << 12 << 31 << 2451910U << true;
       
   164 
       
   165     // and invalid dates:
       
   166     QTest::newRow("ijan") << 2000 << 1 << 32 << 0U << false;
       
   167     QTest::newRow("ifeb") << 2000 << 2 << 30 << 0U << false;
       
   168     QTest::newRow("imar") << 2000 << 3 << 32 << 0U << false;
       
   169     QTest::newRow("iapr") << 2000 << 4 << 31 << 0U << false;
       
   170     QTest::newRow("imay") << 2000 << 5 << 32 << 0U << false;
       
   171     QTest::newRow("ijun") << 2000 << 6 << 31 << 0U << false;
       
   172     QTest::newRow("ijul") << 2000 << 7 << 32 << 0U << false;
       
   173     QTest::newRow("iaug") << 2000 << 8 << 32 << 0U << false;
       
   174     QTest::newRow("isep") << 2000 << 9 << 31 << 0U << false;
       
   175     QTest::newRow("ioct") << 2000 << 10 << 32 << 0U << false;
       
   176     QTest::newRow("inov") << 2000 << 11 << 31 << 0U << false;
       
   177     QTest::newRow("idec") << 2000 << 12 << 32 << 0U << false;
       
   178 
       
   179     // the beginning of the Julian Day calendar:
       
   180     QTest::newRow("jd negative1") << -4714 << 1 << 1 << 0U << false;
       
   181     QTest::newRow("jd negative2") << -4713 << 1 << 1 << 0U << false;
       
   182     QTest::newRow("jd negative3") << -4713 << 1 << 2 << 1U << true;
       
   183     QTest::newRow("jd negative4") << -4713 << 1 << 3 << 2U << true;
       
   184     QTest::newRow("jd 0") << -4713 << 1 << 1 << 0U << false;
       
   185     QTest::newRow("jd 1") << -4713 << 1 << 2 << 1U << true;
       
   186     QTest::newRow("imminent overflow") << 11754508 << 12 << 13 << 4294967295U << true;
       
   187 }
       
   188 
       
   189 void tst_QDate::isValid()
       
   190 {
       
   191     QFETCH(int, year);
       
   192     QFETCH(int, month);
       
   193     QFETCH(int, day);
       
   194 
       
   195     QTEST(QDate::isValid(year, month, day), "valid");
       
   196 
       
   197     QDate d;
       
   198     d.setDate(year, month, day);
       
   199     QTEST(d.isValid(), "valid");
       
   200 }
       
   201 
       
   202 void tst_QDate::julianDay_data()
       
   203 {
       
   204     isValid_data();
       
   205 }
       
   206 
       
   207 void tst_QDate::julianDay()
       
   208 {
       
   209     QFETCH(int, year);
       
   210     QFETCH(int, month);
       
   211     QFETCH(int, day);
       
   212     QFETCH(uint, jd);
       
   213 
       
   214     {
       
   215         QDate d;
       
   216         d.setDate(year, month, day);
       
   217         QCOMPARE(uint(d.toJulianDay()), jd);
       
   218     }
       
   219 
       
   220     if (jd) {
       
   221         QDate d = QDate::fromJulianDay(jd);
       
   222         QCOMPARE(d.year(), year);
       
   223         QCOMPARE(d.month(), month);
       
   224         QCOMPARE(d.day(), day);
       
   225     }
       
   226 }
       
   227 
       
   228 void tst_QDate::weekNumber_data()
       
   229 {
       
   230     QTest::addColumn<int>("expectedWeekNum");
       
   231     QTest::addColumn<int>("expectedYearNum");
       
   232     QTest::addColumn<int>("year");
       
   233     QTest::addColumn<int>("month");
       
   234     QTest::addColumn<int>("day");
       
   235 
       
   236     //next we fill it with data
       
   237     QTest::newRow( "data0" )  << 10 << 2002 << 2002 << 3 << 8;
       
   238     QTest::newRow( "data1" )  << 10 << 2002 << 2002 << 3 << 8;
       
   239     QTest::newRow( "data2" )  << 52 << 1999 << 2000 << 1 << 1;
       
   240     QTest::newRow( "data3" )  << 52 << 1999 << 1999 << 12 << 31;
       
   241     QTest::newRow( "data4" )  << 1 << 2001 << 2001 << 1 << 1;
       
   242     QTest::newRow( "data5" )  << 53 << 1998 << 1998 << 12 << 31;
       
   243     QTest::newRow( "data6" )  << 1 << 1985 << 1984 << 12 << 31;
       
   244     QTest::newRow( "data7" )  << 52 << 2006 << 2006 << 12 << 31;
       
   245 }
       
   246 
       
   247 void tst_QDate::weekNumber()
       
   248 {
       
   249     int yearNumber;
       
   250     QFETCH( int, year );
       
   251     QFETCH( int, month );
       
   252     QFETCH( int, day );
       
   253     QFETCH( int, expectedWeekNum );
       
   254     QFETCH( int, expectedYearNum );
       
   255     QDate dt1( year, month, day );
       
   256     QCOMPARE( dt1.weekNumber( &yearNumber ), expectedWeekNum );
       
   257     QCOMPARE( yearNumber, expectedYearNum );
       
   258 }
       
   259 
       
   260 void tst_QDate::weekNumber_invalid_data()
       
   261 {
       
   262     QTest::addColumn<int>("year");
       
   263     QTest::addColumn<int>("month");
       
   264     QTest::addColumn<int>("day");
       
   265 
       
   266     //next we fill it with data
       
   267     QTest::newRow( "data0" )  << 0 << 0 << 0;
       
   268     QTest::newRow( "data1" )  << 2001 << 1 << 32;
       
   269     QTest::newRow( "data2" )  << 1999 << 2 << 29;
       
   270 }
       
   271 
       
   272 void tst_QDate::weekNumber_invalid()
       
   273 {
       
   274     QDate dt;
       
   275     int yearNumber;
       
   276     QCOMPARE( dt.weekNumber( &yearNumber ), 0 );
       
   277 }
       
   278 
       
   279 void tst_QDate::addDays()
       
   280 {
       
   281     QFETCH( int, year );
       
   282     QFETCH( int, month );
       
   283     QFETCH( int, day );
       
   284     QFETCH( int, amountToAdd );
       
   285     QFETCH( int, expectedYear );
       
   286     QFETCH( int, expectedMonth );
       
   287     QFETCH( int, expectedDay );
       
   288 
       
   289     QDate dt( year, month, day );
       
   290     dt = dt.addDays( amountToAdd );
       
   291 
       
   292     QCOMPARE( dt.year(), expectedYear );
       
   293     QCOMPARE( dt.month(), expectedMonth );
       
   294     QCOMPARE( dt.day(), expectedDay );
       
   295 }
       
   296 
       
   297 void tst_QDate::addDays_data()
       
   298 {
       
   299     QTest::addColumn<int>("year");
       
   300     QTest::addColumn<int>("month");
       
   301     QTest::addColumn<int>("day");
       
   302     QTest::addColumn<int>("amountToAdd");
       
   303     QTest::addColumn<int>("expectedYear");
       
   304     QTest::addColumn<int>("expectedMonth");
       
   305     QTest::addColumn<int>("expectedDay");
       
   306 
       
   307     QTest::newRow( "data0" ) << 2000 << 1 << 1 << 1 << 2000 << 1 << 2;
       
   308     QTest::newRow( "data1" ) << 2000 << 1 << 31 << 1 << 2000 << 2 << 1;
       
   309     QTest::newRow( "data2" ) << 2000 << 2 << 28 << 1 << 2000 << 2 << 29;
       
   310     QTest::newRow( "data3" ) << 2000 << 2 << 29 << 1 << 2000 << 3 << 1;
       
   311     QTest::newRow( "data4" ) << 2000 << 12 << 31 << 1 << 2001 << 1 << 1;
       
   312     QTest::newRow( "data5" ) << 2001 << 2 << 28 << 1 << 2001 << 3 << 1;
       
   313     QTest::newRow( "data6" ) << 2001 << 2 << 28 << 30 << 2001 << 3 << 30;
       
   314     QTest::newRow( "data7" ) << 2001 << 3 << 30 << 5 << 2001 << 4 << 4;
       
   315 
       
   316     QTest::newRow( "data8" ) << 2000 << 1 << 1 << -1 << 1999 << 12 << 31;
       
   317     QTest::newRow( "data9" ) << 2000 << 1 << 31 << -1 << 2000 << 1 << 30;
       
   318     QTest::newRow( "data10" ) << 2000 << 2 << 28 << -1 << 2000 << 2 << 27;
       
   319     QTest::newRow( "data11" ) << 2001 << 2 << 28 << -30 << 2001 << 1 << 29;
       
   320 
       
   321     QDate invalid;
       
   322     QTest::newRow( "data12" ) << -4713 << 1 << 2 << -2
       
   323         << invalid.year() << invalid.month() << invalid.day();
       
   324 }
       
   325 
       
   326 void tst_QDate::addMonths()
       
   327 {
       
   328     QFETCH( int, year );
       
   329     QFETCH( int, month );
       
   330     QFETCH( int, day );
       
   331     QFETCH( int, amountToAdd );
       
   332     QFETCH( int, expectedYear );
       
   333     QFETCH( int, expectedMonth );
       
   334     QFETCH( int, expectedDay );
       
   335 
       
   336     QDate dt( year, month, day );
       
   337     dt = dt.addMonths( amountToAdd );
       
   338 
       
   339     QCOMPARE( dt.year(), expectedYear );
       
   340     QCOMPARE( dt.month(), expectedMonth );
       
   341     QCOMPARE( dt.day(), expectedDay );
       
   342 }
       
   343 
       
   344 void tst_QDate::addMonths_data()
       
   345 {
       
   346     QTest::addColumn<int>("year");
       
   347     QTest::addColumn<int>("month");
       
   348     QTest::addColumn<int>("day");
       
   349     QTest::addColumn<int>("amountToAdd");
       
   350     QTest::addColumn<int>("expectedYear");
       
   351     QTest::addColumn<int>("expectedMonth");
       
   352     QTest::addColumn<int>("expectedDay");
       
   353 
       
   354     QTest::newRow( "data0" ) << 2000 << 1 << 1 << 1 << 2000 << 2 << 1;
       
   355     QTest::newRow( "data1" ) << 2000 << 1 << 31 << 1 << 2000 << 2 << 29;
       
   356     QTest::newRow( "data2" ) << 2000 << 2 << 28 << 1 << 2000 << 3 << 28;
       
   357     QTest::newRow( "data3" ) << 2000 << 2 << 29 << 1 << 2000 << 3 << 29;
       
   358     QTest::newRow( "data4" ) << 2000 << 12 << 31 << 1 << 2001 << 1 << 31;
       
   359     QTest::newRow( "data5" ) << 2001 << 2 << 28 << 1 << 2001 << 3 << 28;
       
   360     QTest::newRow( "data6" ) << 2001 << 2 << 28 << 12 << 2002 << 2 << 28;
       
   361     QTest::newRow( "data7" ) << 2000 << 2 << 29 << 12 << 2001 << 2 << 28;
       
   362     QTest::newRow( "data8" ) << 2000 << 10 << 15 << 4 << 2001 << 2 << 15;
       
   363 
       
   364     QTest::newRow( "data9" ) << 2000 << 1 << 1 << -1 << 1999 << 12 << 1;
       
   365     QTest::newRow( "data10" ) << 2000 << 1 << 31 << -1 << 1999 << 12 << 31;
       
   366     QTest::newRow( "data11" ) << 2000 << 12 << 31 << -1 << 2000 << 11 << 30;
       
   367     QTest::newRow( "data12" ) << 2001 << 2 << 28 << -12 << 2000 << 2 << 28;
       
   368     QTest::newRow( "data13" ) << 2000 << 1 << 31 << -7 << 1999 << 6 << 30;
       
   369     QTest::newRow( "data14" ) << 2000 << 2 << 29 << -12 << 1999 << 2 << 28;
       
   370 
       
   371     // year sign change:
       
   372     QTest::newRow( "data14" ) << 1 << 1 << 1 << -1 << -1 << 12 << 1;
       
   373     QTest::newRow( "data15" ) << 1 << 1 << 1 << -12 << -1 << 1 << 1;
       
   374     QTest::newRow( "data16" ) << -1 << 12 << 1 << 1 << 1 << 1 << 1;
       
   375     QTest::newRow( "data17" ) << -1 << 1 << 1 << 12 << 1 << 1 << 1;
       
   376 
       
   377     // Gregorian/Julian switchover
       
   378     QTest::newRow( "data18" ) << 1582 << 9 << 4 << 1 << 1582 << 10 << 4;
       
   379     QTest::newRow( "data19" ) << 1582 << 9 << 10 << 1 << 1582 << 10 << 15;
       
   380     QTest::newRow( "data20" ) << 1582 << 9 << 20 << 1 << 1582 << 10 << 20;
       
   381     QTest::newRow( "data21" ) << 1582 << 11 << 4 << -1 << 1582 << 10 << 4;
       
   382     QTest::newRow( "data22" ) << 1582 << 11 << 10 << -1 << 1582 << 10 << 4;
       
   383     QTest::newRow( "data23" ) << 1582 << 11 << 20 << -1 << 1582 << 10 << 20;
       
   384 }
       
   385 
       
   386 void tst_QDate::addYears()
       
   387 {
       
   388     QFETCH( int, year );
       
   389     QFETCH( int, month );
       
   390     QFETCH( int, day );
       
   391     QFETCH( int, amountToAdd );
       
   392     QFETCH( int, expectedYear );
       
   393     QFETCH( int, expectedMonth );
       
   394     QFETCH( int, expectedDay );
       
   395 
       
   396     QDate dt( year, month, day );
       
   397     dt = dt.addYears( amountToAdd );
       
   398 
       
   399     QCOMPARE( dt.year(), expectedYear );
       
   400     QCOMPARE( dt.month(), expectedMonth );
       
   401     QCOMPARE( dt.day(), expectedDay );
       
   402 }
       
   403 
       
   404 void tst_QDate::addYears_data()
       
   405 {
       
   406     QTest::addColumn<int>("year");
       
   407     QTest::addColumn<int>("month");
       
   408     QTest::addColumn<int>("day");
       
   409     QTest::addColumn<int>("amountToAdd");
       
   410     QTest::addColumn<int>("expectedYear");
       
   411     QTest::addColumn<int>("expectedMonth");
       
   412     QTest::addColumn<int>("expectedDay");
       
   413 
       
   414     QTest::newRow( "data0" ) << 2000 << 1 << 1 << 1 << 2001 << 1 << 1;
       
   415     QTest::newRow( "data1" ) << 2000 << 1 << 31 << 1 << 2001 << 1 << 31;
       
   416     QTest::newRow( "data2" ) << 2000 << 2 << 28 << 1 << 2001 << 2 << 28;
       
   417     QTest::newRow( "data3" ) << 2000 << 2 << 29 << 1 << 2001 << 2 << 28;
       
   418     QTest::newRow( "data4" ) << 2000 << 12 << 31 << 1 << 2001 << 12 << 31;
       
   419     QTest::newRow( "data5" ) << 2001 << 2 << 28 << 3 << 2004 << 2 << 28;
       
   420     QTest::newRow( "data6" ) << 2000 << 2 << 29 << 4 << 2004 << 2 << 29;
       
   421 
       
   422     QTest::newRow( "data7" ) << 2000 << 1 << 31 << -1 << 1999 << 1 << 31;
       
   423     QTest::newRow( "data9" ) << 2000 << 2 << 29 << -1 << 1999 << 2 << 28;
       
   424     QTest::newRow( "data10" ) << 2000 << 12 << 31 << -1 << 1999 << 12 << 31;
       
   425     QTest::newRow( "data11" ) << 2001 << 2 << 28 << -3 << 1998 << 2 << 28;
       
   426     QTest::newRow( "data12" ) << 2000 << 2 << 29 << -4 << 1996 << 2 << 29;
       
   427     QTest::newRow( "data13" ) << 2000 << 2 << 29 << -5 << 1995 << 2 << 28;
       
   428 
       
   429     QTest::newRow( "data14" ) << 2000 << 1 << 1 << -1999 << 1 << 1 << 1;
       
   430     QTest::newRow( "data15" ) << 2000 << 1 << 1 << -2000 << -1 << 1 << 1;
       
   431     QTest::newRow( "data16" ) << 2000 << 1 << 1 << -2001 << -2 << 1 << 1;
       
   432     QTest::newRow( "data17" ) << -2000 << 1 << 1 << 1999 << -1 << 1 << 1;
       
   433     QTest::newRow( "data18" ) << -2000 << 1 << 1 << 2000 << 1 << 1 << 1;
       
   434     QTest::newRow( "data19" ) << -2000 << 1 << 1 << 2001 << 2 << 1 << 1;
       
   435 }
       
   436 
       
   437 void tst_QDate::operator_eq_eq()
       
   438 {
       
   439     QDate d1(2000,1,2);
       
   440     QDate d2(2000,1,2);
       
   441     QVERIFY( d1 == d2 );
       
   442 
       
   443     d1 = QDate(2001,12,5);
       
   444     d2 = QDate(2001,12,5);
       
   445     QVERIFY( d1 == d2 );
       
   446 
       
   447     d2 = QDate(2002,12,5);
       
   448     QVERIFY( !(d1 == d2) );
       
   449 }
       
   450 
       
   451 void tst_QDate::operator_not_eq()
       
   452 {
       
   453     QDate d1(2000,1,2);
       
   454     QDate d2(2000,1,2);
       
   455     QVERIFY( !(d1 != d2) );
       
   456 
       
   457     d1 = QDate(2001,12,5);
       
   458     d2 = QDate(2001,12,5);
       
   459     QVERIFY( !(d1 != d2) );
       
   460 
       
   461     d2 = QDate(2002,12,5);
       
   462     QVERIFY( d1 != d2 );
       
   463 }
       
   464 
       
   465 void tst_QDate::operator_lt()
       
   466 {
       
   467     QDate d1(2000,1,2);
       
   468     QDate d2(2000,1,2);
       
   469     QVERIFY( !(d1 < d2) );
       
   470 
       
   471     d1 = QDate(2001,12,4);
       
   472     d2 = QDate(2001,12,5);
       
   473     QVERIFY( d1 < d2 );
       
   474 
       
   475     d1 = QDate(2001,11,5);
       
   476     d2 = QDate(2001,12,5);
       
   477     QVERIFY( d1 < d2 );
       
   478 
       
   479     d1 = QDate(2000,12,5);
       
   480     d2 = QDate(2001,12,5);
       
   481     QVERIFY( d1 < d2 );
       
   482 
       
   483     d1 = QDate(2002,12,5);
       
   484     d2 = QDate(2001,12,5);
       
   485     QVERIFY( !(d1 < d2) );
       
   486 
       
   487     d1 = QDate(2001,12,5);
       
   488     d2 = QDate(2001,11,5);
       
   489     QVERIFY( !(d1 < d2) );
       
   490 
       
   491     d1 = QDate(2001,12,6);
       
   492     d2 = QDate(2001,12,5);
       
   493     QVERIFY( !(d1 < d2) );
       
   494 }
       
   495 
       
   496 void tst_QDate::operator_gt()
       
   497 {
       
   498     QDate d1(2000,1,2);
       
   499     QDate d2(2000,1,2);
       
   500     QVERIFY( !(d1 > d2) );
       
   501 
       
   502     d1 = QDate(2001,12,4);
       
   503     d2 = QDate(2001,12,5);
       
   504     QVERIFY( !(d1 > d2) );
       
   505 
       
   506     d1 = QDate(2001,11,5);
       
   507     d2 = QDate(2001,12,5);
       
   508     QVERIFY( !(d1 > d2) );
       
   509 
       
   510     d1 = QDate(2000,12,5);
       
   511     d2 = QDate(2001,12,5);
       
   512     QVERIFY( !(d1 > d2) );
       
   513 
       
   514     d1 = QDate(2002,12,5);
       
   515     d2 = QDate(2001,12,5);
       
   516     QVERIFY( d1 > d2 );
       
   517 
       
   518     d1 = QDate(2001,12,5);
       
   519     d2 = QDate(2001,11,5);
       
   520     QVERIFY( d1 > d2 );
       
   521 
       
   522     d1 = QDate(2001,12,6);
       
   523     d2 = QDate(2001,12,5);
       
   524     QVERIFY( d1 > d2 );
       
   525 }
       
   526 
       
   527 void tst_QDate::operator_lt_eq()
       
   528 {
       
   529     QDate d1(2000,1,2);
       
   530     QDate d2(2000,1,2);
       
   531     QVERIFY( d1 <= d2 );
       
   532 
       
   533     d1 = QDate(2001,12,4);
       
   534     d2 = QDate(2001,12,5);
       
   535     QVERIFY( d1 <= d2 );
       
   536 
       
   537     d1 = QDate(2001,11,5);
       
   538     d2 = QDate(2001,12,5);
       
   539     QVERIFY( d1 <= d2 );
       
   540 
       
   541     d1 = QDate(2000,12,5);
       
   542     d2 = QDate(2001,12,5);
       
   543     QVERIFY( d1 <= d2 );
       
   544 
       
   545     d1 = QDate(2002,12,5);
       
   546     d2 = QDate(2001,12,5);
       
   547     QVERIFY( !(d1 <= d2) );
       
   548 
       
   549     d1 = QDate(2001,12,5);
       
   550     d2 = QDate(2001,11,5);
       
   551     QVERIFY( !(d1 <= d2) );
       
   552 
       
   553     d1 = QDate(2001,12,6);
       
   554     d2 = QDate(2001,12,5);
       
   555     QVERIFY( !(d1 <= d2) );
       
   556 }
       
   557 
       
   558 void tst_QDate::operator_gt_eq()
       
   559 {
       
   560     QDate d1(2000,1,2);
       
   561     QDate d2(2000,1,2);
       
   562     QVERIFY( d1 >= d2 );
       
   563 
       
   564     d1 = QDate(2001,12,4);
       
   565     d2 = QDate(2001,12,5);
       
   566     QVERIFY( !(d1 >= d2) );
       
   567 
       
   568     d1 = QDate(2001,11,5);
       
   569     d2 = QDate(2001,12,5);
       
   570     QVERIFY( !(d1 >= d2) );
       
   571 
       
   572     d1 = QDate(2000,12,5);
       
   573     d2 = QDate(2001,12,5);
       
   574     QVERIFY( !(d1 >= d2) );
       
   575 
       
   576     d1 = QDate(2002,12,5);
       
   577     d2 = QDate(2001,12,5);
       
   578     QVERIFY( d1 >= d2 );
       
   579 
       
   580     d1 = QDate(2001,12,5);
       
   581     d2 = QDate(2001,11,5);
       
   582     QVERIFY( d1 >= d2 );
       
   583 
       
   584     d1 = QDate(2001,12,6);
       
   585     d2 = QDate(2001,12,5);
       
   586     QVERIFY( d1 >= d2 );
       
   587 }
       
   588 
       
   589 void tst_QDate::fromString_data()
       
   590 {
       
   591     // Since we can't define an element of Qt::DateFormat, d1 will be the date
       
   592     // expected when we have a TextDate, and d2 will be the date expected when
       
   593     // we have an ISODate.
       
   594 
       
   595     QTest::addColumn<QString>("str1");
       
   596     QTest::addColumn<QString>("str2");
       
   597     QTest::addColumn<QDate>("d1");
       
   598 
       
   599     QTest::newRow( "data0" ) << QString("Sat May 20 1995") << QString("1995-05-20") << QDate(1995,5,20);
       
   600     QTest::newRow( "data1" ) << QString("Tue Dec 17 2002") << QString("2002-12-17") << QDate(2002,12,17);
       
   601     QDate d( 1999, 11, 14 );
       
   602     QTest::newRow( "data2" ) << d.toString( Qt::TextDate ) << d.toString( Qt::ISODate ) << d;
       
   603 
       
   604     QTest::newRow( "data3" ) << QString("xxx Jan 1 0999") << QString("0999-01-01") << QDate(999, 1, 1);
       
   605     QTest::newRow( "data3b" ) << QString("xxx Jan 1 999") << QString("0999-01-01") << QDate(999, 1, 1);
       
   606     QTest::newRow( "data4" ) << QString("xxx Jan 1 12345") << QString() << QDate(12345, 1, 1);
       
   607     QTest::newRow( "data5" ) << QString("xxx Jan 1 -0001") << QString() << QDate(-1, 1, 1);
       
   608     QTest::newRow( "data6" ) << QString("xxx Jan 1 -4712") << QString() << QDate(-4712, 1, 1);
       
   609     QTest::newRow( "data7" ) << QString("xxx Nov 25 -4713") << QString() << QDate(-4713, 11, 25);
       
   610 }
       
   611 
       
   612 void tst_QDate::fromString()
       
   613 {
       
   614     QFETCH( QString, str1 );
       
   615     QFETCH( QString, str2 );
       
   616     QFETCH( QDate, d1 );
       
   617 
       
   618     QCOMPARE( QDate::fromString( str1, Qt::TextDate ), d1 );
       
   619     if (!str2.isEmpty())
       
   620         QCOMPARE( QDate::fromString( str2, Qt::ISODate ), d1 );
       
   621 }
       
   622 
       
   623 void tst_QDate::fromString_format_data()
       
   624 {
       
   625     QTest::addColumn<QString>("string");
       
   626     QTest::addColumn<QString>("format");
       
   627     QTest::addColumn<QDate>("date");
       
   628 
       
   629     //year with yy is always 19xx for compatibility
       
   630     QTest::newRow( "data0" ) << QString("21052006") << QString("ddMMyyyy") << QDate(2006,5,21);
       
   631     QTest::newRow( "data1" ) << QString("210506") << QString("ddMMyy") << QDate(1906,5,21);
       
   632     QTest::newRow( "data2" ) << QString("21/5/2006") << QString("d/M/yyyy") << QDate(2006,5,21);
       
   633     QTest::newRow( "data3" ) << QString("21/5/06") << QString("d/M/yy") << QDate(1906,5,21);
       
   634     QTest::newRow( "data4" ) << QString("20060521") << QString("yyyyMMdd") << QDate(2006,5,21);
       
   635     QTest::newRow( "data5" ) << QString("060521") << QString("yyMMdd") << QDate(1906,5,21);
       
   636 }
       
   637 
       
   638 void tst_QDate::fromString_format()
       
   639 {
       
   640     QFETCH( QString, string );
       
   641     QFETCH( QString, format );
       
   642     QFETCH( QDate, date );
       
   643 
       
   644     QCOMPARE( QDate::fromString( string, format ), date );
       
   645 }
       
   646 
       
   647 void tst_QDate::toString_format_data()
       
   648 {
       
   649     QTest::addColumn<QDate>("t");
       
   650     QTest::addColumn<QString>("format");
       
   651     QTest::addColumn<QString>("str");
       
   652 
       
   653     QTest::newRow( "data0" ) << QDate(1995,5,20) << QString("d-M-yy") << QString("20-5-95");
       
   654     QTest::newRow( "data1" ) << QDate(2002,12,17) << QString("dd-MM-yyyy") << QString("17-12-2002");
       
   655     QTest::newRow( "data2" ) << QDate(1995,5,20) << QString("M-yy") << QString("5-95");
       
   656     QTest::newRow( "data3" ) << QDate(2002,12,17) << QString("dd") << QString("17");
       
   657     QTest::newRow( "data4" ) << QDate() << QString("dd-mm-yyyy") << QString();
       
   658 }
       
   659 
       
   660 void tst_QDate::toString_format()
       
   661 {
       
   662     QFETCH( QDate, t );
       
   663     QFETCH( QString, format );
       
   664     QFETCH( QString, str );
       
   665 
       
   666     QCOMPARE( t.toString( format ), str );
       
   667 }
       
   668 
       
   669 void tst_QDate::isLeapYear()
       
   670 {
       
   671     QVERIFY(QDate::isLeapYear(-4444));
       
   672     QVERIFY(!QDate::isLeapYear(-4443));
       
   673     QVERIFY(QDate::isLeapYear(-8));
       
   674     QVERIFY(!QDate::isLeapYear(-7));
       
   675     QVERIFY(QDate::isLeapYear(-4));
       
   676     QVERIFY(!QDate::isLeapYear(-3));
       
   677     QVERIFY(!QDate::isLeapYear(-2));
       
   678     QVERIFY(!QDate::isLeapYear(-1));
       
   679     QVERIFY(!QDate::isLeapYear(1));
       
   680     QVERIFY(!QDate::isLeapYear(1));
       
   681     QVERIFY(!QDate::isLeapYear(1));
       
   682     QVERIFY(QDate::isLeapYear(4));
       
   683     QVERIFY(!QDate::isLeapYear(7));
       
   684     QVERIFY(QDate::isLeapYear(8));
       
   685     QVERIFY(QDate::isLeapYear(100));
       
   686     QVERIFY(QDate::isLeapYear(400));
       
   687     QVERIFY(QDate::isLeapYear(700));
       
   688     QVERIFY(QDate::isLeapYear(1500));
       
   689     QVERIFY(QDate::isLeapYear(1600));
       
   690     QVERIFY(!QDate::isLeapYear(1700));
       
   691     QVERIFY(!QDate::isLeapYear(1800));
       
   692     QVERIFY(!QDate::isLeapYear(1900));
       
   693     QVERIFY(QDate::isLeapYear(2000));
       
   694     QVERIFY(!QDate::isLeapYear(2100));
       
   695     QVERIFY(!QDate::isLeapYear(2200));
       
   696     QVERIFY(!QDate::isLeapYear(2300));
       
   697     QVERIFY(QDate::isLeapYear(2400));
       
   698     QVERIFY(!QDate::isLeapYear(2500));
       
   699     QVERIFY(!QDate::isLeapYear(2600));
       
   700     QVERIFY(!QDate::isLeapYear(2700));
       
   701     QVERIFY(QDate::isLeapYear(2800));
       
   702 
       
   703     for (int i = -4713; i <= 10000; ++i) {
       
   704         if (i == 0)
       
   705             continue;
       
   706         QVERIFY(!QDate(i, 2, 29).isValid() == !QDate::isLeapYear(i));
       
   707     }
       
   708 }
       
   709 
       
   710 void tst_QDate::yearsZeroToNinetyNine()
       
   711 {
       
   712     {
       
   713         QDate dt(-1, 2, 3);
       
   714         QCOMPARE(dt.year(), -1);
       
   715         QCOMPARE(dt.month(), 2);
       
   716         QCOMPARE(dt.day(), 3);
       
   717     }
       
   718 
       
   719     {
       
   720         QDate dt(1, 2, 3);
       
   721         QCOMPARE(dt.year(), 1);
       
   722         QCOMPARE(dt.month(), 2);
       
   723         QCOMPARE(dt.day(), 3);
       
   724     }
       
   725 
       
   726     {
       
   727         QDate dt(99, 2, 3);
       
   728         QCOMPARE(dt.year(), 99);
       
   729         QCOMPARE(dt.month(), 2);
       
   730         QCOMPARE(dt.day(), 3);
       
   731     }
       
   732 
       
   733     QVERIFY(!QDate::isValid(0, 2, 3));
       
   734     QVERIFY(QDate::isValid(1, 2, 3));
       
   735     QVERIFY(QDate::isValid(-1, 2, 3));
       
   736 
       
   737     {
       
   738         QDate dt;
       
   739         dt.setYMD(1, 2, 3);
       
   740         QCOMPARE(dt.year(), 1901);
       
   741         QCOMPARE(dt.month(), 2);
       
   742         QCOMPARE(dt.day(), 3);
       
   743     }
       
   744 
       
   745     {
       
   746         QDate dt;
       
   747         dt.setDate(1, 2, 3);
       
   748         QCOMPARE(dt.year(), 1);
       
   749         QCOMPARE(dt.month(), 2);
       
   750         QCOMPARE(dt.day(), 3);
       
   751 
       
   752         dt.setDate(0, 2, 3);
       
   753         QVERIFY(!dt.isValid());
       
   754     }
       
   755 }
       
   756 
       
   757 void tst_QDate::toString()
       
   758 {
       
   759     QDate date(1974,12,1);
       
   760     QCOMPARE(date.toString(Qt::SystemLocaleDate),
       
   761                 QLocale::system().toString(date, QLocale::ShortFormat));
       
   762     QCOMPARE(date.toString(Qt::LocaleDate),
       
   763                 QLocale().toString(date, QLocale::ShortFormat));
       
   764     QLocale::setDefault(QLocale::German);
       
   765     QCOMPARE(date.toString(Qt::SystemLocaleDate),
       
   766                 QLocale::system().toString(date, QLocale::ShortFormat));
       
   767     QCOMPARE(date.toString(Qt::LocaleDate),
       
   768                 QLocale().toString(date, QLocale::ShortFormat));
       
   769 }
       
   770 
       
   771 void tst_QDate::negativeYear() const
       
   772 {
       
   773     QDate y(-20, 3, 4);
       
   774     QVERIFY(y.isValid());
       
   775     QCOMPARE(y.year(), -20);
       
   776 }
       
   777 
       
   778 void tst_QDate::printNegativeYear() const
       
   779 {
       
   780     {
       
   781         QDate date(-500, 3, 4);
       
   782         QVERIFY(date.isValid());
       
   783         QCOMPARE(date.toString(QLatin1String("yyyy")), QString::fromLatin1("-0500"));
       
   784     }
       
   785 
       
   786     {
       
   787         QDate date(-10, 3, 4);
       
   788         QVERIFY(date.isValid());
       
   789         QCOMPARE(date.toString(QLatin1String("yyyy")), QString::fromLatin1("-0010"));
       
   790     }
       
   791 
       
   792     {
       
   793         QDate date(-2, 3, 4);
       
   794         QVERIFY(date.isValid());
       
   795         QCOMPARE(date.toString(QLatin1String("yyyy")), QString::fromLatin1("-0002"));
       
   796     }
       
   797 }
       
   798 
       
   799 void tst_QDate::roundtripGermanLocale() const
       
   800 {
       
   801     /* This code path should not result in warnings. */
       
   802     const QDate theDate(QDate::currentDate());
       
   803     theDate.fromString(theDate.toString(Qt::TextDate), Qt::TextDate);
       
   804 
       
   805     const QDateTime theDateTime(QDateTime::currentDateTime());
       
   806     theDateTime.fromString(theDateTime.toString(Qt::TextDate), Qt::TextDate);
       
   807 }
       
   808 
       
   809 void tst_QDate::shortDayName() const
       
   810 {
       
   811     if (QLocale::system().language() == QLocale::C) {
       
   812         QCOMPARE(QDate::shortDayName(1), QLatin1String("Mon"));
       
   813         QCOMPARE(QDate::shortDayName(7), QLatin1String("Sun"));
       
   814     }
       
   815 
       
   816     QLocale locale = QLocale::system();
       
   817     for(int i = 1; i <= 7; ++i) {
       
   818         QCOMPARE(QDate::shortDayName(i), locale.dayName(i, QLocale::ShortFormat));
       
   819     }
       
   820 }
       
   821 
       
   822 void tst_QDate::standaloneShortDayName() const
       
   823 {
       
   824     if (QLocale::system().language() == QLocale::C) {
       
   825         QCOMPARE(QDate::shortDayName(1, QDate::StandaloneFormat), QLatin1String("Mon"));
       
   826         QCOMPARE(QDate::shortDayName(7, QDate::StandaloneFormat), QLatin1String("Sun"));
       
   827     }
       
   828 
       
   829     QLocale locale = QLocale::system();
       
   830     for(int i = 1; i <= 7; ++i) {
       
   831         QCOMPARE(QDate::shortDayName(i, QDate::StandaloneFormat), locale.standaloneDayName(i, QLocale::ShortFormat));
       
   832     }
       
   833 }
       
   834 
       
   835 void tst_QDate::longDayName() const
       
   836 {
       
   837     if (QLocale::system().language() == QLocale::C) {
       
   838         QCOMPARE(QDate::longDayName(1), QLatin1String("Monday"));
       
   839         QCOMPARE(QDate::longDayName(7), QLatin1String("Sunday"));
       
   840     }
       
   841 
       
   842     QLocale locale = QLocale::system();
       
   843     for(int i = 1; i <= 7; ++i) {
       
   844         QCOMPARE(QDate::longDayName(i), locale.dayName(i, QLocale::LongFormat));
       
   845     }
       
   846 }
       
   847 
       
   848 void tst_QDate::standaloneLongDayName() const
       
   849 {
       
   850     if (QLocale::system().language() == QLocale::C) {
       
   851         QCOMPARE(QDate::longDayName(1, QDate::StandaloneFormat), QLatin1String("Monday"));
       
   852         QCOMPARE(QDate::longDayName(7, QDate::StandaloneFormat), QLatin1String("Sunday"));
       
   853     }
       
   854 
       
   855     QLocale locale = QLocale::system();
       
   856     for(int i = 1; i <= 7; ++i) {
       
   857         QCOMPARE(QDate::longDayName(i, QDate::StandaloneFormat), locale.standaloneDayName(i, QLocale::LongFormat));
       
   858     }
       
   859 }
       
   860 
       
   861 void tst_QDate::shortMonthName() const
       
   862 {
       
   863     if (QLocale::system().language() == QLocale::C) {
       
   864         QCOMPARE(QDate::shortMonthName(1), QLatin1String("Jan"));
       
   865         QCOMPARE(QDate::shortMonthName(8), QLatin1String("Aug"));
       
   866     }
       
   867 
       
   868     QLocale locale = QLocale::system();
       
   869     for(int i = 1; i <= 12; ++i) {
       
   870         QCOMPARE(QDate::shortMonthName(i), locale.monthName(i, QLocale::ShortFormat));
       
   871     }
       
   872 }
       
   873 
       
   874 void tst_QDate::standaloneShortMonthName() const
       
   875 {
       
   876     if (QLocale::system().language() == QLocale::C) {
       
   877         QCOMPARE(QDate::shortMonthName(1, QDate::StandaloneFormat), QLatin1String("Jan"));
       
   878         QCOMPARE(QDate::shortMonthName(8, QDate::StandaloneFormat), QLatin1String("Aug"));
       
   879     }
       
   880 
       
   881     QLocale locale = QLocale::system();
       
   882     for(int i = 1; i <= 12; ++i) {
       
   883         QCOMPARE(QDate::shortMonthName(i, QDate::StandaloneFormat), locale.standaloneMonthName(i, QLocale::ShortFormat));
       
   884     }
       
   885 }
       
   886 
       
   887 void tst_QDate::longMonthName() const
       
   888 {
       
   889     if (QLocale::system().language() == QLocale::C) {
       
   890         QCOMPARE(QDate::longMonthName(1), QLatin1String("January"));
       
   891         QCOMPARE(QDate::longMonthName(8), QLatin1String("August"));
       
   892     }
       
   893 
       
   894     QLocale locale = QLocale::system();
       
   895     for(int i = 1; i <= 12; ++i) {
       
   896         QCOMPARE(QDate::longMonthName(i), locale.monthName(i, QLocale::LongFormat));
       
   897     }
       
   898 }
       
   899 
       
   900 void tst_QDate::standaloneLongMonthName() const
       
   901 {
       
   902     if (QLocale::system().language() == QLocale::C) {
       
   903         QCOMPARE(QDate::longMonthName(1, QDate::StandaloneFormat), QLatin1String("January"));
       
   904         QCOMPARE(QDate::longMonthName(8, QDate::StandaloneFormat), QLatin1String("August"));
       
   905     }
       
   906 
       
   907     QLocale locale = QLocale::system();
       
   908     for(int i = 1; i <= 12; ++i) {
       
   909         QCOMPARE(QDate::longMonthName(i, QDate::StandaloneFormat), locale.standaloneMonthName(i, QLocale::LongFormat));
       
   910     }
       
   911 }
       
   912 
       
   913 QTEST_APPLESS_MAIN(tst_QDate)
       
   914 #include "tst_qdate.moc"