1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
#include <QTest>
#include <QSignalSpy>
#include <QxtSharedPrivate>
#include <QDebug>
#include <QString>
int instances=0;
class MyPrivate;
class My
{
public:
My();
QString string() const;
void setString(QString a);
private:
QxtSharedPrivate<MyPrivate> d;
};
class MyPrivate
{
public:
MyPrivate()
{
instances++;
}
~MyPrivate()
{
instances--;
}
QString string() const
{
return m_string;
}
void setString(QString a)
{
m_string=a;
}
QString m_string;
};
My::My()
{
d=new MyPrivate;
}
QString My::string() const
{
return d().string();
}
void My::setString(QString a)
{
return d().setString(a);
}
class My2Private;
class My2
{
public:
My2();
QString string() const;
void setString(QString a);
void couseDetach();
private:
QxtSharedPrivate<My2Private> d;
};
class My2Private
{
public:
My2Private()
{
instances++;
}
virtual ~My2Private()
{
instances--;
}
My2Private(const My2Private & )
{
instances++;
m_string="foo";
}
QString string() const
{
return m_string;
}
void setString(QString a)
{
m_string=a;
}
QString m_string;
void couseDetach()
{
//nothing. but it's not const
}
};
My2::My2()
{
d=new My2Private;
}
QString My2::string() const
{
return d().string();
}
void My2::setString(QString a)
{
d().setString(a);
}
void My2::couseDetach()
{
d().couseDetach();
}
class QxtSharedPrivateTest : public QObject
{
Q_OBJECT
private slots:
void sanity()
{
{
QCOMPARE(instances,0);
My m1;
QCOMPARE(instances,1);
My m2;
QCOMPARE(instances,2);
}
QCOMPARE(instances,0);
}
void share()
{
My m1;
QCOMPARE(instances,1);
My m2(m1);
QCOMPARE(instances,1);
}
void keepShareOnConstCall()
{
QCOMPARE(instances,0);
My m1;
QCOMPARE(instances,1);
My m2(m1);
QCOMPARE(instances,1);
m1.string();
QCOMPARE(instances,1);
}
void copyOnWrite()
{
QCOMPARE(instances,0);
My2 m1;
QCOMPARE(instances,1);
My2 m2(m1);
QCOMPARE(instances,1);
m1.setString("bla");
QCOMPARE(instances,2);
}
void implicitCopyCtor()
{
QCOMPARE(instances,0);
My m1;
m1.setString("bla");
QCOMPARE(instances,1);
My m2(m1);
QCOMPARE(instances,1);
QVERIFY(m1.string()=="bla");
QCOMPARE(instances,1);
}
void explicitCopyCtor()
{
QCOMPARE(instances,0);
My2 m1;
m1.setString("bla");
QCOMPARE(instances,1);
My2 m2(m1);
QCOMPARE(instances,1);
m2.couseDetach();
QVERIFY(m2.string()=="foo");
QCOMPARE(instances,2);
}
};
int main(int argc, char ** argv)
{
QCoreApplication app(argc,argv);
QxtSharedPrivateTest test1;
return QTest::qExec(&test1,argc,argv);
}
#include "main.moc"
|