| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
|
| 5 |
#include <QTest> |
| 6 |
|
| 7 |
#include <qbytearraymatcher.h> |
| 8 |
#include <qelapsedtimer.h> |
| 9 |
|
| 10 |
#include <numeric> |
| 11 |
#include <string> |
| 12 |
|
| 13 |
#include <thread> |
| 14 |
|
| 15 |
// COM interface |
| 16 |
#if defined(Q_OS_WIN) && defined(interface) |
| 17 |
# undef interface |
| 18 |
#endif |
| 19 |
|
| 20 |
class tst_QByteArrayMatcher : public QObject |
| 21 |
{ |
| 22 |
Q_OBJECT |
| 23 |
|
| 24 |
private slots: |
| 25 |
void overloads(); |
| 26 |
void interface(); |
| 27 |
void indexIn(); |
| 28 |
void staticByteArrayMatcher(); |
| 29 |
void haystacksWithMoreThan4GiBWork(); |
| 30 |
}; |
| 31 |
|
| 32 |
void tst_QByteArrayMatcher::overloads() |
| 33 |
{ |
| 34 |
QByteArray hello = QByteArrayLiteral("hello"); |
| 35 |
QByteArray hello2 = hello.repeated(2); |
| 36 |
{ |
| 37 |
QByteArrayMatcher m("hello"); |
| 38 |
QCOMPARE(m.pattern(), "hello"); |
| 39 |
QCOMPARE(m.indexIn("hello"), 0); |
| 40 |
} |
| 41 |
{ |
| 42 |
QByteArrayMatcher m("hello", qsizetype(3)); |
| 43 |
QCOMPARE(m.pattern(), "hel"); |
| 44 |
QCOMPARE(m.indexIn("hellohello", qsizetype(2)), -1); // haystack is "he", not: from is 2 |
| 45 |
QCOMPARE(m.indexIn("hellohello", qsizetype(3)), 0); // haystack is "hel", not: from is 3 |
| 46 |
} |
| 47 |
{ |
| 48 |
QByteArrayMatcher m(hello); |
| 49 |
QCOMPARE(m.pattern(), "hello"); |
| 50 |
QCOMPARE(m.indexIn(hello), 0); |
| 51 |
QCOMPARE(m.indexIn(hello2, qsizetype(1)), hello.size()); |
| 52 |
} |
| 53 |
{ |
| 54 |
QStaticByteArrayMatcher m("hel"); |
| 55 |
QCOMPARE(m.pattern(), "hel"); |
| 56 |
QCOMPARE(m.indexIn("hello"), qsizetype(0)); |
| 57 |
QCOMPARE(m.indexIn("hellohello", qsizetype(2)), -1); // haystack is "he", not: from is 2 |
| 58 |
QCOMPARE(m.indexIn("hellohello", qsizetype(3)), 0); // haystack is "hel", not: from is 3 |
| 59 |
QCOMPARE(m.indexIn(hello), 0); |
| 60 |
QCOMPARE(m.indexIn(hello2, qsizetype(2)), hello.size()); // from is 2 |
| 61 |
QCOMPARE(m.indexIn(hello2, qsizetype(3)), hello.size()); // from is 3 |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
void tst_QByteArrayMatcher::interface() |
| 66 |
{ |
| 67 |
const char needle[] = "abc123"; |
| 68 |
QByteArray haystack(500, 'a'); |
| 69 |
haystack.insert(6, "123"); |
| 70 |
haystack.insert(31, "abc"); |
| 71 |
haystack.insert(42, "abc123"); |
| 72 |
haystack.insert(84, "abc123"); |
| 73 |
|
| 74 |
QByteArrayMatcher matcher1; |
| 75 |
|
| 76 |
matcher1 = QByteArrayMatcher(QByteArray(needle)); |
| 77 |
QByteArrayMatcher matcher2; |
| 78 |
matcher2.setPattern(QByteArray(needle)); |
| 79 |
|
| 80 |
QByteArrayMatcher matcher3 = QByteArrayMatcher(QByteArray(needle)); |
| 81 |
QByteArrayMatcher matcher4(needle, sizeof(needle) - 1); |
| 82 |
QByteArrayMatcher matcher5(matcher2); |
| 83 |
QByteArrayMatcher matcher6; |
| 84 |
matcher6 = matcher3; |
| 85 |
|
| 86 |
QCOMPARE(matcher1.indexIn(haystack), 42); |
| 87 |
QCOMPARE(matcher2.indexIn(haystack), 42); |
| 88 |
QCOMPARE(matcher3.indexIn(haystack), 42); |
| 89 |
QCOMPARE(matcher4.indexIn(haystack), 42); |
| 90 |
QCOMPARE(matcher5.indexIn(haystack), 42); |
| 91 |
QCOMPARE(matcher6.indexIn(haystack), 42); |
| 92 |
|
| 93 |
QCOMPARE(matcher1.indexIn(haystack.constData(), haystack.size()), 42); |
| 94 |
|
| 95 |
QCOMPARE(matcher1.indexIn(haystack, 43), 84); |
| 96 |
QCOMPARE(matcher1.indexIn(haystack.constData(), haystack.size(), 43), 84); |
| 97 |
QCOMPARE(matcher1.indexIn(haystack, 85), -1); |
| 98 |
QCOMPARE(matcher1.indexIn(haystack.constData(), haystack.size(), 85), -1); |
| 99 |
|
| 100 |
QByteArrayMatcher matcher7(QByteArray("123")); |
| 101 |
QCOMPARE(matcher7.indexIn(haystack), 6); |
| 102 |
|
| 103 |
matcher7 = QByteArrayMatcher(QByteArray("abc")); |
| 104 |
QCOMPARE(matcher7.indexIn(haystack), 31); |
| 105 |
|
| 106 |
matcher7.setPattern(matcher4.pattern()); |
| 107 |
QCOMPARE(matcher7.indexIn(haystack), 42); |
| 108 |
} |
| 109 |
|
| 110 |
#define LONG_STRING__32 "abcdefghijklmnopqrstuvwxyz012345" |
| 111 |
#define LONG_STRING__64 LONG_STRING__32 LONG_STRING__32 |
| 112 |
#define LONG_STRING_128 LONG_STRING__64 LONG_STRING__64 |
| 113 |
#define LONG_STRING_256 LONG_STRING_128 LONG_STRING_128 |
| 114 |
|
| 115 |
void tst_QByteArrayMatcher::indexIn() |
| 116 |
{ |
| 117 |
const char p_data[] = { 0x0, 0x0, 0x1 }; |
| 118 |
QByteArray pattern(p_data, sizeof(p_data)); |
| 119 |
|
| 120 |
QByteArray haystack(8, '\0'); |
| 121 |
haystack[7] = 0x1; |
| 122 |
|
| 123 |
QByteArrayMatcher matcher; |
| 124 |
|
| 125 |
matcher = QByteArrayMatcher(pattern); |
| 126 |
QCOMPARE(matcher.indexIn(haystack, 0), 5); |
| 127 |
QCOMPARE(matcher.indexIn(haystack, 1), 5); |
| 128 |
QCOMPARE(matcher.indexIn(haystack, 2), 5); |
| 129 |
|
| 130 |
matcher.setPattern(pattern); |
| 131 |
QCOMPARE(matcher.indexIn(haystack, 0), 5); |
| 132 |
QCOMPARE(matcher.indexIn(haystack, 1), 5); |
| 133 |
QCOMPARE(matcher.indexIn(haystack, 2), 5); |
| 134 |
|
| 135 |
QByteArray allChars(256, Qt::Uninitialized); |
| 136 |
for (int i = 0; i < 256; ++i) |
| 137 |
allChars[i] = char(i); |
| 138 |
|
| 139 |
matcher = QByteArrayMatcher(allChars); |
| 140 |
haystack = LONG_STRING__32 "x" + matcher.pattern(); |
| 141 |
QCOMPARE(matcher.indexIn(haystack, 0), 33); |
| 142 |
QCOMPARE(matcher.indexIn(haystack, 1), 33); |
| 143 |
QCOMPARE(matcher.indexIn(haystack, 2), 33); |
| 144 |
QCOMPARE(matcher.indexIn(haystack, 33), 33); |
| 145 |
QCOMPARE(matcher.indexIn(haystack, 34), -1); |
| 146 |
|
| 147 |
matcher = QByteArrayMatcher(LONG_STRING_256); |
| 148 |
haystack = LONG_STRING__32 "x" + matcher.pattern(); |
| 149 |
QCOMPARE(matcher.indexIn(haystack, 0), 33); |
| 150 |
QCOMPARE(matcher.indexIn(haystack, 1), 33); |
| 151 |
QCOMPARE(matcher.indexIn(haystack, 2), 33); |
| 152 |
QCOMPARE(matcher.indexIn(haystack, 33), 33); |
| 153 |
QCOMPARE(matcher.indexIn(haystack, 34), -1); |
| 154 |
} |
| 155 |
|
| 156 |
void tst_QByteArrayMatcher::staticByteArrayMatcher() |
| 157 |
{ |
| 158 |
{ |
| 159 |
static constexpr auto smatcher = qMakeStaticByteArrayMatcher("Hello"); |
| 160 |
QCOMPARE(smatcher.pattern(), QByteArrayLiteral("Hello")); |
| 161 |
|
| 162 |
QCOMPARE(smatcher.indexIn(QByteArray("Hello, World!")), 0); |
| 163 |
QCOMPARE(smatcher.indexIn(QByteArray("Hello, World!"), 0), 0); |
| 164 |
QCOMPARE(smatcher.indexIn(QByteArray("Hello, World!"), 1), -1); |
| 165 |
QCOMPARE(smatcher.indexIn(QByteArray("aHello, World!")), 1); |
| 166 |
QCOMPARE(smatcher.indexIn(QByteArray("aaHello, World!")), 2); |
| 167 |
QCOMPARE(smatcher.indexIn(QByteArray("aaaHello, World!")), 3); |
| 168 |
QCOMPARE(smatcher.indexIn(QByteArray("aaaaHello, World!")), 4); |
| 169 |
QCOMPARE(smatcher.indexIn(QByteArray("aaaaaHello, World!")), 5); |
| 170 |
QCOMPARE(smatcher.indexIn(QByteArray("aaaaaaHello, World!")), 6); |
| 171 |
QCOMPARE(smatcher.indexIn(QByteArray("HHello, World!")), 1); |
| 172 |
QCOMPARE(smatcher.indexIn(QByteArray("HeHello, World!")), 2); |
| 173 |
QCOMPARE(smatcher.indexIn(QByteArray("HelHello, World!")), 3); |
| 174 |
QCOMPARE(smatcher.indexIn(QByteArray("HellHello, World!")), 4); |
| 175 |
QCOMPARE(smatcher.indexIn(QByteArray("HellaHello, World!")), 5); |
| 176 |
QCOMPARE(smatcher.indexIn(QByteArray("HellauHello, World!")), 6); |
| 177 |
QCOMPARE(smatcher.indexIn(QByteArray("aHella, World!")), -1); |
| 178 |
QCOMPARE(smatcher.indexIn(QByteArray("aaHella, World!")), -1); |
| 179 |
QCOMPARE(smatcher.indexIn(QByteArray("aaaHella, World!")), -1); |
| 180 |
QCOMPARE(smatcher.indexIn(QByteArray("aaaaHella, World!")), -1); |
| 181 |
QCOMPARE(smatcher.indexIn(QByteArray("aaaaaHella, World!")), -1); |
| 182 |
QCOMPARE(smatcher.indexIn(QByteArray("aaaaaaHella, World!")), -1); |
| 183 |
|
| 184 |
QCOMPARE(smatcher.indexIn(QByteArray("aHello")), 1); |
| 185 |
QCOMPARE(smatcher.indexIn(QByteArray("aaHello")), 2); |
| 186 |
QCOMPARE(smatcher.indexIn(QByteArray("aaaHello")), 3); |
| 187 |
QCOMPARE(smatcher.indexIn(QByteArray("aaaaHello")), 4); |
| 188 |
QCOMPARE(smatcher.indexIn(QByteArray("aaaaaHello")), 5); |
| 189 |
QCOMPARE(smatcher.indexIn(QByteArray("aaaaaaHello")), 6); |
| 190 |
QCOMPARE(smatcher.indexIn(QByteArray("HHello")), 1); |
| 191 |
QCOMPARE(smatcher.indexIn(QByteArray("HeHello")), 2); |
| 192 |
QCOMPARE(smatcher.indexIn(QByteArray("HelHello")), 3); |
| 193 |
QCOMPARE(smatcher.indexIn(QByteArray("HellHello")), 4); |
| 194 |
QCOMPARE(smatcher.indexIn(QByteArray("HellaHello")), 5); |
| 195 |
QCOMPARE(smatcher.indexIn(QByteArray("HellauHello")), 6); |
| 196 |
QCOMPARE(smatcher.indexIn(QByteArray("aHella")), -1); |
| 197 |
QCOMPARE(smatcher.indexIn(QByteArray("aaHella")), -1); |
| 198 |
QCOMPARE(smatcher.indexIn(QByteArray("aaaHella")), -1); |
| 199 |
QCOMPARE(smatcher.indexIn(QByteArray("aaaaHella")), -1); |
| 200 |
QCOMPARE(smatcher.indexIn(QByteArray("aaaaaHella")), -1); |
| 201 |
QCOMPARE(smatcher.indexIn(QByteArray("aaaaaaHella")), -1); |
| 202 |
} |
| 203 |
|
| 204 |
{ |
| 205 |
static constexpr auto smatcher = qMakeStaticByteArrayMatcher(LONG_STRING_256); |
| 206 |
QCOMPARE(smatcher.pattern(), QByteArrayLiteral(LONG_STRING_256)); |
| 207 |
|
| 208 |
QCOMPARE(smatcher.indexIn(QByteArray("a" LONG_STRING_256)), 1); |
| 209 |
QCOMPARE(smatcher.indexIn(QByteArray("aa" LONG_STRING_256)), 2); |
| 210 |
QCOMPARE(smatcher.indexIn(QByteArray("aaa" LONG_STRING_256)), 3); |
| 211 |
QCOMPARE(smatcher.indexIn(QByteArray("aaaa" LONG_STRING_256)), 4); |
| 212 |
QCOMPARE(smatcher.indexIn(QByteArray("aaaaa" LONG_STRING_256)), 5); |
| 213 |
QCOMPARE(smatcher.indexIn(QByteArray("aaaaaa" LONG_STRING_256)), 6); |
| 214 |
QCOMPARE(smatcher.indexIn(QByteArray("a" LONG_STRING_256 "a")), 1); |
| 215 |
QCOMPARE(smatcher.indexIn(QByteArray("aa" LONG_STRING_256 "a")), 2); |
| 216 |
QCOMPARE(smatcher.indexIn(QByteArray("aaa" LONG_STRING_256 "a")), 3); |
| 217 |
QCOMPARE(smatcher.indexIn(QByteArray("aaaa" LONG_STRING_256 "a")), 4); |
| 218 |
QCOMPARE(smatcher.indexIn(QByteArray("aaaaa" LONG_STRING_256 "a")), 5); |
| 219 |
QCOMPARE(smatcher.indexIn(QByteArray("aaaaaa" LONG_STRING_256 "a")), 6); |
| 220 |
QCOMPARE(smatcher.indexIn(QByteArray(LONG_STRING__32 "x" LONG_STRING_256)), 33); |
| 221 |
QCOMPARE(smatcher.indexIn(QByteArray(LONG_STRING__64 "x" LONG_STRING_256)), 65); |
| 222 |
QCOMPARE(smatcher.indexIn(QByteArray(LONG_STRING_128 "x" LONG_STRING_256)), 129); |
| 223 |
} |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
void tst_QByteArrayMatcher::haystacksWithMoreThan4GiBWork() |
| 228 |
{ |
| 229 |
#if QT_POINTER_SIZE > 4 |
| 230 |
// use a large needle to trigger long skips in the Boyer-Moore algorithm |
| 231 |
// (to speed up the test) |
| 232 |
constexpr std::string_view needle = LONG_STRING_256; |
| 233 |
|
| 234 |
// |
| 235 |
// GIVEN: a haystack with more than 4 GiB of data |
| 236 |
// |
| 237 |
|
| 238 |
// don't use QByteArray because freeSpaceAtEnd() may break reserve() |
| 239 |
// semantics and a realloc is the last thing we need here |
| 240 |
std::string large; |
| 241 |
QElapsedTimer timer; |
| 242 |
timer.start(); |
| 243 |
constexpr size_t GiB = 1024 * 1024 * 1024; |
| 244 |
constexpr size_t BaseSize = 4 * GiB + 1; |
| 245 |
try { |
| 246 |
large.reserve(BaseSize + needle.size()); |
| 247 |
large.resize(BaseSize, '\0'); |
| 248 |
large.append(needle); |
| 249 |
} catch (const std::bad_alloc &) { |
| 250 |
QSKIP("Could not allocate 4GiB plus a couple hundred bytes of RAM."); |
| 251 |
} |
| 252 |
QCOMPARE(large.size(), BaseSize + needle.size()); |
| 253 |
qDebug("created dataset in %lld ms", timer.elapsed()); |
| 254 |
|
| 255 |
using MaybeThread = std::thread; |
| 256 |
|
| 257 |
// |
| 258 |
// WHEN: trying to match an occurrence past the 4GiB mark |
| 259 |
// |
| 260 |
|
| 261 |
qsizetype dynamicResult, staticResult; |
| 262 |
|
| 263 |
auto t = MaybeThread{[&]{ |
| 264 |
QByteArrayMatcher m(needle); |
| 265 |
dynamicResult = m.indexIn(large); |
| 266 |
}}; |
| 267 |
{ |
| 268 |
static_assert(needle == LONG_STRING_256); // need a string literal in the following line: |
| 269 |
QStaticByteArrayMatcher m(LONG_STRING_256); |
| 270 |
staticResult = m.indexIn(large.data(), large.size()); |
| 271 |
} |
| 272 |
t.join(); |
| 273 |
|
| 274 |
// |
| 275 |
// THEN: the result index is not trucated |
| 276 |
// |
| 277 |
|
| 278 |
QCOMPARE(staticResult, qsizetype(BaseSize)); |
| 279 |
QCOMPARE(dynamicResult, qsizetype(BaseSize)); |
| 280 |
#else |
| 281 |
QSKIP("This test is 64-bit only."); |
| 282 |
#endif |
| 283 |
|
| 284 |
} |
| 285 |
|
| 286 |
#undef LONG_STRING_256 |
| 287 |
#undef LONG_STRING_128 |
| 288 |
#undef LONG_STRING__64 |
| 289 |
#undef LONG_STRING__32 |
| 290 |
|
| 291 |
QTEST_APPLESS_MAIN(tst_QByteArrayMatcher) |
| 292 |
#include "tst_qbytearraymatcher.moc" |