Some fixes

This commit is contained in:
Matthias Clasen 2010-06-20 22:18:30 -04:00
parent 7bea2a7a4f
commit bad7f1e54f
3 changed files with 50 additions and 59 deletions

View File

@ -1,5 +1,7 @@
#include <glib.h>
#include <locale.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
const gchar **input;

View File

@ -29,17 +29,18 @@
/* Test conversions between offsets and pointers */
static void test_utf8 (gchar *string)
static void test_utf8 (gconstpointer d)
{
gint num_chars;
gchar **p;
const gchar **p;
gint i, j;
const gchar *string = d;
g_assert (g_utf8_validate (string, -1, NULL));
num_chars = g_utf8_strlen (string, -1);
p = (gchar **) g_malloc (num_chars * sizeof (gchar *));
p = (const gchar **) g_malloc (num_chars * sizeof (gchar *));
p[0] = string;
for (i = 1; i < num_chars; i++)
@ -104,9 +105,11 @@ test_misc (void)
int main (int argc, char *argv[])
{
test_utf8 (longline);
test_length ();
test_misc ();
g_test_init (&argc, &argv, NULL);
return 0;
g_test_add_data_func ("/utf8/offsets", longline, test_utf8);
g_test_add_func ("/utf8/lengths", test_length);
g_test_add_func ("/utf8/reverse", test_misc);
return g_test_run ();
}

View File

@ -26,15 +26,14 @@
((Char) & 0xFFFE) != 0xFFFE)
static gboolean any_failed = FALSE;
struct {
typedef struct {
const gchar *text;
gint max_len;
gint offset;
gboolean valid;
} test[] = {
} Test;
Test test[] = {
/* some tests to check max_len handling */
/* length 1 */
{ "abcde", -1, 5, TRUE },
@ -275,45 +274,32 @@ struct {
};
static void
do_test (gint index,
const gchar *text,
gint max_len,
gint offset,
gboolean valid)
do_test (gconstpointer d)
{
const Test *test = d;
const gchar *end;
gboolean result;
result = g_utf8_validate (text, max_len, &end);
result = g_utf8_validate (test->text, test->max_len, &end);
if (result != valid || end - text != offset)
{
GString *str;
const gchar *p;
any_failed = TRUE;
str = g_string_new (0);
for (p = text; *p; p++)
g_string_append_printf (str, "\\x%02hhx", *p);
g_print ("%d: g_utf8_validate (\"%s\", %d) failed, "
"expected %s %d, got %s %d\n",
index,
str->str, max_len,
valid ? "TRUE" : "FALSE", offset,
result ? "TRUE" : "FALSE", (gint) (end - text));
g_string_free (str, FALSE);
}
g_assert (result == test->valid);
g_assert (end - test->text == test->offset);
}
int
main (int argc, char *argv[])
{
gint i;
gchar *path;
g_test_init (&argc, &argv, NULL);
for (i = 0; test[i].text; i++)
do_test (i, test[i].text, test[i].max_len,
test[i].offset, test[i].valid);
return any_failed ? 1 : 0;
{
path = g_strdup_printf ("/utf8/validate/%d", i);
g_test_add_data_func (path, &test[i], do_test);
g_free (path);
}
return g_test_run ();
}