main.c - sfeed_tests - sfeed tests and RSS and Atom files
 (HTM) git clone git://git.codemadness.org/sfeed_tests
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       main.c (5199B)
       ---
            1 #include <stdio.h>
            2 #include <string.h>
            3 
            4 #include "xml.h"
            5 
            6 int count, ok, failed;
            7 
            8 int
            9 test(char *buf, size_t bufsiz,
           10      const char *input, int ret, const char *expect, int expectret)
           11 {
           12         count++;
           13 
           14         ret = xml_entitytostr(input, buf, bufsiz);
           15         if (ret != expectret) {
           16                 printf("[%d] FAILED: input=%s, status=%d, expected status=%d\n",
           17                         count, input, ret, expectret);
           18                 failed++;
           19                 return 0;
           20         }
           21         /* if return status is -1 the result in buf should not be used */
           22         if (ret == -1) {
           23                 ok++;
           24                 return 0;
           25         }
           26 
           27         if (strcmp(expect, buf)) {
           28                 printf("[%d] FAILED: input=%s, expected=%s, got=%s\n",
           29                         count, input, expect, buf);
           30                 failed++;
           31                 return 0;
           32         }
           33         ok++;
           34 
           35         return 1;
           36 }
           37 
           38 int
           39 main(void)
           40 {
           41         char buf[8];
           42 
           43         /* named entities */
           44         test(buf, sizeof(buf), "&amp;", 1, "&", 1);
           45         test(buf, sizeof(buf), "&lt;", 1, "<", 1);
           46         test(buf, sizeof(buf), "&gt;", 1, ">", 1);
           47         test(buf, sizeof(buf), "&apos;", 1, "'", 1);
           48         test(buf, sizeof(buf), "&quot;", 1, "\"", 1);
           49 
           50         /* case-sensitivity of named entities (should not match) */
           51         test(buf, sizeof(buf), "&AMP;", -1, "&", -1);
           52         test(buf, sizeof(buf), "&LT;", -1, "<", -1);
           53         test(buf, sizeof(buf), "&GT;", -1, ">", -1);
           54         test(buf, sizeof(buf), "&APOS;", -1, "'", -1);
           55         test(buf, sizeof(buf), "&QUOT;", -1, "\"", -1);
           56 
           57         /* HTML entities (should not match) */
           58         test(buf, sizeof(buf), "&euro;", -1, "&", -1);
           59         test(buf, sizeof(buf), "&EURO;", -1, "&", -1);
           60 
           61         /* numeric entities */
           62         test(buf, sizeof(buf), "&#65;", 1, "A", 1);
           63         test(buf, sizeof(buf), "&#x41;", 1, "A", 1);
           64 
           65         /* numeric entities rare characters */
           66         /* The XML specification doesn't recommend this. But we allow it */
           67         test(buf, sizeof(buf), "&#x00;", 0, "", 0); /* NUL */
           68         test(buf, sizeof(buf), "&#x01;", 1, "\x01", 1);
           69 
           70         /* surrogate range */
           71         test(buf, sizeof(buf), "&#xd800;", -1, "", -1); /* start range >= */
           72         test(buf, sizeof(buf), "&#xdfff;", -1, "", -1); /* end range <= */
           73         test(buf, sizeof(buf), "&#xdc00;", -1, "\xed\xb0\x80", -1);
           74         /* near range */
           75         test(buf, sizeof(buf), "&#xe0000;", 4, "\xf3\xa0\x80\x80", 4);
           76         test(buf, sizeof(buf), "&#xd7ff;", 3, "\xed\x9f\xbf", 3);
           77         test(buf, sizeof(buf), "&#xd7fb;", 3, "\xed\x9f\xbb", 3);
           78 
           79         test(buf, sizeof(buf), "&#x-1;", -1, "", -1);
           80         test(buf, sizeof(buf), "&#-1;", -1, "", -1);
           81         test(buf, sizeof(buf), "&#x-0;", -1, "", -1);
           82         test(buf, sizeof(buf), "&#-0;", -1, "", -1);
           83         test(buf, sizeof(buf), "&#+1;", -1, "", -1);
           84         test(buf, sizeof(buf), "&amp;;", -1, "", -1);
           85         test(buf, sizeof(buf), "&#65;;", -1, "", -1);
           86         test(buf, sizeof(buf), "&amp;;a", -1, "", -1);
           87         test(buf, sizeof(buf), "&#65;;a", -1, "", -1);
           88         test(buf, sizeof(buf), "&# 65;", -1, "", -1);
           89         test(buf, sizeof(buf), "&#65", -1, "", -1);
           90         test(buf, sizeof(buf), "&", -1, "", -1);
           91         test(buf, sizeof(buf), "& ", -1, "", -1);
           92         test(buf, sizeof(buf), "&#", -1, "", -1);
           93         test(buf, sizeof(buf), "&# ", -1, "", -1);
           94         test(buf, sizeof(buf), "&#x", -1, "", -1);
           95         test(buf, sizeof(buf), "&#x ", -1, "", -1);
           96         test(buf, sizeof(buf), "&;", -1, "", -1);
           97         test(buf, sizeof(buf), "& ;", -1, "", -1);
           98         test(buf, sizeof(buf), "&#;", -1, "", -1);
           99         test(buf, sizeof(buf), "&# ;", -1, "", -1);
          100         test(buf, sizeof(buf), "&#x;", -1, "", -1);
          101         test(buf, sizeof(buf), "&#x ;", -1, "", -1);
          102 
          103         /* just in range (limit) */
          104         test(buf, sizeof(buf), "&#x10ffff;", 4, "\xf4\x8f\xbf\xbf", 4);
          105         test(buf, sizeof(buf), "&#1114111;", 4, "\xf4\x8f\xbf\xbf", 4);
          106         /* outside range (range + 1) */
          107         test(buf, sizeof(buf), "&#x110000;", -1, "", -1);
          108         test(buf, sizeof(buf), "&#1114112;", -1, "", -1);
          109 
          110         test(buf, sizeof(buf), "&#x7e;", 1, "\x7e", 1);
          111         test(buf, sizeof(buf), "&#x7f;", 1, "\x7f", 1);
          112         /* 2 bytes */
          113         test(buf, sizeof(buf), "&#x80;", 2, "\xc2\x80", 2);
          114         test(buf, sizeof(buf), "&#xfe;", 2, "\xc3\xbe", 2);
          115         test(buf, sizeof(buf), "&#xff;", 2, "\xc3\xbf", 2);
          116         test(buf, sizeof(buf), "&#x0370;", 2, "\xcd\xb0", 2);
          117         test(buf, sizeof(buf), "&#x370;", 2, "\xcd\xb0", 2);
          118         test(buf, sizeof(buf), "&#x00000370;", 2, "\xcd\xb0", 2);
          119         /* 3 bytes */
          120         test(buf, sizeof(buf), "&#x169b;", 3, "\xe1\x9a\x9b", 3);
          121         test(buf, sizeof(buf), "&#x169B;", 3, "\xe1\x9a\x9b", 3);
          122         test(buf, sizeof(buf), "&#x0169b;", 3, "\xe1\x9a\x9b", 3);
          123         test(buf, sizeof(buf), "&#x00169b;", 3, "\xe1\x9a\x9b", 3);
          124         /* 4 bytes */
          125         test(buf, sizeof(buf), "&#x10300;", 4, "\xf0\x90\x8c\x80", 4);
          126         test(buf, sizeof(buf), "&#x010300;", 4, "\xf0\x90\x8c\x80", 4);
          127         test(buf, sizeof(buf), "&#x0010300;", 4, "\xf0\x90\x8c\x80", 4);
          128         test(buf, sizeof(buf), "&#x1faf0;", 4, "\xf0\x9f\xab\xb0", 4);
          129         test(buf, sizeof(buf), "&#x1fAf0;", 4, "\xf0\x9f\xab\xb0", 4);
          130 
          131         /* --- */
          132 
          133         /* does not start with "&" */
          134         test(buf, sizeof(buf), " &amp;", -1, "", -1);
          135         test(buf, sizeof(buf), "#65;", -1, "", -1);
          136         test(buf, sizeof(buf), "#x65;", -1, "", -1);
          137 
          138         /* --- */
          139 
          140         /* test buffer size checks */
          141         /* named entities must have bufsiz >= 2 */
          142         test(buf, 0, "&amp;", -1, "", -1);
          143         test(buf, 1, "&amp;", -1, "", -1);
          144         test(buf, 2, "&amp;", 1, "&", 1);
          145 
          146         /* numeric entities must have bufsiz >= 5 */
          147         test(buf, 0, "&#65;", -1, "", -1);
          148         test(buf, 1, "&#65;", -1, "", -1);
          149         test(buf, 4, "&#65;", -1, "", -1);
          150         test(buf, 5, "&#65;", 1, "A", 1);
          151 
          152         printf("%s (%d tested, %d OK, %d failed)\n",
          153                 failed == 0 ? "OK" : "FAILED", count, ok, failed);
          154 
          155         return failed != 0;
          156 }