diff options
| author | Jonathan McCrohan <jmccrohan@gmail.com> | 2012-11-17 16:57:30 +0000 | 
|---|---|---|
| committer | Jonathan McCrohan <jmccrohan@gmail.com> | 2012-11-17 16:57:30 +0000 | 
| commit | f2499612c5594944d3e0891259859668d35c85b2 (patch) | |
| tree | 0672f579db91cfaca7a2935a86991fa67b5aae59 /tinytest | |
| parent | 429e46051dba814e7d6c74368eb1bba550222cbe (diff) | |
| download | libconfig-f2499612c5594944d3e0891259859668d35c85b2.tar.gz | |
Imported Upstream version 1.4.9upstream/1.4.9
Diffstat (limited to 'tinytest')
| -rw-r--r-- | tinytest/Makefile.in | 2 | ||||
| -rw-r--r-- | tinytest/tinytest.c | 217 | ||||
| -rw-r--r-- | tinytest/tinytest.h | 109 | 
3 files changed, 325 insertions, 3 deletions
diff --git a/tinytest/Makefile.in b/tinytest/Makefile.in index 6b52eb8..7243286 100644 --- a/tinytest/Makefile.in +++ b/tinytest/Makefile.in @@ -116,6 +116,7 @@ LIPO = @LIPO@  LN_S = @LN_S@  LTLIBOBJS = @LTLIBOBJS@  MAKEINFO = @MAKEINFO@ +MANIFEST_TOOL = @MANIFEST_TOOL@  MKDIR_P = @MKDIR_P@  NM = @NM@  NMEDIT = @NMEDIT@ @@ -143,6 +144,7 @@ abs_builddir = @abs_builddir@  abs_srcdir = @abs_srcdir@  abs_top_builddir = @abs_top_builddir@  abs_top_srcdir = @abs_top_srcdir@ +ac_ct_AR = @ac_ct_AR@  ac_ct_CC = @ac_ct_CC@  ac_ct_CXX = @ac_ct_CXX@  ac_ct_DUMPBIN = @ac_ct_DUMPBIN@ diff --git a/tinytest/tinytest.c b/tinytest/tinytest.c index 24d0c2f..2c00d5d 100644 --- a/tinytest/tinytest.c +++ b/tinytest/tinytest.c @@ -25,6 +25,13 @@  #include <stdarg.h>  #include <stdio.h> +#ifdef WIN32 +#include <io.h> +#define F_OK 4  // Windows doesn't have F_OK +#else +#include <unistd.h> +#endif +  /*   */ @@ -37,11 +44,105 @@ static const char *__tt_op_strings[] = { "==", "!=", "<", "<=", ">", ">=",                                           "==", "!=", "<", "<=", ">", ">=",                                           "==", "!=", "<", "<=", ">", ">=",                                           "==", "!=", "", "!", -                                         "==file", "!=file" }; +                                         "==file", "!=file", +                                         "==txtfile", "!=txtfile" };  /*   */ +static tt_bool_t __tt_chop(char *s) +{ +  size_t len = strlen(s); +  char *p; +  tt_bool_t eol = TT_FALSE; + +  for(p = s + len - 1; p > s; --p) { +    if ((*p == '\r') || (*p == '\n')) { +      eol = TT_TRUE; +      *p = 0; +    } +  } + +  return(eol); +} + +static tt_bool_t __tt_compare_files_text(const char *file1, const char *file2, +                                         tt_bool_t verbose) +{ +  FILE *fp1, *fp2; +  char buf1[4096], buf2[4096]; +  int line = 1; +  int character = 0; +  tt_bool_t matched = TT_TRUE, eof1, eof2; + +  if(!(fp1 = fopen(file1, "rt"))) +  { +    printf("cannot open file: %s\n", file1); +    return(TT_FALSE); +  } + +  if(!(fp2 = fopen(file2, "rt"))) +  { +    fclose(fp1); +    printf("cannot open file: %s\n", file2); +    return(TT_FALSE); +  } + +  for(;;) +  { +    char *r1, *r2, *p, *q; + +    r1 = fgets(buf1, sizeof(buf1), fp1); +    r2 = fgets(buf2, sizeof(buf2), fp2); + +    if((r1 == NULL) || (r2 == NULL)) +    { +      matched = (r1 == r2) ? TT_TRUE : TT_FALSE; +      break; +    } + +    eof1 = __tt_chop(r1); +    eof2 = __tt_chop(r2); + +    p = r1; +    q = r2; + +    while(*p && *q) +    { +      if(*p != *q) +      { +        matched = TT_FALSE; +        break; +      } + +      ++character; +      ++p; +      ++q; +    } + +    if(eof1 != eof2) +      matched = TT_FALSE; + +    if(matched == TT_FALSE) +      break; + +    if(eof1 && eof2) +    { +      ++line; +      character = 0; +    } +  } + +  fclose(fp1); +  fclose(fp2); + +  if(!matched && verbose) +    printf("files \"%s\" and \"%s\" differ starting at line %d, char %d\n", +           file1, file2, line, character); + +  return(matched); +} +  static tt_bool_t __tt_compare_files(const char *file1, const char *file2,                                      tt_bool_t verbose)  { @@ -445,6 +546,16 @@ void tt_expect(const char *file, int line, const char *aexpr,        result = !__tt_compare_files(a.value.str_val, b.value.str_val, TT_TRUE);        break; +    case TT_OP_TXTFILE_EQ: +      result = __tt_compare_files_text(a.value.str_val, b.value.str_val, +                                       TT_TRUE); +      break; + +    case TT_OP_TXTFILE_NE: +      result = !__tt_compare_files_text(a.value.str_val, b.value.str_val, +                                        TT_TRUE); +      break; +      default:        break;    } @@ -512,4 +623,108 @@ void tt_fail(const char *file, int line, const char *message, ...)    putchar('\n');  } +/* + */ + +tt_bool_t tt_file_exists(const char *file) +{ +  return(access(file, F_OK) == 0); +} + +#ifdef _MSC_VER + +// All of this extra code is because MSVC doesn't support the C99 standard. +// Sigh. + +/* + */ + +void tt_test_int(const char *file, int line, const char *aexpr, tt_op_t op, +                 const char *bexpr, int a, int b, tt_bool_t fatal) +{ +  tt_val_t aval = { TT_VAL_INT }, bval = { TT_VAL_INT }; +  aval.value.int_val = a; +  bval.value.int_val = b; +  tt_expect(file, line, aexpr, op, bexpr, aval, bval, fatal); +} + +/* + */ + +void tt_test_uint(const char *file, int line, const char *aexpr, tt_op_t op, +                  const char *bexpr, unsigned int a, unsigned int b, +                  tt_bool_t fatal) +{ +  tt_val_t aval = { TT_VAL_UINT }, bval = { TT_VAL_UINT }; +  aval.value.uint_val = a; +  bval.value.uint_val = b; +  tt_expect(file, line, aexpr, op, bexpr, aval, bval, fatal); +} + +/* + */ + +void tt_test_int64(const char *file, int line, const char *aexpr, tt_op_t op, +                   const char *bexpr, long long a, long long b, +                   tt_bool_t fatal) +{ +  tt_val_t aval = { TT_VAL_INT64 }, bval = { TT_VAL_INT64 }; +  aval.value.int64_val = a; +  bval.value.int64_val = b; +  tt_expect(file, line, aexpr, op, bexpr, aval, bval, fatal); +} + +/* + */ + +void tt_test_uint64(const char *file, int line, const char *aexpr, +                    tt_op_t op, const char *bexpr, unsigned long long a, +                    unsigned long long b, tt_bool_t fatal) +{ +  tt_val_t aval = { TT_VAL_UINT64 }, bval = { TT_VAL_UINT64 }; +  aval.value.uint64_val = a; +  bval.value.uint64_val = b; +  tt_expect(file, line, aexpr, op, bexpr, aval, bval, fatal); +} + +/* + */ + +void tt_test_double(const char *file, int line, const char *aexpr, tt_op_t op, +                    const char *bexpr, double a, double b, tt_bool_t fatal) +{ +  tt_val_t aval = { TT_VAL_DOUBLE }, bval = { TT_VAL_DOUBLE }; +  aval.value.double_val = a; +  bval.value.double_val = b; +  tt_expect(file, line, aexpr, op, bexpr, aval, bval, fatal); +} + +/* + */ + +void tt_test_str(const char *file, int line, const char *aexpr, tt_op_t op, +                 const char *bexpr, const char *a, const char *b, +                 tt_bool_t fatal) +{ +  tt_val_t aval = { TT_VAL_STR }, bval = { TT_VAL_STR }; +  aval.value.str_val = a; +  bval.value.str_val = b; +  tt_expect(file, line, aexpr, op, bexpr, aval, bval, fatal); +} + +/* + */ + +void tt_test_ptr(const char *file, int line, const char *aexpr, tt_op_t op, +                 const char *bexpr, const void *a, const void *b, +                 tt_bool_t fatal) +{ +  tt_val_t aval = { TT_VAL_PTR }, bval = { TT_VAL_PTR }; +  aval.value.ptr_val = a; +  bval.value.ptr_val = b; +  tt_expect(file, line, aexpr, op, bexpr, aval, bval, fatal); +} + +#endif // WIN32 +  /* end of source file */ diff --git a/tinytest/tinytest.h b/tinytest/tinytest.h index 29a95e8..dd6e32a 100644 --- a/tinytest/tinytest.h +++ b/tinytest/tinytest.h @@ -65,7 +65,8 @@ typedef enum { TT_OP_INT_EQ, TT_OP_INT_NE, TT_OP_INT_LT, TT_OP_INT_LE,                 TT_OP_DOUBLE_GT, TT_OP_DOUBLE_GE, TT_OP_STR_EQ, TT_OP_STR_NE,                 TT_OP_STR_LT, TT_OP_STR_LE, TT_OP_STR_GT, TT_OP_STR_GE,                 TT_OP_PTR_EQ, TT_OP_PTR_NE, TT_OP_TRUE, TT_OP_FALSE, -               TT_OP_FILE_EQ, TT_OP_FILE_NE } tt_op_t; +               TT_OP_FILE_EQ, TT_OP_FILE_NE, TT_OP_TXTFILE_EQ, +               TT_OP_TXTFILE_NE } tt_op_t;  typedef struct tt_val_t  { @@ -101,13 +102,27 @@ extern void tt_expect_bool(const char *file, int line, const char *expr,  extern void tt_fail(const char *file, int line, const char *message, ...); +extern tt_bool_t tt_file_exists(const char *file); + +#ifdef _MSC_VER + +extern void tt_test_int(const char *file, int line, const char *aexpr, +                        tt_op_t op, const char *bexpr, int a, int b, +                        tt_bool_t fatal); + +#define TT_TEST_INT_(A, OP, B, F)                               \ +  tt_test_int(__FILE__, __LINE__, #A, (OP), #B, (A), (B), (F)) + +#else  #define TT_TEST_INT_(A, OP, B, F)                                       \ -  tt_expect(__FILE__, __LINE__, #A, OP, #B,                             \ +  tt_expect(__FILE__, __LINE__, #A, (OP), #B,                           \              (tt_val_t){ TT_VAL_INT, .value.int_val = (A) },             \              (tt_val_t){ TT_VAL_INT, .value.int_val = (B) },             \              (F)) +#endif +  #define TT_EXPECT_INT_EQ(A, B)                          \    TT_TEST_INT_((A), TT_OP_INT_EQ, (B), TT_FALSE) @@ -144,12 +159,25 @@ extern void tt_fail(const char *file, int line, const char *message, ...);  #define TT_ASSERT_INT_GE(A, B)                  \    TT_TEST_INT_((A), TT_OP_INT_GE, (B), TT_TRUE) +#ifdef _MSC_VER + +extern void tt_test_uint(const char *file, int line, const char *aexpr, +                         tt_op_t op, const char *bexpr, unsigned int a, +                         unsigned int b, tt_bool_t fatal); + +#define TT_TEST_UINT_(A, OP, B, F)                              \ +  tt_test_uint(__FILE__, __LINE__, #A, (OP), #B, (A), (B), (F)) + +#else +  #define TT_TEST_UINT_(A, OP, B, F)                                      \    tt_expect(__FILE__, __LINE__, #A, OP, #B,                             \              (tt_val_t){ TT_VAL_UINT, .value.uint_val = (A) },           \              (tt_val_t){ TT_VAL_UINT, .value.uint_val = (B) },           \              (F)) +#endif +  #define TT_EXPECT_UINT_EQ(A, B)                  \    TT_TEST_UINT_((A), TT_OP_UINT_EQ, (B), TT_FALSE) @@ -186,12 +214,25 @@ extern void tt_fail(const char *file, int line, const char *message, ...);  #define TT_ASSERT_UINT_GE(A, B)                  \    TT_TEST_UINT_((A), TT_OP_UINT_GE, (B), TT_TRUE) +#ifdef _MSC_VER + +extern void tt_test_int64(const char *file, int line, const char *aexpr, +                          tt_op_t op, const char *bexpr, long long a, +                          long long b, tt_bool_t fatal); + +#define TT_TEST_INT64_(A, OP, B, F)                                      \ +  tt_test_int64(__FILE__, __LINE__, #A, (OP), #B, (A), (B), (F)) + +#else +  #define TT_TEST_INT64_(A, OP, B, F)                                     \    tt_expect(__FILE__, __LINE__, #A, OP, #B,                             \              (tt_val_t){ TT_VAL_INT64, .value.int64_val = (A) },         \              (tt_val_t){ TT_VAL_INT64, .value.int64_val = (B) },         \              (F)) +#endif +  #define TT_EXPECT_INT64_EQ(A, B)                        \    TT_TEST_INT64_((A), TT_OP_INT64_EQ, (B), TT_FALSE) @@ -228,12 +269,26 @@ extern void tt_fail(const char *file, int line, const char *message, ...);  #define TT_ASSERT_INT64_GE(A, B)                \    TT_TEST_INT64_((A), TT_OP_INT64_GE, (B), TT_TRUE) +#ifdef _MSC_VER + +extern void tt_test_uint64(const char *file, int line, const char *aexpr, +                           tt_op_t op, const char *bexpr, +                           unsigned long long a, unsigned long long b, +                           tt_bool_t fatal); + +#define TT_TEST_UINT64_(A, OP, B, F)                                      \ +  tt_test_uint64(__FILE__, __LINE__, #A, (OP), #B, (A), (B), (F)) + +#else +  #define TT_TEST_UINT64_(A, OP, B, F)                                    \    tt_expect(__FILE__, __LINE__, #A, OP, #B,                             \              (tt_val_t){ TT_VAL_UINT64, .value.uint64_val = (A) },       \              (tt_val_t){ TT_VAL_UINT64, .value.uint64_val = (B) },       \              (F)) +#endif +  #define TT_EXPECT_UINT64_EQ(A, B)                        \    TT_TEST_UINT64_((A), TT_OP_UINT64_EQ, (B), TT_FALSE) @@ -270,12 +325,25 @@ extern void tt_fail(const char *file, int line, const char *message, ...);  #define TT_ASSERT_UINT64_GE(A, B)                \    TT_TEST_UINT64_((A), TT_OP_UINT64_GE, (B), TT_TRUE) +#ifdef _MSC_VER + +extern void tt_test_double(const char *file, int line, const char *aexpr, +                           tt_op_t op, const char *bexpr, double a, +                           double b, tt_bool_t fatal); + +#define TT_TEST_DOUBLE_(A, OP, B, F)                                    \ +  tt_test_double(__FILE__, __LINE__, #A, (OP), #B, (A), (B), (F)) + +#else +  #define TT_TEST_DOUBLE_(A, OP, B, F)                                    \    tt_expect(__FILE__, __LINE__, #A, OP, #B,                             \              (tt_val_t){ TT_VAL_DOUBLE, .value.double_val = (A) },       \              (tt_val_t){ TT_VAL_DOUBLE, .value.double_val = (B) },       \              (F)) +#endif +  #define TT_EXPECT_DOUBLE_EQ(A, B)                       \    TT_TEST_DOUBLE_((A), TT_OP_DOUBLE_EQ, (B), TT_FALSE) @@ -312,12 +380,25 @@ extern void tt_fail(const char *file, int line, const char *message, ...);  #define TT_ASSERT_DOUBLE_GE(A, B)                       \    TT_TEST_DOUBLE_((A), TT_OP_DOUBLE_GE, (B), TT_TRUE) +#ifdef _MSC_VER + +extern void tt_test_str(const char *file, int line, const char *aexpr, +                        tt_op_t op, const char *bexpr, const char *a, +                        const char *b, tt_bool_t fatal); + +#define TT_TEST_STR_(A, OP, B, F)                               \ +  tt_test_str(__FILE__, __LINE__, #A, (OP), #B, (A), (B), (F)) + +#else +  #define TT_TEST_STR_(A, OP, B, F)                                       \    tt_expect(__FILE__, __LINE__, #A, OP, #B,                             \              (tt_val_t){ TT_VAL_STR, .value.str_val = (A) },             \              (tt_val_t){ TT_VAL_STR, .value.str_val = (B) },             \              (F)) +#endif +  #define TT_EXPECT_STR_EQ(A, B)                  \    TT_TEST_STR_((A), TT_OP_STR_EQ, (B), TT_FALSE) @@ -354,12 +435,25 @@ extern void tt_fail(const char *file, int line, const char *message, ...);  #define TT_ASSERT_STR_GE(A, B)                  \    TT_TEST_STR_((A), TT_OP_STR_GE, (B), TT_TRUE) +#ifdef _MSC_VER + +extern void tt_test_ptr(const char *file, int line, const char *aexpr, +                        tt_op_t op, const char *bexpr, const void *a, +                        const void *b, tt_bool_t fatal); + +#define TT_TEST_PTR_(A, OP, B, F)                               \ +  tt_test_ptr(__FILE__, __LINE__, #A, (OP), #B, (A), (B), (F)) + +#else +  #define TT_TEST_PTR_(A, OP, B, F)                                       \    tt_expect(__FILE__, __LINE__, #A, OP, #B,                             \              (tt_val_t){ TT_VAL_PTR, .value.ptr_val = (A) },             \              (tt_val_t){ TT_VAL_PTR, .value.ptr_val = (B) },             \              (F)) +#endif +  #define TT_EXPECT_PTR_EQ(A, B)                  \    TT_TEST_PTR_((A), TT_OP_PTR_EQ, (B), TT_FALSE) @@ -408,6 +502,17 @@ extern void tt_fail(const char *file, int line, const char *message, ...);  #define TT_ASSERT_FILE_NE(A, B)                         \    TT_TEST_STR_((A), TT_OP_FILE_NE, (B), TT_TRUE) +#define TT_EXPECT_TXTFILE_EQ(A, B)                      \ +  TT_TEST_STR_((A), TT_OP_TXTFILE_EQ, (B), TT_FALSE) + +#define TT_ASSERT_TXTFILE_EQ(A, B)                      \ +  TT_TEST_STR_((A), TT_OP_TXTFILE_EQ, (B), TT_TRUE) + +#define TT_EXPECT_TXTFILE_NE(A, B)                      \ +  TT_TEST_STR_((A), TT_OP_TXTFILE_NE, (B), TT_FALSE) + +#define TT_ASSERT_TXTFILE_NE(A, B)                      \ +  TT_TEST_STR_((A), TT_OP_TXTFILE_NE, (B), TT_TRUE)  #define TT_FAIL(M, ...)                         \    tt_fail(__FILE__, __LINE__, (M), __VA_ARGS__)  | 
