1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29
30#include <QtTest/QtTest>
31
32#include <qbytearraymatcher.h>
33
34// COM interface
35#if defined(Q_OS_WIN) && defined(interface)
36# undef interface
37#endif
38
39class tst_QByteArrayMatcher : public QObject
40{
41 Q_OBJECT
42
43private slots:
44 void interface();
45 void indexIn();
46 void staticByteArrayMatcher();
47};
48
49void tst_QByteArrayMatcher::interface()
50{
51 const char needle[] = "abc123";
52 QByteArray haystack(500, 'a');
53 haystack.insert(i: 6, s: "123");
54 haystack.insert(i: 31, s: "abc");
55 haystack.insert(i: 42, s: "abc123");
56 haystack.insert(i: 84, s: "abc123");
57
58 QByteArrayMatcher matcher1;
59
60 matcher1 = QByteArrayMatcher(QByteArray(needle));
61 QByteArrayMatcher matcher2;
62 matcher2.setPattern(QByteArray(needle));
63
64 QByteArrayMatcher matcher3 = QByteArrayMatcher(QByteArray(needle));
65 QByteArrayMatcher matcher4(needle, sizeof(needle) - 1);
66 QByteArrayMatcher matcher5(matcher2);
67 QByteArrayMatcher matcher6;
68 matcher6 = matcher3;
69
70 QCOMPARE(matcher1.indexIn(haystack), 42);
71 QCOMPARE(matcher2.indexIn(haystack), 42);
72 QCOMPARE(matcher3.indexIn(haystack), 42);
73 QCOMPARE(matcher4.indexIn(haystack), 42);
74 QCOMPARE(matcher5.indexIn(haystack), 42);
75 QCOMPARE(matcher6.indexIn(haystack), 42);
76
77 QCOMPARE(matcher1.indexIn(haystack.constData(), haystack.length()), 42);
78
79 QCOMPARE(matcher1.indexIn(haystack, 43), 84);
80 QCOMPARE(matcher1.indexIn(haystack.constData(), haystack.length(), 43), 84);
81 QCOMPARE(matcher1.indexIn(haystack, 85), -1);
82 QCOMPARE(matcher1.indexIn(haystack.constData(), haystack.length(), 85), -1);
83
84 QByteArrayMatcher matcher7(QByteArray("123"));
85 QCOMPARE(matcher7.indexIn(haystack), 6);
86
87 matcher7 = QByteArrayMatcher(QByteArray("abc"));
88 QCOMPARE(matcher7.indexIn(haystack), 31);
89
90 matcher7.setPattern(matcher4.pattern());
91 QCOMPARE(matcher7.indexIn(haystack), 42);
92}
93
94#define LONG_STRING__32 "abcdefghijklmnopqrstuvwxyz012345"
95#define LONG_STRING__64 LONG_STRING__32 LONG_STRING__32
96#define LONG_STRING_128 LONG_STRING__64 LONG_STRING__64
97#define LONG_STRING_256 LONG_STRING_128 LONG_STRING_128
98
99void tst_QByteArrayMatcher::indexIn()
100{
101 const char p_data[] = { 0x0, 0x0, 0x1 };
102 QByteArray pattern(p_data, sizeof(p_data));
103
104 QByteArray haystack(8, '\0');
105 haystack[7] = 0x1;
106
107 QByteArrayMatcher matcher;
108
109 matcher = QByteArrayMatcher(pattern);
110 QCOMPARE(matcher.indexIn(haystack, 0), 5);
111 QCOMPARE(matcher.indexIn(haystack, 1), 5);
112 QCOMPARE(matcher.indexIn(haystack, 2), 5);
113
114 matcher.setPattern(pattern);
115 QCOMPARE(matcher.indexIn(haystack, 0), 5);
116 QCOMPARE(matcher.indexIn(haystack, 1), 5);
117 QCOMPARE(matcher.indexIn(haystack, 2), 5);
118
119 QByteArray allChars(256, Qt::Uninitialized);
120 for (int i = 0; i < 256; ++i)
121 allChars[i] = char(i);
122
123 matcher = QByteArrayMatcher(allChars);
124 haystack = LONG_STRING__32 "x" + matcher.pattern();
125 QCOMPARE(matcher.indexIn(haystack, 0), 33);
126 QCOMPARE(matcher.indexIn(haystack, 1), 33);
127 QCOMPARE(matcher.indexIn(haystack, 2), 33);
128 QCOMPARE(matcher.indexIn(haystack, 33), 33);
129 QCOMPARE(matcher.indexIn(haystack, 34), -1);
130
131 matcher = QByteArrayMatcher(LONG_STRING_256);
132 haystack = LONG_STRING__32 "x" + matcher.pattern();
133 QCOMPARE(matcher.indexIn(haystack, 0), 33);
134 QCOMPARE(matcher.indexIn(haystack, 1), 33);
135 QCOMPARE(matcher.indexIn(haystack, 2), 33);
136 QCOMPARE(matcher.indexIn(haystack, 33), 33);
137 QCOMPARE(matcher.indexIn(haystack, 34), -1);
138}
139
140void tst_QByteArrayMatcher::staticByteArrayMatcher()
141{
142 {
143 static Q_RELAXED_CONSTEXPR auto smatcher = qMakeStaticByteArrayMatcher(pattern: "Hello");
144 QCOMPARE(smatcher.pattern(), QByteArrayLiteral("Hello"));
145
146 QCOMPARE(smatcher.indexIn(QByteArray("Hello, World!")), 0);
147 QCOMPARE(smatcher.indexIn(QByteArray("Hello, World!"), 0), 0);
148 QCOMPARE(smatcher.indexIn(QByteArray("Hello, World!"), 1), -1);
149 QCOMPARE(smatcher.indexIn(QByteArray("aHello, World!")), 1);
150 QCOMPARE(smatcher.indexIn(QByteArray("aaHello, World!")), 2);
151 QCOMPARE(smatcher.indexIn(QByteArray("aaaHello, World!")), 3);
152 QCOMPARE(smatcher.indexIn(QByteArray("aaaaHello, World!")), 4);
153 QCOMPARE(smatcher.indexIn(QByteArray("aaaaaHello, World!")), 5);
154 QCOMPARE(smatcher.indexIn(QByteArray("aaaaaaHello, World!")), 6);
155 QCOMPARE(smatcher.indexIn(QByteArray("HHello, World!")), 1);
156 QCOMPARE(smatcher.indexIn(QByteArray("HeHello, World!")), 2);
157 QCOMPARE(smatcher.indexIn(QByteArray("HelHello, World!")), 3);
158 QCOMPARE(smatcher.indexIn(QByteArray("HellHello, World!")), 4);
159 QCOMPARE(smatcher.indexIn(QByteArray("HellaHello, World!")), 5);
160 QCOMPARE(smatcher.indexIn(QByteArray("HellauHello, World!")), 6);
161 QCOMPARE(smatcher.indexIn(QByteArray("aHella, World!")), -1);
162 QCOMPARE(smatcher.indexIn(QByteArray("aaHella, World!")), -1);
163 QCOMPARE(smatcher.indexIn(QByteArray("aaaHella, World!")), -1);
164 QCOMPARE(smatcher.indexIn(QByteArray("aaaaHella, World!")), -1);
165 QCOMPARE(smatcher.indexIn(QByteArray("aaaaaHella, World!")), -1);
166 QCOMPARE(smatcher.indexIn(QByteArray("aaaaaaHella, World!")), -1);
167
168 QCOMPARE(smatcher.indexIn(QByteArray("aHello")), 1);
169 QCOMPARE(smatcher.indexIn(QByteArray("aaHello")), 2);
170 QCOMPARE(smatcher.indexIn(QByteArray("aaaHello")), 3);
171 QCOMPARE(smatcher.indexIn(QByteArray("aaaaHello")), 4);
172 QCOMPARE(smatcher.indexIn(QByteArray("aaaaaHello")), 5);
173 QCOMPARE(smatcher.indexIn(QByteArray("aaaaaaHello")), 6);
174 QCOMPARE(smatcher.indexIn(QByteArray("HHello")), 1);
175 QCOMPARE(smatcher.indexIn(QByteArray("HeHello")), 2);
176 QCOMPARE(smatcher.indexIn(QByteArray("HelHello")), 3);
177 QCOMPARE(smatcher.indexIn(QByteArray("HellHello")), 4);
178 QCOMPARE(smatcher.indexIn(QByteArray("HellaHello")), 5);
179 QCOMPARE(smatcher.indexIn(QByteArray("HellauHello")), 6);
180 QCOMPARE(smatcher.indexIn(QByteArray("aHella")), -1);
181 QCOMPARE(smatcher.indexIn(QByteArray("aaHella")), -1);
182 QCOMPARE(smatcher.indexIn(QByteArray("aaaHella")), -1);
183 QCOMPARE(smatcher.indexIn(QByteArray("aaaaHella")), -1);
184 QCOMPARE(smatcher.indexIn(QByteArray("aaaaaHella")), -1);
185 QCOMPARE(smatcher.indexIn(QByteArray("aaaaaaHella")), -1);
186 }
187
188 {
189 static Q_RELAXED_CONSTEXPR auto smatcher = qMakeStaticByteArrayMatcher(LONG_STRING_256);
190 QCOMPARE(smatcher.pattern(), QByteArrayLiteral(LONG_STRING_256));
191
192 QCOMPARE(smatcher.indexIn(QByteArray("a" LONG_STRING_256)), 1);
193 QCOMPARE(smatcher.indexIn(QByteArray("aa" LONG_STRING_256)), 2);
194 QCOMPARE(smatcher.indexIn(QByteArray("aaa" LONG_STRING_256)), 3);
195 QCOMPARE(smatcher.indexIn(QByteArray("aaaa" LONG_STRING_256)), 4);
196 QCOMPARE(smatcher.indexIn(QByteArray("aaaaa" LONG_STRING_256)), 5);
197 QCOMPARE(smatcher.indexIn(QByteArray("aaaaaa" LONG_STRING_256)), 6);
198 QCOMPARE(smatcher.indexIn(QByteArray("a" LONG_STRING_256 "a")), 1);
199 QCOMPARE(smatcher.indexIn(QByteArray("aa" LONG_STRING_256 "a")), 2);
200 QCOMPARE(smatcher.indexIn(QByteArray("aaa" LONG_STRING_256 "a")), 3);
201 QCOMPARE(smatcher.indexIn(QByteArray("aaaa" LONG_STRING_256 "a")), 4);
202 QCOMPARE(smatcher.indexIn(QByteArray("aaaaa" LONG_STRING_256 "a")), 5);
203 QCOMPARE(smatcher.indexIn(QByteArray("aaaaaa" LONG_STRING_256 "a")), 6);
204 QCOMPARE(smatcher.indexIn(QByteArray(LONG_STRING__32 "x" LONG_STRING_256)), 33);
205 QCOMPARE(smatcher.indexIn(QByteArray(LONG_STRING__64 "x" LONG_STRING_256)), 65);
206 QCOMPARE(smatcher.indexIn(QByteArray(LONG_STRING_128 "x" LONG_STRING_256)), 129);
207 }
208
209}
210
211#undef LONG_STRING_256
212#undef LONG_STRING_128
213#undef LONG_STRING__64
214#undef LONG_STRING__32
215
216QTEST_APPLESS_MAIN(tst_QByteArrayMatcher)
217#include "tst_qbytearraymatcher.moc"
218

source code of qtbase/tests/auto/corelib/text/qbytearraymatcher/tst_qbytearraymatcher.cpp