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

@ -21,7 +21,7 @@
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
* file for a list of people on the GLib Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GLib at ftp://ftp.gtk.org/pub/gtk/.
* GLib at ftp://ftp.gtk.org/pub/gtk/.
*/
#include <string.h>
@ -29,29 +29,30 @@
/* 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++)
p[i] = g_utf8_next_char (p[i-1]);
for (i = 0; i < num_chars; i++)
for (j = 0; j < num_chars; j++)
for (j = 0; j < num_chars; j++)
{
g_assert (g_utf8_offset_to_pointer (p[i], j - i) == p[j]);
g_assert (g_utf8_pointer_to_offset (p[i], p[j]) == j - i);
g_assert (g_utf8_offset_to_pointer (p[i], j - i) == p[j]);
g_assert (g_utf8_pointer_to_offset (p[i], p[j]) == j - i);
}
g_free (p);
}
@ -104,9 +105,11 @@ test_misc (void)
int main (int argc, char *argv[])
{
test_utf8 (longline);
test_length ();
test_misc ();
return 0;
g_test_init (&argc, &argv, NULL);
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 },
@ -42,14 +41,14 @@ struct {
{ "abcde", 5, 5, TRUE },
{ "abcde", 7, 5, FALSE },
/* length 2 */
{ "\xc2\xa9\xc2\xa9\xc2\xa9", -1, 6, TRUE },
{ "\xc2\xa9\xc2\xa9\xc2\xa9", 1, 0, FALSE },
{ "\xc2\xa9\xc2\xa9\xc2\xa9", 2, 2, TRUE },
{ "\xc2\xa9\xc2\xa9\xc2\xa9", 3, 2, FALSE },
{ "\xc2\xa9\xc2\xa9\xc2\xa9", 4, 4, TRUE },
{ "\xc2\xa9\xc2\xa9\xc2\xa9", 5, 4, FALSE },
{ "\xc2\xa9\xc2\xa9\xc2\xa9", 6, 6, TRUE },
{ "\xc2\xa9\xc2\xa9\xc2\xa9", 7, 6, FALSE },
{ "\xc2\xa9\xc2\xa9\xc2\xa9", -1, 6, TRUE },
{ "\xc2\xa9\xc2\xa9\xc2\xa9", 1, 0, FALSE },
{ "\xc2\xa9\xc2\xa9\xc2\xa9", 2, 2, TRUE },
{ "\xc2\xa9\xc2\xa9\xc2\xa9", 3, 2, FALSE },
{ "\xc2\xa9\xc2\xa9\xc2\xa9", 4, 4, TRUE },
{ "\xc2\xa9\xc2\xa9\xc2\xa9", 5, 4, FALSE },
{ "\xc2\xa9\xc2\xa9\xc2\xa9", 6, 6, TRUE },
{ "\xc2\xa9\xc2\xa9\xc2\xa9", 7, 6, FALSE },
/* length 3 */
{ "\xe2\x89\xa0\xe2\x89\xa0", -1, 6, TRUE },
{ "\xe2\x89\xa0\xe2\x89\xa0", 1, 0, FALSE },
@ -274,46 +273,33 @@ struct {
{ NULL, }
};
static void
do_test (gint index,
const gchar *text,
gint max_len,
gint offset,
gboolean valid)
static void
do_test (gconstpointer d)
{
const Test *test = d;
const gchar *end;
gboolean result;
result = g_utf8_validate (text, max_len, &end);
if (result != valid || end - text != offset)
{
GString *str;
const gchar *p;
result = g_utf8_validate (test->text, test->max_len, &end);
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);
{
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 ();
}