2008-08-29 19:28:42 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: tests/thread/tls.cpp
|
|
|
|
// Purpose: wxTlsValue unit test
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// Created: 2008-08-28
|
|
|
|
// Copyright: (c) 2008 Vadim Zeitlin
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "testprec.h"
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#endif // WX_PRECOMP
|
|
|
|
|
|
|
|
#include "wx/thread.h"
|
|
|
|
#include "wx/tls.h"
|
|
|
|
|
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-01 14:15:24 -04:00
|
|
|
#include <string>
|
|
|
|
|
2008-08-29 19:28:42 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// globals
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
// NB: this struct must be a POD, so don't use wxString as its member
|
|
|
|
struct PerThreadData
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
int number;
|
|
|
|
};
|
|
|
|
|
|
|
|
wxTLS_TYPE(PerThreadData) gs_threadDataVar;
|
|
|
|
#define gs_threadData wxTLS_VALUE(gs_threadDataVar)
|
|
|
|
|
|
|
|
wxTLS_TYPE(int) gs_threadIntVar;
|
|
|
|
#define gs_threadInt wxTLS_VALUE(gs_threadIntVar)
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// test thread
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// this thread arbitrarily modifies all global thread-specific variables to
|
|
|
|
// make sure that the changes in it are not visible from the main thread
|
|
|
|
class TLSTestThread : public wxThread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// ctor both creates and starts the thread
|
|
|
|
TLSTestThread() : wxThread(wxTHREAD_JOINABLE) { Create(); Run(); }
|
|
|
|
|
2018-09-21 13:46:49 -04:00
|
|
|
virtual void *Entry() wxOVERRIDE
|
2008-08-29 19:28:42 -04:00
|
|
|
{
|
|
|
|
gs_threadInt = 17;
|
|
|
|
|
|
|
|
gs_threadData.name = "worker";
|
|
|
|
gs_threadData.number = 2;
|
|
|
|
|
2017-11-01 17:23:15 -04:00
|
|
|
// We can't use Catch asserts outside of the main thread,
|
|
|
|
// unfortunately.
|
|
|
|
wxASSERT( gs_threadData.name == std::string("worker") );
|
|
|
|
wxASSERT( gs_threadData.number == 2 );
|
2008-08-29 19:28:42 -04:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// test class
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class TLSTestCase : public CppUnit::TestCase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TLSTestCase() { }
|
|
|
|
|
|
|
|
private:
|
|
|
|
CPPUNIT_TEST_SUITE( TLSTestCase );
|
|
|
|
CPPUNIT_TEST( TestInt );
|
|
|
|
CPPUNIT_TEST( TestStruct );
|
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
void TestInt();
|
|
|
|
void TestStruct();
|
|
|
|
|
2015-04-23 07:49:01 -04:00
|
|
|
wxDECLARE_NO_COPY_CLASS(TLSTestCase);
|
2008-08-29 19:28:42 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// register in the unnamed registry so that these tests are run by default
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( TLSTestCase );
|
|
|
|
|
2011-04-30 06:57:04 -04:00
|
|
|
// also include in its own registry so that these tests can be run alone
|
2008-08-29 19:28:42 -04:00
|
|
|
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TLSTestCase, "TLSTestCase" );
|
|
|
|
|
|
|
|
void TLSTestCase::TestInt()
|
|
|
|
{
|
|
|
|
CPPUNIT_ASSERT_EQUAL( 0, gs_threadInt );
|
|
|
|
|
|
|
|
gs_threadInt++;
|
|
|
|
CPPUNIT_ASSERT_EQUAL( 1, gs_threadInt );
|
|
|
|
|
|
|
|
TLSTestThread().Wait();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL( 1, gs_threadInt );
|
|
|
|
}
|
|
|
|
|
|
|
|
void TLSTestCase::TestStruct()
|
|
|
|
{
|
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-01 14:15:24 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( NULL, gs_threadData.name );
|
2008-08-29 19:28:42 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( 0, gs_threadData.number );
|
|
|
|
|
|
|
|
gs_threadData.name = "main";
|
|
|
|
gs_threadData.number = 1;
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT_EQUAL( 1, gs_threadData.number );
|
|
|
|
|
|
|
|
TLSTestThread().Wait();
|
|
|
|
|
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-01 14:15:24 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( std::string("main"), gs_threadData.name );
|
2008-08-29 19:28:42 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL( 1, gs_threadData.number );
|
|
|
|
}
|
|
|
|
|