Commit 321015d4fed67106417e8020fe745051bcbede31
1 parent
497bc9eb
Workaround Qt's COMPARE warnings
Showing
1 changed file
with
45 additions
and
29 deletions
FlashMQTests/tst_maintests.cpp
| ... | ... | @@ -10,6 +10,22 @@ |
| 10 | 10 | #include "mainappthread.h" |
| 11 | 11 | #include "twoclienttestcontext.h" |
| 12 | 12 | |
| 13 | +// Dumb Qt version gives warnings when comparing uint with number literal. | |
| 14 | +template <typename T1, typename T2> | |
| 15 | +inline bool myCastCompare(const T1 &t1, const T2 &t2, const char *actual, const char *expected, | |
| 16 | + const char *file, int line) | |
| 17 | +{ | |
| 18 | + T1 t2_ = static_cast<T1>(t2); | |
| 19 | + return QTest::compare_helper(t1 == t2_, "Compared values are not the same", | |
| 20 | + QTest::toString(t1), QTest::toString(t2), actual, expected, file, line); | |
| 21 | +} | |
| 22 | + | |
| 23 | +#define MYCASTCOMPARE(actual, expected) \ | |
| 24 | +do {\ | |
| 25 | + if (!myCastCompare(actual, expected, #actual, #expected, __FILE__, __LINE__))\ | |
| 26 | + return;\ | |
| 27 | +} while (false) | |
| 28 | + | |
| 13 | 29 | class MainTests : public QObject |
| 14 | 30 | { |
| 15 | 31 | Q_OBJECT |
| ... | ... | @@ -57,12 +73,12 @@ void MainTests::test_circbuf() |
| 57 | 73 | { |
| 58 | 74 | CirBuf buf(64); |
| 59 | 75 | |
| 60 | - QCOMPARE(buf.freeSpace(), 63); | |
| 76 | + MYCASTCOMPARE(buf.freeSpace(), 63); | |
| 61 | 77 | |
| 62 | - int write_n = 40; | |
| 78 | + uint write_n = 40; | |
| 63 | 79 | |
| 64 | 80 | char *head = buf.headPtr(); |
| 65 | - for (int i = 0; i < write_n; i++) | |
| 81 | + for (uint i = 0; i < write_n; i++) | |
| 66 | 82 | { |
| 67 | 83 | head[i] = i+1; |
| 68 | 84 | } |
| ... | ... | @@ -70,40 +86,40 @@ void MainTests::test_circbuf() |
| 70 | 86 | buf.advanceHead(write_n); |
| 71 | 87 | |
| 72 | 88 | QCOMPARE(buf.head, write_n); |
| 73 | - QCOMPARE(buf.tail, 0); | |
| 89 | + MYCASTCOMPARE(buf.tail, 0); | |
| 74 | 90 | QCOMPARE(buf.maxReadSize(), write_n); |
| 75 | 91 | QCOMPARE(buf.maxWriteSize(), (64 - write_n - 1)); |
| 76 | 92 | QCOMPARE(buf.freeSpace(), 64 - write_n - 1); |
| 77 | 93 | |
| 78 | - for (int i = 0; i < write_n; i++) | |
| 94 | + for (uint i = 0; i < write_n; i++) | |
| 79 | 95 | { |
| 80 | - QCOMPARE(buf.tailPtr()[i], i+1); | |
| 96 | + MYCASTCOMPARE(buf.tailPtr()[i], i+1); | |
| 81 | 97 | } |
| 82 | 98 | |
| 83 | 99 | buf.advanceTail(write_n); |
| 84 | 100 | QVERIFY(buf.tail == buf.head); |
| 85 | 101 | QCOMPARE(buf.tail, write_n); |
| 86 | - QCOMPARE(buf.maxReadSize(), 0); | |
| 102 | + MYCASTCOMPARE(buf.maxReadSize(), 0); | |
| 87 | 103 | QCOMPARE(buf.maxWriteSize(), (64 - write_n)); // no longer -1, because the head can point to 0 afterwards |
| 88 | - QCOMPARE(buf.freeSpace(), 63); | |
| 104 | + MYCASTCOMPARE(buf.freeSpace(), 63); | |
| 89 | 105 | |
| 90 | 106 | write_n = buf.maxWriteSize(); |
| 91 | 107 | |
| 92 | 108 | head = buf.headPtr(); |
| 93 | - for (int i = 0; i < write_n; i++) | |
| 109 | + for (uint i = 0; i < write_n; i++) | |
| 94 | 110 | { |
| 95 | 111 | head[i] = i+1; |
| 96 | 112 | } |
| 97 | 113 | buf.advanceHead(write_n); |
| 98 | 114 | |
| 99 | - QCOMPARE(buf.head, 0); | |
| 115 | + MYCASTCOMPARE(buf.head, 0); | |
| 100 | 116 | |
| 101 | 117 | // Now write more, starting at the beginning. |
| 102 | 118 | |
| 103 | 119 | write_n = buf.maxWriteSize(); |
| 104 | 120 | |
| 105 | 121 | head = buf.headPtr(); |
| 106 | - for (int i = 0; i < write_n; i++) | |
| 122 | + for (uint i = 0; i < write_n; i++) | |
| 107 | 123 | { |
| 108 | 124 | head[i] = i+100; // Offset by 100 so we can see if we overwrite the tail |
| 109 | 125 | } |
| ... | ... | @@ -136,7 +152,7 @@ void MainTests::test_circbuf_unwrapped_doubling() |
| 136 | 152 | } |
| 137 | 153 | QCOMPARE(buf.buf[64], 0); // Vacant place, because of the circulerness. |
| 138 | 154 | |
| 139 | - QCOMPARE(buf.head, 63); | |
| 155 | + MYCASTCOMPARE(buf.head, 63); | |
| 140 | 156 | |
| 141 | 157 | buf.doubleSize(); |
| 142 | 158 | tail = buf.tailPtr(); |
| ... | ... | @@ -151,10 +167,10 @@ void MainTests::test_circbuf_unwrapped_doubling() |
| 151 | 167 | QCOMPARE(tail[i], 5); |
| 152 | 168 | } |
| 153 | 169 | |
| 154 | - QCOMPARE(buf.tail, 0); | |
| 155 | - QCOMPARE(buf.head, 63); | |
| 156 | - QCOMPARE(buf.maxWriteSize(), 64); | |
| 157 | - QCOMPARE(buf.maxReadSize(), 63); | |
| 170 | + MYCASTCOMPARE(buf.tail, 0); | |
| 171 | + MYCASTCOMPARE(buf.head, 63); | |
| 172 | + MYCASTCOMPARE(buf.maxWriteSize(), 64); | |
| 173 | + MYCASTCOMPARE(buf.maxReadSize(), 63); | |
| 158 | 174 | } |
| 159 | 175 | |
| 160 | 176 | void MainTests::test_circbuf_wrapped_doubling() |
| ... | ... | @@ -170,14 +186,14 @@ void MainTests::test_circbuf_wrapped_doubling() |
| 170 | 186 | } |
| 171 | 187 | buf.advanceHead(w); |
| 172 | 188 | |
| 173 | - QCOMPARE(buf.tail, 0); | |
| 174 | - QCOMPARE(buf.head, w); | |
| 175 | - QCOMPARE(buf.maxReadSize(), 40); | |
| 176 | - QCOMPARE(buf.maxWriteSize(), 23); | |
| 189 | + MYCASTCOMPARE(buf.tail, 0); | |
| 190 | + MYCASTCOMPARE(buf.head, w); | |
| 191 | + MYCASTCOMPARE(buf.maxReadSize(), 40); | |
| 192 | + MYCASTCOMPARE(buf.maxWriteSize(), 23); | |
| 177 | 193 | |
| 178 | 194 | buf.advanceTail(40); |
| 179 | 195 | |
| 180 | - QCOMPARE(buf.maxWriteSize(), 24); | |
| 196 | + MYCASTCOMPARE(buf.maxWriteSize(), 24); | |
| 181 | 197 | |
| 182 | 198 | head = buf.headPtr(); |
| 183 | 199 | for (int i = 0; i < 24; i++) |
| ... | ... | @@ -186,10 +202,10 @@ void MainTests::test_circbuf_wrapped_doubling() |
| 186 | 202 | } |
| 187 | 203 | buf.advanceHead(24); |
| 188 | 204 | |
| 189 | - QCOMPARE(buf.tail, 40); | |
| 190 | - QCOMPARE(buf.head, 0); | |
| 191 | - QCOMPARE(buf.maxReadSize(), 24); | |
| 192 | - QCOMPARE(buf.maxWriteSize(), 39); | |
| 205 | + MYCASTCOMPARE(buf.tail, 40); | |
| 206 | + MYCASTCOMPARE(buf.head, 0); | |
| 207 | + MYCASTCOMPARE(buf.maxReadSize(), 24); | |
| 208 | + MYCASTCOMPARE(buf.maxWriteSize(), 39); | |
| 193 | 209 | |
| 194 | 210 | // Now write a little more, which starts at the start |
| 195 | 211 | |
| ... | ... | @@ -199,18 +215,18 @@ void MainTests::test_circbuf_wrapped_doubling() |
| 199 | 215 | head[i] = 88; |
| 200 | 216 | } |
| 201 | 217 | buf.advanceHead(10); |
| 202 | - QCOMPARE(buf.head, 10); | |
| 218 | + MYCASTCOMPARE(buf.head, 10); | |
| 203 | 219 | |
| 204 | 220 | buf.doubleSize(); |
| 205 | 221 | |
| 206 | 222 | // The 88's that were appended at the start, should now appear at the end; |
| 207 | 223 | for (int i = 64; i < 74; i++) |
| 208 | 224 | { |
| 209 | - QCOMPARE(buf.buf[i], 88); | |
| 225 | + MYCASTCOMPARE(buf.buf[i], 88); | |
| 210 | 226 | } |
| 211 | 227 | |
| 212 | - QCOMPARE(buf.tail, 40); | |
| 213 | - QCOMPARE(buf.head, 74); | |
| 228 | + MYCASTCOMPARE(buf.tail, 40); | |
| 229 | + MYCASTCOMPARE(buf.head, 74); | |
| 214 | 230 | } |
| 215 | 231 | |
| 216 | 232 | void MainTests::test_circbuf_full_wrapped_buffer_doubling() | ... | ... |