Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. #include <inttypes.h>
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. #include <parserutils/charset/mibenum.h>
  8.  
  9. #include <hubbub/hubbub.h>
  10.  
  11. #include "charset/detect.h"
  12. #include "utils/utils.h"
  13.  
  14. #include "testutils.h"
  15.  
  16. typedef struct line_ctx {
  17.         size_t buflen;
  18.         size_t bufused;
  19.         uint8_t *buf;
  20.         char enc[64];
  21.         bool indata;
  22.         bool inenc;
  23. } line_ctx;
  24.  
  25. static bool handle_line(const char *data, size_t datalen, void *pw);
  26. static void run_test(const uint8_t *data, size_t len, char *expected);
  27.  
  28. int main(int argc, char **argv)
  29. {
  30.         line_ctx ctx;
  31.  
  32.         if (argc != 2) {
  33.                 printf("Usage: %s <filename>\n", argv[0]);
  34.                 return 1;
  35.         }
  36.  
  37.         ctx.buflen = parse_filesize(argv[1]);
  38.         if (ctx.buflen == 0)
  39.                 return 1;
  40.  
  41.         ctx.buf = malloc(ctx.buflen);
  42.         if (ctx.buf == NULL) {
  43.                 printf("Failed allocating %u bytes\n",
  44.                                 (unsigned int) ctx.buflen);
  45.                 return 1;
  46.         }
  47.  
  48.         ctx.buf[0] = '\0';
  49.         ctx.enc[0] = '\0';
  50.         ctx.bufused = 0;
  51.         ctx.indata = false;
  52.         ctx.inenc = false;
  53.  
  54.         assert(parse_testfile(argv[1], handle_line, &ctx) == true);
  55.  
  56.         /* and run final test */
  57.         if (ctx.bufused > 0 && ctx.buf[ctx.bufused - 1] == '\n')
  58.                 ctx.bufused -= 1;
  59.  
  60.         run_test(ctx.buf, ctx.bufused, ctx.enc);
  61.  
  62.         free(ctx.buf);
  63.  
  64.         printf("PASS\n");
  65.  
  66.         return 0;
  67. }
  68.  
  69. bool handle_line(const char *data, size_t datalen, void *pw)
  70. {
  71.         line_ctx *ctx = (line_ctx *) pw;
  72.  
  73.         if (data[0] == '#') {
  74.                 if (ctx->inenc) {
  75.                         /* This marks end of testcase, so run it */
  76.  
  77.                         if (ctx->buf[ctx->bufused - 1] == '\n')
  78.                                 ctx->bufused -= 1;
  79.  
  80.                         run_test(ctx->buf, ctx->bufused, ctx->enc);
  81.  
  82.                         ctx->buf[0] = '\0';
  83.                         ctx->enc[0] = '\0';
  84.                         ctx->bufused = 0;
  85.                 }
  86.  
  87.                 ctx->indata = (strncasecmp(data+1, "data", 4) == 0);
  88.                 ctx->inenc  = (strncasecmp(data+1, "encoding", 8) == 0);
  89.         } else {
  90.                 if (ctx->indata) {
  91.                         memcpy(ctx->buf + ctx->bufused, data, datalen);
  92.                         ctx->bufused += datalen;
  93.                 }
  94.                 if (ctx->inenc) {
  95.                         strcpy(ctx->enc, data);
  96.                         if (ctx->enc[strlen(ctx->enc) - 1] == '\n')
  97.                                 ctx->enc[strlen(ctx->enc) - 1] = '\0';
  98.                 }
  99.         }
  100.  
  101.         return true;
  102. }
  103.  
  104. void run_test(const uint8_t *data, size_t len, char *expected)
  105. {
  106.         uint16_t mibenum = 0;
  107.         hubbub_charset_source source = HUBBUB_CHARSET_UNKNOWN;
  108.         static int testnum;
  109.  
  110.         assert(hubbub_charset_extract(data, len,
  111.                         &mibenum, &source) == HUBBUB_OK);
  112.  
  113.         assert(mibenum != 0);
  114.  
  115.         printf("%d: Detected charset %s (%d) Source %d Expected %s (%d)\n",
  116.                         ++testnum, parserutils_charset_mibenum_to_name(mibenum),
  117.                         mibenum, source, expected,
  118.                         parserutils_charset_mibenum_from_name(
  119.                                         expected, strlen(expected)));
  120.  
  121.         assert(mibenum == parserutils_charset_mibenum_from_name(
  122.                         expected, strlen(expected)));
  123. }
  124.