Skip to content

Commit bb52e94

Browse files
author
Vicent Martí
committed
Merge pull request clar-test#40 from arrbee/more-assert-equal-fmts
More flexibility with cl_assert_equal_...
2 parents 7bf638b + c6e8e79 commit bb52e94

File tree

5 files changed

+140
-61
lines changed

5 files changed

+140
-61
lines changed

clar.c

Lines changed: 95 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,59 @@
2424

2525
# define _MAIN_CC __cdecl
2626

27-
# define stat(path, st) _stat(path, st)
28-
# define mkdir(path, mode) _mkdir(path)
29-
# define chdir(path) _chdir(path)
30-
# define access(path, mode) _access(path, mode)
31-
# define strdup(str) _strdup(str)
32-
# define strcasecmp(a,b) _stricmp(a,b)
27+
# ifndef stat
28+
# define stat(path, st) _stat(path, st)
29+
# endif
30+
# ifndef mkdir
31+
# define mkdir(path, mode) _mkdir(path)
32+
# endif
33+
# ifndef chdir
34+
# define chdir(path) _chdir(path)
35+
# endif
36+
# ifndef access
37+
# define access(path, mode) _access(path, mode)
38+
# endif
39+
# ifndef strdup
40+
# define strdup(str) _strdup(str)
41+
# endif
42+
# ifndef strcasecmp
43+
# define strcasecmp(a,b) _stricmp(a,b)
44+
# endif
3345

3446
# ifndef __MINGW32__
3547
# pragma comment(lib, "shell32")
36-
# define strncpy(to, from, to_size) strncpy_s(to, to_size, from, _TRUNCATE)
37-
# define W_OK 02
38-
# define S_ISDIR(x) ((x & _S_IFDIR) != 0)
39-
# define snprint_eq(buf,sz,fmt,...) _snprintf_s(buf,sz,_TRUNCATE,fmt,__VA_ARGS__)
48+
# ifndef strncpy
49+
# define strncpy(to, from, to_size) strncpy_s(to, to_size, from, _TRUNCATE)
50+
# endif
51+
# ifndef W_OK
52+
# define W_OK 02
53+
# endif
54+
# ifndef S_ISDIR
55+
# define S_ISDIR(x) ((x & _S_IFDIR) != 0)
56+
# endif
57+
# define p_snprintf(buf,sz,fmt,...) _snprintf_s(buf,sz,_TRUNCATE,fmt,__VA_ARGS__)
4058
# else
41-
# define snprint_eq snprintf
59+
# define p_snprintf snprintf
60+
# endif
61+
62+
# ifndef PRIuZ
63+
# define PRIuZ "Iu"
64+
# endif
65+
# ifndef PRIxZ
66+
# define PRIxZ "Ix"
4267
# endif
4368
typedef struct _stat STAT_T;
4469
#else
4570
# include <sys/wait.h> /* waitpid(2) */
4671
# include <unistd.h>
4772
# define _MAIN_CC
48-
# define snprint_eq snprintf
73+
# define p_snprintf snprintf
74+
# ifndef PRIuZ
75+
# define PRIuZ "zu"
76+
# endif
77+
# ifndef PRIxZ
78+
# define PRIxZ "zx"
79+
# endif
4980
typedef struct stat STAT_T;
5081
#endif
5182

@@ -406,45 +437,66 @@ void clar__assert(
406437
clar__fail(file, line, error_msg, description, should_abort);
407438
}
408439

