Subversion Repositories Kolibri OS

Rev

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

  1. #ifndef test_testutils_h_
  2. #define test_testutils_h_
  3.  
  4. #include <stdbool.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <sys/types.h>
  9.  
  10. #ifndef UNUSED
  11. #define UNUSED(x) ((x) = (x))
  12. #endif
  13.  
  14. /* Redefine assert, so we can simply use the standard assert mechanism
  15.  * within testcases and exit with the right output for the testrunner
  16.  * to do the right thing. */
  17. void __assert2(const char *expr, const char *function,
  18.                 const char *file, int line);
  19.  
  20. void __assert2(const char *expr, const char *function,
  21.                 const char *file, int line)
  22. {
  23.         UNUSED(function);
  24.         UNUSED(file);
  25.  
  26.         printf("FAIL - %s at line %d\n", expr, line);
  27.  
  28.         exit(EXIT_FAILURE);
  29. }
  30.  
  31. #define assert(expr) \
  32.   ((void) ((expr) || (__assert2 (#expr, __func__, __FILE__, __LINE__), 0)))
  33.  
  34.  
  35. typedef bool (*line_func)(const char *data, size_t datalen, void *pw);
  36.  
  37. static size_t css__parse_strlen(const char *str, size_t limit);
  38. char *css__parse_strnchr(const char *str, size_t len, int chr);
  39. bool css__parse_testfile(const char *filename, line_func callback, void *pw);
  40. size_t css__parse_filesize(const char *filename);
  41.  
  42. /**
  43.  * Testcase datafile parser driver
  44.  *
  45.  * \param filename  Name of file to parse
  46.  * \param callback  Pointer to function to handle each line of input data
  47.  * \param pw        Pointer to client-specific private data
  48.  * \return true on success, false otherwise.
  49.  */
  50. bool css__parse_testfile(const char *filename, line_func callback, void *pw)
  51. {
  52.         FILE *fp;
  53.         char buf[300];
  54.  
  55.         fp = fopen(filename, "rb");
  56.         if (fp == NULL) {
  57.                 printf("Failed opening %s\n", filename);
  58.                 return false;
  59.         }
  60.  
  61.         while (fgets(buf, sizeof buf, fp)) {
  62.                 if (buf[0] == '\n')
  63.                         continue;
  64.  
  65.                 if (!callback(buf, css__parse_strlen(buf, sizeof buf - 1), pw)) {
  66.                         fclose(fp);
  67.                         return false;
  68.                 }
  69.         }
  70.  
  71.         fclose(fp);
  72.  
  73.         return true;
  74. }
  75.  
  76. /**
  77.  * Utility string length measurer; assumes strings are '\n' terminated
  78.  *
  79.  * \param str    String to measure length of
  80.  * \param limit  Upper bound on string length
  81.  * \return String length
  82.  */
  83. size_t css__parse_strlen(const char *str, size_t limit)
  84. {
  85.         size_t len = 0;
  86.  
  87.         if (str == NULL)
  88.                 return 0;
  89.  
  90.         while (len < limit - 1 && *str != '\n') {
  91.                 len++;
  92.                 str++;
  93.         }
  94.  
  95.         len++;
  96.  
  97.         return len;
  98. }
  99.  
  100. /**
  101.  * Length-limited strchr
  102.  *
  103.  * \param str  String to search in
  104.  * \param len  Length of string
  105.  * \param chr  Character to search for
  106.  * \return Pointer to character in string, or NULL if not found
  107.  */
  108. char *css__parse_strnchr(const char *str, size_t len, int chr)
  109. {
  110.         size_t i;
  111.  
  112.         if (str == NULL)
  113.                 return NULL;
  114.  
  115.         for (i = 0; i < len; i++) {
  116.                 if (str[i] == chr)
  117.                         break;
  118.         }
  119.  
  120.         if (i == len)
  121.                 return NULL;
  122.  
  123.         return (char *) str + i;
  124. }
  125.  
  126. /**
  127.  * Read the size of a file
  128.  *
  129.  * \param filename  Name of file to read size of
  130.  * \return File size (in bytes), or 0 on error
  131.  */
  132. size_t css__parse_filesize(const char *filename)
  133. {
  134.         FILE *fp;
  135.         size_t len = 0;
  136.  
  137.         fp = fopen(filename, "rb");
  138.         if (fp == NULL) {
  139.                 printf("Failed opening %s\n", filename);
  140.                 return 0;
  141.         }
  142.  
  143.         fseek(fp, 0, SEEK_END);
  144.         len = ftell(fp);
  145.  
  146.         fclose(fp);
  147.  
  148.         return len;
  149. }
  150.  
  151.  
  152. /**
  153.  * Convert a string representation of an error name to a LibCSS error code
  154.  *
  155.  * \param str  String containing error name
  156.  * \param len  Length of string (bytes)
  157.  * \return LibCSS error code, or CSS_OK if unknown
  158.  */
  159. css_error css_error_from_string(const char *str, size_t len);
  160. css_error css_error_from_string(const char *str, size_t len)
  161. {
  162.         if (strncmp(str, "CSS_OK", len) == 0) {
  163.                 return CSS_OK;
  164.         } else if (strncmp(str, "CSS_NOMEM", len) == 0) {
  165.                 return CSS_NOMEM;
  166.         } else if (strncmp(str, "CSS_BADPARM", len) == 0) {
  167.                 return CSS_BADPARM;
  168.         } else if (strncmp(str, "CSS_INVALID", len) == 0) {
  169.                 return CSS_INVALID;
  170.         } else if (strncmp(str, "CSS_FILENOTFOUND", len) == 0) {
  171.                 return CSS_FILENOTFOUND;
  172.         } else if (strncmp(str, "CSS_NEEDDATA", len) == 0) {
  173.                 return CSS_NEEDDATA;
  174.         } else if (strncmp(str, "CSS_BADCHARSET", len) == 0) {
  175.                 return CSS_BADCHARSET;
  176.         } else if (strncmp(str, "CSS_EOF", len) == 0) {
  177.                 return CSS_EOF;
  178.         }
  179.  
  180.         return CSS_OK;
  181. }
  182.  
  183. #endif
  184.