OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [host/] [libcdl/] [testsuite/] [libcdl/] [cdl2.cxx] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      cdl2.cxx
4
//
5
//      Tests for the CdlHandle class.
6
//
7
//==========================================================================
8
// ####ECOSHOSTGPLCOPYRIGHTBEGIN####                                        
9
// -------------------------------------------                              
10
// This file is part of the eCos host tools.                                
11
// Copyright (C) 1999, 2000 Free Software Foundation, Inc.                  
12
//
13
// This program is free software; you can redistribute it and/or modify     
14
// it under the terms of the GNU General Public License as published by     
15
// the Free Software Foundation; either version 2 or (at your option) any   
16
// later version.                                                           
17
//
18
// This program is distributed in the hope that it will be useful, but      
19
// WITHOUT ANY WARRANTY; without even the implied warranty of               
20
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        
21
// General Public License for more details.                                 
22
//
23
// You should have received a copy of the GNU General Public License        
24
// along with this program; if not, write to the                            
25
// Free Software Foundation, Inc., 51 Franklin Street,                      
26
// Fifth Floor, Boston, MA  02110-1301, USA.                                
27
// -------------------------------------------                              
28
// ####ECOSHOSTGPLCOPYRIGHTEND####                                          
29
//==========================================================================
30
//#####DESCRIPTIONBEGIN####                                             
31
//
32
// Author(s):           bartv
33
// Contributors:        bartv
34
// Date:                1999-01-12
35
// Description:         Large parts of libcdl are implemented using a
36
//                      CdlHandle template and a CdlRefcountSupport class.
37
//                      This tests check that these both work as expected.
38
//
39
//####DESCRIPTIONEND####
40
//==========================================================================
41
 
42
#include <cstdio>
43
#include <cdlconfig.h>
44
#include <cdl.hxx>
45
#include <cyg/infra/cyg_ass.h>
46
#include <cyg/infra/cyg_trac.h>
47
#include <cyg/infra/testcase.h>
48
#include <cstdlib>
49
 
50
#ifndef CYGBLD_LIBCDL_USE_SMART_POINTERS
51
int
52
main(int argc, char** argv)
53
{
54
    CYG_TEST_FAIL_FINISH("Smart pointers not yet enabled - waiting for a working version of Visual C++");
55
    return EXIT_FAILURE;
56
}
57
#else // CYGBLD_LIBCDL_USE_SMART_POINTERS
58
// ----------------------------------------------------------------------------
59
// Miscellaneous statics.
60
 
61
// This routine controls the return value of a class1_body check_this()
62
// operation, allowing test code to make sure that using check_this()
63
// on a smart pointer works as expected.
64
static bool check_this_ok = true;
65
 
66
// ----------------------------------------------------------------------------
67
// This test case makes use of three implementation classes. It is necessary
68
// to have forward declarations of these, and then it is possible to define
69
// handle classes for each one.
70
class class01_body;
71
class class02_body;
72
class derived_body;
73
 
74
typedef CdlHandle<class01_body> class01;
75
typedef CdlHandle<class02_body> class02;
76
typedef CdlHandle<derived_body> derived;
77
 
78
// ----------------------------------------------------------------------------
79
// This test needs three additional classes which are reference-counted and
80
// which are accessed via CdlHandle smart pointers. It is necessary to start 
81
 
82
class class01_body : public CdlRefcountSupport {
83
 
84
    friend class CdlTest;
85
 
86
  public:
87
 
88
    static int class01_objects;
89
    class01_body() : CdlRefcountSupport() {
90
        class01_objects++;
91
        object_number = class01_objects;
92
        modifiable    = 0;
93
        class01_body_cookie = class01_body_magic;
94
    }
95
    ~class01_body() {
96
        class01_objects--;
97
        class01_body_cookie = class01_body_invalid;
98
    }
99
    int
100
    get_number(void) {
101
        return object_number;
102
    }
103
    void
104
    modify(void) {
105
        modifiable++;
106
    }
107
    bool check_this(cyg_assert_class_zeal zeal) const {
108
        CYG_UNUSED_PARAM(cyg_assert_class_zeal, zeal);
109
        if (class01_body_magic != class01_body_cookie) {
110
            return false;
111
        }
112
        return check_this_ok;
113
    }
114
 
115
  private:
116
    // Which object is this?
117
    int object_number;
118
    int modifiable;
119
 
120
    class01_body(const class01_body&);
121
    class01_body& operator=(const class01_body&);
122
    enum {
123
        class01_body_invalid     = 0,
124
        class01_body_magic       = 0x015b19d6
125
    } class01_body_cookie;
126
};
127
 
