コード例 #1
0
void checkUnescaping(const Sid::String& test, const Sid::String& expected)
{
    const char* origData = test.data();
    Sid::String unescaped=test.unescape();
    CPPUNIT_ASSERT_PRINTF(unescaped == expected,
    			"Unescaping \"%s\" did not yield \"%s\" as "
    			"expected, instead yielded \"%s\"\n",
    			!test.isNull() ? test.data() : "(NULL)",
    			!expected.isNull() ? expected.data() : "(NULL)",
    			!unescaped.isNull() ? unescaped.data() : "(NULL)");
    CPPUNIT_ASSERT_MESSAGE("unescape() causes object to relinquish its reference!",
                            origData == test.data());
}
コード例 #2
0
void StringTestCase::escapingAndUnescapingTest()
{
    checkEscaping(Sid::String(), Sid::String());
    checkEscaping(Sid::String(4), "");
    checkEscaping("", "");
    checkEscaping(",", "\\,");
    checkEscaping("\"", "\\\"");
    checkEscaping("\\", "\\\\");
    checkEscaping("foobar", "foobar");
    checkEscaping("foo,bar", "foo\\,bar");
    checkEscaping("foo\"bar", "foo\\\"bar");
    checkEscaping("foo\\bar", "foo\\\\bar");
    checkEscaping(",,", "\\,\\,");
    checkEscaping("\"\"", "\\\"\\\"");
    checkEscaping("\\\\", "\\\\\\\\");

    checkUnescaping("\\", "");
    checkUnescaping("foo\\bar", "foobar");
    checkUnescaping("\\foobar", "foobar");
    checkUnescaping("foobar\\", "foobar");

    // .bwc. This code came from an #ifdef SE_STRING_TEST_PROGRAM block in
    // skype_string.cpp.
    int i;
    Sid::String strings[] = {
    	"Hello World!",
    	"Hel,lo\\ World!",
    	"Hel\"lo Wo\\9rld!",
    	"Hel,lo Wo\\0rld!",
    	Sid::String()
    };
    for (i = 0; !strings[i].isNull(); i++) {
    	Sid::String escaped = strings[i].escape();
    	Sid::String unescaped = escaped.unescape();
    	CPPUNIT_ASSERT_PRINTF( !strcmp((const char*)strings[i], (const char*)unescaped),
    				"String Failed:\n%s\n%s\n%s\n\n", 
    				(const char*)strings[i], 
    				(const char*)escaped, 
    				(const char*)unescaped);
    }

    // 0x5c = '\\', 0x30 = '0', 0x00 = 0, 0x22 = '"'
    const char bins[] = {
    	'1', '2', '3', '4', '5', '6', '7', '8' ,
    	'1', '2', 0x5c, 0x30, '5', '6', '7', '8' ,
    	'1', '2', 0x5c, 0x30, 0x5c, 0x5c, 0x5c, '8' ,
    	'1', '2', 0x5c, 0x30, '5', '6', '7', '8' ,
    	'1', '2', 0x00, '4', '5', '6', '7', '8' ,
    	'1', 0x5c, 0x00, 0x5c, '5', '6', 0x5c, 0x5c ,
    	'1', 0x5c, 0x00, 0x00, '5', 0x00, 0x5c, '8' ,
    	0x00, '2', 0x5c, 0x30, '5', 0x22, 0x22, '8' ,
    	0x5c, '2', 0x5c, 0x30, '5', 0x22, 0x22, 0x00 ,
    	0x00, 0x00, 0x5c, 0x30, '5', 0x22, 0x22, 0x5c ,
    	'E'
    };
    size_t bins_len = 8;
    for (i = 0; bins[i] != 'E'; i += bins_len) {
    	Sid::String escaped = Sid::String::from((char*)(bins + i), bins_len);
    	char *unescaped = (char *)malloc(escaped.length());
    	size_t unescaped_len = escaped.toBinary(unescaped);
    	//printf("%d, escaped.length()=%d unescaped_len=%d\n", i/bins_len, escaped.length(), unescaped_len);
    	CPPUNIT_ASSERT_PRINTF ( !memcmp((char*)(bins + i), unescaped, bins_len) && (unescaped_len == bins_len),
    				"Binary Failed: index %d\n", 
    				i/bins_len);
    	//printf("%s\n", (const char*)escaped.getHexRepresentation());
    	free(unescaped);
    }

    {
    	Sid::String test("foo");
    	test.markAsBinary();
    	CPPUNIT_ASSERT_PRINTF(test.isBinary(), 
    				"%s", "markAsBinary() seems "
    						"to have no effect.\n");
    }
}