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 | #include <qvalidator.h> |
32 | |
33 | class tst_QIntValidator : public QObject |
34 | { |
35 | Q_OBJECT |
36 | private slots: |
37 | void validate_data(); |
38 | void validate(); |
39 | void validateArabic(); |
40 | void validateFrench(); |
41 | void notifySignals(); |
42 | }; |
43 | |
44 | Q_DECLARE_METATYPE(QValidator::State); |
45 | #define INV QValidator::Invalid |
46 | #define INT QValidator::Intermediate |
47 | #define ACC QValidator::Acceptable |
48 | |
49 | void tst_QIntValidator::validate_data() |
50 | { |
51 | QTest::addColumn<int>(name: "minimum" ); |
52 | QTest::addColumn<int>(name: "maximum" ); |
53 | QTest::addColumn<QString>(name: "value" ); |
54 | QTest::addColumn<QValidator::State>(name: "state" ); |
55 | |
56 | QTest::newRow(dataTag: "data0" ) << 0 << 100 << QString("50" ) << ACC; |
57 | QTest::newRow(dataTag: "data1" ) << 0 << 100 << QString("500" ) << INT; |
58 | QTest::newRow(dataTag: "data1a" ) << 0 << 100 << QString("5000" ) << INV; |
59 | QTest::newRow(dataTag: "data1b" ) << -100 << 0 << QString("50" ) << INT; |
60 | QTest::newRow(dataTag: "data1c" ) << -100 << 0 << QString("500" ) << INV; |
61 | QTest::newRow(dataTag: "data1d" ) << -100 << 0 << QString("5000" ) << INV; |
62 | QTest::newRow(dataTag: "data2" ) << 0 << 100 << QString("-35" ) << INV; |
63 | QTest::newRow(dataTag: "data3" ) << 0 << 100 << QString("a" ) << INV; |
64 | QTest::newRow(dataTag: "data4" ) << 0 << 100 << QString("-" ) << INV; |
65 | QTest::newRow(dataTag: "data5" ) << 0 << 100 << QString("100" ) << ACC; |
66 | QTest::newRow(dataTag: "data6" ) << -100 << 100 << QString("-" ) << INT; |
67 | QTest::newRow(dataTag: "data7" ) << -100 << 100 << QString("-500" ) << INV; |
68 | QTest::newRow(dataTag: "data8" ) << -100 << 100 << QString("-100" ) << ACC; |
69 | QTest::newRow(dataTag: "data9" ) << -100 << -10 << QString("10" ) << INT; |
70 | |
71 | QTest::newRow(dataTag: "data10" ) << 100 << 999 << QString("" ) << INT; |
72 | QTest::newRow(dataTag: "data11" ) << 100 << 999 << QString("5" ) << INT; |
73 | QTest::newRow(dataTag: "data12" ) << 100 << 999 << QString("50" ) << INT; |
74 | QTest::newRow(dataTag: "data13" ) << 100 << 999 << QString("99" ) << INT; |
75 | QTest::newRow(dataTag: "data14" ) << 100 << 999 << QString("100" ) << ACC; |
76 | QTest::newRow(dataTag: "data15" ) << 100 << 999 << QString("101" ) << ACC; |
77 | QTest::newRow(dataTag: "data16" ) << 100 << 999 << QString("998" ) << ACC; |
78 | QTest::newRow(dataTag: "data17" ) << 100 << 999 << QString("999" ) << ACC; |
79 | QTest::newRow(dataTag: "data18" ) << 100 << 999 << QString("1000" ) << INV; |
80 | QTest::newRow(dataTag: "data19" ) << 100 << 999 << QString("-10" ) << INV; |
81 | |
82 | QTest::newRow(dataTag: "data20" ) << -999 << -100 << QString("50" ) << INT; |
83 | QTest::newRow(dataTag: "data21" ) << -999 << -100 << QString("-" ) << INT; |
84 | QTest::newRow(dataTag: "data22" ) << -999 << -100 << QString("-1" ) << INT; |
85 | QTest::newRow(dataTag: "data23" ) << -999 << -100 << QString("-10" ) << INT; |
86 | QTest::newRow(dataTag: "data24" ) << -999 << -100 << QString("-100" ) << ACC; |
87 | QTest::newRow(dataTag: "data25" ) << -999 << -100 << QString("-500" ) << ACC; |
88 | QTest::newRow(dataTag: "data26" ) << -999 << -100 << QString("-998" ) << ACC; |
89 | QTest::newRow(dataTag: "data27" ) << -999 << -100 << QString("-999" ) << ACC; |
90 | QTest::newRow(dataTag: "data28" ) << -999 << -100 << QString("-1000" ) << INV; |
91 | QTest::newRow(dataTag: "data29" ) << -999 << -100 << QString("-2000" ) << INV; |
92 | |
93 | QTest::newRow(dataTag: "1.1" ) << 0 << 10 << QString("" ) << INT; |
94 | QTest::newRow(dataTag: "1.2" ) << 10 << 0 << QString("" ) << INT; |
95 | |
96 | QTest::newRow(dataTag: "2.1" ) << 0 << 10 << QString("-" ) << INV; |
97 | QTest::newRow(dataTag: "2.2" ) << 0 << 10 << QString("-0" ) << INV; |
98 | QTest::newRow(dataTag: "2.3" ) << -10 << -1 << QString("+" ) << INV; |
99 | QTest::newRow(dataTag: "2.4" ) << -10 << 10 << QString("-" ) << INT; |
100 | QTest::newRow(dataTag: "2.5" ) << -10 << 10 << QString("+" ) << INT; |
101 | QTest::newRow(dataTag: "2.6" ) << -10 << 10 << QString("+0" ) << ACC; |
102 | QTest::newRow(dataTag: "2.7" ) << -10 << 10 << QString("+1" ) << ACC; |
103 | QTest::newRow(dataTag: "2.8" ) << -10 << 10 << QString("+-" ) << INV; |
104 | QTest::newRow(dataTag: "2.9" ) << -10 << 10 << QString("-+" ) << INV; |
105 | |
106 | QTest::newRow(dataTag: "3.1" ) << 0 << 10 << QString("12345678901234567890" ) << INV; |
107 | QTest::newRow(dataTag: "3.2" ) << 0 << 10 << QString("-12345678901234567890" ) << INV; |
108 | QTest::newRow(dataTag: "3.3" ) << 0 << 10 << QString("000000000000000000000" ) << ACC; |
109 | QTest::newRow(dataTag: "3.4" ) << 1 << 10 << QString("000000000000000000000" ) << INT; |
110 | QTest::newRow(dataTag: "3.5" ) << 0 << 10 << QString("-000000000000000000000" ) << INV; |
111 | QTest::newRow(dataTag: "3.6" ) << -10 << -1 << QString("-000000000000000000000" ) << INT; |
112 | QTest::newRow(dataTag: "3.7" ) << -10 << -1 << QString("-0000000000000000000001" ) << ACC; |
113 | |
114 | QTest::newRow(dataTag: "4.1" ) << 0 << 10 << QString(" " ) << INV; |
115 | QTest::newRow(dataTag: "4.2" ) << 0 << 10 << QString(" 1" ) << INV; |
116 | QTest::newRow(dataTag: "4.3" ) << 0 << 10 << QString("1 " ) << INV; |
117 | QTest::newRow(dataTag: "4.4" ) << 0 << 10 << QString("1.0" ) << INV; |
118 | QTest::newRow(dataTag: "4.5" ) << 0 << 10 << QString("0.1" ) << INV; |
119 | QTest::newRow(dataTag: "4.6" ) << 0 << 10 << QString(".1" ) << INV; |
120 | QTest::newRow(dataTag: "4.7" ) << 0 << 10 << QString("-1.0" ) << INV; |
121 | |
122 | QTest::newRow(dataTag: "5.1" ) << 6 << 8 << QString("5" ) << INT; |
123 | QTest::newRow(dataTag: "5.2" ) << 6 << 8 << QString("7" ) << ACC; |
124 | QTest::newRow(dataTag: "5.3" ) << 6 << 8 << QString("9" ) << INT; |
125 | QTest::newRow(dataTag: "5.3a" ) << 6 << 8 << QString("19" ) << INV; |
126 | QTest::newRow(dataTag: "5.4" ) << -8 << -6 << QString("-5" ) << INT; |
127 | QTest::newRow(dataTag: "5.5" ) << -8 << -6 << QString("-7" ) << ACC; |
128 | QTest::newRow(dataTag: "5.6" ) << -8 << -6 << QString("-9" ) << INV; |
129 | QTest::newRow(dataTag: "5.6a" ) << -8 << -6 << QString("-19" ) << INV; |
130 | QTest::newRow(dataTag: "5.7" ) << -8 << -6 << QString("5" ) << INT; |
131 | QTest::newRow(dataTag: "5.8" ) << -8 << -6 << QString("7" ) << INT; |
132 | QTest::newRow(dataTag: "5.9" ) << -8 << -6 << QString("9" ) << INT; |
133 | QTest::newRow(dataTag: "5.10" ) << -6 << 8 << QString("-5" ) << ACC; |
134 | QTest::newRow(dataTag: "5.11" ) << -6 << 8 << QString("5" ) << ACC; |
135 | QTest::newRow(dataTag: "5.12" ) << -6 << 8 << QString("-7" ) << INV; |
136 | QTest::newRow(dataTag: "5.13" ) << -6 << 8 << QString("7" ) << ACC; |
137 | QTest::newRow(dataTag: "5.14" ) << -6 << 8 << QString("-9" ) << INV; |
138 | QTest::newRow(dataTag: "5.15" ) << -6 << 8 << QString("9" ) << INT; |
139 | |
140 | QTest::newRow(dataTag: "6.1" ) << 100 << 102 << QString("11" ) << INT; |
141 | QTest::newRow(dataTag: "6.2" ) << 100 << 102 << QString("-11" ) << INV; |
142 | |
143 | QTest::newRow(dataTag: "7.1" ) << 0 << 10 << QString("100" ) << INV; |
144 | QTest::newRow(dataTag: "7.2" ) << 0 << -10 << QString("100" ) << INV; |
145 | QTest::newRow(dataTag: "7.3" ) << 0 << -10 << QString("-100" ) << INV; |
146 | QTest::newRow(dataTag: "7.4" ) << -100 << 10 << QString("100" ) << INT; |
147 | |
148 | QTest::newRow(dataTag: "8.1" ) << -100 << -10 << QString("+" ) << INV; |
149 | QTest::newRow(dataTag: "8.2" ) << -100 << -10 << QString("+50" ) << INV; |
150 | QTest::newRow(dataTag: "8.3" ) << -100 << -10 << QString("50" ) << INT; |
151 | QTest::newRow(dataTag: "8.4" ) << 10 << 100 << QString("-" ) << INV; |
152 | QTest::newRow(dataTag: "8.5" ) << 10 << 100 << QString("-50" ) << INV; |
153 | QTest::newRow(dataTag: "8.6" ) << 10 << 100 << QString("5" ) << INT; |
154 | QTest::newRow(dataTag: "8.7" ) << -1 << 100 << QString("-" ) << INT; |
155 | QTest::newRow(dataTag: "8.8" ) << -1 << 100 << QString("-50" ) << INV; |
156 | QTest::newRow(dataTag: "8.9" ) << -1 << 100 << QString("5" ) << ACC; |
157 | QTest::newRow(dataTag: "8.10" ) << -1 << 100 << QString("+" ) << INT; |
158 | QTest::newRow(dataTag: "8.11" ) << -1 << 100 << QString("+50" ) << ACC; |
159 | |
160 | QTest::newRow(dataTag: "9.0" ) << -10 << 10 << QString("000" ) << ACC; |
161 | QTest::newRow(dataTag: "9.1" ) << -10 << 10 << QString("008" ) << ACC; |
162 | QTest::newRow(dataTag: "9.2" ) << -10 << 10 << QString("-008" ) << ACC; |
163 | QTest::newRow(dataTag: "9.3" ) << -10 << 10 << QString("00010" ) << ACC; |
164 | QTest::newRow(dataTag: "9.4" ) << -10 << 10 << QString("-00010" ) << ACC; |
165 | QTest::newRow(dataTag: "9.5" ) << -10 << 10 << QString("00020" ) << INV; |
166 | QTest::newRow(dataTag: "9.6" ) << -10 << 10 << QString("-00020" ) << INV; |
167 | |
168 | } |
169 | |
170 | void tst_QIntValidator::validateArabic() |
171 | { |
172 | QString arabicNum; |
173 | arabicNum += QChar(1633); // "18" in arabic |
174 | arabicNum += QChar(1640); |
175 | |
176 | QIntValidator validator(-20, 20, 0); |
177 | validator.setLocale(QLocale(QLocale::Arabic, QLocale::SaudiArabia)); |
178 | int i; |
179 | QCOMPARE(validator.validate(arabicNum, i), QValidator::Acceptable); |
180 | |
181 | } |
182 | |
183 | |
184 | void tst_QIntValidator::validateFrench() |
185 | { |
186 | QIntValidator validator(-2000, 2000, 0); |
187 | validator.setLocale(QLocale::French); |
188 | int i; |
189 | QString s = QLatin1String("1 " ); |
190 | QCOMPARE(validator.validate(s, i), QValidator::Acceptable); |
191 | validator.fixup(input&: s); |
192 | QCOMPARE(s, s); |
193 | |
194 | s = QLatin1String("1 000" ); |
195 | QCOMPARE(validator.validate(s, i), QValidator::Acceptable); |
196 | validator.fixup(input&: s); |
197 | QCOMPARE(s, s); |
198 | |
199 | |
200 | s = QLatin1String("1 0 00" ); |
201 | QCOMPARE(validator.validate(s, i), QValidator::Intermediate); |
202 | validator.fixup(input&: s); |
203 | QCOMPARE(s, validator.locale().toString(1000)); |
204 | |
205 | // Confim no fallback to C locale |
206 | s = QLatin1String("1,000" ); |
207 | QCOMPARE(validator.validate(s, i), QValidator::Invalid); |
208 | validator.setLocale(QLocale::C); |
209 | QCOMPARE(validator.validate(s, i), QValidator::Acceptable); |
210 | } |
211 | |
212 | void tst_QIntValidator::validate() |
213 | { |
214 | QFETCH(int, minimum); |
215 | QFETCH(int, maximum); |
216 | QFETCH(QString, value); |
217 | QFETCH(QValidator::State, state); |
218 | |
219 | QIntValidator iv(minimum, maximum, 0); |
220 | iv.setLocale(QLocale::C); |
221 | int dummy; |
222 | QCOMPARE((int)iv.validate(value, dummy), (int)state); |
223 | } |
224 | |
225 | void tst_QIntValidator::notifySignals() |
226 | { |
227 | QLocale::setDefault(QLocale("C" )); |
228 | |
229 | QIntValidator iv(0, 10, 0); |
230 | QSignalSpy topSpy(&iv, SIGNAL(topChanged(int))); |
231 | QSignalSpy bottomSpy(&iv, SIGNAL(bottomChanged(int))); |
232 | QSignalSpy changedSpy(&iv, SIGNAL(changed())); |
233 | |
234 | iv.setTop(9); |
235 | QCOMPARE(topSpy.count(), 1); |
236 | QCOMPARE(changedSpy.count(), 1); |
237 | QCOMPARE(iv.top(), 9); |
238 | iv.setBottom(1); |
239 | QCOMPARE(bottomSpy.count(), 1); |
240 | QCOMPARE(changedSpy.count(), 2); |
241 | QCOMPARE(iv.bottom(), 1); |
242 | |
243 | iv.setRange(bottom: 1, top: 8); |
244 | QCOMPARE(topSpy.count(), 2); |
245 | QCOMPARE(bottomSpy.count(), 1); |
246 | QCOMPARE(changedSpy.count(), 3); |
247 | QCOMPARE(iv.top(), 8); |
248 | QCOMPARE(iv.bottom(), 1); |
249 | |
250 | iv.setRange(bottom: 2, top: 8); |
251 | QCOMPARE(topSpy.count(), 2); |
252 | QCOMPARE(bottomSpy.count(), 2); |
253 | QCOMPARE(changedSpy.count(), 4); |
254 | QCOMPARE(iv.top(), 8); |
255 | QCOMPARE(iv.bottom(), 2); |
256 | |
257 | iv.setRange(bottom: 3, top: 7); |
258 | QCOMPARE(topSpy.count(), 3); |
259 | QCOMPARE(bottomSpy.count(), 3); |
260 | QCOMPARE(changedSpy.count(), 5); |
261 | QCOMPARE(iv.top(), 7); |
262 | QCOMPARE(iv.bottom(), 3); |
263 | |
264 | iv.setRange(bottom: 3, top: 7); |
265 | QCOMPARE(topSpy.count(), 3); |
266 | QCOMPARE(bottomSpy.count(), 3); |
267 | QCOMPARE(changedSpy.count(), 5); |
268 | |
269 | iv.setLocale(QLocale("C" )); |
270 | QCOMPARE(changedSpy.count(), 5); |
271 | |
272 | iv.setLocale(QLocale("en" )); |
273 | QCOMPARE(changedSpy.count(), 6); |
274 | } |
275 | |
276 | QTEST_APPLESS_MAIN(tst_QIntValidator) |
277 | #include "tst_qintvalidator.moc" |
278 | |