source: rtems/testsuites/samples/cdtest/main.cc @ 9391f6d

4.115
Last change on this file since 9391f6d was 9391f6d, checked in by Sebastian Huber <sebastian.huber@…>, on 03/10/14 at 15:31:43

tests/samples: Use <rtems/test.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.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#include <rtems.h>
26#include <rtems/test.h>
27
28#include <cstdio>
29#include <cstdlib>
30
31#ifdef RTEMS_TEST_IO_STREAM
32#include <iostream>
33#endif
34
35const char rtems_test_name[] = "CONSTRUCTOR/DESTRUCTOR";
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 * rtems_clock_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    TEST_BEGIN();
183
184    cdtest();
185
186    TEST_END();
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 * rtems_clock_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.