Commit Graph

297 Commits

Author SHA1 Message Date
Vadim Zeitlin
0f8e976ac3 Add wxString::utf8_string()
This adds a yet another conversion function, which is not ideal, but
still better than having to write ToStdString(wxConvUTF8) every time for
losslessly converting wxString to std::string: not only this is too
long, but it's also too easy to forget to specify wxConvUTF8, resulting
in data loss when using non-UTF-8 locale.
2021-03-06 15:12:07 +01:00
Vadim Zeitlin
0aacc6a9ab Remove CppUnit boilerplate in std::string interoperability tests
No real changes, just don't use compatibility macros and remove the
completely useless test case.
2021-03-06 15:05:23 +01:00
Vadim Zeitlin
94289a46b1 Relax check for converting "-" to long long in the unit tests
This somehow succeeds when using gcc 4.8 under Ubuntu 14.04 or MinGW
5.3, so don't fail the tests in this case, but still warn about it
because it seems quite unexpected.
2021-02-22 12:57:58 +01:00
Vadim Zeitlin
a91cb5c344 Use CHECK_FALSE in wxNumberFormatter unit tests
Just replace CHECK(!condition) with CHECK_FALSE(condition), this should
result in slightly more clear error messages.

Also make tests for long and long long more consistent.
2021-02-22 11:55:32 +01:00
Vadim Zeitlin
eb64202ad4 Add support for unsigned long long to wxNumberFormatter
This is necessary in order to deal with the numbers greater than
wxINT64_MAX that can't be represented in just long long.

It also allows to implement the intuitive handling of minus sign for the
unsigned numbers, i.e. not to accept it in FromString(), unlike the
standard functions which do (and parse -1 as 0xffff...fff).

Also extend the tests to check for more boundary cases.
2021-02-22 11:49:19 +01:00
Vadim Zeitlin
959d955a80 Make wxNumberFormatter tests more concise and readable
Define a couple of helper functions to avoid over long lines or having
to break them and also align the tests vertically to allow scanning them
more easily.

No real changes.
2021-02-21 17:13:01 +01:00
Vadim Zeitlin
a015270709 Replace CPPUNIT_ASSERT() macros in wxNumberFormatter unit tests
Just use CHECK() rather than CPPUNIT_ASSERT_EQUAL() etc.
2021-02-21 17:02:55 +01:00
Vadim Zeitlin
5ebce65494 Get rid of CppUnit boilerplate in wxNumberFormatter unit tests
No real changes, just drop CppUnit::TestCase inheritance and the legacy
macros and use TEST_CASE_METHOD() instead.
2021-02-21 16:52:07 +01:00
Vadim Zeitlin
344cc940a0 Impose upper limit on memory allocation in wxString::PrintfV()
Don't loop indefinitely until we run out of memory, possibly after
wrapping around INT_MAX, but impose an arbitrary limit of 128MiB for the
max allocation done by wxString::PrintfV() when the provided format
string or one of the arguments are invalid.

This notably fixes a crash when trying to use "%c" to output an invalid
Unicode character.

Also improve comment explaining DoStringPrintfV() logic and change the
size type to size_t from int.

Co-Authored-By: Arrigo Marchiori <ardovm@yahoo.it>
2020-12-01 21:53:55 +01:00
Vadim Zeitlin
8fb4ab99f1 Always return -1 but set errno in our wxVsnprintf() on error
This makes it more compatible with the standard behaviour of vswprintf()
and allows to use the same logic in the builds using our version of this
function and the normal ones in DoStringPrintfV(), simplifying its
(already rather hairy) logic.

Update the tests to not require any particular return value in case of
buffer overflow, as this differs between Unicode and non-Unicode builds.
When we finally drop the latter, we should just check that it always
returns -1 in this case.

Note that ideal would be to return the actually needed size of the
buffer in case of the error due to buffer being too small, but this
isn't that simple to do and it's probably not worth spending time on
improving this code as long as we still need to use the buffer doubling
strategy in DoStringPrintfV() when using the standard vswprintf().
2020-12-01 21:53:55 +01:00
Vadim Zeitlin
7a2786bf11 Get rid of CppUnit macros in wxVsnprintf() tests too
Use CATCH macros directly.

