Subversion Repositories Kolibri OS

Rev

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

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #include <parserutils/parserutils.h>
  5. #include <parserutils/input/inputstream.h>
  6.  
  7. #include "utils/utils.h"
  8.  
  9. #include "testutils.h"
  10.  
  11. static void *myrealloc(void *ptr, size_t len, void *pw)
  12. {
  13.         UNUSED(pw);
  14.  
  15.         return realloc(ptr, len);
  16. }
  17.  
  18. int main(int argc, char **argv)
  19. {
  20.         parserutils_inputstream *stream;
  21.  
  22.         /* This is specially calculated so that the inputstream is forced to
  23.          * reallocate (it assumes that the inputstream's buffer chunk size
  24.          * is 4k) */
  25. #define BUFFER_SIZE (4096 + 4)
  26.         uint8_t input_buffer[BUFFER_SIZE];
  27. //      uint8_t *buffer;
  28. //      size_t buflen;
  29.         const uint8_t *c;
  30.         size_t clen;
  31.  
  32.         UNUSED(argc);
  33.         UNUSED(argv);
  34.  
  35.         /* Populate the buffer with something sane */
  36.         memset(input_buffer, 'a', BUFFER_SIZE);
  37.         /* Now, set up our test data */
  38.         input_buffer[BUFFER_SIZE - 1] = '5';
  39.         input_buffer[BUFFER_SIZE - 2] = '4';
  40.         input_buffer[BUFFER_SIZE - 3] = '\xbd';
  41.         input_buffer[BUFFER_SIZE - 4] = '\xbf';
  42.         /* This byte will occupy the 4095th byte in the buffer and
  43.          * thus cause the entirety of U+FFFD to be buffered until after
  44.          * the buffer has been enlarged */
  45.         input_buffer[BUFFER_SIZE - 5] = '\xef';
  46.         input_buffer[BUFFER_SIZE - 6] = '3';
  47.         input_buffer[BUFFER_SIZE - 7] = '2';
  48.         input_buffer[BUFFER_SIZE - 8] = '1';
  49.  
  50.         assert(parserutils_inputstream_create("UTF-8", 0,
  51.                         NULL, myrealloc, NULL, &stream) == PARSERUTILS_OK);
  52.  
  53.         assert(parserutils_inputstream_append(stream,
  54.                         input_buffer, BUFFER_SIZE) == PARSERUTILS_OK);
  55.  
  56.         assert(parserutils_inputstream_append(stream, NULL, 0) ==
  57.                         PARSERUTILS_OK);
  58.  
  59.         while (parserutils_inputstream_peek(stream, 0, &c, &clen) !=
  60.                         PARSERUTILS_EOF)
  61.                 parserutils_inputstream_advance(stream, clen);
  62.  
  63. /*
  64.         assert(css_inputstream_claim_buffer(stream, &buffer, &buflen) ==
  65.                         CSS_OK);
  66.  
  67.         assert(buflen == BUFFER_SIZE);
  68.  
  69.         printf("Buffer: '%.*s'\n", 8, buffer + (BUFFER_SIZE - 8));
  70.  
  71.         assert( buffer[BUFFER_SIZE - 6] == '3' &&
  72.                 buffer[BUFFER_SIZE - 5] == (uint8_t) '\xef' &&
  73.                 buffer[BUFFER_SIZE - 4] == (uint8_t) '\xbf' &&
  74.                 buffer[BUFFER_SIZE - 3] == (uint8_t) '\xbd' &&
  75.                 buffer[BUFFER_SIZE - 2] == '4');
  76.  
  77.         free(buffer);
  78. */
  79.  
  80.         parserutils_inputstream_destroy(stream);
  81.  
  82.         printf("PASS\n");
  83.  
  84.         return 0;
  85. }
  86.  
  87.