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

Subversion Repositories openrisc

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

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      cdl1.cxx
4
//
5
//      Basic testing of the CDL utility functions.                                                        
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-06
35
// Description:         There are a number of utility functions in the
36
//                      Cdl class to handle data validation, various
37
//                      conversions, etc.
38
//
39
//####DESCRIPTIONEND####
40
//==========================================================================
41
 
42
#include <cdlconfig.h>
43
#include <cdl.hxx>
44
#include <cyg/infra/cyg_ass.h>
45
#include <cyg/infra/cyg_trac.h>
46
#include <cyg/infra/testcase.h>
47
#include <cstdlib>
48
#include <cmath>
49
 
50
static bool
51
test_is_valid_property_id(void)
52
{
53
    bool result = true;
54
    CYG_REPORT_FUNCNAMETYPE("test_is_valid_property_id", "success %d");
55
 
56
    if (!Cdl::is_valid_property_id(CdlPropertyId_ActiveIf)) {
57
        CYG_TEST_FAIL("ActiveIf should be a valid property id");
58
        result = false;
59
    }
60
    if (!Cdl::is_valid_property_id(CdlPropertyId_DefaultValue)) {
61
        CYG_TEST_FAIL("DefaultValue should be a valid property id");
62
        result = false;
63
    }
64
    if (!Cdl::is_valid_property_id(CdlPropertyId_Wizard)) {
65
        CYG_TEST_FAIL("Wizard should be a valid property id");
66
        result = false;
67
    }
68
    if (Cdl::is_valid_property_id(CdlPropertyId_Unknown)) {
69
        CYG_TEST_FAIL("is_valid_property_id() accepted an invalid number");
70
        result = false;
71
    }
72
    // This test could give spurious negatives in the very long term
73
    union {
74
        int           x;
75
        CdlPropertyId id;
76
    } dummy;
77
    dummy.x = 1234;
78
    if (Cdl::is_valid_property_id(dummy.id)) {
79
        CYG_TEST_FAIL("is_valid_property_id() accepted an invalid number");
80
        result = false;
81
    }
82
 
83
    CYG_REPORT_RETVAL(result);
84
    return result;
85
}
86
 
87
static bool
88
test_is_valid_property_class(void)
89
{
90
    bool result = true;
91
    CYG_REPORT_FUNCNAMETYPE("test_is_valid_property_class", "success %d");
92
 
93
    if (!Cdl::is_valid_property_class(CdlPropertyClass_Minimal)) {
94
        CYG_TEST_FAIL("Minimal should be a valid property class");
95
        result = false;
96
    }
97
    if (!Cdl::is_valid_property_class(CdlPropertyClass_TclCode)) {
98
        CYG_TEST_FAIL("TclCode should be a valid property class");
99
        result = false;
100
    }
101
    if (!Cdl::is_valid_property_class(CdlPropertyClass_GoalExpression)) {
102
        CYG_TEST_FAIL("GoalExpression should be a valid property class");
103
        result = false;
104
    }
105
    if (Cdl::is_valid_property_class(CdlPropertyClass_Unknown)) {
106
        CYG_TEST_FAIL("is_valid_property_class() accepted an invalid number");
107
        result = false;
108
    }
109
    // This test could give spurious negatives in the very long term
110
    union {
111
        int                     x;
112
        CdlPropertyClass        id;
113
    } dummy;
114
    dummy.x = 1234;
115
    if (Cdl::is_valid_property_class(dummy.id)) {
116
        CYG_TEST_FAIL("is_valid_property_class() accepted an invalid number");
117
        result = false;
118
    }
119
 
120
    CYG_REPORT_RETVAL(result);
121
    return result;
122
}
123
 
124
static bool
125
test_is_valid_object_type(void)
126
{
127
    bool result = true;
128
    CYG_REPORT_FUNCNAMETYPE("test_is_valid_object_type", "success %d");
129
 
130
    if (!Cdl::is_valid_object_type(CdlObjectType_Package)) {
131
        CYG_TEST_FAIL("Package should be a valid object type");
132
        result = false;
133
    }
134
    if (!Cdl::is_valid_object_type(CdlObjectType_Component)) {
135
        CYG_TEST_FAIL("Component should be a valid object type");
136
        result = false;
137
    }
138
    if (!Cdl::is_valid_object_type(CdlObjectType_Option)) {
139
        CYG_TEST_FAIL("Option should be a valid object type");
140
        result = false;
141
    }
142
    if (Cdl::is_valid_object_type(CdlObjectType_Unknown)) {
143
        CYG_TEST_FAIL("is_valid_object_type() accepted an invalid number");
144
        result = false;
145
    }
146
    // This test could give spurious negatives in the very long term
147
    union {
148
        int             x;
149
        CdlObjectType   id;
150
    } dummy;
151
    dummy.x = 1234;
152
    if (Cdl::is_valid_object_type(dummy.id)) {
153
        CYG_TEST_FAIL("is_valid_object_type() accepted an invalid number");
154
        result = false;
155
    }
156
    CYG_REPORT_RETVAL(result);
157
    return result;
158
}
159
 
