]> git.saurik.com Git - wxWidgets.git/blame_incremental - tests/thread/tls.cpp
Add wx/meta/removeref.h header defining wxRemoveRef<> helper.
[wxWidgets.git] / tests / thread / tls.cpp
... / ...
CommitLineData
1///////////////////////////////////////////////////////////////////////////////
2// Name: tests/thread/tls.cpp
3// Purpose: wxTlsValue unit test
4// Author: Vadim Zeitlin
5// Created: 2008-08-28
6// RCS-ID: $Id$
7// Copyright: (c) 2008 Vadim Zeitlin
8///////////////////////////////////////////////////////////////////////////////
9
10// ----------------------------------------------------------------------------
11// headers
12// ----------------------------------------------------------------------------
13
14#include "testprec.h"
15
16#ifdef __BORLANDC__
17 #pragma hdrstop
18#endif
19
20#ifndef WX_PRECOMP
21#endif // WX_PRECOMP
22
23#include "wx/thread.h"
24#include "wx/tls.h"
25
26// ----------------------------------------------------------------------------
27// globals
28// ----------------------------------------------------------------------------
29
30namespace
31{
32
33// NB: this struct must be a POD, so don't use wxString as its member
34struct PerThreadData
35{
36 const char *name;
37 int number;
38};
39
40wxTLS_TYPE(PerThreadData) gs_threadDataVar;
41#define gs_threadData wxTLS_VALUE(gs_threadDataVar)
42
43wxTLS_TYPE(int) gs_threadIntVar;
44#define gs_threadInt wxTLS_VALUE(gs_threadIntVar)
45
46// ----------------------------------------------------------------------------
47// test thread
48// ----------------------------------------------------------------------------
49
50// this thread arbitrarily modifies all global thread-specific variables to
51// make sure that the changes in it are not visible from the main thread
52class TLSTestThread : public wxThread
53{
54public:
55 // ctor both creates and starts the thread
56 TLSTestThread() : wxThread(wxTHREAD_JOINABLE) { Create(); Run(); }
57
58 virtual void *Entry()
59 {
60 gs_threadInt = 17;
61
62 gs_threadData.name = "worker";
63 gs_threadData.number = 2;
64
65 CPPUNIT_ASSERT_EQUAL( "worker", gs_threadData.name );
66 CPPUNIT_ASSERT_EQUAL( 2, gs_threadData.number );
67
68 return NULL;
69 }
70};
71
72} // anonymous namespace
73
74// ----------------------------------------------------------------------------
75// test class
76// ----------------------------------------------------------------------------
77
78class TLSTestCase : public CppUnit::TestCase
79{
80public:
81 TLSTestCase() { }
82
83private:
84 CPPUNIT_TEST_SUITE( TLSTestCase );
85 CPPUNIT_TEST( TestInt );
86 CPPUNIT_TEST( TestStruct );
87 CPPUNIT_TEST_SUITE_END();
88
89 void TestInt();
90 void TestStruct();
91
92 DECLARE_NO_COPY_CLASS(TLSTestCase)
93};
94
95// register in the unnamed registry so that these tests are run by default
96CPPUNIT_TEST_SUITE_REGISTRATION( TLSTestCase );
97
98// also include in its own registry so that these tests can be run alone
99CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TLSTestCase, "TLSTestCase" );
100
101void TLSTestCase::TestInt()
102{
103 CPPUNIT_ASSERT_EQUAL( 0, gs_threadInt );
104
105 gs_threadInt++;
106 CPPUNIT_ASSERT_EQUAL( 1, gs_threadInt );
107
108 TLSTestThread().Wait();
109
110 CPPUNIT_ASSERT_EQUAL( 1, gs_threadInt );
111}
112
113void TLSTestCase::TestStruct()
114{
115 CPPUNIT_ASSERT_EQUAL( "", gs_threadData.name );
116 CPPUNIT_ASSERT_EQUAL( 0, gs_threadData.number );
117
118 gs_threadData.name = "main";
119 gs_threadData.number = 1;
120
121 CPPUNIT_ASSERT_EQUAL( 1, gs_threadData.number );
122
123 TLSTestThread().Wait();
124
125 CPPUNIT_ASSERT_EQUAL( "main", gs_threadData.name );
126 CPPUNIT_ASSERT_EQUAL( 1, gs_threadData.number );
127}
128