tests/auto/collections/tst_collections.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
   162     void pointersize();
   162     void pointersize();
   163     void containerInstantiation();
   163     void containerInstantiation();
   164     void qtimerList();
   164     void qtimerList();
   165     void containerTypedefs();
   165     void containerTypedefs();
   166     void forwardDeclared();
   166     void forwardDeclared();
       
   167     void alignment();
   167 };
   168 };
   168 
   169 
   169 struct LargeStatic {
   170 struct LargeStatic {
   170     static int count;
   171     static int count;
   171     LargeStatic():c(count) { ++count; }
   172     LargeStatic():c(count) { ++count; }
  3479     { typedef QStack<T1> C; C *x = 0; C::iterator i; C::const_iterator j; Q_UNUSED(x) Q_UNUSED(i) Q_UNUSED(j) }
  3480     { typedef QStack<T1> C; C *x = 0; C::iterator i; C::const_iterator j; Q_UNUSED(x) Q_UNUSED(i) Q_UNUSED(j) }
  3480     { typedef QQueue<T1> C; C *x = 0; C::iterator i; C::const_iterator j; Q_UNUSED(x) }
  3481     { typedef QQueue<T1> C; C *x = 0; C::iterator i; C::const_iterator j; Q_UNUSED(x) }
  3481     { typedef QSet<T1> C; C *x = 0; /* C::iterator i; */ C::const_iterator j; Q_UNUSED(x) }
  3482     { typedef QSet<T1> C; C *x = 0; /* C::iterator i; */ C::const_iterator j; Q_UNUSED(x) }
  3482 }
  3483 }
  3483 
  3484 
       
  3485 #if defined(Q_ALIGNOF) && defined(Q_DECL_ALIGN)
       
  3486 
       
  3487 class Q_DECL_ALIGN(4) Aligned4
       
  3488 {
       
  3489     char i;
       
  3490 public:
       
  3491     Aligned4(int i = 0) : i(i) {}
       
  3492     bool checkAligned() const
       
  3493     {
       
  3494         return (quintptr(this) & 3) == 0;
       
  3495     }
       
  3496 
       
  3497     inline bool operator==(const Aligned4 &other) const { return i == other.i; }
       
  3498     inline bool operator<(const Aligned4 &other) const { return i < other.i; }
       
  3499     friend inline int qHash(const Aligned4 &a) { return qHash(a.i); }
       
  3500 };
       
  3501 
       
  3502 class Q_DECL_ALIGN(128) Aligned128
       
  3503 {
       
  3504     char i;
       
  3505 public:
       
  3506     Aligned128(int i = 0) : i(i) {}
       
  3507     bool checkAligned() const
       
  3508     {
       
  3509         return (quintptr(this) & 127) == 0;
       
  3510     }
       
  3511 
       
  3512     inline bool operator==(const Aligned128 &other) const { return i == other.i; }
       
  3513     inline bool operator<(const Aligned128 &other) const { return i < other.i; }
       
  3514     friend inline int qHash(const Aligned128 &a) { return qHash(a.i); }
       
  3515 };
       
  3516 
       
  3517 template<typename C>
       
  3518 void testVectorAlignment()
       
  3519 {
       
  3520     typedef typename C::value_type Aligned;
       
  3521     C container;
       
  3522     container.append(Aligned());
       
  3523     QVERIFY(container[0].checkAligned());
       
  3524 
       
  3525     for (int i = 0; i < 200; ++i)
       
  3526         container.append(Aligned());
       
  3527     
       
  3528     for (int i = 0; i < container.size(); ++i)
       
  3529         QVERIFY(container.at(i).checkAligned());
       
  3530 }
       
  3531 
       
  3532 template<typename C>
       
  3533 void testContiguousCacheAlignment()
       
  3534 {
       
  3535     typedef typename C::value_type Aligned;
       
  3536     C container(150);
       
  3537     container.append(Aligned());
       
  3538     QVERIFY(container[container.firstIndex()].checkAligned());
       
  3539 
       
  3540     for (int i = 0; i < 200; ++i)
       
  3541         container.append(Aligned());
       
  3542 
       
  3543     for (int i = container.firstIndex(); i < container.lastIndex(); ++i)
       
  3544         QVERIFY(container.at(i).checkAligned());
       
  3545 }
       
  3546 
       
  3547 template<typename C>
       
  3548 void testAssociativeContainerAlignment()
       
  3549 {
       
  3550     typedef typename C::key_type Key;
       
  3551     typedef typename C::mapped_type Value;
       
  3552     C container;
       
  3553     container.insert(Key(), Value());
       
  3554 
       
  3555     typename C::const_iterator it = container.constBegin();
       
  3556     QVERIFY(it.key().checkAligned());
       
  3557     QVERIFY(it.value().checkAligned());
       
  3558 
       
  3559     // add some more elements
       
  3560     for (int i = 0; i < 200; ++i)
       
  3561         container.insert(Key(i), Value(i));
       
  3562 
       
  3563     it = container.constBegin();
       
  3564     for ( ; it != container.constEnd(); ++it) {
       
  3565         QVERIFY(it.key().checkAligned());
       
  3566         QVERIFY(it.value().checkAligned());
       
  3567     }
       
  3568 }
       
  3569 
       
  3570 void tst_Collections::alignment()
       
  3571 {
       
  3572     testVectorAlignment<QVector<Aligned4> >();
       
  3573     testVectorAlignment<QVector<Aligned128> >();
       
  3574     testContiguousCacheAlignment<QContiguousCache<Aligned4> >();
       
  3575     testContiguousCacheAlignment<QContiguousCache<Aligned128> >();
       
  3576     testAssociativeContainerAlignment<QMap<Aligned4, Aligned4> >();
       
  3577     testAssociativeContainerAlignment<QMap<Aligned4, Aligned128> >();
       
  3578     testAssociativeContainerAlignment<QMap<Aligned128, Aligned4> >();
       
  3579     testAssociativeContainerAlignment<QMap<Aligned128, Aligned128> >();
       
  3580     testAssociativeContainerAlignment<QHash<Aligned4, Aligned4> >();
       
  3581     testAssociativeContainerAlignment<QHash<Aligned4, Aligned128> >();
       
  3582     testAssociativeContainerAlignment<QHash<Aligned128, Aligned4> >();
       
  3583     testAssociativeContainerAlignment<QHash<Aligned128, Aligned128> >();
       
  3584 }
       
  3585 
       
  3586 #else
       
  3587 void tst_Collections::alignment()
       
  3588 {
       
  3589     QSKIP("Compiler doesn't support necessary extension keywords", SkipAll);
       
  3590 }
       
  3591 #endif
       
  3592 
  3484 QTEST_APPLESS_MAIN(tst_Collections)
  3593 QTEST_APPLESS_MAIN(tst_Collections)
  3485 #include "tst_collections.moc"
  3594 #include "tst_collections.moc"