160
static bool
161
test_is_valid_option_flavor(void)
162
{
163
    bool result = true;
164
    CYG_REPORT_FUNCNAMETYPE("test_is_valid_option_flavor", "success %d");
165
 
166
    if (!Cdl::is_valid_option_flavor(CdlOptionFlavor_Bool)) {
167
        CYG_TEST_FAIL("Bool should be a valid option flavor");
168
        result = false;
169
    }
170
    if (!Cdl::is_valid_option_flavor(CdlOptionFlavor_Enum)) {
171
        CYG_TEST_FAIL("Enum should be a valid option flavor");
172
        result = false;
173
    }
174
    if (!Cdl::is_valid_option_flavor(CdlOptionFlavor_Count)) {
175
        CYG_TEST_FAIL("Count should be a valid option flavor");
176
        result = false;
177
    }
178
    if (Cdl::is_valid_option_flavor(CdlOptionFlavor_Unknown)) {
179
        CYG_TEST_FAIL("is_valid_option_flavor() accepted an invalid number");
180
        result = false;
181
    }
182
    // This test could give spurious negatives in the very long term
183
    union {
184
        int             x;
185
        CdlOptionFlavor id;
186
    } dummy;
187
    dummy.x = 1234;
188
    if (Cdl::is_valid_option_flavor(dummy.id)) {
189
        CYG_TEST_FAIL("is_valid_option_flavor() accepted an invalid number");
190
        result = false;
191
    }
192
    CYG_REPORT_RETVAL(result);
193
    return result;
194
}
195
 
196
static bool
197
test_is_valid_value_source(void)
198
{
199
    bool result = true;
200
    CYG_REPORT_FUNCNAMETYPE("test_is_valid_value_source", "success %d");
201
 
202
    if (!Cdl::is_valid_value_source(CdlValueSource_Default)) {
203
        CYG_TEST_FAIL("Default is a valid value source");
204
        result = false;
205
    }
206
    if (!Cdl::is_valid_value_source(CdlValueSource_User)) {
207
        CYG_TEST_FAIL("User is a valid value source");
208
        result = false;
209
    }
210
    if (!Cdl::is_valid_value_source(CdlValueSource_Wizard)) {
211
        CYG_TEST_FAIL("Wizard is a valid value source");
212
        result = false;
213
    }
214
    if (!Cdl::is_valid_value_source(CdlValueSource_Inferred)) {
215
        CYG_TEST_FAIL("Inferred is a valid value source");
216
        result = false;
217
    }
218
    if (Cdl::is_valid_value_source(CdlValueSource_Unknown)) {
219
        CYG_TEST_FAIL("is_valid_value_source() accepted an invalid number");
220
        result = false;
221
    }
222
    // This test could give spurious negatives in the very long term
223
    union {
224
        int             x;
225
        CdlValueSource  id;
226
    } dummy;
227
    dummy.x = 1234;
228
    if (Cdl::is_valid_value_source(dummy.id)) {
229
        CYG_TEST_FAIL("is_valid_value_source() accepted an invalid number");
230
        result = false;
231
    }
232
 
233
    CYG_REPORT_RETVAL(result);
234
    return result;
235
}
236
 
