Improve output on test failures

This adds checks to ensure Q_ALIGNOF is returning the desired alignment
for explicitly-aligned types.

The alignment check is now inlined in the test inside QCOMPARE so we get
slightly more informative errors:

FAIL!  : tst_Collections::alignment() Compared values are not the same
   Actual   (quintptr(&it.value()) % Value::PreferredAlignment): 64
   Expected (quintptr(0)): 0
   Loc: [tst_collections.cpp(3384)]

In this case, this is enough to notice "non-native" alignments are being
requested. Having test parameters otherwise hidden in template arguments
doesn't help the situation.

Change-Id: I05267fd25b71f183cfb98fb5b0a7dfd6c28da816
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
This commit is contained in:
João Abecasis 2012-03-22 01:28:30 +01:00 committed by Qt by Nokia
parent 049157f4f1
commit 7c9e345551

View File

@ -3318,30 +3318,28 @@ class Q_DECL_ALIGN(4) Aligned4
char i; char i;
public: public:
Aligned4(int i = 0) : i(i) {} Aligned4(int i = 0) : i(i) {}
bool checkAligned() const
{ enum { PreferredAlignment = 4 };
return (quintptr(this) & 3) == 0;
}
inline bool operator==(const Aligned4 &other) const { return i == other.i; } inline bool operator==(const Aligned4 &other) const { return i == other.i; }
inline bool operator<(const Aligned4 &other) const { return i < other.i; } inline bool operator<(const Aligned4 &other) const { return i < other.i; }
friend inline int qHash(const Aligned4 &a) { return qHash(a.i); } friend inline int qHash(const Aligned4 &a) { return qHash(a.i); }
}; };
Q_STATIC_ASSERT(Q_ALIGNOF(Aligned4) % 4 == 0);
class Q_DECL_ALIGN(128) Aligned128 class Q_DECL_ALIGN(128) Aligned128
{ {
char i; char i;
public: public:
Aligned128(int i = 0) : i(i) {} Aligned128(int i = 0) : i(i) {}
bool checkAligned() const
{ enum { PreferredAlignment = 128 };
return (quintptr(this) & 127) == 0;
}
inline bool operator==(const Aligned128 &other) const { return i == other.i; } inline bool operator==(const Aligned128 &other) const { return i == other.i; }
inline bool operator<(const Aligned128 &other) const { return i < other.i; } inline bool operator<(const Aligned128 &other) const { return i < other.i; }
friend inline int qHash(const Aligned128 &a) { return qHash(a.i); } friend inline int qHash(const Aligned128 &a) { return qHash(a.i); }
}; };
Q_STATIC_ASSERT(Q_ALIGNOF(Aligned128) % 128 == 0);
template<typename C> template<typename C>
void testVectorAlignment() void testVectorAlignment()
@ -3349,13 +3347,13 @@ void testVectorAlignment()
typedef typename C::value_type Aligned; typedef typename C::value_type Aligned;
C container; C container;
container.append(Aligned()); container.append(Aligned());
QVERIFY(container[0].checkAligned()); QCOMPARE(quintptr(&container[0]) % Aligned::PreferredAlignment, quintptr(0));
for (int i = 0; i < 200; ++i) for (int i = 0; i < 200; ++i)
container.append(Aligned()); container.append(Aligned());
for (int i = 0; i < container.size(); ++i) for (int i = 0; i < container.size(); ++i)
QVERIFY(container.at(i).checkAligned()); QCOMPARE(quintptr(&container.at(i)) % Aligned::PreferredAlignment, quintptr(0));
} }
template<typename C> template<typename C>
@ -3364,13 +3362,13 @@ void testContiguousCacheAlignment()
typedef typename C::value_type Aligned; typedef typename C::value_type Aligned;
C container(150); C container(150);
container.append(Aligned()); container.append(Aligned());
QVERIFY(container[container.firstIndex()].checkAligned()); QCOMPARE(quintptr(&container[container.firstIndex()]) % Aligned::PreferredAlignment, quintptr(0));
for (int i = 0; i < 200; ++i) for (int i = 0; i < 200; ++i)
container.append(Aligned()); container.append(Aligned());
for (int i = container.firstIndex(); i < container.lastIndex(); ++i) for (int i = container.firstIndex(); i < container.lastIndex(); ++i)
QVERIFY(container.at(i).checkAligned()); QCOMPARE(quintptr(&container.at(i)) % Aligned::PreferredAlignment, quintptr(0));
} }
template<typename C> template<typename C>
@ -3382,8 +3380,8 @@ void testAssociativeContainerAlignment()
container.insert(Key(), Value()); container.insert(Key(), Value());
typename C::const_iterator it = container.constBegin(); typename C::const_iterator it = container.constBegin();
QVERIFY(it.key().checkAligned()); QCOMPARE(quintptr(&it.key()) % Key::PreferredAlignment, quintptr(0));
QVERIFY(it.value().checkAligned()); QCOMPARE(quintptr(&it.value()) % Value::PreferredAlignment, quintptr(0));
// add some more elements // add some more elements
for (int i = 0; i < 200; ++i) for (int i = 0; i < 200; ++i)
@ -3391,8 +3389,8 @@ void testAssociativeContainerAlignment()
it = container.constBegin(); it = container.constBegin();
for ( ; it != container.constEnd(); ++it) { for ( ; it != container.constEnd(); ++it) {
QVERIFY(it.key().checkAligned()); QCOMPARE(quintptr(&it.key()) % Key::PreferredAlignment, quintptr(0));
QVERIFY(it.value().checkAligned()); QCOMPARE(quintptr(&it.value()) % Value::PreferredAlignment, quintptr(0));
} }
} }