source: rtems/testsuites/samples/cdtest/main.cc @ 8b474b13

4.104.114.84.9
Last change on this file since 8b474b13 was 8b474b13, checked in by Joel Sherrill <joel.sherrill@…>, on Feb 8, 2006 at 10:27:59 PM

2006-02-08 Joel Sherrill <joel@…>

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