tests/libc: Add 0041-mbrlen() - scc - simple c99 compiler
 (HTM) git clone git://git.simple-cc.org/scc
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) Submodules
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit a378af318684a868015e1efa73b14ab525d3ac27
 (DIR) parent c675740de75457783b309d357f586a768cf31fed
 (HTM) Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
       Date:   Thu, 20 Mar 2025 20:28:37 +0100
       
       tests/libc: Add 0041-mbrlen()
       
       Diffstat:
         M tests/libc/execute/0039-mbrtowc.c   |      46 ++-----------------------------
         A tests/libc/execute/0041-mbrlen.c    |      75 +++++++++++++++++++++++++++++++
         M tests/libc/execute/libc-tests.lst   |       1 +
         A tests/libc/execute/mbtest.h         |      43 ++++++++++++++++++++++++++++++
       
       4 files changed, 121 insertions(+), 44 deletions(-)
       ---
 (DIR) diff --git a/tests/libc/execute/0039-mbrtowc.c b/tests/libc/execute/0039-mbrtowc.c
       @@ -5,6 +5,8 @@
        #include <string.h>
        #include <wchar.h>
        
       +#include "mbtest.h"
       +
        /*
        output:
        testing
       @@ -17,50 +19,6 @@ end:
        
        #define NELEM(x) (sizeof(x)/sizeof(x[0]))
        
       -static wchar_t wc;
       -static struct mbtest {
       -        char *s;
       -        int l;
       -        int r;
       -        int mbstate;
       -        int syserr;
       -        wchar_t *pwc;
       -        wchar_t wc;
       -} tests[] = {
       -        {"\0",                             2,  0, 1, 0,      &wc,  0},
       -        {"\x21",                           2,  1, 1, 0,      &wc,  0x21},
       -        {"\xc2\xa1",                       3,  2, 1, 0,      &wc,  0x00A1},
       -        {"\xc2\xa1",                       2,  2, 1, 0,      &wc,  0x00A1},
       -        {"\xe2\x80\x94",                   4,  3, 1, 0,      &wc,  0x2014},
       -        {"\xf0\x9f\x92\xa9",               5,  4, 1, 0,      &wc,  0x01F4A9},
       -        {"\xf0\x9f\x92\xa9",               5,  4, 1, 0,      NULL, -1},
       -        {"\xf0\x9f\x92\xa9",              -1,  4, 1, 0,      &wc,  0x01F4A9},
       -
       -        {NULL,                             4,  0, 1, 0,      NULL, -1},
       -        {"\xed\x9f\xbf",                   4,  3, 1, 0,      &wc,  0xd7ff},
       -        {"\xed\xa0\x80",                   4, -1, 1, EILSEQ, &wc,  -1},
       -        {"\xed\xb3\xbf",                   4, -1, 1, EILSEQ, &wc,  -1},
       -        {"\xed\xb4\x80",                   4,  3, 1, 0,      &wc,  0xdd00},
       -
       -        {"\xf0\x9f\x92\xa9",               3, -2, 0, 0,      &wc, -1},
       -        {"\xa9",                           2,  1, 1, 0,      &wc, 0x01F4A9},
       -        {"\xf0\x9f\x92\xa9",               3, -2, 0, 0,      &wc, -1},
       -        {NULL,                             4, -1, 1, EILSEQ, &wc, -1},
       -        {"\xa9",                           2, -1, 1, EILSEQ, &wc, -1},
       -        {"\xf0\x9f\x92\xa9",               3, -2, 0, 0,      &wc, -1},
       -        {NULL,                             4, -1, 1, EILSEQ, &wc, -1},
       -        {"\x21",                           2,  1, 1, 0,      &wc,  0x21},
       -        {"\xf0\x9f\x92\xa9",               2, -2, 0, 0,      &wc, -1},
       -        {"\xf0\x9f\x92\xa9",               0, -2, 0, 0,      &wc, -1},
       -        {"\x92\xa9",                       2,  2, 1, 0,      &wc, 0x01F4A9},
       -
       -        {"\x80",                           2, -1, 1, EILSEQ, &wc, -1},
       -        {"\xc0\x80",                       2, -1, 1, EILSEQ, &wc, -1},
       -        {"\xc0\x00",                       2, -1, 1, EILSEQ, &wc, -1},
       -        {"\xf8\x81\x82\x83\x84\x85",      -1, -1, 1, EILSEQ, &wc, -1},
       -        {"\xfe\x81\x82\x83\x84\x85\x86",   8, -1, 1, EILSEQ, &wc, -1},
       -};
       -
        void
        tests_mbrtowc(void)
        {
 (DIR) diff --git a/tests/libc/execute/0041-mbrlen.c b/tests/libc/execute/0041-mbrlen.c
       @@ -0,0 +1,75 @@
       +#include <assert.h>
       +#include <errno.h>
       +#include <stdio.h>
       +#include <stdlib.h>
       +#include <string.h>
       +#include <wchar.h>
       +
       +#include "mbtest.h"
       +
       +/*
       +output:
       +testing
       +testing mbrlen1
       +testing mbrlen2
       +testing mblen
       +done
       +end:
       +*/
       +
       +#define NELEM(x) (sizeof(x)/sizeof(x[0]))
       +
       +void
       +tests_mbrlen(void)
       +{
       +        struct mbtest *tp;
       +        int r;
       +        mbstate_t s;
       +
       +        puts("testing mbrlen1");
       +        for (tp = tests; tp < &tests[NELEM(tests)]; ++tp) {
       +                wc = -1;
       +                errno = 0;
       +                r = mbrlen(tp->s, tp->l, NULL);
       +                assert(tp->r == r);
       +                assert(tp->syserr == errno);
       +        }
       +
       +        puts("testing mbrlen2");
       +        memset(&s, 0, sizeof(s));
       +        for (tp = tests; tp < &tests[NELEM(tests)]; ++tp) {
       +                wc = -1;
       +                errno = 0;
       +                r = mbrlen(tp->s, tp->l, &s);
       +                assert(tp->r == r);
       +                assert(tp->syserr == errno);
       +                assert(mbsinit(&s) != 0 == tp->mbstate);
       +        }
       +}
       +
       +void
       +tests_mblen(void)
       +{
       +        struct mbtest *tp;
       +        int r, rt;
       +
       +        puts("testing mblen");
       +        for (tp = tests; tp < &tests[NELEM(tests)]; ++tp) {
       +                wc = -1;
       +                errno = 0;
       +                r = mblen(tp->s, tp->l);
       +                assert(tp->syserr == errno);
       +                rt = (tp->r >= 0) ? tp->r : -1;
       +                assert(rt == r);
       +        }
       +}
       +
       +int
       +main()
       +{
       +        puts("testing");
       +        tests_mbrlen();
       +        tests_mblen();
       +        puts("done");
       +        return 0;
       +}
 (DIR) diff --git a/tests/libc/execute/libc-tests.lst b/tests/libc/execute/libc-tests.lst
       @@ -37,3 +37,4 @@
        0038-mbsinit
        0039-mbrtowc
        0040-wcrtomb
       +0041-mbrlen
 (DIR) diff --git a/tests/libc/execute/mbtest.h b/tests/libc/execute/mbtest.h
       @@ -0,0 +1,43 @@
       +static wchar_t wc;
       +static struct mbtest {
       +        char *s;
       +        int l;
       +        int r;
       +        int mbstate;
       +        int syserr;
       +        wchar_t *pwc;
       +        wchar_t wc;
       +} tests[] = {
       +        {"\0",                             2,  0, 1, 0,      &wc,  0},
       +        {"\x21",                           2,  1, 1, 0,      &wc,  0x21},
       +        {"\xc2\xa1",                       3,  2, 1, 0,      &wc,  0x00A1},
       +        {"\xc2\xa1",                       2,  2, 1, 0,      &wc,  0x00A1},
       +        {"\xe2\x80\x94",                   4,  3, 1, 0,      &wc,  0x2014},
       +        {"\xf0\x9f\x92\xa9",               5,  4, 1, 0,      &wc,  0x01F4A9},
       +        {"\xf0\x9f\x92\xa9",               5,  4, 1, 0,      NULL, -1},
       +        {"\xf0\x9f\x92\xa9",              -1,  4, 1, 0,      &wc,  0x01F4A9},
       +
       +        {NULL,                             4,  0, 1, 0,      NULL, -1},
       +        {"\xed\x9f\xbf",                   4,  3, 1, 0,      &wc,  0xd7ff},
       +        {"\xed\xa0\x80",                   4, -1, 1, EILSEQ, &wc,  -1},
       +        {"\xed\xb3\xbf",                   4, -1, 1, EILSEQ, &wc,  -1},
       +        {"\xed\xb4\x80",                   4,  3, 1, 0,      &wc,  0xdd00},
       +
       +        {"\xf0\x9f\x92\xa9",               3, -2, 0, 0,      &wc, -1},
       +        {"\xa9",                           2,  1, 1, 0,      &wc, 0x01F4A9},
       +        {"\xf0\x9f\x92\xa9",               3, -2, 0, 0,      &wc, -1},
       +        {NULL,                             4, -1, 1, EILSEQ, &wc, -1},
       +        {"\xa9",                           2, -1, 1, EILSEQ, &wc, -1},
       +        {"\xf0\x9f\x92\xa9",               3, -2, 0, 0,      &wc, -1},
       +        {NULL,                             4, -1, 1, EILSEQ, &wc, -1},
       +        {"\x21",                           2,  1, 1, 0,      &wc,  0x21},
       +        {"\xf0\x9f\x92\xa9",               2, -2, 0, 0,      &wc, -1},
       +        {"\xf0\x9f\x92\xa9",               0, -2, 0, 0,      &wc, -1},
       +        {"\x92\xa9",                       2,  2, 1, 0,      &wc, 0x01F4A9},
       +
       +        {"\x80",                           2, -1, 1, EILSEQ, &wc, -1},
       +        {"\xc0\x80",                       2, -1, 1, EILSEQ, &wc, -1},
       +        {"\xc0\x00",                       2, -1, 1, EILSEQ, &wc, -1},
       +        {"\xf8\x81\x82\x83\x84\x85",      -1, -1, 1, EILSEQ, &wc, -1},
       +        {"\xfe\x81\x82\x83\x84\x85\x86",   8, -1, 1, EILSEQ, &wc, -1},
       +};