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

Last change on this file since 8c637ee was 8c637ee, checked in by Chris Johns <chrisj@…>, on Dec 9, 2016 at 7:13:39 AM

cdtest: Add std::runtime_error() test case

Update #2830.

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