summaryrefslogtreecommitdiffstats
path: root/testsuites/samples/cdtest/main.cc
blob: 1c5057a32b38c438d6cda286d018d6d91eab028d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 *  This routine is the initialization task for this test program.
 *  It is called from init_exec and has the responsibility for creating
 *  and starting the tasks that make up the test.  If the time of day
 *  clock is required for the test, it should also be set to a known
 *  value by this function.
 *
 *  Input parameters:  NONE
 *
 *  Output parameters:  NONE
 *
 *  COPYRIGHT (c) 1994 by Division Incorporated
 *  Based in part on OAR works.
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 *
 *
 *  by Rosimildo da Silva:
 *  Modified the test a bit to indicate when an instance is
 *  global or not, and added code to test C++ exception.
 */

#include <rtems.h>
#include <rtems/bspIo.h>
#include <rtems/test-info.h>
#include <rtems/sysinit.h>

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <stdexcept>

#ifdef RTEMS_TEST_IO_STREAM
#include <iostream>
#endif

const char rtems_test_name[] = "CONSTRUCTOR/DESTRUCTOR";

extern "C"
{
#include <tmacros.h>
extern rtems_task main_task(rtems_task_argument);
}

static int num_inst = 0;

static void check_end_of_test(void)
{
  if ( num_inst == 0 ) {
    TEST_END();
  }
}

class AClass {
public:
  AClass(const char *p = "LOCAL" ) : ptr( p )
    {
        num_inst++;
        printf(
          "%s: Hey I'm in base class constructor number %d for %p.\n",
          p, num_inst, this
        );

	/*
	 * Make sure we use some space
	 */

        string = new char[50];
	sprintf(string, "Instantiation order %d", num_inst);
    };

    virtual ~AClass()
    {
	// MUST USE PRINTK -- RTEMS IS SHUTTING DOWN WHEN THIS RUNS
        printk(
          "%s: Hey I'm in base class destructor number %d for %p.\n",
          ptr, num_inst, this
        );
        printk("Derived class - %s\n", string);
        num_inst--;
        check_end_of_test();
    };

#if __cplusplus >= 201103L
    AClass& operator=(const AClass&) = default;
#endif

    virtual void print()  { printf("%s\n", string); };

protected:
    char  *string;
    const char *ptr;
};

class BClass : public AClass {
public:
  BClass(const char *p = "LOCAL" ) : AClass( p )
    {
        num_inst++;
        printf(
          "%s: Hey I'm in derived class constructor number %d for %p.\n",
          p, num_inst,  this
        );

	/*
	 * Make sure we use some space
	 */

        string = new char[50];
	sprintf(string, "Instantiation order %d", num_inst);
    };

    ~BClass()
    {
        printk(
          "%s: Hey I'm in derived class destructor number %d for %p.\n",
          ptr, num_inst,
          this
        );
        printk("Derived class - %s\n", string);
        num_inst--;
        check_end_of_test();
    };

    void print()  { printf("Derived class - %s\n", string); }
};


class RtemsException
{
public:

    RtemsException( const char *module, int ln, int err = 0 )
    : error( err ), line( ln ), file( module )
    {
      printf( "RtemsException raised=File:%s, Line:%d, Error=%X\n",
               file, line, error );
    }

    void show()
    {
      printf( "RtemsException ---> File:%s, Line:%d, Error=%X\n",
               file, line, error );
    }

private:
   int  error;
   int  line;
   const char *file;

};



AClass foo( "GLOBAL" );
BClass foobar( "GLOBAL" );

void
cdtest(void)
{
    AClass bar, blech, blah;
    BClass bleak;

#ifdef RTEMS_TEST_IO_STREAM
    std::cout << "Testing a C++ I/O stream" << std::endl;
#else
    printf("IO Stream not tested\n");
#endif
    bar = blech;
    rtems_task_wake_after( 5 * rtems_clock_get_ticks_per_second() );
}

//
// main equivalent
//      It can not be called 'main' since the bsp owns that name
//      in many implementations in order to get global constructors
//      run.
//

static void foo_function()
{
    try
    {
      throw "foo_function() throw this exception";
    }
    catch( const char *e )
    {
     printf( "foo_function() catch block called:\n   < %s  >\n", e );
     throw "foo_function() re-throwing execption...";
    }
}

rtems_task main_task(
  rtems_task_argument
)
{
    cdtest();

    printf( "*** TESTING C++ EXCEPTIONS ***\n\n" );

    try
    {
      foo_function();
    }
    catch( const char *e )
    {
       printf( "Success catching a char * exception\n%s\n", e );
    }

    try
    {
      throw std::runtime_error("thrown std::runtime object");
    }
    catch (std::exception const& e)
    {
       printf("throw std::runtime: caught: %s\n", e.what());
    }

    try
    {
      printf( "throw an instance based exception\n" );
		throw RtemsException( __FILE__, __LINE__, 0x55 );
    }
    catch( RtemsException & ex )
    {
       printf( "Success catching RtemsException...\n" );
       ex.show();
    }
    catch(...)
    {
      printf( "Caught another exception.\n" );
    }
    printf( "Exceptions are working properly.\n" );
    rtems_task_wake_after( 5 * rtems_clock_get_ticks_per_second() );
    printf( "Global Dtors should be called after this line....\n" );
    exit(0);
}

/*
 * Exceptions during system initialization work only on targets which do not
 * need a registration of exception frames during the global construction.  In
 * particular, targets which use the DWARF2 unwinder cannot use exceptions
 * during system initialization.
 */
#if defined(__arm__)
#define CAN_DO_EXCEPTIONS_DURING_SYSINIT
#endif

#ifdef CAN_DO_EXCEPTIONS_DURING_SYSINIT
static void early_exception()
{
    try
    {
      throw "early exception";
    }
    catch( const char *e )
    {
      rtems_test_assert(strcmp(e, "early exception") == 0);
      throw "early exception 2";
    }
}
#endif

static void test_exceptions_during_system_init()
{
    TEST_BEGIN();

#ifdef CAN_DO_EXCEPTIONS_DURING_SYSINIT
    try
    {
      early_exception();
    }
    catch( const char *e )
    {
      rtems_test_assert(strcmp(e, "early exception 2") == 0);
    }
#endif
}

RTEMS_SYSINIT_ITEM(
    test_exceptions_during_system_init,
    RTEMS_SYSINIT_IDLE_THREADS,
    RTEMS_SYSINIT_ORDER_LAST
);