237
static bool
238
test_string_to_integer(void)
239
{
240
    bool result = true;
241
    CYG_REPORT_FUNCNAMETYPE("test_string_to_integer", "success %d");
242
 
243
    // Note that there is no robust way of specifying 64 bit constants.
244
    // Some compilers will use a LL suffix, but not all. When a 64 bit
245
    // constant is needed this has to be achieved using arithmetic,
246
    // leaving the compiler to do the hard work.
247
    //
248
    // Compiler bogosity: VC++ does not appear the more normal way
249
    // of initializing an array of structures using nested braces.
250
    struct conversion_data {
251
        bool            ok;
252
        const char*     source;
253
        cdl_int         expected;
254
    } data[] = {
255
        {  true,          "0",            0 },
256
        {  true,          "1",            1 },
257
        {  true,         "-1",           -1  },
258
        {  true,        "0x0",            0  },
259
        {  true,        "0x1",            1  },
260
        {  true,         "01",            1  },
261
        {  true,       "1234",         1234  },
262
        {  true,      "-4567",        -4567  },
263
        {  true, "2147483647",   2147483647  },
264
        {  true,"-2147483648",  ((cdl_int) -2147483647) - 1  },
265
        {  true, "0x7fffffff",   2147483647  },
266
        {  true,"-0x80000000",  ((cdl_int) -2147483647) -1  },
267
        {  true, "0x12ABCDEF",    313249263  },
268
        {  true, "12345678987654321", ((cdl_int) 111111111) * ((cdl_int) 111111111) },
269
        { false,          "A",            0  },
270
        { false,         "0A",            0  },
271
        { false,      "1234*",            0  },
272
        { false,   "finished",            0  }
273
    };
274
 
275
    for (int i = 0; 0 != strcmp("finished", data[i].source); i++) {
276
        cdl_int     res;
277
        std::string source = std::string(data[i].source);
278
        if (data[i].ok) {
279
            if (!Cdl::string_to_integer(source, res)) {
280
                std::string msg = "the string \"" + source + "\" was not converted";
281
                CYG_TEST_FAIL(msg.c_str());
282
                result = false;
283
            } else if (res != data[i].expected) {
284
                std::string msg = "the string \"" + source + "\" was converted incorrectly";
285
                CYG_TEST_FAIL(msg.c_str());
286
                result = false;
287
            }
288
        } else {
289
            if (Cdl::string_to_integer(source, res)) {
290
                std::string msg = "the string \"" + source + "\" is invalid but was still converted";
291
                CYG_TEST_FAIL(msg.c_str());
292
                result = false;
293
            }
294
        }
295
    }
296
 
297
    CYG_REPORT_RETVAL(result);
298
    return result;
299
}
300
 
301
static bool
302
test_integer_to_string(void)
303
{
304
    bool result = true;
305
    CYG_REPORT_FUNCNAMETYPE("test_integer_to_string", "success %d");
306
 
307
    // Note that there is no robust way of specifying 64 bit constants.
308
    // Some compilers will use a LL suffix, but not all. When a 64 bit
309
    // constant is needed this has to be achieved using arithmetic,
310
    // leaving the compiler to do the hard work.
311
    //
312
    // Integer to string conversions cannot fail.
313
    //
314
    // Compiler bogosity: VC++ does not appear the more normal way
315
    // of initializing an array of structures using nested braces.
316
    struct conversion_data {
317
        cdl_int         source;
318
        const char*     expected;
319
    } data[] = {
320
        {                0,           "0"  },
321
        {                1,           "1"  },
322
        {               -1,          "-1"  },
323
        {               10,          "10"  },
324
        {             1234,        "1234"  },
325
        {          -456789,     "-456789"  },
326
        {       2147483647,  "2147483647"  },
327
        { ((cdl_int) 111111111) * ((cdl_int) 111111111), "12345678987654321"  },
328
        {      (cdl_int) 0, "finished"     }
329
    };
330
 
331
    for (int i = 0; 0 != strcmp("finished", data[i].expected); i++) {
332
        std::string res;
333
        if (!Cdl::integer_to_string(data[i].source, res)) {
334
            std::string msg = "the integer \"" + std::string(data[i].expected) + "\" was not converted";
335
            CYG_TEST_FAIL(msg.c_str());
336
            result = false;
337
        } else if (res != data[i].expected) {
338
            std::string msg = "the string \"" + std::string(data[i].expected) + "\" was converted incorrectly";
339
            CYG_TEST_FAIL(msg.c_str());
340
            result = false;
341
        }
342
    }
343
 
344
    // Just a few more tests. Try converting some sequences to a string and back
345
    // again.
346
    for (int j = 0; j < 4; j++) {
347
        cdl_int starting_values[] = { 1, 3, -1, -2 };
348
        cdl_int current_value     = starting_values[j];
349
 
350
        for (int k = 0; k < 60; k++) {
351
            current_value <<= 1;
352
            cdl_int     int_tmp;
353
            std::string str_tmp;
354
            if (!Cdl::integer_to_string(current_value, str_tmp)) {
355
                CYG_TEST_FAIL("unable to convert valid integer to string");
356
                result = false;
357
                break;
358
            }
359
            if (!Cdl::string_to_integer(str_tmp, int_tmp)) {
360
                CYG_TEST_FAIL("unable to convert string back to integer");
361
                result = false;
362
                break;
363
            }
364
            if (current_value != int_tmp) {
365
                CYG_TEST_FAIL("integer conversion to/from strings not idempotent");
366
                result = false;
367
                break;
368
            }
369
        }
370
    }
371
 
372
    CYG_REPORT_RETVAL(result);
373
    return result;
374
}
375
 
