📄 cdl1.cxx
字号:
//==========================================================================//// cdl1.cxx//// Basic testing of the CDL utility functions. ////==========================================================================//####COPYRIGHTBEGIN####// // ----------------------------------------------------------------------------// Copyright (C) 1999, 2000 Red Hat, Inc.//// This file is part of the eCos host tools.//// This program is free software; you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by the Free // Software Foundation; either version 2 of the License, or (at your option) // any later version.// // This program is distributed in the hope that it will be useful, but WITHOUT // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for // more details.// // You should have received a copy of the GNU General Public License along with// this program; if not, write to the Free Software Foundation, Inc., // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.//// ----------------------------------------------------------------------------// //####COPYRIGHTEND####//==========================================================================//#####DESCRIPTIONBEGIN#### //// Author(s): bartv// Contributors: bartv// Date: 1999-01-06// Description: There are a number of utility functions in the// Cdl class to handle data validation, various// conversions, etc.////####DESCRIPTIONEND####//==========================================================================#include <cdlconfig.h>#include <cdl.hxx>#include <cyg/infra/cyg_ass.h>#include <cyg/infra/cyg_trac.h>#include <cyg/infra/testcase.h>#include <cstdlib>#include <cmath>static booltest_is_valid_property_id(void){ bool result = true; CYG_REPORT_FUNCNAMETYPE("test_is_valid_property_id", "success %d"); if (!Cdl::is_valid_property_id(CdlPropertyId_ActiveIf)) { CYG_TEST_FAIL("ActiveIf should be a valid property id"); result = false; } if (!Cdl::is_valid_property_id(CdlPropertyId_DefaultValue)) { CYG_TEST_FAIL("DefaultValue should be a valid property id"); result = false; } if (!Cdl::is_valid_property_id(CdlPropertyId_Wizard)) { CYG_TEST_FAIL("Wizard should be a valid property id"); result = false; } if (Cdl::is_valid_property_id(CdlPropertyId_Unknown)) { CYG_TEST_FAIL("is_valid_property_id() accepted an invalid number"); result = false; } // This test could give spurious negatives in the very long term union { int x; CdlPropertyId id; } dummy; dummy.x = 1234; if (Cdl::is_valid_property_id(dummy.id)) { CYG_TEST_FAIL("is_valid_property_id() accepted an invalid number"); result = false; } CYG_REPORT_RETVAL(result); return result;}static booltest_is_valid_property_class(void){ bool result = true; CYG_REPORT_FUNCNAMETYPE("test_is_valid_property_class", "success %d"); if (!Cdl::is_valid_property_class(CdlPropertyClass_Minimal)) { CYG_TEST_FAIL("Minimal should be a valid property class"); result = false; } if (!Cdl::is_valid_property_class(CdlPropertyClass_TclCode)) { CYG_TEST_FAIL("TclCode should be a valid property class"); result = false; } if (!Cdl::is_valid_property_class(CdlPropertyClass_GoalExpression)) { CYG_TEST_FAIL("GoalExpression should be a valid property class"); result = false; } if (Cdl::is_valid_property_class(CdlPropertyClass_Unknown)) { CYG_TEST_FAIL("is_valid_property_class() accepted an invalid number"); result = false; } // This test could give spurious negatives in the very long term union { int x; CdlPropertyClass id; } dummy; dummy.x = 1234; if (Cdl::is_valid_property_class(dummy.id)) { CYG_TEST_FAIL("is_valid_property_class() accepted an invalid number"); result = false; } CYG_REPORT_RETVAL(result); return result;}static booltest_is_valid_object_type(void){ bool result = true; CYG_REPORT_FUNCNAMETYPE("test_is_valid_object_type", "success %d"); if (!Cdl::is_valid_object_type(CdlObjectType_Package)) { CYG_TEST_FAIL("Package should be a valid object type"); result = false; } if (!Cdl::is_valid_object_type(CdlObjectType_Component)) { CYG_TEST_FAIL("Component should be a valid object type"); result = false; } if (!Cdl::is_valid_object_type(CdlObjectType_Option)) { CYG_TEST_FAIL("Option should be a valid object type"); result = false; } if (Cdl::is_valid_object_type(CdlObjectType_Unknown)) { CYG_TEST_FAIL("is_valid_object_type() accepted an invalid number"); result = false; } // This test could give spurious negatives in the very long term union { int x; CdlObjectType id; } dummy; dummy.x = 1234; if (Cdl::is_valid_object_type(dummy.id)) { CYG_TEST_FAIL("is_valid_object_type() accepted an invalid number"); result = false; } CYG_REPORT_RETVAL(result); return result;}static booltest_is_valid_option_flavor(void){ bool result = true; CYG_REPORT_FUNCNAMETYPE("test_is_valid_option_flavor", "success %d"); if (!Cdl::is_valid_option_flavor(CdlOptionFlavor_Bool)) { CYG_TEST_FAIL("Bool should be a valid option flavor"); result = false; } if (!Cdl::is_valid_option_flavor(CdlOptionFlavor_Enum)) { CYG_TEST_FAIL("Enum should be a valid option flavor"); result = false; } if (!Cdl::is_valid_option_flavor(CdlOptionFlavor_Count)) { CYG_TEST_FAIL("Count should be a valid option flavor"); result = false; } if (Cdl::is_valid_option_flavor(CdlOptionFlavor_Unknown)) { CYG_TEST_FAIL("is_valid_option_flavor() accepted an invalid number"); result = false; } // This test could give spurious negatives in the very long term union { int x; CdlOptionFlavor id; } dummy; dummy.x = 1234; if (Cdl::is_valid_option_flavor(dummy.id)) { CYG_TEST_FAIL("is_valid_option_flavor() accepted an invalid number"); result = false; } CYG_REPORT_RETVAL(result); return result;}static booltest_is_valid_value_source(void){ bool result = true; CYG_REPORT_FUNCNAMETYPE("test_is_valid_value_source", "success %d"); if (!Cdl::is_valid_value_source(CdlValueSource_Default)) { CYG_TEST_FAIL("Default is a valid value source"); result = false; } if (!Cdl::is_valid_value_source(CdlValueSource_User)) { CYG_TEST_FAIL("User is a valid value source"); result = false; } if (!Cdl::is_valid_value_source(CdlValueSource_Wizard)) { CYG_TEST_FAIL("Wizard is a valid value source"); result = false; } if (!Cdl::is_valid_value_source(CdlValueSource_Inferred)) { CYG_TEST_FAIL("Inferred is a valid value source"); result = false; } if (Cdl::is_valid_value_source(CdlValueSource_Unknown)) { CYG_TEST_FAIL("is_valid_value_source() accepted an invalid number"); result = false; } // This test could give spurious negatives in the very long term union { int x; CdlValueSource id; } dummy; dummy.x = 1234; if (Cdl::is_valid_value_source(dummy.id)) { CYG_TEST_FAIL("is_valid_value_source() accepted an invalid number"); result = false; } CYG_REPORT_RETVAL(result); return result;}static booltest_string_to_integer(void){ bool result = true; CYG_REPORT_FUNCNAMETYPE("test_string_to_integer", "success %d"); // Note that there is no robust way of specifying 64 bit constants. // Some compilers will use a LL suffix, but not all. When a 64 bit // constant is needed this has to be achieved using arithmetic, // leaving the compiler to do the hard work. // // Compiler bogosity: VC++ does not appear the more normal way // of initializing an array of structures using nested braces. struct conversion_data { bool ok; const char* source; cdl_int expected; } data[] = { { true, "0", 0 }, { true, "1", 1 }, { true, "-1", -1 }, { true, "0x0", 0 }, { true, "0x1", 1 }, { true, "01", 1 }, { true, "1234", 1234 }, { true, "-4567", -4567 }, { true, "2147483647", 2147483647 }, { true,"-2147483648", ((cdl_int) -2147483647) - 1 }, { true, "0x7fffffff", 2147483647 }, { true,"-0x80000000", ((cdl_int) -2147483647) -1 }, { true, "0x12ABCDEF", 313249263 }, { true, "12345678987654321", ((cdl_int) 111111111) * ((cdl_int) 111111111) }, { false, "A", 0 }, { false, "0A", 0 }, { false, "1234*", 0 }, { false, "finished", 0 } }; for (int i = 0; 0 != strcmp("finished", data[i].source); i++) { cdl_int res; std::string source = std::string(data[i].source); if (data[i].ok) { if (!Cdl::string_to_integer(source, res)) { std::string msg = "the string \"" + source + "\" was not converted"; CYG_TEST_FAIL(msg.c_str()); result = false; } else if (res != data[i].expected) { std::string msg = "the string \"" + source + "\" was converted incorrectly"; CYG_TEST_FAIL(msg.c_str()); result = false; } } else { if (Cdl::string_to_integer(source, res)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -