⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cdl1.cxx

📁 基于ecos的redboot
💻 CXX
📖 第 1 页 / 共 2 页
字号:
                std::string msg = "the string \"" + source + "\" is invalid but was still converted";
                CYG_TEST_FAIL(msg.c_str());
                result = false;
            }
        }
    }
    
    CYG_REPORT_RETVAL(result);
    return result;
}

static bool
test_integer_to_string(void)
{
    bool result = true;
    CYG_REPORT_FUNCNAMETYPE("test_integer_to_string", "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.
    //
    // Integer to string conversions cannot fail.
    //
    // Compiler bogosity: VC++ does not appear the more normal way
    // of initializing an array of structures using nested braces.
    struct conversion_data {
        cdl_int         source;
        const char*     expected;
    } data[] = {
        {                0,           "0"  },
        {                1,           "1"  },
        {               -1,          "-1"  },
        {               10,          "10"  },
        {             1234,        "1234"  },
        {          -456789,     "-456789"  },
        {       2147483647,  "2147483647"  },
        { ((cdl_int) 111111111) * ((cdl_int) 111111111), "12345678987654321"  },
        {      (cdl_int) 0, "finished"     }
    };

    for (int i = 0; 0 != strcmp("finished", data[i].expected); i++) {
        std::string res;
        if (!Cdl::integer_to_string(data[i].source, res)) {
            std::string msg = "the integer \"" + std::string(data[i].expected) + "\" was not converted";
            CYG_TEST_FAIL(msg.c_str());
            result = false;
        } else if (res != data[i].expected) {
            std::string msg = "the string \"" + std::string(data[i].expected) + "\" was converted incorrectly";
            CYG_TEST_FAIL(msg.c_str());
            result = false;
        }
    }

    // Just a few more tests. Try converting some sequences to a string and back
    // again.
    for (int j = 0; j < 4; j++) {
        cdl_int starting_values[] = { 1, 3, -1, -2 };
        cdl_int current_value     = starting_values[j];

        for (int k = 0; k < 60; k++) {
            current_value <<= 1;
            cdl_int     int_tmp;
            std::string str_tmp;
            if (!Cdl::integer_to_string(current_value, str_tmp)) {
                CYG_TEST_FAIL("unable to convert valid integer to string");
                result = false;
                break;
            }
            if (!Cdl::string_to_integer(str_tmp, int_tmp)) {
                CYG_TEST_FAIL("unable to convert string back to integer");
                result = false;
                break;
            }
            if (current_value != int_tmp) {
                CYG_TEST_FAIL("integer conversion to/from strings not idempotent");
                result = false;
                break;
            }
        }
    }
    
    CYG_REPORT_RETVAL(result);
    return result;
}

static bool
test_string_to_bool(void)
{
    bool result = true;
    CYG_REPORT_FUNCNAMETYPE("test_string_to_bool", "success %d");

    // Legal values for true  include 1 and "true".
    // Legal values for false include 0 and "false".
    // A random string should fail to convert.
    bool res;
    if (!Cdl::string_to_bool("1", res)) {
        CYG_TEST_FAIL("the string \"1\" was not converted");
        result = false;
    } else if (res != true) {
        CYG_TEST_FAIL("the string \"1\" did not convert to true");
        result = false;
    }
    if (!Cdl::string_to_bool("true", res)) {
        CYG_TEST_FAIL("the string \"true\" was not converted");
        result = false;
    } else if (res != true) {
        CYG_TEST_FAIL("the string \"true\" did not convert to true");
        result = false;
    }
    if (!Cdl::string_to_bool("0", res)) {
        CYG_TEST_FAIL("the string \"0\" was not converted");
        result = false;
    } else if (res != false) {
        CYG_TEST_FAIL("the string \"0\" did not convert to false");
        result = false;
    }
    if (!Cdl::string_to_bool("false", res)) {
        CYG_TEST_FAIL("the string \"false\" was not converted");
        result = false;
    } else if (res != false) {
        CYG_TEST_FAIL("the string \"false\" did not convert to false");
        result = false;
    }
    if (Cdl::string_to_bool("not a boolean string", res)) {
        CYG_TEST_FAIL("a spurious string was converted to a boolean");
        result = false;
    }
    
    CYG_REPORT_RETVAL(result);
    return result;
}

static bool
test_bool_to_string(void)
{
    bool result = true;
    CYG_REPORT_FUNCNAMETYPE("test_bool_to_string", "success %d");

    std::string str;
    if (!Cdl::bool_to_string(true, str)) {
        CYG_TEST_FAIL("bool_to_string() failed for `true'");
        result = false;
    } else if ("1" != str) {
        CYG_TEST_FAIL("boolean value true translated incorrectly");
        result = false;
    }
    if (!Cdl::bool_to_string(false,str)) {
        CYG_TEST_FAIL("bool_to_string() failed for `true'");
        result = false;
    } else if ("0" != str) {
        CYG_TEST_FAIL("boolean value false translated incorrectly");
        result = false;
    }
    // There is no easy way to test failure conditions. The trick
    // of sticking a random number into a union will not work, there
    // are absolutely no guarantees about the internal implementation
    // of the bool data type in C++
    
    CYG_REPORT_RETVAL(result);
    return result;
}

// This test is not intended to be comprehensive. In particular it is
// not very easy to validate the results, issues like locales get in the way.
static bool
test_double_to_string(void)
{
    bool result = true;
    CYG_REPORT_FUNCNAMETYPE("test_double_to_string", "success %d");

    std::string str;
    if (!Cdl::double_to_string(0.0, str)) {
        CYG_TEST_FAIL("double_to_string() failed for 0.0");
        result = false;
    } else if (('0' != str[0]) && (('-' != str[0]) && ('0' != str[1]))) {
        fprintf(stderr, "result of conversion is %s\n", str.c_str());
        CYG_TEST_FAIL("double_to_string() returned strange result for 0.0");
        result = false;
    }
    
    if (!Cdl::double_to_string(3.141592, str)) {
        CYG_TEST_FAIL("double_to_string() failed for pi");
        result = false;
    } else if (('3' != str[0]) || ('1' != str[2]) || ('4' != str[3]) || ('1' != str[4])) {
        CYG_TEST_FAIL("double_to_string() returned strange result for pi");
        result = false;
    }

    if (!Cdl::double_to_string(-1.23456789E15, str)) {
        CYG_TEST_FAIL("double_to_string() failed for large but legal value");
        result = false;
    } else if (('-' != str[0]) && ('1' != str[1]) && (10 >= str.size())) {
        CYG_TEST_FAIL("double_to_string() returned strange result for large but legal value");
        result = false;
    }

    CYG_REPORT_RETVAL(result);
    return result;
}

// This test is not intended to be comprehensive.
static bool
test_string_to_double(void)
{
    bool result = true;
    CYG_REPORT_FUNCNAMETYPE("test_string_to_double", "success %d");

    double val;
    if (!Cdl::string_to_double("0.0", val)) {
        CYG_TEST_FAIL("string_to_double() failed for 0.0");
        result = false;
    } else if (fabs(val) > 0.000001) {
        CYG_TEST_FAIL("string_to_double() produced strange result for 0.0");
        result = false;
    }
    if (!Cdl::string_to_double("3.141592", val)) {
        CYG_TEST_FAIL("string_to_double() failed for pi");
        result = false;
    } else if (fabs(val - 3.141592) > 0.000001) {
        CYG_TEST_FAIL("string_to_double() produced strange result for pi");
        result = false;
    }
    if (!Cdl::string_to_double("-1.23456789E15", val)) {
        CYG_TEST_FAIL("string_to_double() failed for large but legal value");
        result = false;
    } else if (fabs(val - -1.23456789E15) > 0.000001) {
        CYG_TEST_FAIL("string_to_double() produced strange result for large but legal value");
        result = false;
    }
    if (Cdl::string_to_double("random junk", val)) {
        CYG_TEST_FAIL("string_to_double() succeeded for junk data");
        result = false;
    }
    if (Cdl::string_to_double("1.23456789E1234", val)) {
        CYG_TEST_FAIL("string_to_double() succeeded for impossibly large value");
        result = false;
    }
    if (Cdl::string_to_double("1.0 and then some", val)) {
        CYG_TEST_FAIL("string_to_double() succeeded for number followed by junk");
        result = false;
    }
    
    CYG_REPORT_RETVAL(result);
    return result;
}

int
main(int argc, char** argv)
{
    CYG_REPORT_FUNCNAMETYPE("main", "result %d");
    
    if (test_is_valid_property_id()) {
        CYG_TEST_PASS("is_valid_property_id()");
    }
    if (test_is_valid_property_class()) {
        CYG_TEST_PASS("is_valid_property_class()");
    }
    if (test_is_valid_object_type()) {
        CYG_TEST_PASS("is_valid_object_type()");
    }
    if (test_is_valid_option_flavor()) {
        CYG_TEST_PASS("is_valid_option_flavor");
    }
    if (test_is_valid_value_source()) {
        CYG_TEST_PASS("is_valid_value_source");
    }
    if (test_string_to_integer()) {
        CYG_TEST_PASS("string_to_integer");
    }
    if (test_string_to_bool()) {
        CYG_TEST_PASS("string to bool");
    }
    if (test_integer_to_string()) {
        CYG_TEST_PASS("integer_to_string");
    }
    if (test_bool_to_string()) {
        CYG_TEST_PASS("bool_to_string");
    }
    if (test_string_to_double()) {
        CYG_TEST_PASS("string_to_double");
    }
    if (test_double_to_string()) {
        CYG_TEST_PASS("double_to_string");
    }
    
    CYG_REPORT_RETVAL(EXIT_SUCCESS);
    return EXIT_SUCCESS;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -