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

📄 test_xstring.cpp

📁 经典的string 函数库学习资料
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/** * @file test_xstring.cpp * @brief 字符串的包装,测试用例 * @author 泥偶 * @since  2003-08-29 * @date   2003-09-12 */#include <sys/time.h>#include <cstdio>#include <iostream>#include <stdexcept>#include "xstring.hpp"using namespace std;using namespace x;void test(const bool expression, const string& output = ""){    static int i = 0;    ++i;    printf("%5d", i);    if (expression)    {        cout << " ................................... PASS" << endl;        return;    }    cout << " ................................... FAIL" << endl;    cout << "====== The actual result is following =======" << endl;    cout << output << endl;    cout << "====== The actual result is above ===========" << endl;    exit(1);}int main(){    {        cout << "xstring()" << endl;        xstring str2;        test(str2.empty());    }    {        cout << "xstring(const string& str)" << endl;        string str1 = "Hello World!";        xstring str2(str1);        test(str2 == str1);    }    {        cout << "xstring(const string& str, size_type pos, size_type n = npos)" << endl;        string str1 = "Hello World!";        xstring str2(str1, 6, 5);        test(str2 == "World", str2);        xstring str3(str1, 6);        test(str3 == "World!", str3);    }    {        cout << "xstring(const char* s)" << endl;        xstring str2("Hello World!");        test(str2 == "Hello World!");    }    {        cout << "xstring(const char* s, size_type n)" << endl;        xstring str2("Hello World!", 5);        test(str2 == "Hello");    }    {        cout << "xstring(size_type n, char c)" << endl;        xstring str2(5, 'c');        test(str2 == "ccccc");    }    {        cout << "xstring(iterator begin, iterator end)" << endl;        string str1 = "Hello World!";        xstring str2(str1.begin() + 6, str1.end() - 1);        test(str2 == "World", str2);    }    {        cout << "xstring(const xstring& str)" << endl;        xstring str1 = "Hello World!";        xstring str2(str1);        test(str2 == str1);    }    {        cout << "xstring(const xstring& str, size_type pos, size_type n = npos)" << endl;        xstring str1 = "Hello World!";        xstring str2(str1, 6, 5);        test(str2 == "World", str2);        xstring str3(str1, 6);        test(str3 == "World!", str3);    }    {        cout << "xstring(const char v)" << endl;        char v = 'a';        xstring str2(v);        test(str2 == "a", str2);    }    {        cout << "xstring(const signed char v)" << endl;        signed char v = 'a';        xstring str2(v);        test(str2 == "a", str2);    }    {        cout << "xstring(const unsigned char v)" << endl;        unsigned char v = 'a';        xstring str2(v);        test(str2 == "a", str2);    }    {        cout << "xstring(const int v)" << endl;        xstring str2(static_cast<int>(0));        test(str2 == "0");        xstring str3(static_cast<int>(1));        test(str3 == "1");        xstring str4(static_cast<int>(-1));        test(str4 == "-1");        xstring str5(static_cast<int>(2147483647));        test(str5 == "2147483647", str5);        xstring str6(static_cast<int>(2147483648U));        test(str6 == "-2147483648", str6);    }    {        cout << "xstring(const short int v)" << endl;        xstring str2(static_cast<short int>(0));        test(str2 == "0");        xstring str3(static_cast<short int>(1));        test(str3 == "1");        xstring str4(static_cast<short int>(-1));        test(str4 == "-1");        xstring str5(static_cast<short int>(32767));        test(str5 == "32767", str5);        xstring str6(static_cast<short int>(-32768));        test(str6 == "-32768", str6);    }    {        cout << "xstring(const long int v)" << endl;        xstring str2(static_cast<long int>(0));        test(str2 == "0");        xstring str3(static_cast<long int>(1));        test(str3 == "1");        xstring str4(static_cast<long int>(-1));        test(str4 == "-1");        xstring str5(static_cast<long int>(2147483647L));        test(str5 == "2147483647", str5);        xstring str6(static_cast<long int>(2147483648LU));        test(str6 == "-2147483648", str6);    }    {        cout << "xstring(const long long int v)" << endl;        xstring str2(static_cast<long long int>(0));        test(str2 == "0");        xstring str3(static_cast<long long int>(1));        test(str3 == "1");        xstring str4(static_cast<long long int>(-1));        test(str4 == "-1");        xstring str5(static_cast<long long int>(2147483647LL));        test(str5 == "2147483647", str5);        xstring str6(static_cast<long long int>(-2147483648LL));        test(str6 == "-2147483648", str6);        xstring str7(static_cast<long long int>(9223372036854775807LL));        test(str7 == "9223372036854775807", str7);        xstring str8(static_cast<long long int>(9223372036854775808LLU));        test(str8 == "-9223372036854775808", str8);    }    {        cout << "xstring(const unsigned int v)" << endl;        xstring str2(static_cast<unsigned int>(0));        test(str2 == "0", str2);        xstring str3(static_cast<unsigned int>(1));        test(str3 == "1", str3);        xstring str4(static_cast<unsigned int>(-1));        test(str4 == "4294967295", str4);        xstring str5(static_cast<unsigned int>(4294967295U));        test(str5 == "4294967295", str5);    }    {        cout << "xstring(const short unsigned int v)" << endl;        xstring str2(static_cast<short unsigned int>(0));        test(str2 == "0", str2);        xstring str3(static_cast<short unsigned int>(1));        test(str3 == "1", str3);        xstring str4(static_cast<short unsigned int>(-1));        test(str4 == "65535", str4);        xstring str5(static_cast<short unsigned int>(65535));        test(str5 == "65535", str5);    }    {        cout << "xstring(const long unsigned int v)" << endl;        xstring str2(static_cast<long unsigned int>(0));        test(str2 == "0", str2);        xstring str3(static_cast<long unsigned int>(1));        test(str3 == "1", str3);        xstring str4(static_cast<long unsigned int>(-1));        test(str4 == "4294967295", str4);        xstring str5(static_cast<long unsigned int>(4294967295LU));        test(str5 == "4294967295", str5);    }    {        cout << "xstring(const long long unsigned int v)" << endl;        xstring str2(static_cast<long long unsigned int>(0));        test(str2 == "0", str2);        xstring str3(static_cast<long long unsigned int>(1));        test(str3 == "1", str3);        xstring str4(static_cast<long long unsigned int>(-1));        test(str4 == "18446744073709551615", str4);        xstring str5(static_cast<long long unsigned int>(18446744073709551615LLU));        test(str5 == "18446744073709551615", str5);    }    {        cout << "xstring(const float v)" << endl;        xstring str2(static_cast<float>(0));        test(str2 == "0.000000", str2);        xstring str3(static_cast<float>(1));        test(str3 == "1.000000", str3);        xstring str4(static_cast<float>(-1));        test(str4 == "-1.000000", str4);        xstring str5(static_cast<float>(1e38));        cout << str5 << endl;        xstring str6(static_cast<float>(3.4028e38));        cout << str6 << endl;        xstring str7(static_cast<float>(1e39));        cout << str7 << endl;        xstring str8(static_cast<float>(1e40));        cout << str8 << endl;    }    {        cout << "xstring(const double v)" << endl;        xstring str2(static_cast<double>(0));        test(str2 == "0.000000", str2);        xstring str3(static_cast<double>(1));        test(str3 == "1.000000", str3);        xstring str4(static_cast<double>(-1));        test(str4 == "-1.000000", str4);        xstring str5(static_cast<double>(1e38));        cout << str5 << endl;        xstring str6(static_cast<double>(1e39));        cout << str6 << endl;        xstring str7(static_cast<double>(1e200));        cout << str7 << endl;        xstring str8(static_cast<double>(1e300));        cout << str8 << endl;        xstring str9(static_cast<double>(1e400));        cout << str9 << endl;    }    {        cout << "xstring(const long double v)" << endl;        xstring str2(static_cast<long double>(0));        test(str2 == "0.000000", str2);        xstring str3(static_cast<long double>(1));        test(str3 == "1.000000", str3);        xstring str4(static_cast<long double>(-1));        test(str4 == "-1.000000", str4);        xstring str5(static_cast<long double>(1e38));        cout << str5 << endl;        xstring str6(static_cast<long double>(1e39));        cout << str6 << endl;        xstring str7(static_cast<long double>(1e200));        cout << str7 << endl;        xstring str8(static_cast<long double>(1e300));        cout << str8 << endl;        xstring str9(static_cast<long double>(1e400));        cout << str9 << endl;        xstring str10(static_cast<long double>(1e-38));        cout << str10 << endl;        xstring str11(static_cast<long double>(1e-39));        cout << str11 << endl;        xstring str12(static_cast<long double>(1e-100));        cout << str12 << endl;    }    {	cout << "xstring(int*)" << endl;	int i = 0;	xstring str(&i);        test(str.size() > 2, str);        test(str.substr(0, 2) == "0x", str);    }    {	cout << "xstring(float*)" << endl;	float f = 0;	xstring str(&f);        test(str.size() > 2, str);        test(str.substr(0, 2) == "0x", str);    }    {	cout << "xstring(void*)" << endl;	int i = 0;	xstring str(static_cast<void*>(&i));        test(str.size() > 2, str);        test(str.substr(0, 2) == "0x", str);    }    {        cout << "xstring(NULL)" << endl;        try        {            xstring str = NULL;            test(str == "0", str);        }	catch (logic_error& e)	{            // attempt to create string with null pointer            test(true);	}        catch (exception& e)        {	    // cout << typeid(e).name() << endl;            test(false, e.what());        }        catch (...)        {            test(false, "Unknown exception!");        }    }    {        cout << "to<int>(void)" << endl;        int i;        i = xstring("0").to<int>();        test(i == 0);        i = xstring("1").to<int>();        test(i == 1);        i = xstring("-1").to<int>();        test(i == -1);    }    {        cout << "to<long int>(void)" << endl;        int i;        i = xstring("0").to<long int>();        test(i == 0);        i = xstring("1").to<long int>();        test(i == 1);        i = xstring("-1").to<long int>();        test(i == -1);    }    {        cout << "to<float>(void)" << endl;        float v;        v = xstring("0").to<float>();        test(v == 0);        v = xstring("1").to<float>();        test(v == 1);        v = xstring("-1").to<float>();        test(v == -1);    }    {        cout << "to<double>(void)" << endl;        double v;        v = xstring("0").to<double>();        test(v == 0);        v = xstring("1").to<double>();

⌨️ 快捷键说明

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