]> git.saurik.com Git - wxWidgets.git/blame - tests/validators/valnum.cpp
Fix compilation with MinGW -std=c++11 option.
[wxWidgets.git] / tests / validators / valnum.cpp
CommitLineData
a54cf371
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: tests/validators/valnum.cpp
3// Purpose: Unit tests for numeric validators.
4// Author: Vadim Zeitlin
5// Created: 2011-01-18
6// RCS-ID: $Id$
7// Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
8///////////////////////////////////////////////////////////////////////////////
9
10#include "testprec.h"
11
12#ifdef __BORLANDC__
13 #pragma hdrstop
14#endif
15
16#ifndef WX_PRECOMP
17 #include "wx/app.h"
18 #include "wx/intl.h"
19 #include "wx/textctrl.h"
20 #include "wx/validate.h"
21#endif // WX_PRECOMP
22
23#include "wx/valnum.h"
24
25#include "asserthelper.h"
26#include "testableframe.h"
27#include "wx/uiaction.h"
28
29class NumValidatorTestCase : public CppUnit::TestCase
30{
31public:
32 NumValidatorTestCase() { }
33
34 void setUp();
35 void tearDown();
36
37private:
38 CPPUNIT_TEST_SUITE( NumValidatorTestCase );
39 CPPUNIT_TEST( TransferInt );
40 CPPUNIT_TEST( TransferUnsigned );
41 CPPUNIT_TEST( TransferFloat );
42 CPPUNIT_TEST( ZeroAsBlank );
43 CPPUNIT_TEST( NoTrailingZeroes );
44 WXUISIM_TEST( Interactive );
45 CPPUNIT_TEST_SUITE_END();
46
47 void TransferInt();
48 void TransferUnsigned();
49 void TransferFloat();
50 void ZeroAsBlank();
51 void NoTrailingZeroes();
52#if wxUSE_UIACTIONSIMULATOR
53 void Interactive();
54#endif // wxUSE_UIACTIONSIMULATOR
55
56 wxTextCtrl *m_text;
57
58 wxDECLARE_NO_COPY_CLASS(NumValidatorTestCase);
59};
60
61// register in the unnamed registry so that these tests are run by default
62CPPUNIT_TEST_SUITE_REGISTRATION( NumValidatorTestCase );
63
e3778b4d 64// also include in its own registry so that these tests can be run alone
a54cf371
VZ
65CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( NumValidatorTestCase, "NumValidatorTestCase" );
66
67void NumValidatorTestCase::setUp()
68{
69 m_text = new wxTextCtrl(wxTheApp->GetTopWindow(), wxID_ANY);
70}
71
72void NumValidatorTestCase::tearDown()
73{
74 wxTheApp->GetTopWindow()->DestroyChildren();
75}
76
77void NumValidatorTestCase::TransferInt()
78{
79 int value = 0;
80 wxIntegerValidator<int> valInt(&value);
81 valInt.SetWindow(m_text);
82
83 CPPUNIT_ASSERT( valInt.TransferToWindow() );
84 CPPUNIT_ASSERT_EQUAL( "0", m_text->GetValue() );
85
86 value = 17;
87 CPPUNIT_ASSERT( valInt.TransferToWindow() );
88 CPPUNIT_ASSERT_EQUAL( "17", m_text->GetValue() );
89
90
91 m_text->ChangeValue("foobar");
92 CPPUNIT_ASSERT( !valInt.TransferFromWindow() );
93
94 m_text->ChangeValue("-234");
95 CPPUNIT_ASSERT( valInt.TransferFromWindow() );
96 CPPUNIT_ASSERT_EQUAL( -234, value );
97
98 m_text->ChangeValue("9223372036854775808"); // == LLONG_MAX + 1
99 CPPUNIT_ASSERT( !valInt.TransferFromWindow() );
100
101 m_text->Clear();
102 CPPUNIT_ASSERT( !valInt.TransferFromWindow() );
103}
104
105void NumValidatorTestCase::TransferUnsigned()
106{
107 unsigned value = 0;
108 wxIntegerValidator<unsigned> valUnsigned(&value);
109 valUnsigned.SetWindow(m_text);
110
111 CPPUNIT_ASSERT( valUnsigned.TransferToWindow() );
112 CPPUNIT_ASSERT_EQUAL( "0", m_text->GetValue() );
113
114 value = 17;
115 CPPUNIT_ASSERT( valUnsigned.TransferToWindow() );
116 CPPUNIT_ASSERT_EQUAL( "17", m_text->GetValue() );
117
118
119 m_text->ChangeValue("foobar");
120 CPPUNIT_ASSERT( !valUnsigned.TransferFromWindow() );
121
122 m_text->ChangeValue("-234");
123 CPPUNIT_ASSERT( !valUnsigned.TransferFromWindow() );
124
125 m_text->ChangeValue("234");
126 CPPUNIT_ASSERT( valUnsigned.TransferFromWindow() );
127 CPPUNIT_ASSERT_EQUAL( 234, value );
128
129 m_text->ChangeValue("18446744073709551616"); // == ULLONG_MAX + 1
130 CPPUNIT_ASSERT( !valUnsigned.TransferFromWindow() );
131
132 m_text->Clear();
133 CPPUNIT_ASSERT( !valUnsigned.TransferFromWindow() );
134}
135
136void NumValidatorTestCase::TransferFloat()
137{
c9ed413a 138 // We need a locale with point as decimal separator.
fb76ad8f 139 wxLocale loc(wxLANGUAGE_ENGLISH_UK, wxLOCALE_DONT_LOAD_DEFAULT);
c9ed413a 140
a54cf371
VZ
141 float value = 0;
142 wxFloatingPointValidator<float> valFloat(3, &value);
143 valFloat.SetWindow(m_text);
144
145 CPPUNIT_ASSERT( valFloat.TransferToWindow() );
146 CPPUNIT_ASSERT_EQUAL( "0.000", m_text->GetValue() );
147
148 value = 1.234f;
149 CPPUNIT_ASSERT( valFloat.TransferToWindow() );
150 CPPUNIT_ASSERT_EQUAL( "1.234", m_text->GetValue() );
151
152 value = 1.2345678f;
153 CPPUNIT_ASSERT( valFloat.TransferToWindow() );
154 CPPUNIT_ASSERT_EQUAL( "1.235", m_text->GetValue() );
155
156
157 m_text->ChangeValue("foobar");
158 CPPUNIT_ASSERT( !valFloat.TransferFromWindow() );
159
160 m_text->ChangeValue("-234.567");
161 CPPUNIT_ASSERT( valFloat.TransferFromWindow() );
162 CPPUNIT_ASSERT_EQUAL( -234.567f, value );
163
164 m_text->Clear();
165 CPPUNIT_ASSERT( !valFloat.TransferFromWindow() );
166}
167
168void NumValidatorTestCase::ZeroAsBlank()
169{
170 long value = 0;
171 m_text->SetValidator(
172 wxMakeIntegerValidator(&value, wxNUM_VAL_ZERO_AS_BLANK));
173
174 wxValidator * const val = m_text->GetValidator();
175
176 CPPUNIT_ASSERT( val->TransferToWindow() );
177 CPPUNIT_ASSERT_EQUAL( "", m_text->GetValue() );
178
179 value++;
180 CPPUNIT_ASSERT( val->TransferFromWindow() );
181 CPPUNIT_ASSERT_EQUAL( 0, value );
182}
183
184void NumValidatorTestCase::NoTrailingZeroes()
185{
c9ed413a 186 // We need a locale with point as decimal separator.
fb76ad8f 187 wxLocale loc(wxLANGUAGE_ENGLISH_UK, wxLOCALE_DONT_LOAD_DEFAULT);
c9ed413a 188
a54cf371
VZ
189 double value = 1.2;
190 m_text->SetValidator(
191 wxMakeFloatingPointValidator(3, &value, wxNUM_VAL_NO_TRAILING_ZEROES));
192
193 wxValidator * const val = m_text->GetValidator();
194
195 CPPUNIT_ASSERT( val->TransferToWindow() );
196 CPPUNIT_ASSERT_EQUAL( "1.2", m_text->GetValue() );
197
198 value = 1.234;
199 CPPUNIT_ASSERT( val->TransferToWindow() );
200 CPPUNIT_ASSERT_EQUAL( "1.234", m_text->GetValue() );
201}
202
203#if wxUSE_UIACTIONSIMULATOR
204
205void NumValidatorTestCase::Interactive()
206{
f1287154 207#ifdef __WXMSW__
7067a74d
VZ
208 // FIXME: This test fails on MSW buildbot slaves although works fine on
209 // development machine, no idea why. It seems to be a problem with
210 // wxUIActionSimulator rather the wxListCtrl control itself however.
f1287154 211 if ( IsAutomaticTest() )
7067a74d 212 return;
f1287154 213#endif // __WXMSW__
7067a74d 214
a54cf371
VZ
215 // Set a locale using comma as thousands separator character.
216 wxLocale loc(wxLANGUAGE_ENGLISH_UK, wxLOCALE_DONT_LOAD_DEFAULT);
217
218 m_text->SetValidator(
219 wxIntegerValidator<unsigned>(NULL, wxNUM_VAL_THOUSANDS_SEPARATOR));
220
221 // Create a sibling text control to be able to switch focus and thus
222 // trigger the control validation/normalization.
223 wxTextCtrl * const text2 = new wxTextCtrl(m_text->GetParent(), wxID_ANY);
224 text2->Move(10, 80); // Just to see it better while debugging...
225 wxFloatingPointValidator<float> valFloat(3);
226 valFloat.SetRange(-10., 10.);
227 text2->SetValidator(valFloat);
228
229 wxUIActionSimulator sim;
230
231 // Entering '-' in a control with positive range is not allowed.
232 m_text->SetFocus();
233 sim.Char('-');
234 wxYield();
235 CPPUNIT_ASSERT_EQUAL( "", m_text->GetValue() );
236
237 // Neither is entering '.' or any non-digit character.
238 sim.Text(".a+/");
239 wxYield();
240 CPPUNIT_ASSERT_EQUAL( "", m_text->GetValue() );
241
242 // Entering digits should work though and after leaving the control the
243 // contents should be normalized.
244 sim.Text("1234567");
245 wxYield();
246 text2->SetFocus();
247 wxYield();
248 if ( loc.IsOk() )
249 CPPUNIT_ASSERT_EQUAL( "1,234,567", m_text->GetValue() );
250 else
251 CPPUNIT_ASSERT_EQUAL( "1234567", m_text->GetValue() );
252
253
254 // Entering both '-' and '.' in this control should work but only in the
255 // correct order.
256 sim.Char('-');
257 wxYield();
258 CPPUNIT_ASSERT_EQUAL( "-", text2->GetValue() );
259
260 text2->SetInsertionPoint(0);
261 sim.Char('.');
262 wxYield();
263 CPPUNIT_ASSERT_EQUAL( "-", text2->GetValue() );
264
265 text2->SetInsertionPointEnd();
266 sim.Char('.');
267 wxYield();
268 CPPUNIT_ASSERT_EQUAL( "-.", text2->GetValue() );
269
270 // Adding up to three digits after the point should work.
271 sim.Text("987");
272 wxYield();
273 CPPUNIT_ASSERT_EQUAL( "-.987", text2->GetValue() );
274
275 // But no more.
276 sim.Text("654");
277 wxYield();
278 CPPUNIT_ASSERT_EQUAL( "-.987", text2->GetValue() );
279
280 // We can remove one digit and another one though.
281 sim.Char(WXK_BACK);
282 sim.Char(WXK_BACK);
283 sim.Char('6');
284 wxYield();
285 CPPUNIT_ASSERT_EQUAL( "-.96", text2->GetValue() );
286
287
288 // Also test the range constraint.
289 text2->Clear();
290
291 sim.Char('9');
292 wxYield();
293 CPPUNIT_ASSERT_EQUAL( "9", text2->GetValue() );
294
295 sim.Char('9');
296 wxYield();
297 CPPUNIT_ASSERT_EQUAL( "9", text2->GetValue() );
298}
299
300#endif // wxUSE_UIACTIONSIMULATOR