128
class class02_body : public CdlRefcountSupport {
129
 
130
    friend class CdlTest;
131
 
132
  public:
133
    static int class02_objects;
134
    class02_body() : CdlRefcountSupport() {
135
        class02_objects++;
136
        class02_body_cookie = class02_body_magic;
137
    }
138
    ~class02_body() {
139
        class02_objects--;
140
        class02_body_cookie = class02_body_invalid;
141
    }
142
 
143
    bool check_this(cyg_assert_class_zeal zeal) const {
144
        CYG_UNUSED_PARAM(cyg_assert_class_zeal, zeal);
145
        return class02_body_magic == class02_body_cookie;
146
    }
147
 
148
  private:
149
    class02_body(const class02_body&);
150
    class02_body& operator=(const class02_body&);
151
    enum {
152
        class02_body_invalid     = 0,
153
        class02_body_magic       = 0x3225c96c
154
    } class02_body_cookie;
155
};
156
 
157
class derived_body : public class01_body {
158
 
159
    friend class CdlTest;
160
 
161
  public:
162
    static int derived_objects;
163
    derived_body() : class01_body() {
164
        derived_objects++;
165
        derived_body_cookie = derived_body_magic;
166
    }
167
    ~derived_body() {
168
        derived_objects--;
169
        derived_body_cookie = derived_body_invalid;
170
    }
171
    bool check_this(cyg_assert_class_zeal zeal) const {
172
        if (derived_body_magic != derived_body_cookie) {
173
            return false;
174
        }
175
        return class01_body::check_this(zeal);
176
    }
177
 
178
  private:
179
    derived_body(const derived_body&);
180
    derived_body& operator=(const derived_body&);
181
    enum {
182
        derived_body_invalid    = 0,
183
        derived_body_magic      = 0x7ed15350
184
    } derived_body_cookie;
185
};
186
 
187
int class01_body::class01_objects   = 0;
188
int class02_body::class02_objects   = 0;
189
int derived_body::derived_objects   = 0;
190
 
191
// ----------------------------------------------------------------------------
192
// The actual test code.
193
 
194
bool
195
check_const_arg(const class01 const_ptr)
196
{
197
    // Make sure that read-only access is allowed and goes to the right
198
    // object
199
    if (!const_ptr->check_this(cyg_quick)) {
200
        CYG_TEST_FAIL("check_this() on a constant pointer should be fine");
201
        return false;
202
    }
203
    check_this_ok = false;
204
    if (const_ptr->check_this(cyg_quick)) {
205
        CYG_TEST_FAIL("check_this() on a constant pointer should be fine");
206
        check_this_ok = true;
207
        return false;
208
    }
209
    check_this_ok = true;
210
    return true;
211
}
212
 
