1 //==========================================================================
5 // Basic testing of the CDL utility functions.
7 //==========================================================================
8 //####COPYRIGHTBEGIN####
10 // ----------------------------------------------------------------------------
11 // Copyright (C) 1999, 2000 Red Hat, Inc.
13 // This file is part of the eCos host tools.
15 // This program is free software; you can redistribute it and/or modify it
16 // under the terms of the GNU General Public License as published by the Free
17 // Software Foundation; either version 2 of the License, or (at your option)
20 // This program is distributed in the hope that it will be useful, but WITHOUT
21 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
25 // You should have received a copy of the GNU General Public License along with
26 // this program; if not, write to the Free Software Foundation, Inc.,
27 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 // ----------------------------------------------------------------------------
31 //####COPYRIGHTEND####
32 //==========================================================================
33 //#####DESCRIPTIONBEGIN####
36 // Contributors: bartv
38 // Description: There are a number of utility functions in the
39 // Cdl class to handle data validation, various
42 //####DESCRIPTIONEND####
43 //==========================================================================
45 #include <cdlconfig.h>
47 #include <cyg/infra/cyg_ass.h>
48 #include <cyg/infra/cyg_trac.h>
49 #include <cyg/infra/testcase.h>
54 test_is_valid_property_id(void)
57 CYG_REPORT_FUNCNAMETYPE("test_is_valid_property_id", "success %d");
59 if (!Cdl::is_valid_property_id(CdlPropertyId_ActiveIf)) {
60 CYG_TEST_FAIL("ActiveIf should be a valid property id");
63 if (!Cdl::is_valid_property_id(CdlPropertyId_DefaultValue)) {
64 CYG_TEST_FAIL("DefaultValue should be a valid property id");
67 if (!Cdl::is_valid_property_id(CdlPropertyId_Wizard)) {
68 CYG_TEST_FAIL("Wizard should be a valid property id");
71 if (Cdl::is_valid_property_id(CdlPropertyId_Unknown)) {
72 CYG_TEST_FAIL("is_valid_property_id() accepted an invalid number");
75 // This test could give spurious negatives in the very long term
81 if (Cdl::is_valid_property_id(dummy.id)) {
82 CYG_TEST_FAIL("is_valid_property_id() accepted an invalid number");
86 CYG_REPORT_RETVAL(result);
91 test_is_valid_property_class(void)
94 CYG_REPORT_FUNCNAMETYPE("test_is_valid_property_class", "success %d");
96 if (!Cdl::is_valid_property_class(CdlPropertyClass_Minimal)) {
97 CYG_TEST_FAIL("Minimal should be a valid property class");
100 if (!Cdl::is_valid_property_class(CdlPropertyClass_TclCode)) {
101 CYG_TEST_FAIL("TclCode should be a valid property class");
104 if (!Cdl::is_valid_property_class(CdlPropertyClass_GoalExpression)) {
105 CYG_TEST_FAIL("GoalExpression should be a valid property class");
108 if (Cdl::is_valid_property_class(CdlPropertyClass_Unknown)) {
109 CYG_TEST_FAIL("is_valid_property_class() accepted an invalid number");
112 // This test could give spurious negatives in the very long term
118 if (Cdl::is_valid_property_class(dummy.id)) {
119 CYG_TEST_FAIL("is_valid_property_class() accepted an invalid number");
123 CYG_REPORT_RETVAL(result);
128 test_is_valid_object_type(void)
131 CYG_REPORT_FUNCNAMETYPE("test_is_valid_object_type", "success %d");
133 if (!Cdl::is_valid_object_type(CdlObjectType_Package)) {
134 CYG_TEST_FAIL("Package should be a valid object type");
137 if (!Cdl::is_valid_object_type(CdlObjectType_Component)) {
138 CYG_TEST_FAIL("Component should be a valid object type");
141 if (!Cdl::is_valid_object_type(CdlObjectType_Option)) {
142 CYG_TEST_FAIL("Option should be a valid object type");
145 if (Cdl::is_valid_object_type(CdlObjectType_Unknown)) {
146 CYG_TEST_FAIL("is_valid_object_type() accepted an invalid number");
149 // This test could give spurious negatives in the very long term
155 if (Cdl::is_valid_object_type(dummy.id)) {
156 CYG_TEST_FAIL("is_valid_object_type() accepted an invalid number");
159 CYG_REPORT_RETVAL(result);
164 test_is_valid_option_flavor(void)
167 CYG_REPORT_FUNCNAMETYPE("test_is_valid_option_flavor", "success %d");
169 if (!Cdl::is_valid_option_flavor(CdlOptionFlavor_Bool)) {
170 CYG_TEST_FAIL("Bool should be a valid option flavor");
173 if (!Cdl::is_valid_option_flavor(CdlOptionFlavor_Enum)) {
174 CYG_TEST_FAIL("Enum should be a valid option flavor");
177 if (!Cdl::is_valid_option_flavor(CdlOptionFlavor_Count)) {
178 CYG_TEST_FAIL("Count should be a valid option flavor");
181 if (Cdl::is_valid_option_flavor(CdlOptionFlavor_Unknown)) {
182 CYG_TEST_FAIL("is_valid_option_flavor() accepted an invalid number");
185 // This test could give spurious negatives in the very long term
191 if (Cdl::is_valid_option_flavor(dummy.id)) {
192 CYG_TEST_FAIL("is_valid_option_flavor() accepted an invalid number");
195 CYG_REPORT_RETVAL(result);
200 test_is_valid_value_source(void)
203 CYG_REPORT_FUNCNAMETYPE("test_is_valid_value_source", "success %d");
205 if (!Cdl::is_valid_value_source(CdlValueSource_Default)) {
206 CYG_TEST_FAIL("Default is a valid value source");
209 if (!Cdl::is_valid_value_source(CdlValueSource_User)) {
210 CYG_TEST_FAIL("User is a valid value source");
213 if (!Cdl::is_valid_value_source(CdlValueSource_Wizard)) {
214 CYG_TEST_FAIL("Wizard is a valid value source");
217 if (!Cdl::is_valid_value_source(CdlValueSource_Inferred)) {
218 CYG_TEST_FAIL("Inferred is a valid value source");
221 if (Cdl::is_valid_value_source(CdlValueSource_Unknown)) {
222 CYG_TEST_FAIL("is_valid_value_source() accepted an invalid number");
225 // This test could give spurious negatives in the very long term
231 if (Cdl::is_valid_value_source(dummy.id)) {
232 CYG_TEST_FAIL("is_valid_value_source() accepted an invalid number");
236 CYG_REPORT_RETVAL(result);
241 test_string_to_integer(void)
244 CYG_REPORT_FUNCNAMETYPE("test_string_to_integer", "success %d");
246 // Note that there is no robust way of specifying 64 bit constants.
247 // Some compilers will use a LL suffix, but not all. When a 64 bit
248 // constant is needed this has to be achieved using arithmetic,
249 // leaving the compiler to do the hard work.
251 // Compiler bogosity: VC++ does not appear the more normal way
252 // of initializing an array of structures using nested braces.
253 struct conversion_data {
264 { true, "1234", 1234 },
265 { true, "-4567", -4567 },
266 { true, "2147483647", 2147483647 },
267 { true,"-2147483648", ((cdl_int) -2147483647) - 1 },
268 { true, "0x7fffffff", 2147483647 },
269 { true,"-0x80000000", ((cdl_int) -2147483647) -1 },
270 { true, "0x12ABCDEF", 313249263 },
271 { true, "12345678987654321", ((cdl_int) 111111111) * ((cdl_int) 111111111) },
274 { false, "1234*", 0 },
275 { false, "finished", 0 }
278 for (int i = 0; 0 != strcmp("finished", data[i].source); i++) {
280 std::string source = std::string(data[i].source);
282 if (!Cdl::string_to_integer(source, res)) {
283 std::string msg = "the string \"" + source + "\" was not converted";
284 CYG_TEST_FAIL(msg.c_str());
286 } else if (res != data[i].expected) {
287 std::string msg = "the string \"" + source + "\" was converted incorrectly";
288 CYG_TEST_FAIL(msg.c_str());
292 if (Cdl::string_to_integer(source, res)) {
293 std::string msg = "the string \"" + source + "\" is invalid but was still converted";
294 CYG_TEST_FAIL(msg.c_str());
300 CYG_REPORT_RETVAL(result);
305 test_integer_to_string(void)
308 CYG_REPORT_FUNCNAMETYPE("test_integer_to_string", "success %d");
310 // Note that there is no robust way of specifying 64 bit constants.
311 // Some compilers will use a LL suffix, but not all. When a 64 bit
312 // constant is needed this has to be achieved using arithmetic,
313 // leaving the compiler to do the hard work.
315 // Integer to string conversions cannot fail.
317 // Compiler bogosity: VC++ does not appear the more normal way
318 // of initializing an array of structures using nested braces.
319 struct conversion_data {
321 const char* expected;
328 { -456789, "-456789" },
329 { 2147483647, "2147483647" },
330 { ((cdl_int) 111111111) * ((cdl_int) 111111111), "12345678987654321" },
331 { (cdl_int) 0, "finished" }
334 for (int i = 0; 0 != strcmp("finished", data[i].expected); i++) {
336 if (!Cdl::integer_to_string(data[i].source, res)) {
337 std::string msg = "the integer \"" + std::string(data[i].expected) + "\" was not converted";
338 CYG_TEST_FAIL(msg.c_str());
340 } else if (res != data[i].expected) {
341 std::string msg = "the string \"" + std::string(data[i].expected) + "\" was converted incorrectly";
342 CYG_TEST_FAIL(msg.c_str());
347 // Just a few more tests. Try converting some sequences to a string and back
349 for (int j = 0; j < 4; j++) {
350 cdl_int starting_values[] = { 1, 3, -1, -2 };
351 cdl_int current_value = starting_values[j];
353 for (int k = 0; k < 60; k++) {
357 if (!Cdl::integer_to_string(current_value, str_tmp)) {
358 CYG_TEST_FAIL("unable to convert valid integer to string");
362 if (!Cdl::string_to_integer(str_tmp, int_tmp)) {
363 CYG_TEST_FAIL("unable to convert string back to integer");
367 if (current_value != int_tmp) {
368 CYG_TEST_FAIL("integer conversion to/from strings not idempotent");
375 CYG_REPORT_RETVAL(result);
380 test_string_to_bool(void)
383 CYG_REPORT_FUNCNAMETYPE("test_string_to_bool", "success %d");
385 // Legal values for true include 1 and "true".
386 // Legal values for false include 0 and "false".
387 // A random string should fail to convert.
389 if (!Cdl::string_to_bool("1", res)) {
390 CYG_TEST_FAIL("the string \"1\" was not converted");
392 } else if (res != true) {
393 CYG_TEST_FAIL("the string \"1\" did not convert to true");
396 if (!Cdl::string_to_bool("true", res)) {
397 CYG_TEST_FAIL("the string \"true\" was not converted");
399 } else if (res != true) {
400 CYG_TEST_FAIL("the string \"true\" did not convert to true");
403 if (!Cdl::string_to_bool("0", res)) {
404 CYG_TEST_FAIL("the string \"0\" was not converted");
406 } else if (res != false) {
407 CYG_TEST_FAIL("the string \"0\" did not convert to false");
410 if (!Cdl::string_to_bool("false", res)) {
411 CYG_TEST_FAIL("the string \"false\" was not converted");
413 } else if (res != false) {
414 CYG_TEST_FAIL("the string \"false\" did not convert to false");
417 if (Cdl::string_to_bool("not a boolean string", res)) {
418 CYG_TEST_FAIL("a spurious string was converted to a boolean");
422 CYG_REPORT_RETVAL(result);
427 test_bool_to_string(void)
430 CYG_REPORT_FUNCNAMETYPE("test_bool_to_string", "success %d");
433 if (!Cdl::bool_to_string(true, str)) {
434 CYG_TEST_FAIL("bool_to_string() failed for `true'");
436 } else if ("1" != str) {
437 CYG_TEST_FAIL("boolean value true translated incorrectly");
440 if (!Cdl::bool_to_string(false,str)) {
441 CYG_TEST_FAIL("bool_to_string() failed for `true'");
443 } else if ("0" != str) {
444 CYG_TEST_FAIL("boolean value false translated incorrectly");
447 // There is no easy way to test failure conditions. The trick
448 // of sticking a random number into a union will not work, there
449 // are absolutely no guarantees about the internal implementation
450 // of the bool data type in C++
452 CYG_REPORT_RETVAL(result);
456 // This test is not intended to be comprehensive. In particular it is
457 // not very easy to validate the results, issues like locales get in the way.
459 test_double_to_string(void)
462 CYG_REPORT_FUNCNAMETYPE("test_double_to_string", "success %d");
465 if (!Cdl::double_to_string(0.0, str)) {
466 CYG_TEST_FAIL("double_to_string() failed for 0.0");
468 } else if (('0' != str[0]) && (('-' != str[0]) && ('0' != str[1]))) {
469 fprintf(stderr, "result of conversion is %s\n", str.c_str());
470 CYG_TEST_FAIL("double_to_string() returned strange result for 0.0");
474 if (!Cdl::double_to_string(3.141592, str)) {
475 CYG_TEST_FAIL("double_to_string() failed for pi");
477 } else if (('3' != str[0]) || ('1' != str[2]) || ('4' != str[3]) || ('1' != str[4])) {
478 CYG_TEST_FAIL("double_to_string() returned strange result for pi");
482 if (!Cdl::double_to_string(-1.23456789E15, str)) {
483 CYG_TEST_FAIL("double_to_string() failed for large but legal value");
485 } else if (('-' != str[0]) && ('1' != str[1]) && (10 >= str.size())) {
486 CYG_TEST_FAIL("double_to_string() returned strange result for large but legal value");
490 CYG_REPORT_RETVAL(result);
494 // This test is not intended to be comprehensive.
496 test_string_to_double(void)
499 CYG_REPORT_FUNCNAMETYPE("test_string_to_double", "success %d");
502 if (!Cdl::string_to_double("0.0", val)) {
503 CYG_TEST_FAIL("string_to_double() failed for 0.0");
505 } else if (fabs(val) > 0.000001) {
506 CYG_TEST_FAIL("string_to_double() produced strange result for 0.0");
509 if (!Cdl::string_to_double("3.141592", val)) {
510 CYG_TEST_FAIL("string_to_double() failed for pi");
512 } else if (fabs(val - 3.141592) > 0.000001) {
513 CYG_TEST_FAIL("string_to_double() produced strange result for pi");
516 if (!Cdl::string_to_double("-1.23456789E15", val)) {
517 CYG_TEST_FAIL("string_to_double() failed for large but legal value");
519 } else if (fabs(val - -1.23456789E15) > 0.000001) {
520 CYG_TEST_FAIL("string_to_double() produced strange result for large but legal value");
523 if (Cdl::string_to_double("random junk", val)) {
524 CYG_TEST_FAIL("string_to_double() succeeded for junk data");
527 if (Cdl::string_to_double("1.23456789E1234", val)) {
528 CYG_TEST_FAIL("string_to_double() succeeded for impossibly large value");
531 if (Cdl::string_to_double("1.0 and then some", val)) {
532 CYG_TEST_FAIL("string_to_double() succeeded for number followed by junk");
536 CYG_REPORT_RETVAL(result);
541 main(int argc, char** argv)
543 CYG_REPORT_FUNCNAMETYPE("main", "result %d");
545 if (test_is_valid_property_id()) {
546 CYG_TEST_PASS("is_valid_property_id()");
548 if (test_is_valid_property_class()) {
549 CYG_TEST_PASS("is_valid_property_class()");
551 if (test_is_valid_object_type()) {
552 CYG_TEST_PASS("is_valid_object_type()");
554 if (test_is_valid_option_flavor()) {
555 CYG_TEST_PASS("is_valid_option_flavor");
557 if (test_is_valid_value_source()) {
558 CYG_TEST_PASS("is_valid_value_source");
560 if (test_string_to_integer()) {
561 CYG_TEST_PASS("string_to_integer");
563 if (test_string_to_bool()) {
564 CYG_TEST_PASS("string to bool");
566 if (test_integer_to_string()) {
567 CYG_TEST_PASS("integer_to_string");
569 if (test_bool_to_string()) {
570 CYG_TEST_PASS("bool_to_string");
572 if (test_string_to_double()) {
573 CYG_TEST_PASS("string_to_double");
575 if (test_double_to_string()) {
576 CYG_TEST_PASS("double_to_string");
579 CYG_REPORT_RETVAL(EXIT_SUCCESS);