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
33#include <qvalidator.h>
34
35class tst_QDoubleValidator : public QObject
36{
37 Q_OBJECT
38private slots:
39 void validate_data();
40 void validate();
41 void zeroPaddedExponent_data();
42 void zeroPaddedExponent();
43 void validateThouSep_data();
44 void validateThouSep();
45 void validateIntEquiv_data();
46 void validateIntEquiv();
47 void notifySignals();
48};
49
50Q_DECLARE_METATYPE(QValidator::State);
51#define INV QValidator::Invalid
52#define ITM QValidator::Intermediate
53#define ACC QValidator::Acceptable
54
55void tst_QDoubleValidator::validateThouSep_data()
56{
57 QTest::addColumn<QString>(name: "localeName");
58 QTest::addColumn<QString>(name: "value");
59 QTest::addColumn<bool>(name: "rejectGroupSeparator");
60 QTest::addColumn<QValidator::State>(name: "result");
61
62 QTest::newRow(dataTag: "1,000C") << "C" << QString("1,000") << false << ACC;
63 QTest::newRow(dataTag: "1,000.1C") << "C" << QString("1,000.1") << false << ACC;
64 QTest::newRow(dataTag: "1,000.1C_reject") << "C" << QString("1,000.1") << true << INV;
65 QTest::newRow(dataTag: "1.000C") << "C" << QString("1.000") << false << ACC;
66
67 QTest::newRow(dataTag: "1,000de") << "de" << QString("1,000") << false << ACC;
68 QTest::newRow(dataTag: "1.000de") << "de" << QString("1.000") << false << ACC;
69
70 QTest::newRow(dataTag: ".C") << "C" << QString(".") << false << ITM;
71 QTest::newRow(dataTag: ".de") << "de" << QString(".") << false << INV;
72 QTest::newRow(dataTag: "1.000,1de") << "de" << QString("1.000,1") << false << ACC;
73 QTest::newRow(dataTag: "1.000,1de_reject") << "de" << QString("1.000,1") << true << INV;
74 QTest::newRow(dataTag: ",C") << "C" << QString(",") << false << INV;
75 QTest::newRow(dataTag: ",de") << "de" << QString(",") << false << ITM;
76 QTest::newRow(dataTag: "1,23") << "en_AU" << QString("1,00") << false << ITM;
77}
78
79void tst_QDoubleValidator::validateThouSep()
80{
81 QFETCH(QString, localeName);
82 QFETCH(QString, value);
83 QFETCH(bool, rejectGroupSeparator);
84 QFETCH(QValidator::State, result);
85 int dummy = 0;
86
87 QDoubleValidator iv(-10000, 10000, 3, 0);
88 iv.setNotation(QDoubleValidator::ScientificNotation);
89 QLocale locale(localeName);
90 if (rejectGroupSeparator)
91 locale.setNumberOptions(QLocale::RejectGroupSeparator);
92 iv.setLocale(locale);
93
94 QCOMPARE(iv.validate(value, dummy), result);
95}
96
97void tst_QDoubleValidator::validate_data()
98{
99 QTest::addColumn<QString>(name: "localeName");
100 QTest::addColumn<double>(name: "minimum");
101 QTest::addColumn<double>(name: "maximum");
102 QTest::addColumn<int>(name: "decimals");
103 QTest::addColumn<QString>(name: "value");
104 QTest::addColumn<QValidator::State>(name: "scientific_state");
105 QTest::addColumn<QValidator::State>(name: "standard_state");
106
107 QTest::newRow(dataTag: "data0") << "C" << 0.0 << 100.0 << 1 << QString("50.0") << ACC << ACC;
108 QTest::newRow(dataTag: "data1") << "C" << 00.0 << 100.0 << 1 << QString("500.0") << ITM << ITM;
109 QTest::newRow(dataTag: "data1a") << "C" << 00.0 << 100.0 << 1 << QString("5001.0") << ITM << INV;
110 QTest::newRow(dataTag: "data2") << "C" << 00.0 << 100.0 << 1 << QString("-35.0") << INV << INV;
111 QTest::newRow(dataTag: "data3") << "C" << 00.0 << 100.0 << 1 << QString("a") << INV << INV;
112 QTest::newRow(dataTag: "data4") << "C" << 0.0 << 100.0 << 1 << QString("-") << INV << INV;
113 QTest::newRow(dataTag: "data5") << "C" << 0.0 << 100.0 << 1 << QString("100.0") << ACC << ACC;
114 QTest::newRow(dataTag: "data6") << "C" << -100.0 << 100.0 << 1 << QString("-") << ITM << ITM;
115 QTest::newRow(dataTag: "data7") << "C" << -100.0 << 100.0 << 1 << QString("-500.0") << ITM << ITM;
116 QTest::newRow(dataTag: "data8") << "C" << -100.0 << 100.0 << 1 << QString("-100") << ACC << ACC;
117 QTest::newRow(dataTag: "data9") << "C" << -100.0 << -10.0 << 1 << QString("10") << ITM << ITM;
118 QTest::newRow(dataTag: "data10") << "C" << 0.3 << 0.5 << 5 << QString("0.34567") << ACC << ACC;
119 QTest::newRow(dataTag: "data11") << "C" << -0.3 << -0.5 << 5 << QString("-0.345678") << INV << INV;
120 QTest::newRow(dataTag: "data12") << "C" << -0.32 << 0.32 << 1 << QString("0") << ACC << ACC;
121 QTest::newRow(dataTag: "data13") << "C" << 0.0 << 100.0 << 1 << QString("3456a") << INV << INV;
122 QTest::newRow(dataTag: "data14") << "C" << -100.0 << 100.0 << 1 << QString("-3456a") << INV << INV;
123 QTest::newRow(dataTag: "data15") << "C" << -100.0 << 100.0 << 1 << QString("a-3456") << INV << INV;
124 QTest::newRow(dataTag: "data16") << "C" << -100.0 << 100.0 << 1 << QString("a-3456a") << INV << INV;
125 QTest::newRow(dataTag: "data17") << "C" << 1229.0 << 1231.0 << 0 << QString("123e") << ITM << INV;
126 QTest::newRow(dataTag: "data18") << "C" << 1229.0 << 1231.0 << 0 << QString("123e+") << ITM << INV;
127 QTest::newRow(dataTag: "data19") << "C" << 1229.0 << 1231.0 << 0 << QString("123e+1") << ACC << INV;
128 QTest::newRow(dataTag: "data20") << "C" << 12290.0 << 12310.0 << 0 << QString("123e+2") << ACC << INV;
129 QTest::newRow(dataTag: "data21") << "C" << 12.290 << 12.310 << 2 << QString("123e-") << ITM << INV;
130 QTest::newRow(dataTag: "data22") << "C" << 12.290 << 12.310 << 2 << QString("123e-1") << ACC << INV;
131 QTest::newRow(dataTag: "data23") << "C" << 1.2290 << 1.2310 << 3 << QString("123e-2") << ACC << INV;
132 QTest::newRow(dataTag: "data24") << "C" << 1229.0 << 1231.0 << 0 << QString("123E") << ITM << INV;
133 QTest::newRow(dataTag: "data25") << "C" << 1229.0 << 1231.0 << 0 << QString("123E+") << ITM << INV;
134 QTest::newRow(dataTag: "data26") << "C" << 1229.0 << 1231.0 << 0 << QString("123E+1") << ACC << INV;
135 QTest::newRow(dataTag: "data27") << "C" << 12290.0 << 12310.0 << 0 << QString("123E+2") << ACC << INV;
136 QTest::newRow(dataTag: "data28") << "C" << 12.290 << 12.310 << 2 << QString("123E-") << ITM << INV;
137 QTest::newRow(dataTag: "data29") << "C" << 12.290 << 12.310 << 2 << QString("123E-1") << ACC << INV;
138 QTest::newRow(dataTag: "data30") << "C" << 1.2290 << 1.2310 << 3 << QString("123E-2") << ACC << INV;
139 QTest::newRow(dataTag: "data31") << "C" << 1.2290 << 1.2310 << 3 << QString("e") << ITM << INV;
140 QTest::newRow(dataTag: "data32") << "C" << 1.2290 << 1.2310 << 3 << QString("e+") << ITM << INV;
141 QTest::newRow(dataTag: "data33") << "C" << 1.2290 << 1.2310 << 3 << QString("e+1") << ITM << INV;
142 QTest::newRow(dataTag: "data34") << "C" << 1.2290 << 1.2310 << 3 << QString("e-") << ITM << INV;
143 QTest::newRow(dataTag: "data35") << "C" << 1.2290 << 1.2310 << 3 << QString("e-1") << ITM << INV;
144 QTest::newRow(dataTag: "data36") << "C" << 1.2290 << 1.2310 << 3 << QString("E") << ITM << INV;
145 QTest::newRow(dataTag: "data37") << "C" << 1.2290 << 1.2310 << 3 << QString("E+") << ITM << INV;
146 QTest::newRow(dataTag: "data38") << "C" << 1.2290 << 1.2310 << 3 << QString("E+1") << ITM << INV;
147 QTest::newRow(dataTag: "data39") << "C" << 1.2290 << 1.2310 << 3 << QString("E-") << ITM << INV;
148 QTest::newRow(dataTag: "data40") << "C" << 1.2290 << 1.2310 << 3 << QString("E-1") << ITM << INV;
149 QTest::newRow(dataTag: "data41") << "C" << -100.0 << 100.0 << 0 << QString("10e") << ITM << INV;
150 QTest::newRow(dataTag: "data42") << "C" << -100.0 << 100.0 << 0 << QString("10e+") << ITM << INV;
151 QTest::newRow(dataTag: "data43") << "C" << 0.01 << 0.09 << 2 << QString("0") << ITM << ITM;
152 QTest::newRow(dataTag: "data44") << "C" << 0.0 << 10.0 << 1 << QString("11") << ITM << ITM;
153 QTest::newRow(dataTag: "data45") << "C" << 0.0 << 10.0 << 2 << QString("11") << ITM << ITM;
154 QTest::newRow(dataTag: "data46") << "C" << 0.0 << 100.0 << 0 << QString("0.") << ACC << ACC;
155 QTest::newRow(dataTag: "data47") << "C" << 0.0 << 100.0 << 0 << QString("0.0") << INV << INV;
156 QTest::newRow(dataTag: "data48") << "C" << 0.0 << 100.0 << 1 << QString("0.0") << ACC << ACC;
157 QTest::newRow(dataTag: "data49") << "C" << 0.0 << 100.0 << 0 << QString(".") << ITM << ITM;
158 QTest::newRow(dataTag: "data50") << "C" << 0.0 << 100.0 << 1 << QString(".") << ITM << ITM;
159 QTest::newRow(dataTag: "data51") << "C" << 0.0 << 2.0 << 2 << QString("9.99") << ITM << ITM;
160 QTest::newRow(dataTag: "data52") << "C" << 100.0 << 200.0 << 4 << QString("999.9999") << ITM << ITM;
161 QTest::newRow(dataTag: "data53") << "C" << 0.0 << 2.0 << 2 << QString("9.9999") << INV << INV;
162 QTest::newRow(dataTag: "data54") << "C" << 100.0 << 200.0 << 4 << QString("9999.9999") << ITM << INV;
163
164 QTest::newRow(dataTag: "data_de0") << "de" << 0.0 << 100.0 << 1 << QString("50,0") << ACC << ACC;
165 QTest::newRow(dataTag: "data_de1") << "de" << 00.0 << 100.0 << 1 << QString("500,0") << ITM << ITM;
166 QTest::newRow(dataTag: "data_de1a") << "de" << 00.0 << 100.0 << 1 << QString("5001,0") << ITM << INV;
167 QTest::newRow(dataTag: "data_de0C") << "de" << 0.0 << 100.0 << 1 << QString("50,0") << ACC << ACC;
168 QTest::newRow(dataTag: "data_de1C") << "de" << 00.0 << 100.0 << 1 << QString("500,0") << ITM << ITM;
169 QTest::newRow(dataTag: "data_de1aC") << "de" << 00.0 << 100.0 << 1 << QString("5001,0") << ITM << INV;
170 QTest::newRow(dataTag: "data_de2") << "de" << 00.0 << 100.0 << 1 << QString("-35,0") << INV << INV;
171 QTest::newRow(dataTag: "data_de3") << "de" << 00.0 << 100.0 << 1 << QString("a") << INV << INV;
172 QTest::newRow(dataTag: "data_de4") << "de" << 0.0 << 100.0 << 1 << QString("-") << INV << INV;
173 QTest::newRow(dataTag: "data_de5") << "de" << 0.0 << 100.0 << 1 << QString("100,0") << ACC << ACC;
174 QTest::newRow(dataTag: "data_de6") << "de" << -100.0 << 100.0 << 1 << QString("-") << ITM << ITM;
175 QTest::newRow(dataTag: "data_de7") << "de" << -100.0 << 100.0 << 1 << QString("-500,0") << ITM << ITM;
176 QTest::newRow(dataTag: "data_de8") << "de" << -100.0 << 100.0 << 1 << QString("-100") << ACC << ACC;
177 QTest::newRow(dataTag: "data_de9") << "de" << -100.0 << -10.0 << 1 << QString("10") << ITM << ITM;
178 QTest::newRow(dataTag: "data_de10") << "de" << 0.3 << 0.5 << 5 << QString("0,34567") << ACC << ACC;
179 QTest::newRow(dataTag: "data_de11") << "de" << -0.3 << -0.5 << 5 << QString("-0,345678") << INV << INV;
180 QTest::newRow(dataTag: "data_de12") << "de" << -0.32 << 0.32 << 1 << QString("0") << ACC << ACC;
181 QTest::newRow(dataTag: "data_de13") << "de" << 0.0 << 100.0 << 1 << QString("3456a") << INV << INV;
182 QTest::newRow(dataTag: "data_de14") << "de" << -100.0 << 100.0 << 1 << QString("-3456a") << INV << INV;
183 QTest::newRow(dataTag: "data_de15") << "de" << -100.0 << 100.0 << 1 << QString("a-3456") << INV << INV;
184 QTest::newRow(dataTag: "data_de16") << "de" << -100.0 << 100.0 << 1 << QString("a-3456a") << INV << INV;
185 QTest::newRow(dataTag: "data_de17") << "de" << 1229.0 << 1231.0 << 0 << QString("123e") << ITM << INV;
186 QTest::newRow(dataTag: "data_de18") << "de" << 1229.0 << 1231.0 << 0 << QString("123e+") << ITM << INV;
187 QTest::newRow(dataTag: "data_de19") << "de" << 1229.0 << 1231.0 << 0 << QString("123e+1") << ACC << INV;
188 QTest::newRow(dataTag: "data_de20") << "de" << 12290.0 << 12310.0 << 0 << QString("123e+2") << ACC << INV;
189 QTest::newRow(dataTag: "data_de21") << "de" << 12.290 << 12.310 << 2 << QString("123e-") << ITM << INV;
190 QTest::newRow(dataTag: "data_de22") << "de" << 12.290 << 12.310 << 2 << QString("123e-1") << ACC << INV;
191 QTest::newRow(dataTag: "data_de23") << "de" << 1.2290 << 1.2310 << 3 << QString("123e-2") << ACC << INV;
192 QTest::newRow(dataTag: "data_de24") << "de" << 1229.0 << 1231.0 << 0 << QString("123E") << ITM << INV;
193 QTest::newRow(dataTag: "data_de25") << "de" << 1229.0 << 1231.0 << 0 << QString("123E+") << ITM << INV;
194 QTest::newRow(dataTag: "data_de26") << "de" << 1229.0 << 1231.0 << 0 << QString("123E+1") << ACC << INV;
195 QTest::newRow(dataTag: "data_de27") << "de" << 12290.0 << 12310.0 << 0 << QString("123E+2") << ACC << INV;
196 QTest::newRow(dataTag: "data_de28") << "de" << 12.290 << 12.310 << 2 << QString("123E-") << ITM << INV;
197 QTest::newRow(dataTag: "data_de29") << "de" << 12.290 << 12.310 << 2 << QString("123E-1") << ACC << INV;
198 QTest::newRow(dataTag: "data_de30") << "de" << 1.2290 << 1.2310 << 3 << QString("123E-2") << ACC << INV;
199 QTest::newRow(dataTag: "data_de31") << "de" << 1.2290 << 1.2310 << 3 << QString("e") << ITM << INV;
200 QTest::newRow(dataTag: "data_de32") << "de" << 1.2290 << 1.2310 << 3 << QString("e+") << ITM << INV;
201 QTest::newRow(dataTag: "data_de33") << "de" << 1.2290 << 1.2310 << 3 << QString("e+1") << ITM << INV;
202 QTest::newRow(dataTag: "data_de34") << "de" << 1.2290 << 1.2310 << 3 << QString("e-") << ITM << INV;
203 QTest::newRow(dataTag: "data_de35") << "de" << 1.2290 << 1.2310 << 3 << QString("e-1") << ITM << INV;
204 QTest::newRow(dataTag: "data_de36") << "de" << 1.2290 << 1.2310 << 3 << QString("E") << ITM << INV;
205 QTest::newRow(dataTag: "data_de37") << "de" << 1.2290 << 1.2310 << 3 << QString("E+") << ITM << INV;
206 QTest::newRow(dataTag: "data_de38") << "de" << 1.2290 << 1.2310 << 3 << QString("E+1") << ITM << INV;
207 QTest::newRow(dataTag: "data_de39") << "de" << 1.2290 << 1.2310 << 3 << QString("E-") << ITM << INV;
208 QTest::newRow(dataTag: "data_de40") << "de" << 1.2290 << 1.2310 << 3 << QString("E-1") << ITM << INV;
209 QTest::newRow(dataTag: "data_de41") << "de" << -100.0 << 100.0 << 0 << QString("10e") << ITM << INV;
210 QTest::newRow(dataTag: "data_de42") << "de" << -100.0 << 100.0 << 0 << QString("10e+") << ITM << INV;
211 QTest::newRow(dataTag: "data_de43") << "de" << 0.01 << 0.09 << 2 << QString("0") << ITM << ITM;
212 QTest::newRow(dataTag: "data_de44") << "de" << 0.0 << 10.0 << 1 << QString("11") << ITM << ITM;
213 QTest::newRow(dataTag: "data_de45") << "de" << 0.0 << 10.0 << 2 << QString("11") << ITM << ITM;
214 QTest::newRow(dataTag: "data_de46") << "de" << 0.0 << 2.0 << 2 << QString("9,99") << ITM << ITM;
215 QTest::newRow(dataTag: "data_de47") << "de" << 100.0 << 200.0 << 4 << QString("999,9999") << ITM << ITM;
216 QTest::newRow(dataTag: "data_de48") << "de" << 0.0 << 2.0 << 2 << QString("9,9999") << INV << INV;
217 QTest::newRow(dataTag: "data_de49") << "de" << 100.0 << 200.0 << 4 << QString("9999,9999") << ITM << INV;
218
219 QString arabicNum;
220 arabicNum += QChar(1633); // "18.4" in arabic
221 arabicNum += QChar(1640);
222 arabicNum += QChar(1643);
223 arabicNum += QChar(1636);
224 QTest::newRow(dataTag: "arabic") << "ar" << 0.0 << 20.0 << 2 << arabicNum << ACC << ACC;
225
226 // Confim no fallback to C locale
227 QTest::newRow(dataTag: "data_C1") << "de" << 0.0 << 1000.0 << 2 << QString("1.000,00") << ACC << ACC;
228 QTest::newRow(dataTag: "data_C2") << "de" << 0.0 << 1000.0 << 2 << QString("1,000.00") << INV << INV;
229}
230
231void tst_QDoubleValidator::validate()
232{
233 QFETCH(QString, localeName);
234 QFETCH(double, minimum);
235 QFETCH(double, maximum);
236 QFETCH(int, decimals);
237 QFETCH(QString, value);
238 QFETCH(QValidator::State, scientific_state);
239 QFETCH(QValidator::State, standard_state);
240
241 QLocale::setDefault(QLocale(localeName));
242
243 QDoubleValidator dv(minimum, maximum, decimals, 0);
244 int dummy;
245 QCOMPARE((int)dv.validate(value, dummy), (int)scientific_state);
246 dv.setNotation(QDoubleValidator::StandardNotation);
247 QCOMPARE((int)dv.validate(value, dummy), (int)standard_state);
248}
249
250void tst_QDoubleValidator::zeroPaddedExponent_data()
251{
252 QTest::addColumn<double>(name: "minimum");
253 QTest::addColumn<double>(name: "maximum");
254 QTest::addColumn<int>(name: "decimals");
255 QTest::addColumn<QString>(name: "value");
256 QTest::addColumn<bool>(name: "rejectZeroPaddedExponent");
257 QTest::addColumn<QValidator::State>(name: "state");
258
259 QTest::newRow(dataTag: "data01") << 1229.0 << 1231.0 << 0 << QString("123e+1") << false << ACC;
260 QTest::newRow(dataTag: "data02") << 12290.0 << 12310.0 << 0 << QString("123e2") << false << ACC;
261 QTest::newRow(dataTag: "data03") << 12.290 << 12.310 << 2 << QString("123e-") << false << ITM;
262 QTest::newRow(dataTag: "data04") << 12.290 << 12.310 << 2 << QString("123e-1") << false << ACC;
263 QTest::newRow(dataTag: "data05") << 1.2290 << 1.2310 << 3 << QString("123e-2") << false << ACC;
264
265 QTest::newRow(dataTag: "data11") << 1229.0 << 1231.0 << 0 << QString("123e+1") << true << ACC;
266 QTest::newRow(dataTag: "data12") << 12290.0 << 12310.0 << 0 << QString("123e2") << true << ACC;
267 QTest::newRow(dataTag: "data13") << 12.290 << 12.310 << 2 << QString("123e-") << true << ITM;
268 QTest::newRow(dataTag: "data14") << 12.290 << 12.310 << 2 << QString("123e-1") << true << ACC;
269 QTest::newRow(dataTag: "data15") << 1.2290 << 1.2310 << 3 << QString("123e-2") << true << ACC;
270
271 QTest::newRow(dataTag: "data21") << 1229.0 << 1231.0 << 0 << QString("123e+01") << false << ACC;
272 QTest::newRow(dataTag: "data22") << 12290.0 << 12310.0 << 0 << QString("123e02") << false << ACC;
273 QTest::newRow(dataTag: "data23") << 12.290 << 12.310 << 2 << QString("123e-0") << false << ITM;
274 QTest::newRow(dataTag: "data24") << 12.290 << 12.310 << 2 << QString("123e-01") << false << ACC;
275 QTest::newRow(dataTag: "data25") << 1.2290 << 1.2310 << 3 << QString("123e-02") << false << ACC;
276
277 QTest::newRow(dataTag: "data31") << 1229.0 << 1231.0 << 0 << QString("123e+01") << true << INV;
278 QTest::newRow(dataTag: "data32") << 12290.0 << 12310.0 << 0 << QString("123e02") << true << INV;
279 QTest::newRow(dataTag: "data33") << 12.290 << 12.310 << 2 << QString("123e-0") << true << INV;
280 QTest::newRow(dataTag: "data34") << 12.290 << 12.310 << 2 << QString("123e-01") << true << INV;
281 QTest::newRow(dataTag: "data35") << 1.2290 << 1.2310 << 3 << QString("123e-02") << true << INV;
282
283}
284
285void tst_QDoubleValidator::zeroPaddedExponent()
286{
287 QFETCH(double, minimum);
288 QFETCH(double, maximum);
289 QFETCH(int, decimals);
290 QFETCH(QString, value);
291 QFETCH(bool, rejectZeroPaddedExponent);
292 QFETCH(QValidator::State, state);
293
294 QLocale locale(QLocale::C);
295 if (rejectZeroPaddedExponent)
296 locale.setNumberOptions(QLocale::RejectLeadingZeroInExponent);
297
298 QDoubleValidator dv(minimum, maximum, decimals, 0);
299 dv.setLocale(locale);
300 int dummy;
301 QCOMPARE((int)dv.validate(value, dummy), (int)state);
302}
303
304void tst_QDoubleValidator::notifySignals()
305{
306 QLocale::setDefault(QLocale("C"));
307
308 QDoubleValidator dv(0.1, 0.9, 10, 0);
309 QSignalSpy topSpy(&dv, SIGNAL(topChanged(double)));
310 QSignalSpy bottomSpy(&dv, SIGNAL(bottomChanged(double)));
311 QSignalSpy decSpy(&dv, SIGNAL(decimalsChanged(int)));
312 QSignalSpy changedSpy(&dv, SIGNAL(changed()));
313
314 qRegisterMetaType<QDoubleValidator::Notation>(typeName: "QDoubleValidator::Notation");
315 QSignalSpy notSpy(&dv, SIGNAL(notationChanged(QDoubleValidator::Notation)));
316
317 dv.setTop(0.8);
318 QCOMPARE(topSpy.count(), 1);
319 QCOMPARE(changedSpy.count(), 1);
320 QCOMPARE(dv.top(), 0.8);
321 dv.setBottom(0.2);
322 QCOMPARE(bottomSpy.count(), 1);
323 QCOMPARE(changedSpy.count(), 2);
324 QCOMPARE(dv.bottom(), 0.2);
325
326 dv.setRange(bottom: 0.2, top: 0.7);
327 QCOMPARE(topSpy.count(), 2);
328 QCOMPARE(bottomSpy.count(), 1);
329 QCOMPARE(decSpy.count(), 1);
330 QCOMPARE(changedSpy.count(), 3);
331 QCOMPARE(dv.bottom(), 0.2);
332 QCOMPARE(dv.top(), 0.7);
333 QCOMPARE(dv.decimals(), 0);
334
335 dv.setRange(bottom: 0.3, top: 0.7);
336 QCOMPARE(topSpy.count(), 2);
337 QCOMPARE(bottomSpy.count(), 2);
338 QCOMPARE(changedSpy.count(), 4);
339 QCOMPARE(dv.bottom(), 0.3);
340 QCOMPARE(dv.top(), 0.7);
341 QCOMPARE(dv.decimals(), 0);
342
343 dv.setRange(bottom: 0.4, top: 0.6);
344 QCOMPARE(topSpy.count(), 3);
345 QCOMPARE(bottomSpy.count(), 3);
346 QCOMPARE(changedSpy.count(), 5);
347 QCOMPARE(dv.bottom(), 0.4);
348 QCOMPARE(dv.top(), 0.6);
349 QCOMPARE(dv.decimals(), 0);
350
351 dv.setDecimals(10);
352 QCOMPARE(decSpy.count(), 2);
353 QCOMPARE(changedSpy.count(), 6);
354 QCOMPARE(dv.decimals(), 10);
355
356
357 dv.setRange(bottom: 0.4, top: 0.6, decimals: 100);
358 QCOMPARE(topSpy.count(), 3);
359 QCOMPARE(bottomSpy.count(), 3);
360 QCOMPARE(decSpy.count(), 3);
361 QCOMPARE(changedSpy.count(), 7);
362 QCOMPARE(dv.bottom(), 0.4);
363 QCOMPARE(dv.top(), 0.6);
364 QCOMPARE(dv.decimals(), 100);
365
366 dv.setNotation(QDoubleValidator::StandardNotation);
367 QCOMPARE(notSpy.count(), 1);
368 QCOMPARE(changedSpy.count(), 8);
369 QCOMPARE(dv.notation(), QDoubleValidator::StandardNotation);
370
371 dv.setRange(bottom: dv.bottom(), top: dv.top(), decimals: dv.decimals());
372 QCOMPARE(topSpy.count(), 3);
373 QCOMPARE(bottomSpy.count(), 3);
374 QCOMPARE(decSpy.count(), 3);
375 QCOMPARE(changedSpy.count(), 8);
376
377 dv.setNotation(dv.notation());
378 QCOMPARE(notSpy.count(), 1);
379 QCOMPARE(changedSpy.count(), 8);
380
381 dv.setLocale(QLocale("C"));
382 QCOMPARE(changedSpy.count(), 8);
383
384 dv.setLocale(QLocale("en"));
385 QCOMPARE(changedSpy.count(), 9);
386}
387
388void tst_QDoubleValidator::validateIntEquiv_data()
389{
390 QTest::addColumn<double>(name: "minimum");
391 QTest::addColumn<double>(name: "maximum");
392 QTest::addColumn<QString>(name: "input");
393 QTest::addColumn<QValidator::State>(name: "state");
394
395 QTest::newRow(dataTag: "1.1") << 0.0 << 10.0 << QString("") << ITM;
396 QTest::newRow(dataTag: "1.2") << 10.0 << 0.0 << QString("") << ITM;
397
398 QTest::newRow(dataTag: "2.1") << 0.0 << 10.0 << QString("-") << INV;
399 QTest::newRow(dataTag: "2.2") << 0.0 << 10.0 << QString("-0") << INV;
400 QTest::newRow(dataTag: "2.3") << -10.0 << -1.0 << QString("+") << INV;
401 QTest::newRow(dataTag: "2.4") << -10.0 << 10.0 << QString("-") << ITM;
402 QTest::newRow(dataTag: "2.5") << -10.0 << 10.0 << QString("+") << ITM;
403 QTest::newRow(dataTag: "2.5a") << -10.0 << -9.0 << QString("+") << INV;
404 QTest::newRow(dataTag: "2.6") << -10.0 << 10.0 << QString("+0") << ACC;
405 QTest::newRow(dataTag: "2.7") << -10.0 << 10.0 << QString("+1") << ACC;
406 QTest::newRow(dataTag: "2.8") << -10.0 << 10.0 << QString("+-") << INV;
407 QTest::newRow(dataTag: "2.9") << -10.0 << 10.0 << QString("-+") << INV;
408
409 QTest::newRow(dataTag: "3.1") << 0.0 << 10.0 << QString("12345678901234567890") << INV;
410 QTest::newRow(dataTag: "3.2") << 0.0 << 10.0 << QString("-12345678901234567890") << INV;
411 QTest::newRow(dataTag: "3.3") << 0.0 << 10.0 << QString("000000000000000000000") << ACC;
412 QTest::newRow(dataTag: "3.4") << 1.0 << 10.0 << QString("000000000000000000000") << ITM;
413 QTest::newRow(dataTag: "3.5") << 0.0 << 10.0 << QString("-000000000000000000000") << INV;
414 QTest::newRow(dataTag: "3.6") << -10.0 << -1.0 << QString("-000000000000000000000") << ITM;
415 QTest::newRow(dataTag: "3.7") << -10.0 << -1.0 << QString("-0000000000000000000001") << ACC;
416
417 QTest::newRow(dataTag: "4.1") << 0.0 << 10.0 << QString(" ") << INV;
418 QTest::newRow(dataTag: "4.2") << 0.0 << 10.0 << QString(" 1") << INV;
419 QTest::newRow(dataTag: "4.3") << 0.0 << 10.0 << QString("1 ") << INV;
420 QTest::newRow(dataTag: "4.4") << 0.0 << 10.0 << QString("1.0") << INV;
421 QTest::newRow(dataTag: "4.5") << 0.0 << 10.0 << QString("0.1") << INV;
422 QTest::newRow(dataTag: "4.6") << 0.0 << 10.0 << QString(".1") << INV;
423 QTest::newRow(dataTag: "4.7") << 0.0 << 10.0 << QString("-1.0") << INV;
424
425 QTest::newRow(dataTag: "5.1a") << 6.0 << 8.0 << QString("5") << ITM;
426 QTest::newRow(dataTag: "5.1b") << 6.0 << 8.0 << QString("56") << INV;
427 QTest::newRow(dataTag: "5.2") << 6.0 << 8.0 << QString("7") << ACC;
428 QTest::newRow(dataTag: "5.3a") << 6.0 << 8.0 << QString("9") << ITM;
429 QTest::newRow(dataTag: "5.3b") << 6.0 << 8.0 << QString("-") << INV;
430 QTest::newRow(dataTag: "5.4a") << -8.0 << -6.0 << QString("+") << INV;
431 QTest::newRow(dataTag: "5.4b") << -8.0 << -6.0 << QString("+5") << INV;
432 QTest::newRow(dataTag: "5.4c") << -8.0 << -6.0 << QString("-5") << ITM;
433 QTest::newRow(dataTag: "5.5") << -8.0 << -6.0 << QString("-7") << ACC;
434 QTest::newRow(dataTag: "5.6") << -8.0 << -6.0 << QString("-9") << ITM;
435 QTest::newRow(dataTag: "5.7") << -8.0 << -6.0 << QString("5") << ITM;
436 QTest::newRow(dataTag: "5.8") << -8.0 << -6.0 << QString("7") << ITM;
437 QTest::newRow(dataTag: "5.9") << -8.0 << -6.0 << QString("9") << ITM;
438 QTest::newRow(dataTag: "5.10") << -6.0 << 8.0 << QString("-5") << ACC;
439 QTest::newRow(dataTag: "5.11") << -6.0 << 8.0 << QString("5") << ACC;
440 QTest::newRow(dataTag: "5.12") << -6.0 << 8.0 << QString("-7") << ITM;
441 QTest::newRow(dataTag: "5.13") << -6.0 << 8.0 << QString("7") << ACC;
442 QTest::newRow(dataTag: "5.14") << -6.0 << 8.0 << QString("-9") << ITM;
443 QTest::newRow(dataTag: "5.15") << -6.0 << 8.0 << QString("9") << ITM;
444
445 QTest::newRow(dataTag: "6.1") << 100.0 << 102.0 << QString("11") << ITM;
446 QTest::newRow(dataTag: "6.2") << 100.0 << 102.0 << QString("-11") << INV;
447
448 QTest::newRow(dataTag: "7.1") << 0.0 << 10.0 << QString("100") << INV;
449 QTest::newRow(dataTag: "7.2") << 0.0 << -10.0 << QString("100") << INV;
450 QTest::newRow(dataTag: "7.3") << 0.0 << -10.0 << QString("-100") << INV;
451 QTest::newRow(dataTag: "7.4") << -100.0 << 10.0 << QString("100") << ITM;
452}
453
454void tst_QDoubleValidator::validateIntEquiv()
455{
456 QFETCH(double, minimum);
457 QFETCH(double, maximum);
458 QFETCH(QString, input);
459 QFETCH(QValidator::State, state);
460
461 QLocale::setDefault(QLocale("C"));
462
463 QDoubleValidator dv(minimum, maximum, 0, 0);
464 dv.setNotation(QDoubleValidator::StandardNotation);
465 int dummy;
466 QCOMPARE(dv.validate(input, dummy), state);
467}
468
469QTEST_APPLESS_MAIN(tst_QDoubleValidator)
470#include "tst_qdoublevalidator.moc"
471

source code of qtbase/tests/auto/gui/util/qdoublevalidator/tst_qdoublevalidator.cpp