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