376
static bool
377
test_string_to_bool(void)
378
{
379
    bool result = true;
380
    CYG_REPORT_FUNCNAMETYPE("test_string_to_bool", "success %d");
381
 
382
    // Legal values for true  include 1 and "true".
383
    // Legal values for false include 0 and "false".
384
    // A random string should fail to convert.
385
    bool res;
386
    if (!Cdl::string_to_bool("1", res)) {
387
        CYG_TEST_FAIL("the string \"1\" was not converted");
388
        result = false;
389
    } else if (res != true) {
390
        CYG_TEST_FAIL("the string \"1\" did not convert to true");
391
        result = false;
392
    }
393
    if (!Cdl::string_to_bool("true", res)) {
394
        CYG_TEST_FAIL("the string \"true\" was not converted");
395
        result = false;
396
    } else if (res != true) {
397
        CYG_TEST_FAIL("the string \"true\" did not convert to true");
398
        result = false;
399
    }
400
    if (!Cdl::string_to_bool("0", res)) {
401
        CYG_TEST_FAIL("the string \"0\" was not converted");
402
        result = false;
403
    } else if (res != false) {
404
        CYG_TEST_FAIL("the string \"0\" did not convert to false");
405
        result = false;
406
    }
407
    if (!Cdl::string_to_bool("false", res)) {
408
        CYG_TEST_FAIL("the string \"false\" was not converted");
409
        result = false;
410
    } else if (res != false) {
411
        CYG_TEST_FAIL("the string \"false\" did not convert to false");
412
        result = false;
413
    }
414
    if (Cdl::string_to_bool("not a boolean string", res)) {
415
        CYG_TEST_FAIL("a spurious string was converted to a boolean");
416
        result = false;
417
    }
418
 
419
    CYG_REPORT_RETVAL(result);
420
    return result;
421
}
422
 
423
static bool
424
test_bool_to_string(void)
425
{
426
    bool result = true;
427
    CYG_REPORT_FUNCNAMETYPE("test_bool_to_string", "success %d");
428
 
429
    std::string str;
430
    if (!Cdl::bool_to_string(true, str)) {
431
        CYG_TEST_FAIL("bool_to_string() failed for `true'");
432
        result = false;
433
    } else if ("1" != str) {
434
        CYG_TEST_FAIL("boolean value true translated incorrectly");
435
        result = false;
436
    }
437
    if (!Cdl::bool_to_string(false,str)) {
438
        CYG_TEST_FAIL("bool_to_string() failed for `true'");
439
        result = false;
440
    } else if ("0" != str) {
441
        CYG_TEST_FAIL("boolean value false translated incorrectly");
442
        result = false;
443
    }
444
    // There is no easy way to test failure conditions. The trick
445
    // of sticking a random number into a union will not work, there
446
    // are absolutely no guarantees about the internal implementation
447
    // of the bool data type in C++
448
 
449
    CYG_REPORT_RETVAL(result);
450
    return result;
451
}
452
 
453
// This test is not intended to be comprehensive. In particular it is
454
// not very easy to validate the results, issues like locales get in the way.
455
static bool
456
test_double_to_string(void)
457
{
458
    bool result = true;
459
    CYG_REPORT_FUNCNAMETYPE("test_double_to_string", "success %d");
460
 
461
    std::string str;
462
    if (!Cdl::double_to_string(0.0, str)) {
463
        CYG_TEST_FAIL("double_to_string() failed for 0.0");
464
        result = false;
465
    } else if (('0' != str[0]) && (('-' != str[0]) && ('0' != str[1]))) {
466
        fprintf(stderr, "result of conversion is %s\n", str.c_str());
467
        CYG_TEST_FAIL("double_to_string() returned strange result for 0.0");
468
        result = false;
469
    }
470
 
471
    if (!Cdl::double_to_string(3.141592, str)) {
472
        CYG_TEST_FAIL("double_to_string() failed for pi");
473
        result = false;
474
    } else if (('3' != str[0]) || ('1' != str[2]) || ('4' != str[3]) || ('1' != str[4])) {
475
        CYG_TEST_FAIL("double_to_string() returned strange result for pi");
476
        result = false;
477
    }
478
 
479
    if (!Cdl::double_to_string(-1.23456789E15, str)) {
480
        CYG_TEST_FAIL("double_to_string() failed for large but legal value");
481
        result = false;
482
    } else if (('-' != str[0]) && ('1' != str[1]) && (10 >= str.size())) {
483
        CYG_TEST_FAIL("double_to_string() returned strange result for large but legal value");
484
        result = false;
485
    }
486
 
487
    CYG_REPORT_RETVAL(result);
488
    return result;
489
}
490
 
