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.OARcorp.com/rtems/license.html. |
---|
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 | * $Id$ |
---|
26 | */ |
---|
27 | // #define RTEMS_TEST_IO_STREAM |
---|
28 | |
---|
29 | #include <rtems.h> |
---|
30 | #include <stdio.h> |
---|
31 | #include <stdlib.h> |
---|
32 | #ifdef RTEMS_TEST_IO_STREAM |
---|
33 | #include <iostream.h> |
---|
34 | #endif |
---|
35 | |
---|
36 | extern "C" |
---|
37 | { |
---|
38 | #include <tmacros.h> |
---|
39 | extern rtems_task main_task(rtems_task_argument); |
---|
40 | } |
---|
41 | |
---|
42 | static int num_inst = 0; |
---|
43 | |
---|
44 | class A { |
---|
45 | public: |
---|
46 | A(const char *p = "LOCAL" ) : ptr( p ) |
---|
47 | { |
---|
48 | num_inst++; |
---|
49 | printf( |
---|
50 | "%s: Hey I'm in base class constructor number %d for %p.\n", |
---|
51 | p, num_inst, this |
---|
52 | ); |
---|
53 | |
---|
54 | /* |
---|
55 | * Make sure we use some space |
---|
56 | */ |
---|
57 | |
---|
58 | string = new char[50]; |
---|
59 | sprintf(string, "Instantiation order %d", num_inst); |
---|
60 | }; |
---|
61 | |
---|
62 | virtual ~A() |
---|
63 | { |
---|
64 | printf( |
---|
65 | "%s: Hey I'm in base class destructor number %d for %p.\n", |
---|
66 | ptr, num_inst, this |
---|
67 | ); |
---|
68 | print(); |
---|
69 | num_inst--; |
---|
70 | }; |
---|
71 | |
---|
72 | virtual void print() { printf("%s\n", string); }; |
---|
73 | |
---|
74 | protected: |
---|
75 | char *string; |
---|
76 | const char *ptr; |
---|
77 | }; |
---|
78 | |
---|
79 | class B : public A { |
---|
80 | public: |
---|
81 | B(const char *p = "LOCAL" ) : A( p ) |
---|
82 | { |
---|
83 | num_inst++; |
---|
84 | printf( |
---|
85 | "%s: Hey I'm in derived class constructor number %d for %p.\n", |
---|
86 | p, num_inst, this |
---|
87 | ); |
---|
88 | |
---|
89 | /* |
---|
90 | * Make sure we use some space |
---|
91 | */ |
---|
92 | |
---|
93 | string = new char[50]; |
---|
94 | sprintf(string, "Instantiation order %d", num_inst); |
---|
95 | }; |
---|
96 | |
---|
97 | ~B() |
---|
98 | { |
---|
99 | printf( |
---|
100 | "%s: Hey I'm in derived class destructor number %d for %p.\n", |
---|
101 | ptr, num_inst, |
---|
102 | this |
---|
103 | ); |
---|
104 | print(); |
---|
105 | num_inst--; |
---|
106 | }; |
---|
107 | |
---|
108 | void print() { printf("Derived class - %s\n", string); } |
---|
109 | }; |
---|
110 | |
---|
111 | |
---|
112 | class RtemsException |
---|
113 | { |
---|
114 | public: |
---|
115 | |
---|
116 | RtemsException( char *module, int ln, int err = 0 ) |
---|
117 | : error( err ), line( ln ), file( module ) |
---|
118 | { |
---|
119 | printf( "RtemsException raised=File:%s, Line:%d, Error=%X\n", |
---|
120 | file, line, error ); |
---|
121 | } |
---|
122 | |
---|
123 | void show() |
---|
124 | { |
---|
125 | printf( "RtemsException ---> File:%s, Line:%d, Error=%X\n", |
---|
126 | file, line, error ); |
---|
127 | } |
---|
128 | |
---|
129 | private: |
---|
130 | int error; |
---|
131 | int line; |
---|
132 | char *file; |
---|
133 | |
---|
134 | }; |
---|
135 | |
---|
136 | |
---|
137 | |
---|
138 | A foo( "GLOBAL" ); |
---|
139 | B foobar( "GLOBAL" ); |
---|
140 | |
---|
141 | void |
---|
142 | cdtest(void) |
---|
143 | { |
---|
144 | A bar, blech, blah; |
---|
145 | B bleak; |
---|
146 | |
---|
147 | #ifdef RTEMS_TEST_IO_STREAM |
---|
148 | cout << "Testing a C++ I/O stream" << endl; |
---|
149 | #else |
---|
150 | printf("IO Stream not tested\n"); |
---|
151 | #endif |
---|
152 | bar = blech; |
---|
153 | rtems_task_wake_after( 5 * get_ticks_per_second() ); |
---|
154 | } |
---|
155 | |
---|
156 | // |
---|
157 | // main equivalent |
---|
158 | // It can not be called 'main' since the bsp owns that name |
---|
159 | // in many implementations in order to get global constructors |
---|
160 | // run. |
---|
161 | // |
---|
162 | |
---|
163 | static void foo_function() |
---|
164 | { |
---|
165 | try |
---|
166 | { |
---|
167 | throw "foo_function() throw this exception"; |
---|
168 | } |
---|
169 | catch( const char *e ) |
---|
170 | { |
---|
171 | printf( "foo_function() catch block called:\n < %s >\n", e ); |
---|
172 | throw "foo_function() re-throwing execption..."; |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | rtems_task main_task( |
---|
177 | rtems_task_argument |
---|
178 | ) |
---|
179 | { |
---|
180 | printf( "\n\n*** CONSTRUCTOR/DESTRUCTOR TEST ***\n" ); |
---|
181 | |
---|
182 | cdtest(); |
---|
183 | |
---|
184 | printf( "*** END OF CONSTRUCTOR/DESTRUCTOR TEST ***\n\n\n" ); |
---|
185 | |
---|
186 | |
---|
187 | printf( "*** TESTING C++ EXCEPTIONS ***\n\n" ); |
---|
188 | |
---|
189 | try |
---|
190 | { |
---|
191 | foo_function(); |
---|
192 | } |
---|
193 | catch( const char *e ) |
---|
194 | { |
---|
195 | printf( "Success catching a char * exception\n%s\n", e ); |
---|
196 | } |
---|
197 | try |
---|
198 | { |
---|
199 | printf( "throw an instance based exception\n" ); |
---|
200 | throw RtemsException( __FILE__, __LINE__, 0x55 ); |
---|
201 | } |
---|
202 | catch( RtemsException & ex ) |
---|
203 | { |
---|
204 | printf( "Success catching RtemsException...\n" ); |
---|
205 | ex.show(); |
---|
206 | } |
---|
207 | catch(...) |
---|
208 | { |
---|
209 | printf( "Caught another exception.\n" ); |
---|
210 | } |
---|
211 | printf( "Exceptions are working properly.\n" ); |
---|
212 | rtems_task_wake_after( 5 * get_ticks_per_second() ); |
---|
213 | printf( "Global Dtors should be called after this line....\n" ); |
---|
214 | exit(0); |
---|
215 | } |
---|