Alien-SVN
view release on metacpan or search on metacpan
src/subversion/subversion/tests/libsvn_subr/utf-test.c view on Meta::CPAN
{TRUE, {'a', 'b', '\xF2', '\x81', '\x81', '\x81', 'x', 'y', '\0'}},
{FALSE, {'a', 'b', '\xF2', '\xC1', '\x81', '\x81', 'x', 'y', '\0'}},
{FALSE, {'a', 'b', '\xF2', '\x91', '\xC1', '\x81', 'x', 'y', '\0'}},
{FALSE, {'a', 'b', '\xF2', '\x91', '\x81', '\xC1', 'x', 'y', '\0'}},
{FALSE, {'a', 'b', '\xF4', 'x', 'y', '\0'}},
{FALSE, {'a', 'b', '\xF4', '\x91', 'x', 'y', '\0'}},
{FALSE, {'a', 'b', '\xF4', '\x91', '\x81', 'x', 'y', '\0'}},
{FALSE, {'a', 'b', '\xF4', '\x91', '\x81', '\x81', 'x', 'y', '\0'}},
{TRUE, {'a', 'b', '\xF4', '\x81', '\x81', '\x81', 'x', 'y', '\0'}},
{FALSE, {'a', 'b', '\xF4', '\xC1', '\x81', '\x81', 'x', 'y', '\0'}},
{FALSE, {'a', 'b', '\xF4', '\x91', '\xC1', '\x81', 'x', 'y', '\0'}},
{FALSE, {'a', 'b', '\xF4', '\x91', '\x81', '\xC1', 'x', 'y', '\0'}},
{FALSE, {'a', 'b', '\xF5', 'x', 'y', '\0'}},
{FALSE, {'a', 'b', '\xF5', '\x81', 'x', 'y', '\0'}},
{TRUE, {'a', 'b', '\xF4', '\x81', '\x81', '\x81', 'x', 'y',
'a', 'b', '\xF2', '\x91', '\x81', '\x81', 'x', 'y', '\0'}},
{FALSE, {'a', 'b', '\xF4', '\x81', '\x81', '\x81', 'x', 'y',
'a', 'b', '\xF2', '\x91', '\x81', '\xC1', 'x', 'y', '\0'}},
{FALSE, {'a', 'b', '\xF4', '\x81', '\x81', '\x81', 'x', 'y',
'a', 'b', '\xF2', '\x91', '\x81', 'x', 'y', '\0'}},
{-1},
};
int i = 0;
while (tests[i].valid != -1)
{
const char *last = svn_utf__last_valid(tests[i].string,
strlen(tests[i].string));
apr_size_t len = strlen(tests[i].string);
if ((svn_utf__cstring_is_valid(tests[i].string) != tests[i].valid)
||
(svn_utf__is_valid(tests[i].string, len) != tests[i].valid))
return svn_error_createf
(SVN_ERR_TEST_FAILED, NULL, "is_valid test %d failed", i);
if (!svn_utf__is_valid(tests[i].string, last - tests[i].string)
||
(tests[i].valid && *last))
return svn_error_createf
(SVN_ERR_TEST_FAILED, NULL, "last_valid test %d failed", i);
++i;
}
return SVN_NO_ERROR;
}
/* Compare the two different implementations using random data. */
static svn_error_t *
utf_validate2(apr_pool_t *pool)
{
int i;
seed_val();
/* We want enough iterations so that most runs get both valid and invalid
strings. We also want enough iterations such that a deliberate error
in one of the implementations will trigger a failure. By experiment
the second requirement requires a much larger number of iterations
that the first. */
for (i = 0; i < 100000; ++i)
{
unsigned int j;
char str[64];
apr_size_t len;
/* A random string; experiment shows that it's occasionally (less
than 1%) valid but usually invalid. */
for (j = 0; j < sizeof(str) - 1; ++j)
str[j] = (char)range_rand(0, 255);
str[sizeof(str) - 1] = 0;
len = strlen(str);
if (svn_utf__last_valid(str, len) != svn_utf__last_valid2(str, len))
{
/* Duplicate calls for easy debugging */
svn_utf__last_valid(str, len);
svn_utf__last_valid2(str, len);
return svn_error_createf
(SVN_ERR_TEST_FAILED, NULL, "is_valid2 test %d failed", i);
}
}
return SVN_NO_ERROR;
}
/* Test conversion from different codepages to utf8. */
static svn_error_t *
test_utf_cstring_to_utf8_ex2(apr_pool_t *pool)
{
apr_size_t i;
apr_pool_t *subpool = svn_pool_create(pool);
struct data {
const char *string;
const char *expected_result;
const char *from_page;
} tests[] = {
{"ascii text\n", "ascii text\n", "unexistant-page"},
{"Edelwei\xdf", "Edelwei\xc3\x9f", "ISO-8859-1"}
};
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
{
const char *dest;
svn_pool_clear(subpool);
SVN_ERR(svn_utf_cstring_to_utf8_ex2(&dest, tests[i].string,
tests[i].from_page, pool));
if (strcmp(dest, tests[i].expected_result))
{
return svn_error_createf
(SVN_ERR_TEST_FAILED, NULL,
"svn_utf_cstring_to_utf8_ex2 ('%s', '%s') returned ('%s') "
"instead of ('%s')",
tests[i].string, tests[i].from_page,
dest,
( run in 0.600 second using v1.01-cache-2.11-cpan-71847e10f99 )