Also remove some unnecessary casts and wxT().
2020-12-01 00:53:07 +01:00
Vadim Zeitlin
388dfb9fad Replace various CMPn() macros with a single CMP() vararg one
Reduce duplication and simplify the test code by using the same macro
for any number of printf() arguments.

Note that this relies on "##__VA_ARGS__" gcc extension to work even for
CMP0(), i.e. when there are no parameters at all. If this ever becomes a
problem, we can always reintroduce a separate CMP_NO_ARGS() macro.
2020-12-01 00:51:35 +01:00
Vadim Zeitlin
db9727ac26 Remove CppUnit test case boilerplate from wxVsnprintf() unit test
Use separate CATCH test cases for different tests.

No real changes, just update and simplify.
2020-11-30 17:17:20 +01:00
Vadim Zeitlin
f4db86cc77 Fix signed/unsigned comparison warnings in wxVsnprintf test
These warnings only appeared in build configurations using our own
implementation, i.e. not in the default build.
2020-11-30 17:10:05 +01:00
Vadim Zeitlin
9e4d51dfca Disable tests not working in ANSI build
Most of them can't be expected to work, e.g. non-ASCII file names can't
be supported without Unicode support.

Some others, e.g. test for NULs in wxDataOutputStream, are questionable,
as it seems that it might be possible to fix them in ANSI build too, but
for now just do the simplest thing to make the tests pass on Travis.
2020-10-18 23:47:29 +02:00
PB
f57f214122 Remove BCC-specific hdrstop pragma from everywhere 2020-10-12 21:58:37 +02:00
Vadim Zeitlin
00cdab77c5 Define wxStrtox() overloads taking nullptr
Instead of specializing wxStrtoxCharType and then testing whether endptr
is null, just define separate, and simpler, overloads of wxStrtox()
functions taking nullptr_t -- we can avoid the unnecessary test
completely in this case, as nullptr is, by definition, always null
anyhow.

Also add a test of using wxStrtol() with nullptr too.

This should fix the build with older gcc and MSVS versions.
2020-02-05 03:54:25 +01:00
Paul Cornett
948ddc6e0f Eliminate -Wcast-qual warnings with GCC and Clang
Use const_cast, mutable, and various other changes to avoid -Wcast-qual
2020-02-02 22:50:32 -08:00
Vadim Zeitlin
55efc9e607 Allow using wxStrtox() functions with nullptr with MSVS too
Add a unit test checking that it compiles (and works).

This extends the changes of 63b1f00eb8 to
cover MSVS as well.
2020-01-31 17:13:06 +01:00
Vadim Zeitlin
c34a7a2e30 Convert CRT functions test case to use CATCH macros directly
Get rid of legacy CppUnit-compatible macros.

No real changes.
2020-01-31 16:54:06 +01:00
Vadim Zeitlin
8cba51978e Add a check for using wxString::Format() with a very long string
This used to fail until it was fixed in the previous commit.

See #18586.
2019-11-17 18:30:33 +01:00
Vadim Zeitlin
b690caba84 Replace CPPUNIT_XXX macros with CHECKs
This results in better error messages and also allows the test to
continue executing even if a check fails.
2019-11-16 17:05:06 +01:00
Vadim Zeitlin
505e694347 Get rid of useless VarArgTestCase test fixture
We don't need it any more, just define individual tests as independent
test cases.

This required using some other pointer instead of "this" in one of the
tests, but it doesn't really matter which pointer we use there anyhow.

No real changes.
2019-11-16 17:01:57 +01:00
Lauri Nurmi
4739b9dedd Add C++20-style starts_with()/ends_with() functions to wxString
C++20 introduces these two functions, along with some overloads, to
std::string. Add similar functions to wxString, which simply call the
already existing StartsWith() and EndsWith().

Closes https://github.com/wxWidgets/wxWidgets/pull/1452
2019-07-31 00:52:29 +02:00
Vadim Zeitlin
91b3bfedf8 Fix using std::reverse() with wxString iterators in a proper way
The solution with specializing std::iter_swap() for wxString::iterator
was not conforming as the iterator was still not swappable, as it is
required to be.

Fix this by providing std::swap() overload for wxString::iterator, which
is correct and even simpler.