213
int
214
main(int argc, char** argv)
215
{
216
    bool ok = true;
217
 
218
    // Make sure that smart pointers do not impose any kind of overhead.
219
    if ((sizeof(void *) != sizeof(class01)) ||
220
        (sizeof(void *) != sizeof(class02)) ||
221
        (sizeof(void *) != sizeof(derived))) {
222
 
223
        CYG_TEST_FAIL("smart pointers are not the same size as dumb pointers");
224
    } else {
225
        CYG_TEST_PASS("smart pointers are the same size as dumb pointers");
226
    }
227
 
228
    // Start by creating a number of objects to be manipulated.
229
    class01_body *      class01_obj1    = new class01_body;
230
    class01_body *      class01_obj2    = new class01_body;
231
    class02_body *      class02_obj1    = new class02_body;
232
    derived_body *      derived_obj1    = new derived_body;
233
 
234
    // Quick sanity check
235
    if ((1 != derived_body::derived_objects) ||
236
        (1 != class02_body::class02_objects) ||
237
        (3 != class01_body::class01_objects)) {
238
        CYG_TEST_FAIL("Testcase has created an invalid number of objects");
239
    }
240
 
241
    // Convert the basic objects to smart pointers. If this code compiles
242
    // then the test succeeds.
243
    class01     class01_ptr1    = class01(class01_obj1);
244
    class01     class01_ptr2    = class01(class01_obj2);
245
    class02     class02_ptr1    = class02(class02_obj1);
246
    derived     derived_ptr1    = derived(derived_obj1);
247
    CYG_TEST_PASS("conversion to smart pointers works");
248
 
249
    // Also create a couple of other smart pointers. These should be
250
    // initialised to 0.
251
    class01      class01_ptr3;
252
    class01      class01_ptr4     = 0;
253
    class01      class01_ptr5     = class01(0);
254
    CYG_TEST_PASS("smart pointers can have the value zero");
255
 
256
    // Try to dereference the smart pointers.
257
    if ((1 != class01_ptr1->get_number()) ||
258
        (2 != class01_ptr2->get_number()) ||
259
        (3 != derived_ptr1->get_number())) {
260
        CYG_TEST_FAIL("-> dereferencing operator broken");
261
    } else {
262
        CYG_TEST_PASS("-> dereferencing operator functional");
263
    }
264
    if ((1 != (*class01_ptr1).get_number()) ||
265
        (2 != (*class01_ptr2).get_number()) ||
266
        (3 != (*derived_ptr1).get_number())) {
267
        CYG_TEST_FAIL("* dereferencing operator broken");
268
    } else {
269
        CYG_TEST_PASS("* dereferencing operator functional");
270
    }
271
 
272
    // Also try to access the check_this() member functions
273
    if (!class01_ptr1->check_this(cyg_quick)) {
274
    }
275
 
276
    // Do a couple of if's. This checks that the !operator is
277
    // functional. Some of the checks are there to make sure that the
278
    // compiler does the right thing.
279
    ok = true;
280
    if (!class01_ptr1) {
281
        CYG_TEST_FAIL("!(assigned smart pointer) is true");
282
        ok = false;
283
    }
284
    if (0 == class01_ptr1) {
285
        CYG_TEST_FAIL("0 == assigned smart pointer");
286
        ok = false;
287
    }
288
    if (0 != class01_ptr3) {
289
        CYG_TEST_FAIL("0 != unassigned smart pointer");
290
        ok = false;
291
    }
292
    if (class01_ptr1 == 0) {
293
        CYG_TEST_FAIL("0 == assigned smart pointer");
294
        ok = false;
295
    }
296
    if (class01_ptr3 != 0) {
297
        CYG_TEST_FAIL("0 != unassigned smart pointer");
298
        ok = false;
299
    }
300
    if (class01_ptr1 == class01_ptr2) {
301
        CYG_TEST_FAIL("comparing two different smart pointers succeeds");
302
        ok = false;
303
    }
304
    if (class01_ptr1 != class01_ptr2) {
305
        // Do nothing
306
    } else {
307
        CYG_TEST_FAIL("comparing two different smart pointers succeeds");
308
        ok = false;
309
    }
310
#if 0
311
    // Comparing base and derived smart pointers directly does not work yet.
312
    if (class01_ptr1 == derived_ptr1) {
313
        CYG_TEST_FAIL("comparing different base and derived pointers succeeds");
314
    }
315
#endif
316
    if (ok) {
317
        CYG_TEST_PASS("smart pointer comparisons work");
318
    }
319
 
320
    // Try some  assignment operators.
321
    class01_ptr3 = class01_ptr1;
322
    class01_ptr4 = derived_ptr1;
323
    class01_ptr5 = class01_ptr2;
324
 
325
    // After doing all of these assignments there should be no change in
326
    // the number of underlying objects.
327
    ok = true;
328
    if ((1 != derived_body::derived_objects) ||
329
        (1 != class02_body::class02_objects)   ||
330
        (3 != class01_body::class01_objects)) {
331
        ok = false;
332
        CYG_TEST_FAIL("Assignment of smart pointers has changed the underlying number of objects");
333
    }
334
    if ((class01_ptr1 != class01_ptr3) ||
335
        (class01_ptr2 != class01_ptr5)) {
336
        ok = false;
337
        CYG_TEST_FAIL("Assignment of smart pointers has not worked");
338
    }
339
    if (class01_ptr4.get_dumb_pointer() != derived_ptr1.get_dumb_pointer()) {
340
        ok = false;
341
        CYG_TEST_FAIL("Assignment of derived to base smart pointer has not worked");
342
    }
343
    if ((2 != class01_ptr1->get_refcount()) ||
344
        (2 != class01_ptr2->get_refcount()) ||
345
        (2 != class01_ptr4->get_refcount()) ||
346
        (2 != derived_ptr1->get_refcount())) {
347
        ok = false;
348
        CYG_TEST_FAIL("Reference counts after assignment operators do not match up");
349
    }
350
    if (ok) {
351
        CYG_TEST_PASS("Assignment of smart pointers");
352
    }
353
 
354
    // Now try assigning zero. Incidentally this is necessary if the underlying
355
    // objects are to be destroyed again at the end.
356
    class01_ptr3 = 0;
357
    class01_ptr4 = 0;
358
    class01_ptr5 = 0;
359
 
360
    ok = true;
361
    if (0 != class01_ptr3) {
362
        ok = false;
363
        CYG_TEST_FAIL("assigning 0 to a smart pointer does not work");
364
    }
365
    if ((1 != class01_ptr1->get_refcount()) ||
366
        (1 != class01_ptr2->get_refcount()) ||
367
        (1 != derived_ptr1->get_refcount())) {
368
        ok = false;
369
        CYG_TEST_FAIL("Reference counts after assignment operators do not match up");
370
    }
371
    if (ok) {
372
        CYG_TEST_PASS("Assigning zero to smart pointers");
373
    }
374
 
375
    // Make sure that implicit casts to const work. This is really
376
    // a compiler test.
377
    if (check_const_arg(class01_ptr1) &&
378
        check_const_arg(derived_ptr1)) {
379
        CYG_TEST_PASS("Implicit cast to const smart pointer");
380
    }
381
 
382
#if 0
383
    // All of this code should fail to compile.
384
    // Applying delete to a smart pointer does not work. Use destroy() instead.
385
    delete class01_ptr1;
386
#endif
387
#if 0
388
    // Attempts to do incompatible assignments should fail.
389
    class01_ptr1 = class02_ptr1;
390
#endif
391
#if 0    
392
    // Comparing completely different types should fail.
393
    if (class01_ptr1 == class02_ptr1) {
394
        CYG_TEST_FAIL("it should not be possible to compare objects of different types");
395
    }
396
#endif
397
#if 0
398
    {
399
        const class01 const_class01_ptr = class01_ptr1;
400
        const_class01_ptr->modify();
401
    }
402
#endif
403
#if 0
404
    {
405
        const class01 const_derived_ptr = derived_ptr1;
406
        const_derived_ptr->modify();
407
    }
408
#endif
409
 
410
    // Check that destroy() actually gets rid of the underlying objects.
411
    class01_ptr1.destroy();
412
    class01_ptr2.destroy();
413
    class02_ptr1.destroy();
414
    derived_ptr1.destroy();
415
    if ((0 != derived_body::derived_objects) ||
416
        (0 != class02_body::class02_objects)    ||
417
        (0 != class01_body::class01_objects)) {
418
        CYG_TEST_FAIL("There are still objects after the smart pointers have been destroyed");
419
    } else {
420
        CYG_TEST_PASS("Using destroy() on the smart pointers cleans up the underlying objects");
421
    }
422
 
423
    return EXIT_SUCCESS;
424
}
425
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.