qtmobility/tests/auto/qlatin1constant/tst_qlatin1constant.cpp
changeset 4 90517678cc4f
child 5 453da2cfceef
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
       
     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 Qt Mobility Components.
       
     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 #include <QtTest/QtTest>
       
    43 
       
    44 #include "qtcontactsglobal.h"
       
    45 #include <QSet>
       
    46 #include <QMetaType>
       
    47 #include <QTypeInfo>
       
    48 
       
    49 //TESTED_CLASS=
       
    50 //TESTED_FILES=
       
    51 
       
    52 QTM_USE_NAMESPACE
       
    53 
       
    54 Q_DEFINE_LATIN1_CONSTANT(a, "a");
       
    55 Q_DEFINE_LATIN1_CONSTANT(a2, "a");
       
    56 Q_DEFINE_LATIN1_CONSTANT(b, "b");
       
    57 Q_DEFINE_LATIN1_CONSTANT(b2, "b");
       
    58 Q_DEFINE_LATIN1_CONSTANT(bb, "bb");
       
    59 Q_DEFINE_LATIN1_CONSTANT(bb2, "bb");
       
    60 
       
    61 Q_DEFINE_LATIN1_CONSTANT(z, "");
       
    62 Q_DEFINE_LATIN1_CONSTANT(z2, "");
       
    63 
       
    64 Q_DEFINE_LATIN1_CONSTANT(z3, "\0");
       
    65 Q_DEFINE_LATIN1_CONSTANT(soup, "alphabet soup"); // but you can't have any
       
    66 
       
    67 QLatin1String ln(0);
       
    68 QLatin1String lz("");
       
    69 QLatin1String la("a");
       
    70 QLatin1String lb("b");
       
    71 QLatin1String lbb("bb");
       
    72 QLatin1String lsoup("alphabet soup");
       
    73 
       
    74 QString sn;
       
    75 QString sz("");
       
    76 QString sa(la);
       
    77 QString sb(lb);
       
    78 QString sbb(lbb);
       
    79 QString ssoup("alphabet soup");
       
    80 
       
    81 class tst_QLatin1Constant: public QObject
       
    82 {
       
    83 Q_OBJECT
       
    84 
       
    85 public:
       
    86     tst_QLatin1Constant();
       
    87     virtual ~tst_QLatin1Constant();
       
    88 
       
    89     // Overload testers
       
    90     int overloaded(const char *) {return 1;}
       
    91     //int overloaded(const QLatin1String& ) {return 2;}
       
    92     int overloaded(QLatin1String ) {return 3;}
       
    93     int overloaded(const QString& ) {return 4;}
       
    94     //int overloaded(QString ){return 5;}
       
    95     //template<int N> int overloaded(const QLatin1Constant<N>& ) {return 6;}
       
    96     template<int N> int overloaded(QLatin1Constant<N> ) {return 7;}
       
    97     int overloaded(const QVariant&) {return 8;}
       
    98 
       
    99     // More overload testers
       
   100     int overloaded2(QLatin1String) {return 3;}
       
   101     int overloaded2(const QString&) {return 4;}
       
   102 
       
   103     int overloaded3(const char*) {return 1;}
       
   104     int overloaded3(QLatin1String) {return 3;}
       
   105 
       
   106     int overloaded4(const char*) {return 1;}
       
   107     int overloaded4(const QString&) {return 4;}
       
   108 
       
   109     // Conversion testers
       
   110     bool charfunc(const char* str) {return qstrcmp(str, "alphabet soup") == 0;}
       
   111     bool latfunc(QLatin1String lat) {return qstrcmp(lat.latin1(), "alphabet soup") == 0;}
       
   112     bool latreffunc(const QLatin1String& lat) {return qstrcmp(lat.latin1(), "alphabet soup") == 0;}
       
   113     bool strfunc(QString str) {return str == QString::fromAscii("alphabet soup");}
       
   114     bool strreffunc(const QString& str) {return str == QString::fromAscii("alphabet soup");}
       
   115     bool varfunc(const QVariant& var) {return (var.type() == QVariant::String) && var.toString() == QString::fromAscii("alphabet soup");}
       
   116 
       
   117 private slots:
       
   118     void hash();
       
   119     void conversion();
       
   120     void overloads();
       
   121     void equals();
       
   122     void latinEquals();
       
   123     void stringEquals();
       
   124     void ordering();
       
   125     void latinaccessor();
       
   126 };
       
   127 
       
   128 tst_QLatin1Constant::tst_QLatin1Constant()
       
   129 {
       
   130 }
       
   131 
       
   132 tst_QLatin1Constant::~tst_QLatin1Constant()
       
   133 {
       
   134 }
       
   135 
       
   136 void tst_QLatin1Constant::hash()
       
   137 {
       
   138     // Test that if a == b, hash(a) == hash(b)
       
   139     // (also for ===)
       
   140     QVERIFY(qHash(a) == qHash(a));
       
   141     QVERIFY(qHash(a) == qHash(a2));
       
   142     QVERIFY(qHash(b) == qHash(b));
       
   143     QVERIFY(qHash(b) == qHash(b));
       
   144     QVERIFY(qHash(bb) == qHash(bb));
       
   145     QVERIFY(qHash(bb) == qHash(bb));
       
   146 
       
   147     // As a convenience, make sure that hashing
       
   148     // the same string gives the same results
       
   149     // no matter the storage
       
   150     QVERIFY(qHash(a) == qHash(la));
       
   151     QVERIFY(qHash(a) == qHash(sa));
       
   152 }
       
   153 
       
   154 void tst_QLatin1Constant::equals()
       
   155 {
       
   156     // Check symmetry and dupes
       
   157     QVERIFY(a == a);
       
   158     QVERIFY(a == a2);
       
   159     QVERIFY(a2 == a);
       
   160     QVERIFY(b == b);
       
   161     QVERIFY(b == b2);
       
   162     QVERIFY(b2 == b2);
       
   163     QVERIFY(bb == bb);
       
   164     QVERIFY(bb == bb2);
       
   165     QVERIFY(bb2 == bb);
       
   166 
       
   167     QVERIFY(z == z);
       
   168     QVERIFY(z == z2);
       
   169     QVERIFY(z2 == z);
       
   170 
       
   171     // Now make sure that the length is taken into account
       
   172     QVERIFY(b != bb2);
       
   173     QVERIFY(bb2 != b);
       
   174     QVERIFY(a != z);
       
   175     QVERIFY(z != a);
       
   176 
       
   177     // and just in case something is really wrong
       
   178     QVERIFY(a != b);
       
   179     QVERIFY(b != a);
       
   180 }
       
   181 
       
   182 void tst_QLatin1Constant::latinaccessor()
       
   183 {
       
   184     QVERIFY(a.chars == a.latin1());
       
   185     QVERIFY(z.latin1() == z.chars);
       
   186 }
       
   187 
       
   188 void tst_QLatin1Constant::latinEquals()
       
   189 {
       
   190     // Test operator== with latin1 strings
       
   191     QVERIFY(a == la);
       
   192     QVERIFY(la == a);
       
   193     QVERIFY(a2 == la);
       
   194     QVERIFY(la == a2);
       
   195     QVERIFY(b == lb);
       
   196     QVERIFY(lb == b);
       
   197     QVERIFY(bb == lbb);
       
   198     QVERIFY(lbb == bb);
       
   199 
       
   200     QVERIFY(b != lbb);
       
   201     QVERIFY(lbb != b);
       
   202 
       
   203     QVERIFY(a != lb);
       
   204     QVERIFY(lb != a);
       
   205 
       
   206     QVERIFY(z == lz);
       
   207     QVERIFY((z == ln) == (lz == ln)); // QLatin1String(0) != QLatin1String("")
       
   208     QVERIFY(lz == z);
       
   209     QVERIFY((ln == z) == (ln == lz));
       
   210 }
       
   211 
       
   212 void tst_QLatin1Constant::stringEquals()
       
   213 {
       
   214     // Test operator== with QStrings
       
   215     QVERIFY(a == sa);
       
   216     QVERIFY(sa == a);
       
   217     QVERIFY(a2 == sa);
       
   218     QVERIFY(sa == a2);
       
   219     QVERIFY(b == sb);
       
   220     QVERIFY(sb == b);
       
   221     QVERIFY(bb == sbb);
       
   222     QVERIFY(sbb == bb);
       
   223 
       
   224     QVERIFY(b != sbb);
       
   225     QVERIFY(sbb != b);
       
   226 
       
   227     QVERIFY(a != sb);
       
   228     QVERIFY(sb != a);
       
   229 
       
   230     QVERIFY(z == sz);
       
   231     QVERIFY((z == sn) == (sz == sn)); // QString(0) != QString("")
       
   232     QVERIFY(sz == z);
       
   233     QVERIFY((sn == z) == (sn == sz));
       
   234 }
       
   235 
       
   236 void tst_QLatin1Constant::conversion()
       
   237 {
       
   238     QVERIFY(charfunc("alphabet soup"));
       
   239     QVERIFY(charfunc(soup.chars));
       
   240     QVERIFY(charfunc(soup.latin1()));
       
   241 
       
   242     QVERIFY(latfunc(lsoup));
       
   243     QVERIFY(latreffunc(lsoup));
       
   244 
       
   245     QVERIFY(strfunc(ssoup));
       
   246     QVERIFY(strreffunc(ssoup));
       
   247 
       
   248     // See if soup gets converted appropriately
       
   249     QVERIFY(latfunc(soup));
       
   250     QVERIFY(strfunc(soup));
       
   251     QVERIFY(latreffunc(soup));
       
   252     QVERIFY(strreffunc(soup));
       
   253     QVERIFY(varfunc(soup));
       
   254 
       
   255     // Now we also want to make sure that converting to QLatin1String doesn't copy the string
       
   256     QLatin1String lsoup2 = soup; // implicit operator QLatin1String
       
   257     QLatin1String lsoup3 = (QLatin1String) soup; // explicit operator QLatin1String
       
   258     QLatin1String lsoup4 = QLatin1String(soup); // implicit operator QLatin1String
       
   259 
       
   260     QVERIFY(lsoup2.latin1() == soup.latin1());
       
   261     QVERIFY(lsoup3.latin1() == soup.latin1());
       
   262     QVERIFY(lsoup4.latin1() == soup.latin1());
       
   263 }
       
   264 
       
   265 void tst_QLatin1Constant::overloads()
       
   266 {
       
   267     QVERIFY(overloaded("alphabet soup") == 1);
       
   268     QVERIFY(overloaded(soup) == 7);
       
   269     QVERIFY(overloaded(lsoup) == 2 || overloaded(lsoup) == 3);
       
   270     QVERIFY(overloaded(ssoup) == 4 || overloaded(ssoup) == 5);
       
   271 
       
   272     QVERIFY(overloaded2(lsoup) == 3);
       
   273     QVERIFY(overloaded2(ssoup) == 4);
       
   274     QCOMPARE(overloaded2(soup.latin1()), 4); // XXX grr, can't call with just soup [ambiguous], this goes to QString
       
   275 
       
   276     QVERIFY(overloaded3(lsoup) == 3);
       
   277     QCOMPARE(overloaded3(soup), 3); // XXX this goes with QLatin1String
       
   278 
       
   279     QVERIFY(overloaded4(ssoup) == 4);
       
   280     QCOMPARE(overloaded4(soup), 4); // XXX this goes with QString
       
   281 }
       
   282 
       
   283 void tst_QLatin1Constant::ordering()
       
   284 {
       
   285     QVERIFY(z < a);
       
   286     QVERIFY(!(a < z));
       
   287     QVERIFY(a < b);
       
   288     QVERIFY(!(b < a));
       
   289     QVERIFY(a < bb);
       
   290     QVERIFY(!(bb < a));
       
   291     QVERIFY(b < bb);
       
   292     QVERIFY(!(bb < b));
       
   293 
       
   294     QVERIFY(!(a < a));
       
   295     QVERIFY(!(z < z));
       
   296 }
       
   297 
       
   298 QTEST_MAIN(tst_QLatin1Constant)
       
   299 #include "tst_qlatin1constant.moc"