409-
void clar__assert_equal_s(
410-
const char *s1,
411-
const char *s2,
440+
void clar__assert_equal(
412441
const char *file,
413442
int line,
414443
const char *err,
415-
int should_abort)
444+
int should_abort,
445+
const char *fmt,
446+
...)
416447
{
417-
int match = (s1 == NULL || s2 == NULL) ? (s1 == s2) : (strcmp(s1, s2) == 0);
418-
419-
if (!match) {
420-
char buf[4096];
421-
422-
if (s1 && s2) {
423-
int pos;
424-
for (pos = 0; s1[pos] == s2[pos] && s1[pos] && s2[pos]; ++pos)
425-
/* find differing byte offset */;
426-
snprint_eq(buf, sizeof(buf), "'%s' != '%s' (at byte %d)", s1, s2, pos);
427-
} else {
428-
snprint_eq(buf, sizeof(buf), "'%s' != '%s'", s1, s2);
448+
va_list args;
449+
char buf[4096];
450+
int is_equal = 1;
451+
452+
va_start(args, fmt);
453+
454+
if (!strcmp("%s", fmt)) {
455+
const char *s1 = va_arg(args, const char *);
456+
const char *s2 = va_arg(args, const char *);
457+
is_equal = (!s1 || !s2) ? (s1 == s2) : !strcmp(s1, s2);
458+
459+
if (!is_equal) {
460+
if (s1 && s2) {
461+
int pos;
462+
for (pos = 0; s1[pos] == s2[pos] && s1[pos] && s2[pos]; ++pos)
463+
/* find differing byte offset */;
464+
p_snprintf(buf, sizeof(buf), "'%s' != '%s' (at byte %d)",
465+
s1, s2, pos);
466+
} else {
467+
p_snprintf(buf, sizeof(buf), "'%s' != '%s'", s1, s2);
468+
}
469+
}
470+
}
471+
else if (!strcmp(PRIuZ, fmt) || !strcmp(PRIxZ, fmt)) {
472+
size_t sz1 = va_arg(args, size_t), sz2 = va_arg(args, size_t);
473+
is_equal = (sz1 == sz2);
474+
if (!is_equal) {
475+
int offset = p_snprintf(buf, sizeof(buf), fmt, sz1);
476+
strncat(buf, " != ", sizeof(buf) - offset);
477+
p_snprintf(buf + offset + 4, sizeof(buf) - offset - 4, fmt, sz2);
478+
}
479+
}
480+
else if (!strcmp("%p", fmt)) {
481+
void *p1 = va_arg(args, void *), *p2 = va_arg(args, void *);
482+
is_equal = (p1 == p2);
483+
if (!is_equal)
484+
p_snprintf(buf, sizeof(buf), "%p != %p", p1, p2);
485+
}
486+
else {
487+
int i1 = va_arg(args, int), i2 = va_arg(args, int);
488+
is_equal = (i1 == i2);
489+
if (!is_equal) {
490+
int offset = p_snprintf(buf, sizeof(buf), fmt, i1);
491+
strncat(buf, " != ", sizeof(buf) - offset);
492+
p_snprintf(buf + offset + 4, sizeof(buf) - offset - 4, fmt, i2);
429493
}
430-
431-
clar__fail(file, line, err, buf, should_abort);
432494
}
433-
}
434495

435-
void clar__assert_equal_i(
436-
int i1,
437-
int i2,
438-
const char *file,
439-
int line,
440-
const char *err,
441-
int should_abort)
442-
{
443-
if (i1 != i2) {
444-
char buf[128];
445-
snprint_eq(buf, sizeof(buf), "%d != %d", i1, i2);
496+
va_end(args);
497+
498+
if (!is_equal)
446499
clar__fail(file, line, err, buf, should_abort);
447-
}
448500
}
449501

450502
void cl_set_cleanup(void (*cleanup)(void *), void *opaque)

clar.h

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,17 @@ void cl_fixture_cleanup(const char *fixture_name);
5757
/**
5858
* Typed assertion macros
5959
*/
60-
#define cl_assert_equal_s(s1,s2) clar__assert_equal_s((s1),(s2),__FILE__,__LINE__,"String mismatch: " #s1 " != " #s2, 1)
61-
#define cl_assert_equal_s_(s1,s2,note) clar__assert_equal_s((s1),(s2),__FILE__,__LINE__,"String mismatch: " #s1 " != " #s2 " (" #note ")", 1)
60+
#define cl_assert_equal_s(s1,s2) clar__assert_equal(__FILE__,__LINE__,"String mismatch: " #s1 " != " #s2, 1, "%s", (s1), (s2))
61+
#define cl_assert_equal_s_(s1,s2,note) clar__assert_equal(__FILE__,__LINE__,"String mismatch: " #s1 " != " #s2 " (" #note ")", 1, "%s", (s1), (s2))
6262

63-
#define cl_assert_equal_i(i1,i2) clar__assert_equal_i((i1),(i2),__FILE__,__LINE__,#i1 " != " #i2, 1)
64-
#define cl_assert_equal_i_(i1,i2,note) clar__assert_equal_i((i1),(i2),__FILE__,__LINE__,#i1 " != " #i2 " (" #note ")", 1)
63+
#define cl_assert_equal_i(i1,i2) clar__assert_equal(__FILE__,__LINE__,#i1 " != " #i2, 1, "%d", (int)(i1), (int)(i2))
64+
#define cl_assert_equal_i_(i1,i2,note) clar__assert_equal(__FILE__,__LINE__,#i1 " != " #i2 " (" #note ")", 1, "%d", (i1), (i2))
65+
#define cl_assert_equal_i_fmt(i1,i2,fmt) clar__assert_equal(__FILE__,__LINE__,#i1 " != " #i2, 1, (fmt), (int)(i1), (int)(i2))
6566

66-
#define cl_assert_equal_b(b1,b2) clar__assert_equal_i(!!(b1),!!(b2),__FILE__,__LINE__,#b1 " != " #b2, 1)
67+
#define cl_assert_equal_b(b1,b2) clar__assert_equal(__FILE__,__LINE__,#b1 " != " #b2, 1, "%d", (int)((b1) != 0),(int)((b2) != 0))
68+
69+
#define cl_assert_equal_p(p1,p2) clar__assert_equal(__FILE__,__LINE__,"Pointer mismatch: " #p1 " != " #p2, 1, "%p", (p1), (p2))
6770

68-
#define cl_assert_equal_p(p1,p2) cl_assert((p1) == (p2))
6971

7072
void clar__fail(
7173
const char *file,
@@ -82,7 +84,12 @@ void clar__assert(
8284
const char *description,
8385
int should_abort);
8486

85-
void clar__assert_equal_s(const char *,const char *,const char *,int,const char *,int);
86-
void clar__assert_equal_i(int,int,const char *,int,const char *,int);
87+
void clar__assert_equal(
88+
const char *file,
89+
int line,
90+
const char *err,
91+
int should_abort,
92+
const char *fmt,
93+
...);
8794

8895
#endif

clar/sandbox.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@ find_tmp_path(char *buffer, size_t length)
4343
}
4444

4545
#else
46-
DWORD env_len;
47-
48-
if ((env_len = GetEnvironmentVariable("CLAR_TMP", buffer, length)) > 0 &&
49-
env_len < length)
46+
DWORD env_len = GetEnvironmentVariable("CLAR_TMP", buffer, (DWORD)length);
47+
if (env_len > 0 && env_len < (DWORD)length)
5048
return 0;
5149

5250
if (GetTempPath((DWORD)length, buffer))

test/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ int main(int argc, char *argv[])
3434
ret = clar_test(argc, argv);
3535

3636
/* Your custom cleanup here */
37-
cl_assert_equal_i(3, global_test_counter);
37+
cl_assert_equal_i(7, global_test_counter);
3838

3939
return ret;
4040
}

test/sample.c

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ void test_sample__1(void)
2727
cl_assert(1);
2828
cl_must_pass(0); /* 0 == success */
2929
cl_must_fail(-1); /* <0 == failure */
30+
cl_must_pass(-1); /* demonstrate a failing call */
3031
}
3132

3233
void test_sample__2(void)
@@ -35,19 +36,40 @@ void test_sample__2(void)
3536

3637
cl_assert(file_size("test/nonexistent") == -1);
3738
cl_assert(file_size("test/file") > 0);
39+
cl_assert(100 == 101);
3840
}
3941

40-
void test_sample__3(void)
42+
void test_sample__strings(void)
4143
{
4244
const char *actual = "expected";
43-
int value = 100;
44-
4545
cl_assert_equal_s("expected", actual);
4646
cl_assert_equal_s_("expected", actual, "second try with annotation");
47+
cl_assert_equal_s_("mismatched", actual, "this one fails");
48+
}
4749

50+
void test_sample__int(void)
51+
{
52+
int value = 100;
4853
cl_assert_equal_i(100, value);
49-
cl_assert_equal_i_(101, value, "extra message");
54+
cl_assert_equal_i_(101, value, "extra note on failing test");
55+
}
56+
57+
void test_sample__int_fmt(void)
58+
{
59+
int value = 100;
60+
cl_assert_equal_i_fmt(022, value, "%04o");
61+
}
62+
63+
void test_sample__bool(void)
64+
{
65+
int value = 100;
66+
cl_assert_equal_b(1, value); /* test equality as booleans */
67+
cl_assert_equal_b(0, value);
68+
}
5069

51-
cl_assert_equal_b(1, value); /* equal as booleans */
70+
void test_sample__ptr(void)
71+
{
72+
const char *actual = "expected";
5273
cl_assert_equal_p(actual, actual); /* pointers to same object */
74+
cl_assert_equal_p(&actual, actual);
5375
}

0 commit comments

Comments
 (0)