Changeset View
Changeset View
Standalone View
Standalone View
autotests/krandomtest.cpp
Show First 20 Lines • Show All 77 Lines • ▼ Show 20 Line(s) | |||||
78 | 78 | | |||
79 | class KRandomTest : public QObject | 79 | class KRandomTest : public QObject | ||
80 | { | 80 | { | ||
81 | Q_OBJECT | 81 | Q_OBJECT | ||
82 | 82 | | |||
83 | private Q_SLOTS: | 83 | private Q_SLOTS: | ||
84 | void test_random(); | 84 | void test_random(); | ||
85 | void test_randomString(); | 85 | void test_randomString(); | ||
86 | void test_randomStringThreaded(); | ||||
86 | void test_KRS(); | 87 | void test_KRS(); | ||
87 | }; | 88 | }; | ||
88 | 89 | | |||
89 | void KRandomTest::test_random() | 90 | void KRandomTest::test_random() | ||
90 | { | 91 | { | ||
91 | int testValue = KRandom::random(); | 92 | int testValue = KRandom::random(); | ||
92 | 93 | | |||
93 | QVERIFY(testValue >= 0); | 94 | QVERIFY(testValue >= 0); | ||
▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Line(s) | 118 | { | |||
145 | 146 | | |||
146 | generate(out1.begin(), out1.end(), [&]() { return krs1.getInt(maxInt); }); | 147 | generate(out1.begin(), out1.end(), [&]() { return krs1.getInt(maxInt); }); | ||
147 | generate(out2.begin(), out2.end(), [&]() { return krs2.getInt(maxInt); }); | 148 | generate(out2.begin(), out2.end(), [&]() { return krs2.getInt(maxInt); }); | ||
148 | QVERIFY(seqsAreEqual(out1, out2)); | 149 | QVERIFY(seqsAreEqual(out1, out2)); | ||
149 | QVERIFY(all_of(out1.begin(), out1.end(), [&](int x) { return x < maxInt; })); | 150 | QVERIFY(all_of(out1.begin(), out1.end(), [&](int x) { return x < maxInt; })); | ||
150 | QVERIFY(all_of(out2.begin(), out2.end(), [&](int x) { return x < maxInt; })); | 151 | QVERIFY(all_of(out2.begin(), out2.end(), [&](int x) { return x < maxInt; })); | ||
151 | } | 152 | } | ||
152 | 153 | | |||
154 | class KRandomTestThread : public QThread | ||||
155 | { | ||||
156 | protected: | ||||
157 | void run() override | ||||
158 | { | ||||
159 | result = KRandom::randomString(32); | ||||
160 | }; | ||||
161 | public: | ||||
162 | QString result; | ||||
163 | }; | ||||
164 | | ||||
165 | void KRandomTest::test_randomStringThreaded() | ||||
166 | { | ||||
167 | const int size = 5; | ||||
168 | KRandomTestThread* threads[size]; | ||||
dfaure: variable size array, which is not in the standard.
https://stackoverflow. | |||||
aacid: They are in C++11, no? | |||||
Sorry. Should have waited longer for your comment. I thought "const int" was const... Will fix in a minute. tfry: Sorry. Should have waited longer for your comment.
I thought "const int" was const... Will fix… | |||||
169 | for (int i=0; i < size; ++i) { | ||||
170 | threads[i] = new KRandomTestThread(); | ||||
171 | threads[i]->start(); | ||||
172 | } | ||||
173 | QSet<QString> results; | ||||
174 | for (int i=0; i < size; ++i) { | ||||
175 | threads[i]->wait(2000); | ||||
176 | results.insert(threads[i]->result); | ||||
177 | } | ||||
178 | // each thread should have returned a unique result | ||||
The unittest doesn't check that at all. I guess it can't, reliably, but then what is the comment for? dfaure: The unittest doesn't check that at all. I guess it can't, reliably, but then what is the… | |||||
If the results are not unique, some of the results will already be in the set, and so results.insert() does not increase the size of the set. Only if each thread result is unique, the set size will correspond to the number of threads. tfry: If the results are not unique, some of the results will already be in the set, and so results. | |||||
dfaure: Oh I see, you're right. | |||||
179 | QVERIFY(results.size() == size); | ||||
dfaure: QCOMPARE | |||||
180 | for (int i=0; i < size; ++i) { | ||||
181 | delete threads[i]; | ||||
182 | } | ||||
183 | } | ||||
184 | | ||||
153 | // Used by getChildRandSeq... outputs random numbers to stdout and then | 185 | // Used by getChildRandSeq... outputs random numbers to stdout and then | ||
154 | // exits the process. | 186 | // exits the process. | ||
155 | static void childGenRandom(int count) | 187 | static void childGenRandom(int count) | ||
156 | { | 188 | { | ||
157 | // No logic to 300, just wanted to avoid it accidentally being 2.4B... | 189 | // No logic to 300, just wanted to avoid it accidentally being 2.4B... | ||
158 | if (count <= 0 || count > 300) { | 190 | if (count <= 0 || count > 300) { | ||
159 | exit(-1); | 191 | exit(-1); | ||
160 | } | 192 | } | ||
Show All 24 Lines |
variable size array, which is not in the standard.
https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard
Make size static to fix it.