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.  
  8. #ifndef UNUSED
  9. #define UNUSED(x) ((x) = (x))
  10. #endif
  11.  
  12. /* Redefine assert, so we can simply use the standard assert mechanism
  13.  * within testcases and exit with the right output for the testrunner
  14.  * to do the right thing. */
  15. void __assert2(const char *expr, const char *function,
  16.                 const char *file, int line);
  17.  
  18. void __assert2(const char *expr, const char *function,
  19.                 const char *file, int line)
  20. {
  21.         UNUSED(function);
  22.         UNUSED(file);
  23.  
  24.         printf("FAIL - %s at line %d\n", expr, line);
  25.  
  26.         exit(EXIT_FAILURE);
  27. }
  28.  
  29. #define assert(expr) \
  30.   ((void) ((expr) || (__assert2 (#expr, __func__, __FILE__, __LINE__), 0)))
  31.  
  32.  
  33. typedef bool (*line_func)(const char *data, size_t datalen, void *pw);
  34.  
  35. static size_t parse_strlen(const char *str, size_t limit);
  36. bool parse_testfile(const char *filename, line_func callback, void *pw);
  37. size_t parse_filesize(const char *filename);
  38.  
  39. /**
  40.  * Testcase datafile parser driver
  41.  *
  42.  * \param filename  Name of file to parse
  43.  * \param callback  Pointer to function to handle each line of input data
  44.  * \param pw        Pointer to client-specific private data
  45.  * \return true on success, false otherwise.
  46.  */
  47. bool parse_testfile(const char *filename, line_func callback, void *pw)
  48. {
  49.         FILE *fp;
  50.         char buf[300];
  51.  
  52.         fp = fopen(filename, "rb");
  53.         if (fp == NULL) {
  54.                 printf("Failed opening %s\n", filename);
  55.                 return false;
  56.         }
  57.  
  58.         while (fgets(buf, sizeof buf, fp)) {
  59.                 if (buf[0] == '\n')
  60.                         continue;
  61.  
  62.                 if (!callback(buf, parse_strlen(buf, sizeof buf - 1), pw)) {
  63.                         fclose(fp);
  64.                         return false;
  65.                 }
  66.         }
  67.  
  68.         fclose(fp);
  69.  
  70.         return true;
  71. }
  72.  
  73. /**
  74.  * Utility string length measurer; assumes strings are '\n' terminated
  75.  *
  76.  * \param str    String to measure length of
  77.  * \param limit  Upper bound on string length
  78.  * \return String length
  79.  */
  80. size_t parse_strlen(const char *str, size_t limit)
  81. {
  82.         size_t len = 0;
  83.  
  84.         if (str == NULL)
  85.                 return 0;
  86.  
  87.         while (len < limit - 1 && *str != '\n') {
  88.                 len++;
  89.                 str++;
  90.         }
  91.  
  92.         len++;
  93.  
  94.         return len;
  95. }
  96.  
  97. /**
  98.  * Read the size of a file
  99.  *
  100.  * \param filename  Name of file to read size of
  101.  * \return File size (in bytes), or 0 on error
  102.  */
  103. size_t parse_filesize(const char *filename)
  104. {
  105.         FILE *fp;
  106.         size_t len = 0;
  107.  
  108.         fp = fopen(filename, "rb");
  109.         if (fp == NULL) {
  110.                 printf("Failed opening %s\n", filename);
  111.                 return 0;
  112.         }
  113.  
  114.         fseek(fp, 0, SEEK_END);
  115.         len = ftell(fp);
  116.  
  117.         fclose(fp);
  118.  
  119.         return len;
  120. }
  121.  
  122.  
  123. #endif
  124.