From 03ba9fc5358bb3dd78c4e8708abefaad106475d0 Mon Sep 17 00:00:00 2001 From: Dov Grobgeld Date: Sat, 10 Mar 2018 18:37:47 +0200 Subject: [PATCH] Remove glib dependancy from test.c and test-character.c . --- configure.ac | 20 -- meson_options.txt | 2 - test/unicode-conformance/Makefile.am | 2 - test/unicode-conformance/meson.build | 31 ++- test/unicode-conformance/test-character.c | 218 +++++++++++-------- test/unicode-conformance/test.c | 242 ++++++++++++++-------- 6 files changed, 299 insertions(+), 216 deletions(-) diff --git a/configure.ac b/configure.ac index b3424da..615d27d 100644 --- a/configure.ac +++ b/configure.ac @@ -147,26 +147,6 @@ if test x$enable_deprecated = xno; then [Don not build deprecated functionality]) fi -# --with[out]-glib -AC_ARG_WITH(glib, - AC_HELP_STRING([--with-glib=@<:@no/auto/yes@:>@], - [use Glib @<:@default=auto@:>@])) -GLIB_PACKAGE=glib-2.0 -GLIB_MINVERSION=2.4 -if test x$with_glib = xyes; then - PKG_CHECK_MODULES(GLIB,$GLIB_PACKAGE >= $GLIB_MINVERSION) - FRIBIDI_USE_GLIB=1 -else -if test x$with_glib = xno; then - FRIBIDI_USE_GLIB=0 -else - PKG_CHECK_MODULES(GLIB,$GLIB_PACKAGE >= $GLIB_MINVERSION, - FRIBIDI_USE_GLIB=1, - FRIBIDI_USE_GLIB=0) -fi -fi -AM_CONDITIONAL(FRIBIDI_USE_GLIB, test x$FRIBIDI_USE_GLIB = x1) - # --disable-docs AC_ARG_ENABLE(docs, AC_HELP_STRING([--disable-docs], diff --git a/meson_options.txt b/meson_options.txt index 8af9b9e..c312fe8 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -2,5 +2,3 @@ option('deprecated', type : 'boolean', value : true, description: 'Build deprecated functionality') option('docs', type : 'boolean', value : true, description: 'Build documentation') -option('glib', type : 'boolean', value : true, - description: 'Use GLib (in unit tests)') diff --git a/test/unicode-conformance/Makefile.am b/test/unicode-conformance/Makefile.am index 2c9304c..7096156 100644 --- a/test/unicode-conformance/Makefile.am +++ b/test/unicode-conformance/Makefile.am @@ -21,10 +21,8 @@ AM_CPPFLAGS = \ $(GLIB_CFLAGS) LDADD = $(top_builddir)/lib/libfribidi.la $(GLIB_LIBS) -if FRIBIDI_USE_GLIB check_PROGRAMS = test test-character TESTS = $(TEST_DATAS) -endif test_SOURCES = test.c test_character_SOURCES = test-character.c diff --git a/test/unicode-conformance/meson.build b/test/unicode-conformance/meson.build index 9a77ea7..25a3c1f 100644 --- a/test/unicode-conformance/meson.build +++ b/test/unicode-conformance/meson.build @@ -1,19 +1,12 @@ -with_glib = get_option('glib') -if with_glib - glib_dep = dependency('glib-2.0', version: '>= 2.4', required: false) - if glib_dep.found() - tests = [ - ['BidiTest', 'test.c'], - ['BidiCharacterTest', 'test-character.c'], - ] - foreach t : tests - exe = executable(t[0], - t[1], fribidi_unicode_version_h, - c_args: ['-DHAVE_CONFIG_H'], - include_directories: incs, - dependencies: glib_dep, - link_with: libfribidi) - test(t[0], exe, args: files('@0@.txt'.format(t[0]))) - endforeach - endif -endif +tests = [ + ['BidiTest', 'test.c'], + ['BidiCharacterTest', 'test-character.c'], +] +foreach t : tests + exe = executable(t[0], + t[1], fribidi_unicode_version_h, + c_args: ['-DHAVE_CONFIG_H'], + include_directories: incs, + link_with: libfribidi) + test(t[0], exe, args: files('@0@.txt'.format(t[0]))) +endforeach diff --git a/test/unicode-conformance/test-character.c b/test/unicode-conformance/test-character.c index 75abe34..9f58e79 100644 --- a/test/unicode-conformance/test-character.c +++ b/test/unicode-conformance/test-character.c @@ -22,10 +22,89 @@ #include #include #include -#include #include #include +#define FALSE 0 +#define TRUE 1 +#define LINE_SIZE 2048 /* Size of biggest line in test file */ + +/* Glib like arrays */ +typedef struct { + int capacity; + int len; + int *data; +} int_array_t; + +typedef struct { + int capacity; + int len; + char *data; +} char_array_t; + +#define ARRAY_CHUNK_SIZE 32 +int_array_t *new_int_array() +{ + int_array_t *arr = (int_array_t*)malloc(sizeof(int_array_t)); + arr->len = 0; + arr->capacity = ARRAY_CHUNK_SIZE; + arr->data = (int*)malloc(arr->capacity * sizeof(int)); + + return arr; +} + +void int_array_add(int_array_t *arr, int val) +{ + if (arr->len == arr->capacity) + { + arr->capacity += ARRAY_CHUNK_SIZE; + arr->data = (int*)realloc(arr->data, arr->capacity*sizeof(int)); + } + arr->data[arr->len++] = val; +} + +int *int_array_free(int_array_t *arr, int free_data) +{ + int *data = arr->data; + if (free_data) { + data = NULL; + free(arr->data); + } + free(arr); + return data; +} + +char_array_t *new_char_array() +{ + char_array_t *arr = (char_array_t*)malloc(sizeof(char_array_t)); + arr->len = 0; + arr->capacity = ARRAY_CHUNK_SIZE; + arr->data = (char*)malloc(arr->capacity); + + return arr; +} + +void char_array_add(char_array_t *arr, char val) +{ + if (arr->len == arr->capacity) + { + arr->capacity += ARRAY_CHUNK_SIZE; + arr->data = (char*)realloc(arr->data, arr->capacity * sizeof(char)); + } + arr->data[arr->len++] = val; +} + +char *char_array_free(char_array_t *arr, int free_data) +{ + char *data = arr->data; + if (free_data) { + data = NULL; + free(arr->data); + } + free(arr); + return data; +} + static void die(const char *fmt, ...) { va_list ap; @@ -53,13 +132,13 @@ void parse_test_line (char *line, int *visual_ordering_len ) { - GArray *code_points_array, *levels_array, *visual_ordering_array; + int_array_t *code_points_array, *visual_ordering_array; + char_array_t *levels_array; char *end; int level; - - code_points_array = g_array_new (FALSE, FALSE, sizeof (FriBidiChar)); - levels_array = g_array_new (FALSE, FALSE, sizeof (FriBidiLevel)); + code_points_array = new_int_array (); + levels_array = new_char_array (); /* Field 0. Code points */ for(;;) @@ -74,13 +153,13 @@ void parse_test_line (char *line, break; c = parse_uni_char (line, end - line); - g_array_append_val (code_points_array, c); + int_array_add(code_points_array, c); line = end; } *code_points_len = code_points_array->len; - *code_points = (FriBidiChar *) g_array_free (code_points_array, FALSE); + *code_points = (FriBidiChar *) int_array_free (code_points_array, FALSE); if (*line == ';') line++; @@ -120,7 +199,7 @@ void parse_test_line (char *line, level = strtol (line, &end, 10); if (errno != EINVAL && line != end) { - g_array_append_val (levels_array, level); + char_array_add (levels_array, level); line = end; continue; } @@ -131,7 +210,7 @@ void parse_test_line (char *line, if (*line == 'x') { level = (FriBidiLevel) -1; - g_array_append_val (levels_array, level); + char_array_add (levels_array, level); line++; continue; } @@ -139,13 +218,13 @@ void parse_test_line (char *line, if (*line == ';') break; - g_assert_not_reached (); + die("Oops! I shouldn't be here!\n"); } if (levels_array->len != *code_points_len) die("Oops! Different lengths for levels and codepoints at line %d!\n", line_no); - *resolved_levels = (FriBidiLevel*)g_array_free (levels_array, FALSE); + *resolved_levels = (FriBidiLevel*)char_array_free (levels_array, FALSE); if (*line == ';') line++; @@ -153,25 +232,22 @@ void parse_test_line (char *line, die("Oops! Didn't find expected ; at line %d\n", line_no); /* Field 4 - resulting visual ordering */ - visual_ordering_array = g_array_new (FALSE, FALSE, sizeof(int)); + visual_ordering_array = new_int_array (); for(; errno = 0, level = strtol (line, &end, 10), line != end && errno != EINVAL; line = end) { - g_array_append_val (visual_ordering_array, level); + int_array_add (visual_ordering_array, level); } *visual_ordering_len = visual_ordering_array->len; - *visual_ordering = (int*)g_array_free (visual_ordering_array, FALSE); + *visual_ordering = (int*)int_array_free (visual_ordering_array, FALSE); } int main (int argc, char **argv) { - GError *error; int next_arg; - GIOChannel *channel; - GIOStatus status; + FILE *channel; const char *filename; - gchar *line = NULL; - gsize length, terminator_pos; + char line[LINE_SIZE]; int numerrs = 0; int line_no = 0; FriBidiChar *code_points = NULL; @@ -186,11 +262,11 @@ main (int argc, char **argv) FriBidiBracketType *bracket_types = NULL; FriBidiStrIndex *ltor = NULL; int ltor_len; - gboolean debug = FALSE; + int debug = FALSE; if (argc < 2) { - g_printerr ("usage: %s [--debug] test-file-name\n", argv[0]); + fprintf (stderr, "usage: %s [--debug] test-file-name\n", argv[0]); exit (1); } @@ -206,43 +282,21 @@ main (int argc, char **argv) die("Unknown option %s!\n", arg); } - filename = argv[next_arg++]; + filename = argv[next_arg++]; - error = NULL; - channel = g_io_channel_new_file (filename, "r", &error); - if (!channel) - { - g_printerr ("%s\n", error->message); - exit (1); - } - - fribidi_set_debug(debug); - - while (TRUE) - { - error = NULL; - g_free (line); - status = g_io_channel_read_line (channel, &line, &length, &terminator_pos, &error); - switch (status) - { - case G_IO_STATUS_ERROR: - g_printerr ("%s\n", error->message); - exit (1); + channel = fopen(filename, "r"); + if (!channel) + die ("Failed opening %s\n", filename); - case G_IO_STATUS_EOF: - goto done; - - case G_IO_STATUS_AGAIN: - continue; - - case G_IO_STATUS_NORMAL: - line[terminator_pos] = '\0'; - break; - } + while (!feof(channel)) { + fgets(line, LINE_SIZE, channel); + int len = strlen(line); + if (len == LINE_SIZE-1) + die("LINE_SIZE=%d too small at line %d!\n", LINE_SIZE, line_no); line_no++; - if (line[0] == '#' || line[0] == '\0') + if (line[0] == '#' || line[0] == '\n') continue; parse_test_line (line, @@ -257,23 +311,23 @@ main (int argc, char **argv) ); /* Test it */ - g_free(bracket_types); - bracket_types = g_malloc ( sizeof(FriBidiBracketType) * code_points_len); + free(bracket_types); + bracket_types = malloc ( sizeof(FriBidiBracketType) * code_points_len); - g_free(types); - types = g_malloc ( sizeof(FriBidiCharType) * code_points_len); + free(types); + types = malloc ( sizeof(FriBidiCharType) * code_points_len); - g_free(levels); - levels = g_malloc (sizeof (FriBidiLevel) * code_points_len); + free(levels); + levels = malloc (sizeof (FriBidiLevel) * code_points_len); - g_free (ltor); - ltor = g_malloc (sizeof (FriBidiStrIndex) * code_points_len); + free (ltor); + ltor = malloc (sizeof (FriBidiStrIndex) * code_points_len); { FriBidiParType base_dir; int i, j; - gboolean matches; + int matches; int types_len = code_points_len; int levels_len = types_len; FriBidiBracketType NoBracket = FRIBIDI_NO_BRACKET; @@ -347,31 +401,31 @@ main (int argc, char **argv) { numerrs++; - g_printerr ("failure on line %d\n", line_no); - g_printerr ("input is: %s\n", line); - g_printerr ("base dir: %s\n", paragraph_dir==0 ? "LTR" + fprintf (stderr, "failure on line %d\n", line_no); + fprintf (stderr, "input is: %s\n", line); + fprintf (stderr, "base dir: %s\n", paragraph_dir==0 ? "LTR" : paragraph_dir==1 ? "RTL" : "AUTO"); - g_printerr ("expected levels:"); + fprintf (stderr, "expected levels:"); for (i = 0; i < code_points_len; i++) if (expected_levels[i] == (FriBidiLevel) -1) - g_printerr (" x"); + fprintf (stderr, " x"); else - g_printerr (" %d", expected_levels[i]); - g_printerr ("\n"); - g_printerr ("returned levels:"); + fprintf (stderr, " %d", expected_levels[i]); + fprintf (stderr, "\n"); + fprintf (stderr, "returned levels:"); for (i = 0; i < levels_len; i++) - g_printerr (" %d", levels[i]); - g_printerr ("\n"); + fprintf (stderr, " %d", levels[i]); + fprintf (stderr, "\n"); - g_printerr ("expected order:"); + fprintf (stderr, "expected order:"); for (i = 0; i < expected_ltor_len; i++) - g_printerr (" %d", expected_ltor[i]); - g_printerr ("\n"); - g_printerr ("returned order:"); + fprintf (stderr, " %d", expected_ltor[i]); + fprintf (stderr, "\n"); + fprintf (stderr, "returned order:"); for (i = 0; i < ltor_len; i++) - g_printerr (" %d", ltor[i]); - g_printerr ("\n"); + fprintf (stderr, " %d", ltor[i]); + fprintf (stderr, "\n"); if (debug) { @@ -396,17 +450,13 @@ main (int argc, char **argv) fribidi_set_debug (0); } - g_printerr ("\n"); + fprintf (stderr, "\n"); } } } -done: - if (error) - g_error_free (error); - if (numerrs) - g_printerr ("%d errors\n", numerrs); + fprintf (stderr, "%d errors\n", numerrs); else printf("No errors found! :-)\n"); diff --git a/test/unicode-conformance/test.c b/test/unicode-conformance/test.c index 41521e1..12d0fbb 100644 --- a/test/unicode-conformance/test.c +++ b/test/unicode-conformance/test.c @@ -18,14 +18,103 @@ */ #include -#include #include #include #include +#include #include #include +#define TRUE 1 +#define FALSE 0 + +/* Glib array types */ +typedef struct { + int capacity; + int len; + int *data; +} int_array_t; + +typedef struct { + int capacity; + int len; + char *data; +} char_array_t; + +#define LINE_SIZE 2048 /* Size of largest example line in BidiTest */ +#define ARRAY_CHUNK_SIZE 32 +int_array_t *new_int_array() +{ + int_array_t *arr = (int_array_t*)malloc(sizeof(int_array_t)); + arr->len = 0; + arr->capacity = ARRAY_CHUNK_SIZE; + arr->data = (int*)malloc(arr->capacity * sizeof(int)); + + return arr; +} + +void int_array_add(int_array_t *arr, int val) +{ + if (arr->len == arr->capacity) + { + arr->capacity += ARRAY_CHUNK_SIZE; + arr->data = (int*)realloc(arr->data, arr->capacity*sizeof(int)); + } + arr->data[arr->len++] = val; +} + +int *int_array_free(int_array_t *arr, int free_data) +{ + int *data = arr->data; + if (free_data) { + data = NULL; + free(arr->data); + } + free(arr); + return data; +} + +char_array_t *new_char_array() +{ + char_array_t *arr = (char_array_t*)malloc(sizeof(char_array_t)); + arr->len = 0; + arr->capacity = ARRAY_CHUNK_SIZE; + arr->data = (char*)malloc(arr->capacity); + + return arr; +} + +void char_array_add(char_array_t *arr, char val) +{ + if (arr->len == arr->capacity) + { + arr->capacity += ARRAY_CHUNK_SIZE; + arr->data = (char*)realloc(arr->data, arr->capacity * sizeof(char)); + } + arr->data[arr->len++] = val; +} + +char *char_array_free(char_array_t *arr, int free_data) +{ + char *data = arr->data; + if (free_data) { + data = NULL; + free(arr->data); + } + free(arr); + return data; +} + +static void die(const char *fmt, ...) +{ + va_list ap; + va_start(ap,fmt); + + vfprintf(stderr, fmt, ap); + exit(-1); +} + static FriBidiCharType parse_char_type (const char *s, int len) { @@ -56,19 +145,20 @@ parse_char_type (const char *s, int len) MATCH ("FSI", FRIBIDI_TYPE_FSI); MATCH ("PDI", FRIBIDI_TYPE_PDI); - g_assert_not_reached (); + die("Oops. I shouldn't reach here!\n"); + return -1; } static FriBidiLevel * parse_levels_line (const char *line, FriBidiLevel *len) { - GArray *levels; + char_array_t *levels; if (!strncmp (line, "@Levels:", 8)) line += 8; - levels = g_array_new (FALSE, FALSE, sizeof (FriBidiLevel)); + levels = new_char_array (); while (*line) { @@ -79,7 +169,7 @@ parse_levels_line (const char *line, l = strtol (line, &end, 10); if (errno != EINVAL && line != end) { - g_array_append_val (levels, l); + char_array_add (levels, l); line = end; continue; } @@ -89,8 +179,7 @@ parse_levels_line (const char *line, if (*line == 'x') { - l = (FriBidiLevel) -1; - g_array_append_val (levels, l); + char_array_add (levels, -1); line++; continue; } @@ -98,32 +187,32 @@ parse_levels_line (const char *line, if (!*line) break; - g_assert_not_reached (); + die("Oops. I shouldn't be here!\n"); } *len = levels->len; - return (FriBidiLevel *) g_array_free (levels, FALSE); + return (FriBidiLevel *) char_array_free(levels, FALSE); } static FriBidiStrIndex * parse_reorder_line (const char *line, FriBidiStrIndex *len) { - GArray *map; + int_array_t *map; FriBidiStrIndex l; char *end; if (!strncmp (line, "@Reorder:", 9)) line += 9; - map = g_array_new (FALSE, FALSE, sizeof (FriBidiStrIndex)); + map = new_int_array (); for(; errno = 0, l = strtol (line, &end, 10), line != end && errno != EINVAL; line = end) { - g_array_append_val (map, l); + int_array_add (map, l); } *len = map->len; - return (FriBidiStrIndex *) g_array_free (map, FALSE); + return (FriBidiStrIndex *) int_array_free (map, FALSE); } static FriBidiCharType * @@ -131,11 +220,11 @@ parse_test_line (const char *line, FriBidiStrIndex *len, int *base_dir_flags) { - GArray *types; + int_array_t *types; FriBidiCharType c; const char *end; - types = g_array_new (FALSE, FALSE, sizeof (FriBidiCharType)); + types = new_int_array(); for(;;) { while (isspace (*line)) @@ -147,7 +236,7 @@ parse_test_line (const char *line, break; c = parse_char_type (line, end - line); - g_array_append_val (types, c); + int_array_add (types, c); line = end; } @@ -157,17 +246,14 @@ parse_test_line (const char *line, *base_dir_flags = strtol (line, NULL, 10); *len = types->len; - return (FriBidiCharType *) g_array_free (types, FALSE); + return (FriBidiCharType *) int_array_free (types, FALSE); } int main (int argc, char **argv) { - GIOChannel *channel; - GIOStatus status; - GError *error; - gchar *line = NULL; - gsize length, terminator_pos; + FILE *channel; + char line[LINE_SIZE]; FriBidiStrIndex *expected_ltor = NULL; FriBidiStrIndex expected_ltor_len = 0; FriBidiStrIndex *ltor = NULL; @@ -182,14 +268,12 @@ main (int argc, char **argv) int numerrs = 0; int numtests = 0; int line_no = 0; - gboolean debug = FALSE; + int debug = FALSE; const char *filename; int next_arg; - if (argc < 2) { - g_printerr ("usage: %s [--debug] test-file-name\n", argv[0]); - exit (1); - } + if (argc < 2) + die ("usage: %s [--debug] test-file-name\n", argv[0]); next_arg = 1; if (!strcmp (argv[next_arg], "--debug")) { @@ -199,47 +283,30 @@ main (int argc, char **argv) filename = argv[next_arg++]; - error = NULL; - channel = g_io_channel_new_file (filename, "r", &error); - if (!channel) { - g_printerr ("%s\n", error->message); - exit (1); - } - - while (TRUE) { - error = NULL; - g_free (line); - status = g_io_channel_read_line (channel, &line, &length, &terminator_pos, &error); - switch (status) { - case G_IO_STATUS_ERROR: - g_printerr ("%s\n", error->message); - exit (1); - - case G_IO_STATUS_EOF: - goto done; + channel = fopen(filename, "r"); + if (!channel) + die ("Failed opening %s\n", filename); - case G_IO_STATUS_AGAIN: - continue; - - case G_IO_STATUS_NORMAL: - line[terminator_pos] = '\0'; - break; - } + while (!feof(channel)) { + fgets(line, LINE_SIZE, channel); + int len = strlen(line); + if (len == LINE_SIZE-1) + die("LINE_SIZE too small at line %d!\n", line_no); line_no++; - if (line[0] == '#' || line[0] == '\0') + if (line[0] == '#') continue; if (line[0] == '@') { if (!strncmp (line, "@Reorder:", 9)) { - g_free (expected_ltor); + free (expected_ltor); expected_ltor = parse_reorder_line (line, &expected_ltor_len); continue; } if (!strncmp (line, "@Levels:", 8)) { - g_free (expected_levels); + free (expected_levels); expected_levels = parse_levels_line (line, &expected_levels_len); continue; } @@ -247,21 +314,21 @@ main (int argc, char **argv) } /* Test line */ - g_free (types); + free (types); types = parse_test_line (line, &types_len, &base_dir_flags); - g_free (levels); - levels = g_malloc (sizeof (FriBidiLevel) * types_len); + free (levels); + levels = malloc (sizeof (FriBidiLevel) * types_len); levels_len = types_len; - g_free (ltor); - ltor = g_malloc (sizeof (FriBidiStrIndex) * types_len); + free (ltor); + ltor = malloc (sizeof (FriBidiStrIndex) * types_len); /* Test it */ for (base_dir_mode = 0; base_dir_mode < 3; base_dir_mode++) { FriBidiParType base_dir; int i, j; - gboolean matches; + int matches; if ((base_dir_flags & (1<