File: charwidth.c

package info (click to toggle)
mame 0.228%2Bdfsg.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 956,280 kB
  • sloc: cpp: 4,712,769; xml: 1,946,353; ansic: 829,986; sh: 49,187; lisp: 18,100; python: 17,897; makefile: 10,815; cs: 10,047; javascript: 8,196; yacc: 7,565; java: 7,151; objc: 5,791; asm: 4,639; perl: 2,850; ada: 1,681; lex: 1,174; pascal: 1,139; ruby: 317; awk: 35
file content (71 lines) | stat: -rw-r--r-- 2,706 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "tests.h"
#include <ctype.h>
#include <wchar.h>

static int my_isprint(int c) {
     int cat = utf8proc_get_property(c)->category;
     return (UTF8PROC_CATEGORY_LU <= cat && cat <= UTF8PROC_CATEGORY_ZS) ||
          (c == 0x0601 || c == 0x0602 || c == 0x0603 || c == 0x06dd);
}

int main(int argc, char **argv)
{
     int c, error = 0, updates = 0;

     (void) argc; /* unused */
     (void) argv; /* unused */

     /* some simple sanity tests of the character widths */
     for (c = 0; c <= 0x110000; ++c) {
          int cat = utf8proc_get_property(c)->category;
          int w = utf8proc_charwidth(c);
          if ((cat == UTF8PROC_CATEGORY_MN || cat == UTF8PROC_CATEGORY_ME) &&
              w > 0) {
               fprintf(stderr, "nonzero width %d for combining char %x\n", w, c);
               error = 1;
          }
          if (w == 0 &&
			  ((cat >= UTF8PROC_CATEGORY_LU && cat <= UTF8PROC_CATEGORY_LO) ||
			   (cat >= UTF8PROC_CATEGORY_ND && cat <= UTF8PROC_CATEGORY_SC) ||
			   (cat >= UTF8PROC_CATEGORY_SO && cat <= UTF8PROC_CATEGORY_ZS))) {
               fprintf(stderr, "zero width for symbol-like char %x\n", c);
               error = 1;
          }
          if (c <= 127 && ((!isprint(c) && w > 0) ||
                           (isprint(c) && wcwidth(c) != w))) {
               fprintf(stderr, "wcwidth %d mismatch %d for %s ASCII %x\n",
                       wcwidth(c), w, 
                       isprint(c) ? "printable" : "non-printable", c);
               error = 1;
          }
          if (!my_isprint(c) && w > 0) {
               fprintf(stderr, "non-printing %x had width %d\n", c, w);
               error = 1;
          }
     }
     check(!error, "utf8proc_charwidth FAILED tests.");

     /* print some other information by compariing with system wcwidth */
     printf("Mismatches with system wcwidth (not necessarily errors):\n");
     for (c = 0; c <= 0x110000; ++c) {
          int w = utf8proc_charwidth(c);
          int wc = wcwidth(c);
          if (sizeof(wchar_t) == 2 && c >= (1<<16)) continue;
          /* lots of these errors for out-of-date system unicode tables */
          if (wc == -1 && my_isprint(c) && w > 0) {
			   updates += 1;
#if 0
               printf("  wcwidth(%x) = -1 for printable char\n", c);
#endif
		  }
          if (wc == -1 && !my_isprint(c) && w > 0)
               printf("  wcwidth(%x) = -1 for non-printable width-%d char\n", c, w);
          if (wc >= 0 && wc != w)
               printf("  wcwidth(%x) = %d != charwidth %d\n", c, wc, w);
     }
	 printf("   ... (positive widths for %d chars unknown to wcwidth) ...\n",
			updates);
     printf("Character-width tests SUCCEEDED.\n");

     return 0;
}