File indexing completed on 2022-11-29 18:25:16

0001 /* Copyright (c) 2005 Frerich Raabe <raabe@kde.org>
0002  *
0003  * Redistribution and use in source and binary forms, with or without
0004  * modification, are permitted provided that the following conditions
0005  * are met:
0006  *
0007  * 1. Redistributions of source code must retain the above copyright
0008  *    notice, this list of conditions and the following disclaimer.
0009  * 2. Redistributions in binary form must reproduce the above copyright
0010  *    notice, this list of conditions and the following disclaimer in the
0011  *    documentation and/or other materials provided with the distribution.
0012  *
0013  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0014  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0015  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0016  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0017  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0018  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0019  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0020  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0021  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0022  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0023  */
0024 #include "ksharedptrtest.h"
0025 #include "ksharedptr.h"
0026 
0027 #include <QTest>
0028 
0029 QTEST_MAIN(KSharedPtrTest)
0030 
0031 class SharedString : public KShared
0032 {
0033 public:
0034     SharedString(const QString &data) : mStr(data) {}
0035     bool operator == (const SharedString &o) const
0036     {
0037         return mStr == o.mStr;
0038     }
0039     QString mStr;
0040 };
0041 
0042 void KSharedPtrTest::testWithStrings()
0043 {
0044     SharedString s = QString::fromLatin1("Hello");
0045     SharedString s2 = QString::fromLatin1("Foo");
0046     SharedString s3 = QString::fromLatin1("Bar");
0047 
0048     KSharedPtr<SharedString>u(new SharedString(s));
0049     QCOMPARE(*u, s);
0050     QVERIFY(u.isUnique());
0051 
0052     KSharedPtr<SharedString> v;
0053     QVERIFY(u.isUnique());
0054     QVERIFY(!v);
0055 
0056     v = u;
0057     QVERIFY(!u.isUnique());
0058     QCOMPARE(*v, s);
0059     QVERIFY(!v.isUnique());
0060 
0061     KSharedPtr<SharedString> w = v;
0062     QVERIFY(!u.isUnique());
0063     QCOMPARE(u.count(), 3);
0064     QVERIFY(!v.isUnique());
0065     QCOMPARE(v.count(), 3);
0066     QVERIFY(!w.isUnique());
0067     QCOMPARE(w.count(), 3);
0068 
0069 //  v->clear();
0070     v = nullptr;
0071     QVERIFY(u);
0072     QVERIFY(!u.isUnique());
0073     QCOMPARE(u.count(), 2);
0074     QVERIFY(!v);
0075     QVERIFY(!v.isUnique());
0076     QCOMPARE(*w, s);
0077     QVERIFY(!w.isUnique());
0078     QCOMPARE(w.count(), 2);
0079 
0080     u = v = w;
0081     QCOMPARE(*u, s);
0082     QVERIFY(!u.isUnique());
0083     QCOMPARE(*v, s);
0084     QVERIFY(!v.isUnique());
0085     QCOMPARE(*w, s);
0086     QVERIFY(!w.isUnique());
0087 
0088     u->mStr = s2.mStr;
0089     QCOMPARE(*u, s2);
0090     QVERIFY(!u.isUnique());
0091     QCOMPARE(*v, s2);
0092     QVERIFY(!v.isUnique());
0093 //  QCOMPARE( *w, s2 );
0094 //  QVERIFY( !w.isUnique() );
0095 }
0096 
0097 static int dtor_called = 0;
0098 class Base : public KShared
0099 {
0100 public:
0101     virtual ~Base()
0102     {
0103         ++dtor_called;
0104     }
0105 };
0106 
0107 void KSharedPtrTest::testDeletion()
0108 {
0109     dtor_called = 0;
0110     {
0111         Base *obj = new Base;
0112         KSharedPtr<Base> ptrBase(obj);
0113         QCOMPARE(ptrBase.data(), obj);
0114         QCOMPARE(dtor_called, 0);   // no dtor called yet
0115     }
0116     QCOMPARE(dtor_called, 1);
0117 }
0118 
0119 class Derived : public Base
0120 {
0121 public:
0122     Derived()
0123     {
0124         /*qDebug( "Derived created %p", (void*)this );*/
0125     }
0126     ~Derived() override
0127     {
0128         /*qDebug( "Derived deleted %p", (void*)this );*/
0129     }
0130 };
0131 
0132 void KSharedPtrTest::testDifferentTypes()
0133 {
0134     dtor_called = 0;
0135     {
0136         Derived *obj = new Derived;
0137         KSharedPtr<Base> ptrBase(obj);
0138         // then we call some method that takes a KSharedPtr<Base> as argument
0139         // and there we downcast again:
0140         KSharedPtr<Derived> ptrDerived = KSharedPtr<Derived>::staticCast(ptrBase);
0141         QCOMPARE(dtor_called, 0);   // no dtor called yet
0142         QCOMPARE(ptrDerived.data(), obj);
0143 
0144         // now test assignment operator
0145         ptrDerived = KSharedPtr<Derived>::dynamicCast(ptrBase);
0146         QCOMPARE(dtor_called, 0);   // no dtor called yet
0147         QCOMPARE(ptrDerived.data(), obj);
0148     }
0149     QCOMPARE(dtor_called, 1);
0150 }
0151 
0152 void KSharedPtrTest::testOrdering()
0153 {
0154     Base *obj = new Base;
0155     KSharedPtr<Base> ptrBase(obj);
0156 
0157     Base *obj2 = new Base;
0158     KSharedPtr<Base> ptrBase2(obj2);
0159 
0160     QVERIFY(obj != obj2);
0161     QVERIFY(ptrBase != ptrBase2);
0162     QVERIFY(ptrBase < ptrBase2 || ptrBase2 < ptrBase);
0163 
0164     QMap<KSharedPtr<Base>, int> map;
0165     map.insert(ptrBase, 1);
0166     QVERIFY(map.contains(ptrBase));
0167     QVERIFY(!map.contains(ptrBase2));
0168     QCOMPARE(map.value(ptrBase, 0),  1);
0169     map.insert(ptrBase2, 2);
0170     QVERIFY(map.contains(ptrBase2));
0171     QCOMPARE(map.value(ptrBase2, 0),  2);
0172     QCOMPARE(map.count(), 2);
0173 }
0174