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 <glib.h>
#include <locale.h> #include <locale.h>
#include <stdlib.h>
#include <string.h>
typedef struct { typedef struct {
const gchar **input; const gchar **input;

View File

@ -29,17 +29,18 @@
/* Test conversions between offsets and pointers */ /* Test conversions between offsets and pointers */
static void test_utf8 (gchar *string) static void test_utf8 (gconstpointer d)
{ {
gint num_chars; gint num_chars;
gchar **p; const gchar **p;
gint i, j; gint i, j;
const gchar *string = d;
g_assert (g_utf8_validate (string, -1, NULL)); g_assert (g_utf8_validate (string, -1, NULL));
num_chars = g_utf8_strlen (string, -1); 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; p[0] = string;
for (i = 1; i < num_chars; i++) for (i = 1; i < num_chars; i++)
@ -104,9 +105,11 @@ test_misc (void)
int main (int argc, char *argv[]) int main (int argc, char *argv[])
{ {
test_utf8 (longline); g_test_init (&argc, &argv, NULL);
test_length ();
test_misc ();
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) ((Char) & 0xFFFE) != 0xFFFE)
typedef struct {
static gboolean any_failed = FALSE;
struct {
const gchar *text; const gchar *text;
gint max_len; gint max_len;
gint offset; gint offset;
gboolean valid; gboolean valid;
} test[] = { } Test;
Test test[] = {
/* some tests to check max_len handling */ /* some tests to check max_len handling */
/* length 1 */ /* length 1 */
{ "abcde", -1, 5, TRUE }, { "abcde", -1, 5, TRUE },
@ -275,45 +274,32 @@ struct {
}; };
static void static void
do_test (gint index, do_test (gconstpointer d)
const gchar *text,
gint max_len,
gint offset,
gboolean valid)
{ {
const Test *test = d;
const gchar *end; const gchar *end;
gboolean result; 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) g_assert (result == test->valid);
{ g_assert (end - test->text == test->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);
}
} }
int int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {
gint i; gint i;
gchar *path;
g_test_init (&argc, &argv, NULL);
for (i = 0; test[i].text; i++) for (i = 0; test[i].text; i++)
do_test (i, test[i].text, test[i].max_len, {
test[i].offset, test[i].valid); path = g_strdup_printf ("/utf8/validate/%d", i);
g_test_add_data_func (path, &test[i], do_test);
g_free (path);
}
return any_failed ? 1 : 0; return g_test_run ();
} }