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

iterate.c (4565B)


      1#include "tests.h"
      2#include <ctype.h>
      3#include <wchar.h>
      4
      5static  int     tests;
      6static  int     error;
      7
      8#define CHECKVALID(pos, val, len) buf[pos] = val; testbytes(buf,len,len,__LINE__)
      9#define CHECKINVALID(pos, val, len) buf[pos] = val; testbytes(buf,len,UTF8PROC_ERROR_INVALIDUTF8,__LINE__)
     10
     11static void testbytes(utf8proc_uint8_t *buf, utf8proc_ssize_t len, utf8proc_ssize_t retval, int line)
     12{
     13    utf8proc_int32_t out[16];
     14    utf8proc_ssize_t ret;
     15
     16    /* Make a copy to ensure that memory is left uninitialized after "len"
     17     * bytes. This way, Valgrind can detect overreads.
     18     */
     19    utf8proc_uint8_t tmp[16];
     20    memcpy(tmp, buf, (unsigned long int)len);
     21
     22    tests++;
     23    if ((ret = utf8proc_iterate(tmp, len, out)) != retval) {
     24        fprintf(stderr, "Failed (%d):", line);
     25        for (utf8proc_ssize_t i = 0; i < len ; i++) {
     26            fprintf(stderr, " 0x%02x", tmp[i]);
     27        }
     28        fprintf(stderr, " -> %zd\n", ret);
     29        error++;
     30    }
     31}
     32
     33int main(int argc, char **argv)
     34{
     35    utf8proc_int32_t byt;
     36    utf8proc_uint8_t buf[16];
     37
     38    (void) argc; (void) argv; /* unused */
     39
     40    tests = error = 0;
     41
     42    // Check valid sequences that were considered valid erroneously before
     43    buf[0] = 0xef;
     44    buf[1] = 0xb7;
     45    for (byt = 0x90; byt < 0xa0; byt++) {
     46        CHECKVALID(2, byt, 3);
     47    }
     48    // Check 0xfffe and 0xffff
     49    buf[1] = 0xbf;
     50    CHECKVALID(2, 0xbe, 3);
     51    CHECKVALID(2, 0xbf, 3);
     52    // Check 0x??fffe & 0x??ffff
     53    for (byt = 0x1fffe; byt < 0x110000; byt += 0x10000) {
     54        buf[0] = 0xf0 | (byt >> 18);
     55        buf[1] = 0x80 | ((byt >> 12) & 0x3f);
     56        CHECKVALID(3, 0xbe, 4);
     57        CHECKVALID(3, 0xbf, 4);
     58    }
     59
     60    // Continuation byte not after lead
     61    for (byt = 0x80; byt < 0xc0; byt++) {
     62        CHECKINVALID(0, byt, 1);
     63    }
     64
     65    // Continuation byte not after lead
     66    for (byt = 0x80; byt < 0xc0; byt++) {
     67        CHECKINVALID(0, byt, 1);
     68    }
     69
     70    // Test lead bytes
     71    for (byt = 0xc0; byt <= 0xff; byt++) {
     72	// Single lead byte at end of string
     73        CHECKINVALID(0, byt, 1);
     74        // Lead followed by non-continuation character < 0x80
     75        CHECKINVALID(1, 65, 2);
     76	// Lead followed by non-continuation character > 0xbf
     77        CHECKINVALID(1, 0xc0, 2);
     78    }
     79
     80    // Test overlong 2-byte
     81    buf[0] = 0xc0;
     82    for (byt = 0x81; byt <= 0xbf; byt++) {
     83        CHECKINVALID(1, byt, 2);
     84    }
     85    buf[0] = 0xc1;
     86    for (byt = 0x80; byt <= 0xbf; byt++) {
     87        CHECKINVALID(1, byt, 2);
     88    }
     89
     90    // Test overlong 3-byte
     91    buf[0] = 0xe0;
     92    buf[2] = 0x80;
     93    for (byt = 0x80; byt <= 0x9f; byt++) {
     94        CHECKINVALID(1, byt, 3);
     95    }
     96
     97    // Test overlong 4-byte
     98    buf[0] = 0xf0;
     99    buf[2] = 0x80;
    100    buf[3] = 0x80;
    101    for (byt = 0x80; byt <= 0x8f; byt++) {
    102        CHECKINVALID(1, byt, 4);
    103    }
    104
    105    // Test 4-byte > 0x10ffff
    106    buf[0] = 0xf4;
    107    buf[2] = 0x80;
    108    buf[3] = 0x80;
    109    for (byt = 0x90; byt <= 0xbf; byt++) {
    110        CHECKINVALID(1, byt, 4);
    111    }
    112    buf[1] = 0x80;
    113    for (byt = 0xf5; byt <= 0xf7; byt++) {
    114        CHECKINVALID(0, byt, 4);
    115    }
    116
    117    // Test 5-byte
    118    buf[4] = 0x80;
    119    for (byt = 0xf8; byt <= 0xfb; byt++) {
    120        CHECKINVALID(0, byt, 5);
    121    }
    122
    123    // Test 6-byte
    124    buf[5] = 0x80;
    125    for (byt = 0xfc; byt <= 0xfd; byt++) {
    126        CHECKINVALID(0, byt, 6);
    127    }
    128
    129    // Test 7-byte
    130    buf[6] = 0x80;
    131    CHECKINVALID(0, 0xfe, 7);
    132
    133    // Three and above byte sequences
    134    for (byt = 0xe0; byt < 0xf0; byt++) {
    135        // Lead followed by only 1 continuation byte
    136        CHECKINVALID(0, byt, 2);
    137        // Lead ended by non-continuation character < 0x80
    138        CHECKINVALID(2, 65, 3);
    139        // Lead ended by non-continuation character > 0xbf
    140        CHECKINVALID(2, 0xc0, 3);
    141    }
    142
    143    // 3-byte encoded surrogate character(s)
    144    buf[0] = 0xed; buf[2] = 0x80;
    145    // Single surrogate
    146    CHECKINVALID(1, 0xa0, 3);
    147    // Trailing surrogate first
    148    CHECKINVALID(1, 0xb0, 3);
    149
    150    // Four byte sequences
    151    buf[1] = 0x80;
    152    for (byt = 0xf0; byt < 0xf5; byt++) {
    153        // Lead followed by only 1 continuation bytes
    154        CHECKINVALID(0, byt, 2);
    155        // Lead followed by only 2 continuation bytes
    156        CHECKINVALID(0, byt, 3);
    157        // Lead followed by non-continuation character < 0x80
    158        CHECKINVALID(3, 65, 4);
    159        // Lead followed by non-continuation character > 0xbf
    160        CHECKINVALID(3, 0xc0, 4);
    161
    162    }
    163
    164     check(!error, "utf8proc_iterate FAILED %d tests out of %d", error, tests);
    165     printf("utf8proc_iterate tests SUCCEEDED, (%d) tests passed.\n", tests);
    166
    167     return 0;
    168}