Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // qmake -project QT="core testlib" CONFIG+=release
- #include <QtCore>
- #include <QtTest>
- class tst_Likely : public QObject
- {
- Q_OBJECT
- private slots:
- void test_normal();
- void test_unlikely();
- void test_likely();
- };
- #define test_(name, expr) \
- void tst_Likely::test_##name() { \
- int sum = 0; \
- QBENCHMARK { \
- for (int i = 0; i < 0x7fffffff; i++) { \
- if (expr) \
- sum++; \
- else \
- sum--; \
- } \
- } \
- if (sum < 0) \
- qDebug() << "unlikely :("; \
- else \
- qDebug() << "likely :)"; \
- }
- test_(normal, i % 100000 == 0)
- test_(unlikely, Q_UNLIKELY(i % 100000 == 0))
- test_(likely, Q_LIKELY(i % 100000 == 0))
- #undef test_
- QTEST_MAIN(tst_Likely)
- #include "main.moc"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement