1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
///////////////////////////////////////////////////////////////////////////////
// Name: tests/validators/valnum.cpp
// Purpose: Unit tests for numeric validators.
// Author: Vadim Zeitlin
// Created: 2011-01-18
// Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
///////////////////////////////////////////////////////////////////////////////
#include "testprec.h"
#ifndef WX_PRECOMP
#include "wx/app.h"
#include "wx/intl.h"
#include "wx/textctrl.h"
#include "wx/validate.h"
#endif // WX_PRECOMP
#include "wx/valnum.h"
#include "asserthelper.h"
#include "testableframe.h"
#include "wx/scopeguard.h"
#include "wx/uiaction.h"
class NumValidatorTestCase
{
public:
NumValidatorTestCase();
~NumValidatorTestCase();
protected:
wxTextCtrl* const m_text;
wxDECLARE_NO_COPY_CLASS(NumValidatorTestCase);
};
NumValidatorTestCase::NumValidatorTestCase()
: m_text(new wxTextCtrl(wxTheApp->GetTopWindow(), wxID_ANY))
{
}
NumValidatorTestCase::~NumValidatorTestCase()
{
delete m_text;
}
TEST_CASE_METHOD(NumValidatorTestCase, "ValNum::TransferInt", "[valnum]")
{
int value = 0;
wxIntegerValidator<int> valInt(&value);
valInt.SetWindow(m_text);
CHECK( valInt.TransferToWindow() );
CHECK( m_text->GetValue() == "0" );
value = 17;
CHECK( valInt.TransferToWindow() );
CHECK( m_text->GetValue() == "17" );
m_text->ChangeValue("foobar");
CHECK( !valInt.TransferFromWindow() );
m_text->ChangeValue("-234");
CHECK( valInt.TransferFromWindow() );
CHECK( value == -234 );
m_text->ChangeValue("9223372036854775808"); // == LLONG_MAX + 1
CHECK( !valInt.TransferFromWindow() );
m_text->Clear();
CHECK( !valInt.TransferFromWindow() );
}
TEST_CASE_METHOD(NumValidatorTestCase, "ValNum::TransferUnsigned", "[valnum]")
{
unsigned value = 0;
wxIntegerValidator<unsigned> valUnsigned(&value);
valUnsigned.SetWindow(m_text);
CHECK( valUnsigned.TransferToWindow() );
CHECK( m_text->GetValue() == "0" );
value = 17;
CHECK( valUnsigned.TransferToWindow() );
CHECK( m_text->GetValue() == "17" );
m_text->ChangeValue("foobar");
CHECK( !valUnsigned.TransferFromWindow() );
m_text->ChangeValue("-234");
CHECK( !valUnsigned.TransferFromWindow() );
m_text->ChangeValue("234");
CHECK( valUnsigned.TransferFromWindow() );
CHECK( value == 234 );
m_text->ChangeValue("4294967295"); // == ULONG_MAX in 32 bits
CHECK( valUnsigned.TransferFromWindow() );
CHECK( value == wxUINT32_MAX );
CHECK( valUnsigned.TransferToWindow() );
CHECK( m_text->GetValue() == "4294967295" );
m_text->ChangeValue("4294967296"); // == ULONG_MAX + 1
CHECK( !valUnsigned.TransferFromWindow() );
m_text->ChangeValue("18446744073709551616"); // == ULLONG_MAX + 1
CHECK( !valUnsigned.TransferFromWindow() );
m_text->Clear();
CHECK( !valUnsigned.TransferFromWindow() );
}
TEST_CASE_METHOD(NumValidatorTestCase, "ValNum::TransferUnsignedRange", "[valnum]")
{
unsigned value = 1;
wxIntegerValidator<unsigned> valUnsigned(&value, 1, 20);
valUnsigned.SetWindow(m_text);
CHECK( valUnsigned.TransferToWindow() );
CHECK( m_text->GetValue() == "1" );
value = 17;
CHECK( valUnsigned.TransferToWindow() );
CHECK( m_text->GetValue() == "17" );
m_text->ChangeValue("foobar");
CHECK( !valUnsigned.TransferFromWindow() );
m_text->ChangeValue("0");
CHECK( !valUnsigned.TransferFromWindow() );
m_text->ChangeValue("1");
CHECK( valUnsigned.TransferFromWindow() );
CHECK( value == 1);
m_text->ChangeValue("20");
CHECK( valUnsigned.TransferFromWindow() );
CHECK( value == 20);
m_text->ChangeValue("21");
CHECK( !valUnsigned.TransferFromWindow() );
m_text->Clear();
CHECK( !valUnsigned.TransferFromWindow() );
}
TEST_CASE_METHOD(NumValidatorTestCase, "ValNum::TransferULL", "[valnum]")
{
unsigned long long value = 0;
wxIntegerValidator<unsigned long long> valULL(&value);
valULL.SetWindow(m_text);
SECTION("LLONG_MAX")
{
m_text->ChangeValue("9223372036854775807"); // == LLONG_MAX
REQUIRE( valULL.TransferFromWindow() );
CHECK( value == static_cast<wxULongLong_t>(wxINT64_MAX) );
REQUIRE( valULL.TransferToWindow() );
CHECK( m_text->GetValue() == "9223372036854775807" );
}
SECTION("LLONG_MAX+1")
{
m_text->ChangeValue("9223372036854775808"); // == LLONG_MAX + 1
REQUIRE( valULL.TransferFromWindow() );
CHECK( value == static_cast<wxULongLong_t>(wxINT64_MAX) + 1 );
REQUIRE( valULL.TransferToWindow() );
CHECK( m_text->GetValue() == "9223372036854775808" );
}
SECTION("ULLONG_MAX")
{
m_text->ChangeValue("18446744073709551615"); // == ULLONG_MAX
REQUIRE( valULL.TransferFromWindow() );
CHECK( value == wxUINT64_MAX );
REQUIRE( valULL.TransferToWindow() );
CHECK( m_text->GetValue() == "18446744073709551615" );
}
SECTION("ULLONG_MAX+1")
{
m_text->ChangeValue("18446744073709551616"); // == ULLONG_MAX + 1
CHECK( !valULL.TransferFromWindow() );
}
}
TEST_CASE_METHOD(NumValidatorTestCase, "ValNum::TransferFloat", "[valnum]")
{
// We need a locale with point as decimal separator.
wxLocale loc(wxLANGUAGE_ENGLISH_UK, wxLOCALE_DONT_LOAD_DEFAULT);
float value = 0;
wxFloatingPointValidator<float> valFloat(3, &value);
valFloat.SetWindow(m_text);
CHECK( valFloat.TransferToWindow() );
CHECK( m_text->GetValue() == "0.000" );
value = 1.234f;
CHECK( valFloat.TransferToWindow() );
CHECK( m_text->GetValue() == "1.234" );
value = 1.2345678f;
CHECK( valFloat.TransferToWindow() );
CHECK( m_text->GetValue() == "1.235" );
m_text->ChangeValue("foobar");
CHECK( !valFloat.TransferFromWindow() );
m_text->ChangeValue("-234.567");
CHECK( valFloat.TransferFromWindow() );
CHECK( value == -234.567f );
m_text->Clear();
CHECK( !valFloat.TransferFromWindow() );
}
TEST_CASE_METHOD(NumValidatorTestCase, "ValNum::ZeroAsBlank", "[valnum]")
{
long value = 0;
m_text->SetValidator(
wxMakeIntegerValidator(&value, wxNUM_VAL_ZERO_AS_BLANK));
wxValidator * const val = m_text->GetValidator();
CHECK( val->TransferToWindow() );
CHECK( m_text->GetValue() == "" );
value++;
CHECK( val->TransferFromWindow() );
CHECK( value == 0 );
}
TEST_CASE_METHOD(NumValidatorTestCase, "ValNum::NoTrailingZeroes", "[valnum]")
{
// We need a locale with point as decimal separator.
wxLocale loc(wxLANGUAGE_ENGLISH_UK, wxLOCALE_DONT_LOAD_DEFAULT);
double value = 1.2;
m_text->SetValidator(
wxMakeFloatingPointValidator(3, &value, wxNUM_VAL_NO_TRAILING_ZEROES));
wxValidator * const val = m_text->GetValidator();
CHECK( val->TransferToWindow() );
CHECK( m_text->GetValue() == "1.2" );
value = 1.234;
CHECK( val->TransferToWindow() );
CHECK( m_text->GetValue() == "1.234" );
}
#if wxUSE_UIACTIONSIMULATOR
TEST_CASE_METHOD(NumValidatorTestCase, "ValNum::Interactive", "[valnum]")
{
// Set a locale using comma as thousands separator character.
wxLocale loc(wxLANGUAGE_ENGLISH_UK, wxLOCALE_DONT_LOAD_DEFAULT);
m_text->SetValidator(
wxIntegerValidator<unsigned>(NULL, wxNUM_VAL_THOUSANDS_SEPARATOR));
// Create a sibling text control to be able to switch focus and thus
// trigger the control validation/normalization.
wxTextCtrl * const text2 = new wxTextCtrl(m_text->GetParent(), wxID_ANY);
wxON_BLOCK_EXIT_OBJ0( *text2, wxWindow::Destroy );
text2->Move(10, 80); // Just to see it better while debugging...
wxFloatingPointValidator<float> valFloat(3);
valFloat.SetRange(-10., 10.);
text2->SetValidator(valFloat);
wxUIActionSimulator sim;
// Entering '-' in a control with positive range is not allowed.
m_text->SetFocus();
sim.Char('-');
wxYield();
CHECK( m_text->GetValue() == "" );
// Neither is entering '.' or any non-digit character.
sim.Text(".a+/");
wxYield();
CHECK( m_text->GetValue() == "" );
// Entering digits should work though and after leaving the control the
// contents should be normalized.
sim.Text("1234567");
wxYield();
text2->SetFocus();
wxYield();
if ( loc.IsOk() )
CHECK( m_text->GetValue() == "1,234,567" );
else
CHECK( m_text->GetValue() == "1234567" );
// Entering both '-' and '.' in this control should work but only in the
// correct order.
sim.Char('-');
wxYield();
CHECK( text2->GetValue() == "-" );
text2->SetInsertionPoint(0);
sim.Char('.');
wxYield();
CHECK( text2->GetValue() == "-" );
text2->SetInsertionPointEnd();
sim.Char('.');
wxYield();
CHECK( text2->GetValue() == "-." );
// Adding up to three digits after the point should work.
sim.Text("987");
wxYield();
CHECK( text2->GetValue() == "-.987" );
// But no more.
sim.Text("654");
wxYield();
CHECK( text2->GetValue() == "-.987" );
// We can remove one digit and another one though.
sim.Char(WXK_BACK);
sim.Char(WXK_BACK);
sim.Char('6');
wxYield();
CHECK( text2->GetValue() == "-.96" );
// Also test the range constraint.
text2->Clear();
sim.Char('9');
wxYield();
CHECK( text2->GetValue() == "9" );
// Entering a value which is out of range is allowed.
sim.Char('9');
wxYield();
CHECK( text2->GetValue() == "99" );
// But it must be clamped to the valid range on focus loss.
m_text->SetFocus();
wxYield();
CHECK( text2->GetValue() == "10.000" );
// Repeat the test with a too small invalid value.
text2->Clear();
text2->SetFocus();
sim.Text("-22");
wxYield();
CHECK( text2->GetValue() == "-22" );
m_text->SetFocus();
wxYield();
CHECK( text2->GetValue() == "-10.000" );
}
#endif // wxUSE_UIACTIONSIMULATOR
|