Subversion Repositories Kolibri OS

Rev

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

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