491
// This test is not intended to be comprehensive.
492
static bool
493
test_string_to_double(void)
494
{
495
    bool result = true;
496
    CYG_REPORT_FUNCNAMETYPE("test_string_to_double", "success %d");
497
 
498
    double val;
499
    if (!Cdl::string_to_double("0.0", val)) {
500
        CYG_TEST_FAIL("string_to_double() failed for 0.0");
501
        result = false;
502
    } else if (fabs(val) > 0.000001) {
503
        CYG_TEST_FAIL("string_to_double() produced strange result for 0.0");
504
        result = false;
505
    }
506
    if (!Cdl::string_to_double("3.141592", val)) {
507
        CYG_TEST_FAIL("string_to_double() failed for pi");
508
        result = false;
509
    } else if (fabs(val - 3.141592) > 0.000001) {
510
        CYG_TEST_FAIL("string_to_double() produced strange result for pi");
511
        result = false;
512
    }
513
    if (!Cdl::string_to_double("-1.23456789E15", val)) {
514
        CYG_TEST_FAIL("string_to_double() failed for large but legal value");
515
        result = false;
516
    } else if (fabs(val - -1.23456789E15) > 0.000001) {
517
        CYG_TEST_FAIL("string_to_double() produced strange result for large but legal value");
518
        result = false;
519
    }
520
    if (Cdl::string_to_double("random junk", val)) {
521
        CYG_TEST_FAIL("string_to_double() succeeded for junk data");
522
        result = false;
523
    }
524
    if (Cdl::string_to_double("1.23456789E1234", val)) {
525
        CYG_TEST_FAIL("string_to_double() succeeded for impossibly large value");
526
        result = false;
527
    }
528
    if (Cdl::string_to_double("1.0 and then some", val)) {
529
        CYG_TEST_FAIL("string_to_double() succeeded for number followed by junk");
530
        result = false;
531
    }
532
 
533
    CYG_REPORT_RETVAL(result);
534
    return result;
535
}
536
 
537
int
538
main(int argc, char** argv)
539
{
540
    CYG_REPORT_FUNCNAMETYPE("main", "result %d");
541
 
542
    if (test_is_valid_property_id()) {
543
        CYG_TEST_PASS("is_valid_property_id()");
544
    }
545
    if (test_is_valid_property_class()) {
546
        CYG_TEST_PASS("is_valid_property_class()");
547
    }
548
    if (test_is_valid_object_type()) {
549
        CYG_TEST_PASS("is_valid_object_type()");
550
    }
551
    if (test_is_valid_option_flavor()) {
552
        CYG_TEST_PASS("is_valid_option_flavor");
553
    }
554
    if (test_is_valid_value_source()) {
555
        CYG_TEST_PASS("is_valid_value_source");
556
    }
557
    if (test_string_to_integer()) {
558
        CYG_TEST_PASS("string_to_integer");
559
    }
560
    if (test_string_to_bool()) {
561
        CYG_TEST_PASS("string to bool");
562
    }
563
    if (test_integer_to_string()) {
564
        CYG_TEST_PASS("integer_to_string");
565
    }
566
    if (test_bool_to_string()) {
567
        CYG_TEST_PASS("bool_to_string");
568
    }
569
    if (test_string_to_double()) {
570
        CYG_TEST_PASS("string_to_double");
571
    }
572
    if (test_double_to_string()) {
573
        CYG_TEST_PASS("double_to_string");
574
    }
575
 
576
    CYG_REPORT_RETVAL(EXIT_SUCCESS);
577
    return EXIT_SUCCESS;
578
}
579
 

powered by: WebSVN 2.1.0

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