This allows std::reverse(s.begin(), s.end()) work with clang too and
incidentally avoids warnings about the code relying on non-conforming
extensions with MSVS 2017 which were due to the fact that iter_swap()
workaround wasn't enabled for it, while the new swap() overload is.
2019-04-26 03:20:38 +02:00
Vadim Zeitlin
5a78b82673 Add support for passing nullptr to wx pseudo-vararg functions
Allow passing literal nullptr as an argument corresponding to "%p" in
the format string.

See https://github.com/wxWidgets/wxWidgets/pull/1251

Closes #18355.
2019-03-09 01:14:31 +01:00
Vadim Zeitlin
2a907769fb Suppress all -Wmissing-field-initializers in the test suite
There were many dozens of such warnings given for the various arrays
used in the tests and it just doesn't seem feasible nor desirable to fix
them all, so just suppress the warnings for all these arrays to get rid
of them with recent (7+?) versions of gcc.
2019-02-02 17:25:37 +01:00
Vadim Zeitlin
dcee3ce899 Suppress some harmless clang warnings in the test suite
Use wxCLANG_WARNING_SUPPRESS() to avoid multiple warnings in CATCH
headers as well as in some of our own tests.
2019-01-04 23:51:31 +01:00
Maarten Bent
7c1ab06ea5 Add more wxOVERRIDE 2018-09-22 14:44:07 +02:00
Maarten Bent
bcfc9a8a26 Fix printf tests for recent MinGW versions
The default mantissa length was changed from 3 to 2 in MinGW 5.0.4.
Fix the test for printing pointer addresses in 64bit MinGW.
2018-08-02 21:10:20 +02:00
Maarten Bent
f3ae302b4d Prevent segfault in vararg test with MinGW
The segmentation fault is caused by swscanf with __USE_MINGW_ANSI_STDIO=1.
See https://sourceforge.net/p/mingw-w64/mailman/message/36118530/
2018-08-02 20:50:55 +02:00
Maarten Bent
57180d68c8 Add wxOVERRIDE to test files
And cleanup some tailing spaces and tabs.
2018-07-29 12:08:53 +02:00
pavel-t
9c29b6de48 Fix building tests with wxUSE_STL=1 and wxUSE_UNSAFE_WXSTRING_CONV=0 2018-02-08 10:05:21 +02:00
Vadim Zeitlin
4e4617a9e6 Show value if comparison fails in VarArgTestCase
Use string comparison instead of wxStrcmp() to view the actual value of
the string if it doesn't have the expected value.
2017-11-25 17:28:42 +01:00
Vadim Zeitlin
471a771382 Fix built-in wxPrintf() for repeated positional parameters
Allow wxPrintf("%1$s %1$s", "foo") to work.

Take into account the possibility that the number of format specifiers
and the number of actual arguments can be different.

Closes #9367.
2017-11-25 11:13:02 +01:00
Vadim Zeitlin
a50541795c Compilation fix for VsnprintfTestCase with Catch
Rewrite an assert using "||" as 2 asserts to allow it compile with Catch
(this wasn't detected before as this code is only compiled when using
our own vsnprintf() implementation, which is usually not the case).
2017-11-24 22:55:11 +01:00
Vadim Zeitlin
9c1964ee1d Fix a unit test in UTF-8 build using UTF-8 strings only
Avoid the optimized mb_str() available in this build returning directly
a "char*" as it doesn't preserve the length of the string if it contains
NULs. Use mb_str(wxMBConv) overload instead which always returns the
buffer of the correct length.

Arguably, this is a problem of wxString API and maybe this optimized
mb_str() overload shouldn't be provided at all. OTOH the main reason for
wxUSE_UTF8_LOCALE_ONLY existence is optimization, so it seems a pity to
deliberately make the code less efficient when using it.
2017-11-03 23:11:32 +01:00
Vadim Zeitlin
e70fc11ef1 Replace CppUnit with Catch for unit tests
Drop the legacy CppUnit testing framework used for the unit tests.
Replacing it with Catch has the advantage of not requiring CppUnit
libraries to be installed on the system in order to be able to run
tests (Catch is header-only and a copy of it is now included in the
main repository itself) and, in the future, of being able to write
the tests in a much more natural way.

