libgrapheme

unicode string library
git clone git://git.suckless.org/libgrapheme
Log | Files | Refs | README | LICENSE

util.c (1053B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <stdbool.h>
      3 #include <stddef.h>
      4 #include <stdint.h>
      5 #include <stdio.h>
      6 #include <string.h>
      7 
      8 #include "../grapheme.h"
      9 #include "../gen/types.h"
     10 #include "util.h"
     11 
     12 int
     13 run_break_tests(size_t (*next_break)(const uint_least32_t *, size_t),
     14                 const struct break_test *test, size_t testlen, const char *argv0)
     15 {
     16 	size_t i, j, off, res, failed;
     17 
     18 	/* character break test */
     19 	for (i = 0, failed = 0; i < testlen; i++) {
     20 		for (j = 0, off = 0; off < test[i].cplen; off += res) {
     21 			res = next_break(test[i].cp + off, test[i].cplen - off);
     22 
     23 			/* check if our resulting offset matches */
     24 			if (j == test[i].lenlen ||
     25 			    res != test[i].len[j++]) {
     26 				fprintf(stderr, "%s: Failed test %zu \"%s\".\n",
     27 				        argv0, i, test[i].descr);
     28 				fprintf(stderr, "J=%zu: EXPECTED len %zu, got %zu\n", j-1, test[i].len[j-1], res);
     29 				failed++;
     30 				break;
     31 			}
     32 		}
     33 	}
     34 	printf("%s: %zu/%zu tests passed.\n", argv0,
     35 	       testlen - failed, testlen);
     36 
     37 	return (failed > 0) ? 1 : 0;
     38 }