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