For now, however, avoid changing the existing tests code as much as
[reasonably] possible to avoid introducing bugs in them and provide
the CppUnit compatibility macros in the new wx/catch_cppunit.h header
which allow to preserve the 99% of the existing code unchanged. Some
of the required changes are:

 - Decompose asserts using "a && b" conditions into multiple asserts
   checking "a" and "b" independently. This would have been better
   even with CppUnit (to know which part of condition exactly failed)
   and is required with Catch.

 - Use extra parentheses around such conditions when they can't be
   easily decomposed in the arrays test, due to the use of macros.
   This is not ideal from the point of view of messages given when
   the tests fail but will do for now.

 - Rewrite asserts using "a || b" as a combination of condition
   checks and assert macros. Again, this is better anyhow, and is
   required with Catch. Incidentally, this allowed to fix a bug in
   the "exec" unit test which didn't leave enough time for the new
   process to be launched before trying to kill it.

 - Remove multiple CPPUNIT_TEST_SUITE_NAMED_REGISTRATION() macros,
   our emulation of this macro can be used only once.

 - Provide string conversions using Catch-specific StringMaker for
   a couple of types.

 - Replace custom wxImage comparison with a Catch-specific matcher
   class.

 - Remove most of test running logic from test.cpp, in particular don't
   parse command line ourselves any longer but use Catch built-in
   command line parser. This is a source of a minor regression:
   previously, both "Foo" and "FooTestCase" could be used as the name of
   the test to run, but now only the latter is accepted.
2017-11-02 01:53:16 +01:00
Artur Wieczorek
8d94d4d9d3 Pass arguments of the same type to CPPUNIT_ASSERT_EQUAL
This is to avoid potential implicit conversions.
2017-07-30 09:56:43 +02:00
ARATA Mizuki
ad47857072 Add a test case for constructing wxString with supplementary wxUniChar values
See #11827
2017-05-01 18:18:47 +09:00
Cătălin Răceanu
e8207c8c59 Fix building tests with wxUSE_UNSAFE_WXSTRING_CONV==0
Avoid relying on implicit conversions to std::string in the tests code.
2017-02-24 15:37:37 +01:00
Artur Wieczorek
c6e4507c07 Add unit tests for wxHexToDec()
See #12814.
2017-02-23 20:50:18 +01:00
Artur Wieczorek
2ecd4a07ba Add unit tests for wxDecToHex() 2017-02-23 20:48:28 +01:00
Vadim Zeitlin
7122288416 Fix wxString::From[C]Double() unit test for MSVC 14 and later
Since MSVC 14, VC CRT uses standard-conforming 2 digits for the exponent
instead of the non-standard 3 used previously.
2016-12-09 17:44:04 +01:00
Vadim Zeitlin
fe2052fb35 Skip the recently added wxString test broken with libc++
This should have been part of 90eaa1bbe3 and
should finally really fix the build with libc++ after the changes of
14ec2691f6.
2016-07-16 01:46:45 +02:00
Vadim Zeitlin
14ec2691f6 Allow swapping wxString iterators in C++11 mode
Specialize std::iter_swap() for wxString::iterator in C++11 mode as the
default implementation of it doesn't work for iterators using "reference" type
different from "value_type&" as is the case for wxString::iterator.

This notably allows the code using std::reverse() with wxString iterators to
compile in C++11 mode, which wasn't the case before.
2016-07-14 16:36:24 +02:00
Lauri Nurmi
e45a9543d6 Fix unit test when char is unsigned by default
Skip test relying on char being signed if it isn't by default, as is the case
with gcc on some ARM platforms, for example.

Closes https://github.com/wxWidgets/wxWidgets/pull/299
2016-06-17 14:49:21 +02:00
Vadim Zeitlin
ba5a0a3878 Fix more typos in comments and wxFAIL messages
Closes https://github.com/wxWidgets/wxWidgets/pull/270
2016-04-14 16:08:28 +02:00
ARATA Mizuki
1809ade5ad Test the conversion of wxString from/to UTF-8 std::string in StdStringTestCase::StdConversion 2016-03-25 16:39:21 +09:00
ARATA Mizuki
a2f0374052 Fix check for string termination in IsValidUtf8String()
The current code incorrectly returned true if the string contained
an invalid UTF-8 sequence after an embedded NUL.

Check the entire string if the length was explicitly given instead of stopping
at the first NUL.

Closes https://github.com/wxWidgets/wxWidgets/pull/236
2016-02-27 16:42:30 +01:00