1 | /* |
---|
2 | * This routine is the initialization task for this test program. |
---|
3 | * It is called from init_exec and has the responsibility for creating |
---|
4 | * and starting the tasks that make up the test. If the time of day |
---|
5 | * clock is required for the test, it should also be set to a known |
---|
6 | * value by this function. |
---|
7 | * |
---|
8 | * Input parameters: NONE |
---|
9 | * |
---|
10 | * Output parameters: NONE |
---|
11 | * |
---|
12 | * COPYRIGHT (c) 1994 by Division Incorporated |
---|
13 | * Based in part on OAR works. |
---|
14 | * |
---|
15 | * The license and distribution terms for this file may be |
---|
16 | * found in the file LICENSE in this distribution or at |
---|
17 | * http://www.rtems.org/license/LICENSE. |
---|
18 | * |
---|
19 | * |
---|
20 | * by Rosimildo da Silva: |
---|
21 | * Modified the test a bit to indicate when an instance is |
---|
22 | * global or not, and added code to test C++ exception. |
---|
23 | */ |
---|
24 | |
---|
25 | #include <rtems.h> |
---|
26 | #include <rtems/bspIo.h> |
---|
27 | #include <rtems/test.h> |
---|
28 | |
---|
29 | #include <cstdio> |
---|
30 | #include <cstdlib> |
---|
31 | #include <stdexcept> |
---|
32 | |
---|
33 | #ifdef RTEMS_TEST_IO_STREAM |
---|
34 | #include <iostream> |
---|
35 | #endif |
---|
36 | |
---|
37 | const char rtems_test_name[] = "CONSTRUCTOR/DESTRUCTOR"; |
---|
38 | |
---|
39 | extern "C" |
---|
40 | { |
---|
41 | #include <tmacros.h> |
---|
42 | extern rtems_task main_task(rtems_task_argument); |
---|
43 | } |
---|
44 | |
---|
45 | static int num_inst = 0; |
---|
46 | |
---|
47 | static void check_begin_of_test(void) |
---|
48 | { |
---|
49 | if ( num_inst == 0 ) { |
---|
50 | printf(TEST_BEGIN_STRING); |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | static void check_end_of_test(void) |
---|
55 | { |
---|
56 | if ( num_inst == 0 ) { |
---|
57 | printk(TEST_END_STRING); |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | class AClass { |
---|
62 | public: |
---|
63 | AClass(const char *p = "LOCAL" ) : ptr( p ) |
---|
64 | { |
---|
65 | check_begin_of_test(); |
---|
66 | num_inst++; |
---|
67 | printf( |
---|
68 | "%s: Hey I'm in base class constructor number %d for %p.\n", |
---|
69 | p, num_inst, this |
---|
70 | ); |
---|
71 | |
---|
72 | /* |
---|
73 | * Make sure we use some space |
---|
74 | */ |
---|
75 | |
---|
76 | string = new char[50]; |
---|
77 | sprintf(string, "Instantiation order %d", num_inst); |
---|
78 | }; |
---|
79 | |
---|
80 | virtual ~AClass() |
---|
81 | { |
---|
82 | // MUST USE PRINTK -- RTEMS IS SHUTTING DOWN WHEN THIS RUNS |
---|
83 | printk( |
---|
84 | "%s: Hey I'm in base class destructor number %d for %p.\n", |
---|
85 | ptr, num_inst, this |
---|
86 | ); |
---|
87 | printk("Derived class - %s\n", string); |
---|
88 | num_inst--; |
---|
89 | check_end_of_test(); |
---|
90 | }; |
---|
91 | |
---|
92 | virtual void print() { printf("%s\n", string); }; |
---|
93 | |
---|
94 | protected: |
---|
95 | char *string; |
---|
96 | const char *ptr; |
---|
97 | }; |
---|
98 | |
---|
99 | class BClass : public AClass { |
---|
100 | public: |
---|
101 | BClass(const char *p = "LOCAL" ) : AClass( p ) |
---|
102 | { |
---|
103 | check_begin_of_test(); |
---|
104 | num_inst++; |
---|
105 | printf( |
---|
106 | "%s: Hey I'm in derived class constructor number %d for %p.\n", |
---|
107 | p, num_inst, this |
---|
108 | ); |
---|
109 | |
---|
110 | /* |
---|
111 | * Make sure we use some space |
---|
112 | */ |
---|
113 | |
---|
114 | string = new char[50]; |
---|
115 | sprintf(string, "Instantiation order %d", num_inst); |
---|
116 | }; |
---|
117 | |
---|
118 | ~BClass() |
---|
119 | { |
---|
120 | printk( |
---|
121 | "%s: Hey I'm in derived class destructor number %d for %p.\n", |
---|
122 | ptr, num_inst, |
---|
123 | this |
---|
124 | ); |
---|
125 | printk("Derived class - %s\n", string); |
---|
126 | num_inst--; |
---|
127 | check_end_of_test(); |
---|
128 | }; |
---|
129 | |
---|
130 | void print() { printf("Derived class - %s\n", string); } |
---|
131 | }; |
---|
132 | |
---|
133 | |
---|
134 | class RtemsException |
---|
135 | { |
---|
136 | public: |
---|
137 | |
---|
138 | RtemsException( const char *module, int ln, int err = 0 ) |
---|
139 | : error( err ), line( ln ), file( module ) |
---|
140 | { |
---|
141 | printf( "RtemsException raised=File:%s, Line:%d, Error=%X\n", |
---|
142 | file, line, error ); |
---|
143 | } |
---|
144 | |
---|
145 | void show() |
---|
146 | { |
---|
147 | printf( "RtemsException ---> File:%s, Line:%d, Error=%X\n", |
---|
148 | file, line, error ); |
---|
149 | } |
---|
150 | |
---|
151 | private: |
---|
152 | int error; |
---|
153 | int line; |
---|
154 | const char *file; |
---|
155 | |
---|
156 | }; |
---|
157 | |
---|
158 | |
---|
159 | |
---|
160 | AClass foo( "GLOBAL" ); |
---|
161 | BClass foobar( "GLOBAL" ); |
---|
162 | |
---|
163 | void |
---|
164 | cdtest(void) |
---|
165 | { |
---|
166 | AClass bar, blech, blah; |
---|
167 | BClass bleak; |
---|
168 | |
---|
169 | #ifdef RTEMS_TEST_IO_STREAM |
---|
170 | std::cout << "Testing a C++ I/O stream" << std::endl; |
---|
171 | #else |
---|
172 | printf("IO Stream not tested\n"); |
---|
173 | #endif |
---|
174 | bar = blech; |
---|
175 | rtems_task_wake_after( 5 * rtems_clock_get_ticks_per_second() ); |
---|
176 | } |
---|
177 | |
---|
178 | // |
---|
179 | // main equivalent |
---|
180 | // It can not be called 'main' since the bsp owns that name |
---|
181 | // in many implementations in order to get global constructors |
---|
182 | // run. |
---|
183 | // |
---|
184 | |
---|
185 | static void foo_function() |
---|
186 | { |
---|
187 | try |
---|
188 | { |
---|
189 | throw "foo_function() throw this exception"; |
---|
190 | } |
---|
191 | catch( const char *e ) |
---|
192 | { |
---|
193 | printf( "foo_function() catch block called:\n < %s >\n", e ); |
---|
194 | throw "foo_function() re-throwing execption..."; |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | rtems_task main_task( |
---|
199 | rtems_task_argument |
---|
200 | ) |
---|
201 | { |
---|
202 | cdtest(); |
---|
203 | |
---|
204 | printf( "*** TESTING C++ EXCEPTIONS ***\n\n" ); |
---|
205 | |
---|
206 | try |
---|
207 | { |
---|
208 | foo_function(); |
---|
209 | } |
---|
210 | catch( const char *e ) |
---|
211 | { |
---|
212 | printf( "Success catching a char * exception\n%s\n", e ); |
---|
213 | } |
---|
214 | |
---|
215 | try |
---|
216 | { |
---|
217 | throw std::runtime_error("thrown std::runtime object"); |
---|
218 | } |
---|
219 | catch (std::exception const& e) |
---|
220 | { |
---|
221 | printf("throw std::runtime: caught: %s\n", e.what()); |
---|
222 | } |
---|
223 | |
---|
224 | try |
---|
225 | { |
---|
226 | printf( "throw an instance based exception\n" ); |
---|
227 | throw RtemsException( __FILE__, __LINE__, 0x55 ); |
---|
228 | } |
---|
229 | catch( RtemsException & ex ) |
---|
230 | { |
---|
231 | printf( "Success catching RtemsException...\n" ); |
---|
232 | ex.show(); |
---|
233 | } |
---|
234 | catch(...) |
---|
235 | { |
---|
236 | printf( "Caught another exception.\n" ); |
---|
237 | } |
---|
238 | printf( "Exceptions are working properly.\n" ); |
---|
239 | rtems_task_wake_after( 5 * rtems_clock_get_ticks_per_second() ); |
---|
240 | printf( "Global Dtors should be called after this line....\n" ); |
---|
241 | exit(0); |
---|
242 | } |
---|