Advertisement
tasuku

main.cpp

Jan 10th, 2014
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // qmake -project QT="core testlib" CONFIG+=release
  2. #include <QtCore>
  3. #include <QtTest>
  4.  
  5. class tst_Likely : public QObject
  6. {
  7.     Q_OBJECT
  8.  
  9. private slots:
  10.     void test_normal();
  11.     void test_unlikely();
  12.     void test_likely();
  13. };
  14.  
  15. #define test_(name, expr) \
  16. void tst_Likely::test_##name() { \
  17.     int sum = 0; \
  18.     QBENCHMARK { \
  19.         for (int i = 0; i < 0x7fffffff; i++) { \
  20.             if (expr) \
  21.                 sum++; \
  22.             else \
  23.                 sum--; \
  24.         } \
  25.     } \
  26.     if (sum < 0) \
  27.         qDebug() << "unlikely :("; \
  28.     else \
  29.         qDebug() << "likely :)"; \
  30. }
  31.  
  32. test_(normal, i % 100000 == 0)
  33. test_(unlikely, Q_UNLIKELY(i % 100000 == 0))
  34. test_(likely, Q_LIKELY(i % 100000 == 0))
  35.  
  36. #undef test_
  37.  
  38. QTEST_MAIN(tst_Likely)
  39. #include "main.moc"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement