source: rtems/testsuites/samples/cdtest/main.cc @ ede1a41

Last change on this file since ede1a41 was ede1a41, checked in by Sebastian Huber <sebastian.huber@…>, on Jun 21, 2016 at 11:12:31 AM

Make rtems/print.h independent of rtems/bspIo.h

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