utf8proc

A clean C library for processing UTF-8 Unicode data
git clone https://git.sinitax.com/juliastrings/utf8proc
Log | Files | Refs | README | LICENSE | sfeed.txt

iscase.c (2155B)


      1#include "tests.h"
      2
      3int read_range(FILE *f, utf8proc_int32_t *start, utf8proc_int32_t *end)
      4{
      5     unsigned char buf[8192];
      6     size_t len = simple_getline(buf, f);
      7     size_t pos = skipspaces(buf, 0);
      8     unsigned char s[16];
      9     if (pos == len || buf[pos] == '#') return 0;
     10     pos += encode(s, buf + pos) - 1;
     11     check(s[0], "invalid line %s in data", buf);
     12     utf8proc_iterate((utf8proc_uint8_t*) s, -1, start);
     13     if (buf[pos] == '.' && buf[pos+1] == '.') {
     14          encode(s, buf + pos + 2);
     15          check(s[0], "invalid line %s in data", buf);
     16          utf8proc_iterate((utf8proc_uint8_t*) s, -1, end);
     17     }
     18     else
     19          *end = *start;
     20     return 1;
     21}
     22
     23int test_iscase(const char *fname, int (*iscase)(utf8proc_int32_t),
     24                utf8proc_int32_t (*thatcase)(utf8proc_int32_t))
     25{
     26     FILE *f = fopen(fname, "r");
     27     int lines = 0, tests = 0, success = 1;
     28     utf8proc_int32_t c = 0;
     29
     30     check(f != NULL, "error opening data file \"%s\"\n", fname);
     31
     32     while (success && !feof(f)) {
     33          utf8proc_int32_t start, end;
     34          if (read_range(f, &start, &end)) {
     35               for (; c < start; ++c) {
     36                    check(!iscase(c), "failed !iscase(%04x) in %s\n", c, fname);
     37               }
     38               for (; c <= end; ++c) {
     39                    check(iscase(c), "failed iscase(%04x) in %s\n", c, fname);
     40                    check(thatcase(c) == c, "inconsistent thatcase(%04x) in %s\n", c, fname);
     41                    ++tests;
     42               }
     43          }
     44          ++lines;
     45     }
     46     for (; c <= 0x110000; ++c) {
     47          check(!iscase(c), "failed !iscase(%04x) in %s\n", c, fname);
     48     }
     49
     50     printf("Checked %d characters from %d lines of %s\n", tests, lines, fname);
     51     fclose(f);
     52     return success;
     53}
     54
     55int main(int argc, char **argv)
     56{
     57     check(argc == 3, "Expected Lowercase.txt and Uppercase.txt as arguments");
     58     check(test_iscase(argv[1], utf8proc_islower, utf8proc_tolower), "Lowercase tests failed");
     59     check(test_iscase(argv[2], utf8proc_isupper, utf8proc_toupper), "Uppercase tests failed");
     60     printf("utf8proc iscase tests SUCCEEDED.\n");
     61     return 0;
     62}