libgrapheme

Freestanding C library for unicode string handling
git clone https://git.sinitax.com/suckless/libgrapheme
Log | Files | Refs | README | LICENSE | sfeed.txt

commit 1773b981c259ad1c20738fe9c38dc453b9dde885
parent a25a1e7894ccb4f13b1d4f07d65ab172b09df478
Author: Laslo Hunhold <dev@frign.de>
Date:   Tue, 14 Dec 2021 16:23:44 +0100

Refactor error-messages and test-output

Make it more uniform and easier to see where the error messages
originate. This also only affects the header-generation and test-suite,
as libgrapheme does not do any stdio.

Signed-off-by: Laslo Hunhold <dev@frign.de>

Diffstat:
Mgen/util.c | 16++++++++--------
Mtest/grapheme-performance.c | 8+++++---
Mtest/grapheme.c | 10++++++----
Mtest/utf8-decode.c | 12+++++++-----
Mtest/utf8-encode.c | 12+++++++-----
5 files changed, 33 insertions(+), 25 deletions(-)

diff --git a/gen/util.c b/gen/util.c @@ -91,7 +91,7 @@ range_list_append(struct range **range, size_t *nranges, const struct range *new } else { /* need to append new entry */ if ((*range = realloc(*range, (++(*nranges)) * sizeof(**range))) == NULL) { - fprintf(stderr, "realloc: %s\n", strerror(errno)); + fprintf(stderr, "range_list_append: realloc: %s.\n", strerror(errno)); exit(1); } (*range)[*nranges - 1].lower = new->lower; @@ -109,7 +109,7 @@ parse_file_with_callback(char *fname, int (*callback)(char *, char **, size_t, c /* open file */ if (!(fp = fopen(fname, "r"))) { - fprintf(stderr, "fopen '%s': %s\n", fname, + fprintf(stderr, "parse_file_with_callback: fopen '%s': %s.\n", fname, strerror(errno)); exit(1); } @@ -132,7 +132,7 @@ parse_file_with_callback(char *fname, int (*callback)(char *, char **, size_t, c if (++nfields > fieldbufsize) { if ((field = realloc(field, nfields * sizeof(*field))) == NULL) { - fprintf(stderr, "realloc: %s\n", strerror(errno)); + fprintf(stderr, "parse_file_with_callback: realloc: %s.\n", strerror(errno)); exit(1); } fieldbufsize = nfields; @@ -291,7 +291,7 @@ segment_test_callback(char *fname, char **field, size_t nfields, char *comment, /* append new testcase and initialize with zeroes */ if ((*test = realloc(*test, ++(*ntests) * sizeof(**test))) == NULL) { - fprintf(stderr, "realloc: %s\n", strerror(errno)); + fprintf(stderr, "segment_test_callback: realloc: %s.\n", strerror(errno)); return 1; } t = &(*test)[*ntests - 1]; @@ -310,7 +310,7 @@ segment_test_callback(char *fname, char **field, size_t nfields, char *comment, */ if ((t->len = realloc(t->len, ++t->lenlen * sizeof(*t->len))) == NULL) { - fprintf(stderr, "realloc: %s\n", + fprintf(stderr, "segment_test_callback: realloc: %s.\n", strerror(errno)); return 1; } @@ -320,7 +320,7 @@ segment_test_callback(char *fname, char **field, size_t nfields, char *comment, * '×' indicates a non-breakpoint, do nothing */ } else { - fprintf(stderr, "malformed delimiter '%s'\n", + fprintf(stderr, "segment_test_callback: Malformed delimiter '%s'.\n", token); return 1; } @@ -328,7 +328,7 @@ segment_test_callback(char *fname, char **field, size_t nfields, char *comment, /* add code point to cp-array */ if ((t->cp = realloc(t->cp, ++t->cplen * sizeof(*t->cp))) == NULL) { - fprintf(stderr, "realloc: %s\n", strerror(errno)); + fprintf(stderr, "segment_test_callback: realloc: %s.\n", strerror(errno)); return 1; } if (hextocp(token, strlen(token), &t->cp[t->cplen - 1])) { @@ -346,7 +346,7 @@ segment_test_callback(char *fname, char **field, size_t nfields, char *comment, /* store comment */ if (((*test)[*ntests - 1].descr = strdup(comment)) == NULL) { - fprintf(stderr, "strdup: %s\n", strerror(errno)); + fprintf(stderr, "segment_test_callback: strdup: %s.\n", strerror(errno)); return 1; } diff --git a/test/grapheme-performance.c b/test/grapheme-performance.c @@ -19,7 +19,7 @@ time_diff(struct timespec *a, struct timespec *b) } int -main(void) +main(int argc, char *argv[]) { struct timespec start, end; size_t i, j, bufsiz, off; @@ -27,12 +27,14 @@ main(void) LG_SEGMENTATION_STATE state; double cp_per_sec; + (void)argc; + /* allocate and generate buffer */ for (i = 0, bufsiz = 0; i < LEN(grapheme_test); i++) { bufsiz += grapheme_test[i].cplen; } if (!(buf = calloc(bufsiz, sizeof(*buf)))) { - fprintf(stderr, "calloc: Out of memory.\n"); + fprintf(stderr, "%s: calloc: Out of memory.\n", argv[0]); return 1; } for (i = 0, off = 0; i < LEN(grapheme_test); i++) { @@ -43,7 +45,7 @@ main(void) } /* run test */ - printf("Grapheme break performance test: "); + printf("%s: Running benchmark ", argv[0]); fflush(stdout); clock_gettime(CLOCK_MONOTONIC, &start); diff --git a/test/grapheme.c b/test/grapheme.c @@ -10,11 +10,13 @@ #define LEN(x) (sizeof(x) / sizeof(*(x))) int -main(void) +main(int argc, char *argv[]) { LG_SEGMENTATION_STATE state; size_t i, j, k, len, failed; + (void)argc; + /* grapheme break test */ for (i = 0, failed = 0; i < LEN(grapheme_test); i++) { memset(&state, 0, sizeof(state)); @@ -26,8 +28,8 @@ main(void) /* check if our resulting length matches */ if (k == grapheme_test[i].lenlen || len != grapheme_test[i].len[k++]) { - fprintf(stderr, "Failed \"%s\"\n", - grapheme_test[i].descr); + fprintf(stderr, "%s: Failed test \"%s\".\n", + argv[0], grapheme_test[i].descr); failed++; break; } @@ -37,7 +39,7 @@ main(void) } } } - printf("Grapheme break test: Passed %zu out of %zu tests.\n", + printf("%s: %zu/%zu tests passed.\n", argv[0], LEN(grapheme_test) - failed, LEN(grapheme_test)); return (failed > 0) ? 1 : 0; diff --git a/test/utf8-decode.c b/test/utf8-decode.c @@ -247,10 +247,12 @@ static const struct { }; int -main(void) +main(int argc, char *argv[]) { size_t i, failed; + (void)argc; + /* UTF-8 decoder test */ for (i = 0, failed = 0; i < LEN(dec_test); i++) { size_t len; @@ -261,14 +263,14 @@ main(void) if (len != dec_test[i].exp_len || cp != dec_test[i].exp_cp) { - fprintf(stderr, "Failed UTF-8-decoder test %zu: " - "Expected (%zx,%u), but got (%zx,%u)\n", - i, dec_test[i].exp_len, + fprintf(stderr, "%s: Failed test %zu: " + "Expected (%zx,%u), but got (%zx,%u).\n", + argv[0], i, dec_test[i].exp_len, dec_test[i].exp_cp, len, cp); failed++; } } - printf("UTF-8 decoder test: Passed %zu out of %zu tests.\n", + printf("%s: %zu/%zu tests passed.\n", argv[0], LEN(dec_test) - failed, LEN(dec_test)); return (failed > 0) ? 1 : 0; diff --git a/test/utf8-encode.c b/test/utf8-encode.c @@ -52,10 +52,12 @@ static const struct { }; int -main(void) +main(int argc, char *argv[]) { size_t i, j, failed; + (void)argc; + /* UTF-8 encoder test */ for (i = 0, failed = 0; i < LEN(enc_test); i++) { uint8_t arr[4]; @@ -65,8 +67,8 @@ main(void) if (len != enc_test[i].exp_len || memcmp(arr, enc_test[i].exp_arr, len)) { - fprintf(stderr, "Failed UTF-8-encoder test %zu: " - "Expected (", i); + fprintf(stderr, "%s, Failed test %zu: " + "Expected (", argv[0], i); for (j = 0; j < enc_test[i].exp_len; j++) { fprintf(stderr, "0x%x", enc_test[i].exp_arr[j]); @@ -81,11 +83,11 @@ main(void) fprintf(stderr, " "); } } - fprintf(stderr, ")\n"); + fprintf(stderr, ").\n"); failed++; } } - printf("UTF-8 encoder test: Passed %zu out of %zu tests.\n", + printf("%s: %zu/%zu tests passed.\n", argv[0], LEN(enc_test) - failed, LEN(enc_test)); return (failed > 0) ? 1 : 0;