libgrapheme

grapheme cluster utility library
git clone git://git.suckless.org/libgrapheme
Log | Files | Refs | LICENSE

grapheme.c (1147B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <stddef.h>
      3 #include <stdint.h>
      4 #include <stdio.h>
      5 #include <string.h>
      6 
      7 #include "../grapheme.h"
      8 #include "../gen/grapheme-test.h"
      9 #include "util.h"
     10 
     11 int
     12 main(int argc, char *argv[])
     13 {
     14 	LG_SEGMENTATION_STATE state;
     15 	size_t i, j, k, len, failed;
     16 
     17 	(void)argc;
     18 
     19 	/* grapheme break test */
     20 	for (i = 0, failed = 0; i < LEN(grapheme_test); i++) {
     21 		memset(&state, 0, sizeof(state));
     22 		for (j = 0, k = 0, len = 1; j < grapheme_test[i].cplen; j++) {
     23 			if ((j + 1) == grapheme_test[i].cplen ||
     24 			    lg_grapheme_isbreak(grapheme_test[i].cp[j],
     25 			                        grapheme_test[i].cp[j + 1],
     26 			                        &state)) {
     27 				/* check if our resulting length matches */
     28 				if (k == grapheme_test[i].lenlen ||
     29 				    len != grapheme_test[i].len[k++]) {
     30 					fprintf(stderr, "%s: Failed test \"%s\".\n",
     31 					        argv[0], grapheme_test[i].descr);
     32 					failed++;
     33 					break;
     34 				}
     35 				len = 1;
     36 			} else {
     37 				len++;
     38 			}
     39 		}
     40 	}
     41 	printf("%s: %zu/%zu tests passed.\n", argv[0],
     42 	       LEN(grapheme_test) - failed, LEN(grapheme_test));
     43 
     44 	return (failed > 0) ? 1 : 0;
     45 }