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

4.9
Last change on this file since beda88f was beda88f, checked in by Joel Sherrill <joel.sherrill@…>, on Mar 2, 2009 at 5:28:35 PM

2009-03-02 Joel Sherrill <joel.sherrill@…>

  • cdtest/main.cc: Use printk() in global destructors since printf() no longer works.
  • 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.com/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 *  $Id$
26 */
27
28#include <rtems.h>
29
30#include <cstdio>
31#include <cstdlib>
32
33#ifdef RTEMS_TEST_IO_STREAM
34#include <iostream>
35#endif
36
37extern "C" 
38{
39#include <tmacros.h>
40extern rtems_task main_task(rtems_task_argument);
41}
42
43static int num_inst = 0;
44
45class AClass {
46public:
47  AClass(const char *p = "LOCAL" ) : ptr( p )
48    {
49        num_inst++;
50        printf(
51          "%s: Hey I'm in base class constructor number %d for %p.\n",
52          p, num_inst, this
53        );
54
55        /*
56         * Make sure we use some space
57         */
58
59        string = new char[50];
60        sprintf(string, "Instantiation order %d", num_inst);
61    };
62
63    virtual ~AClass()
64    {
65        // MUST USE PRINTK -- RTEMS IS SHUTTING DOWN WHEN THIS RUNS
66        printk(
67          "%s: Hey I'm in base class destructor number %d for %p.\n",
68          ptr, num_inst, this
69        );
70        printk("Derived class - %s\n", string);
71        num_inst--;
72    };
73
74    virtual void print()  { printf("%s\n", string); };
75
76protected:
77    char  *string;
78    const char *ptr;
79};
80
81class BClass : public AClass {
82public:
83  BClass(const char *p = "LOCAL" ) : AClass( p ) 
84    {
85        num_inst++;
86        printf(
87          "%s: Hey I'm in derived class constructor number %d for %p.\n",
88          p, num_inst,  this
89        );
90
91        /*
92         * Make sure we use some space
93         */
94
95        string = new char[50];
96        sprintf(string, "Instantiation order %d", num_inst);
97    };
98
99    ~BClass()
100    {
101        printk(
102          "%s: Hey I'm in derived class destructor number %d for %p.\n",
103          ptr, num_inst,
104          this
105        );
106        printk("Derived class - %s\n", string);
107        num_inst--;
108    };
109
110    void print()  { printf("Derived class - %s\n", string); }
111};
112
113
114class RtemsException 
115{
116public:
117   
118    RtemsException( const char *module, int ln, int err = 0 )
119    : error( err ), line( ln ), file( module )
120    {
121      printf( "RtemsException raised=File:%s, Line:%d, Error=%X\n",
122               file, line, error ); 
123    }
124
125    void show()
126    {
127      printf( "RtemsException ---> File:%s, Line:%d, Error=%X\n",
128               file, line, error ); 
129    }
130
131private:
132   int  error;
133   int  line;
134   const char *file;
135
136};
137
138
139
140AClass foo( "GLOBAL" );
141BClass foobar( "GLOBAL" );
142
143void
144cdtest(void)
145{
146    AClass bar, blech, blah;
147    BClass bleak;
148
149#ifdef RTEMS_TEST_IO_STREAM
150    std::cout << "Testing a C++ I/O stream" << std::endl;
151#else
152    printf("IO Stream not tested\n");
153#endif
154    bar = blech;
155    rtems_task_wake_after( 5 * get_ticks_per_second() );
156}
157
158//
159// main equivalent
160//      It can not be called 'main' since the bsp owns that name
161//      in many implementations in order to get global constructors
162//      run.
163//
164
165static void foo_function()
166{
167    try 
168    {
169      throw "foo_function() throw this exception"; 
170    }
171    catch( const char *e )
172    {
173     printf( "foo_function() catch block called:\n   < %s  >\n", e );
174     throw "foo_function() re-throwing execption..."; 
175    }
176}
177
178rtems_task main_task(
179  rtems_task_argument
180)
181{
182    printf( "\n\n*** CONSTRUCTOR/DESTRUCTOR TEST ***\n" );
183
184    cdtest();
185
186    printf( "*** END OF CONSTRUCTOR/DESTRUCTOR TEST ***\n\n\n" );
187
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 * 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.