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/tinytest.c | |
parent | 429e46051dba814e7d6c74368eb1bba550222cbe (diff) | |
download | libconfig-f2499612c5594944d3e0891259859668d35c85b2.tar.gz |
Imported Upstream version 1.4.9upstream/1.4.9
Diffstat (limited to 'tinytest/tinytest.c')
-rw-r--r-- | tinytest/tinytest.c | 217 |
1 files changed, 216 insertions, 1 deletions
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 */ |