Commit cf94024c589591e9a5c46a71f8fdce9f8b467161

Russell Belfer 2013-09-04T11:42:48

Update clar

diff --git a/tests-clar/checkout/checkout_helpers.c b/tests-clar/checkout/checkout_helpers.c
index 8da024d..f55f7b6 100644
--- a/tests-clar/checkout/checkout_helpers.c
+++ b/tests-clar/checkout/checkout_helpers.c
@@ -74,8 +74,8 @@ static void check_file_contents_internal(
 	if (strip_cr)
 		strip_cr_from_buf(&buf);
 
-	clar__assert_equal_i((int)expected_len, (int)buf.size, file, line, "strlen(expected_content) != strlen(actual_content)", 1);
-	clar__assert_equal_s(expected_content, buf.ptr, file, line, msg, 1);
+	clar__assert_equal(file, line, "strlen(expected_content) != strlen(actual_content)", 1, PRIuZ, expected_len, (size_t)buf.size);
+	clar__assert_equal(file, line, msg, 1, "%s", expected_content, buf.ptr);
 }
 
 void check_file_contents_at_line(
diff --git a/tests-clar/clar.c b/tests-clar/clar.c
index 585af8a..5189e79 100644
--- a/tests-clar/clar.c
+++ b/tests-clar/clar.c
@@ -24,28 +24,59 @@
 
 #	define _MAIN_CC __cdecl
 
-#	define stat(path, st) _stat(path, st)
-#	define mkdir(path, mode) _mkdir(path)
-#	define chdir(path) _chdir(path)
-#	define access(path, mode) _access(path, mode)
-#	define strdup(str) _strdup(str)
-#	define strcasecmp(a,b) _stricmp(a,b)
+#	ifndef stat
+#		define stat(path, st) _stat(path, st)
+#	endif
+#	ifndef mkdir
+#		define mkdir(path, mode) _mkdir(path)
+#	endif
+#	ifndef chdir
+#		define chdir(path) _chdir(path)
+#	endif
+#	ifndef access
+#		define access(path, mode) _access(path, mode)
+#	endif
+#	ifndef strdup
+#		define strdup(str) _strdup(str)
+#	endif
+#	ifndef strcasecmp
+#		define strcasecmp(a,b) _stricmp(a,b)
+#	endif
 
 #	ifndef __MINGW32__
 #		pragma comment(lib, "shell32")
-#		define strncpy(to, from, to_size) strncpy_s(to, to_size, from, _TRUNCATE)
-#		define W_OK 02
-#		define S_ISDIR(x) ((x & _S_IFDIR) != 0)
-#		define snprint_eq(buf,sz,fmt,...) _snprintf_s(buf,sz,_TRUNCATE,fmt,__VA_ARGS__)
+#		ifndef strncpy
+#			define strncpy(to, from, to_size) strncpy_s(to, to_size, from, _TRUNCATE)
+#		endif
+#		ifndef W_OK
+#			define W_OK 02
+#		endif
+#		ifndef S_ISDIR
+#			define S_ISDIR(x) ((x & _S_IFDIR) != 0)
+#		endif
+#		define p_snprintf(buf,sz,fmt,...) _snprintf_s(buf,sz,_TRUNCATE,fmt,__VA_ARGS__)
 #	else
-#		define snprint_eq snprintf
+#		define p_snprintf snprintf
+#	endif
+
+#	ifndef PRIuZ
+#		define PRIuZ "Iu"
+#	endif
+#	ifndef PRIxZ
+#		define PRIxZ "Ix"
 #	endif
 	typedef struct _stat STAT_T;
 #else
 #	include <sys/wait.h> /* waitpid(2) */
 #	include <unistd.h>
 #	define _MAIN_CC
-#	define snprint_eq snprintf
+#	define p_snprintf snprintf
+#	ifndef PRIuZ
+#		define PRIuZ "zu"
+#	endif
+#	ifndef PRIxZ
+#		define PRIxZ "zx"
+#	endif
 	typedef struct stat STAT_T;
 #endif
 
@@ -406,45 +437,66 @@ void clar__assert(
 	clar__fail(file, line, error_msg, description, should_abort);
 }
 
-void clar__assert_equal_s(
-	const char *s1,
-	const char *s2,
+void clar__assert_equal(
 	const char *file,
 	int line,
 	const char *err,
-	int should_abort)
+	int should_abort,
+	const char *fmt,
+	...)
 {
-	int match = (s1 == NULL || s2 == NULL) ? (s1 == s2) : (strcmp(s1, s2) == 0);
-
-	if (!match) {
-		char buf[4096];
-
-		if (s1 && s2) {
-			int pos;
-			for (pos = 0; s1[pos] == s2[pos] && s1[pos] && s2[pos]; ++pos)
-				/* find differing byte offset */;
-			snprint_eq(buf, sizeof(buf), "'%s' != '%s' (at byte %d)", s1, s2, pos);
-		} else {
-			snprint_eq(buf, sizeof(buf), "'%s' != '%s'", s1, s2);
+	va_list args;
+	char buf[4096];
+	int is_equal = 1;
+
+	va_start(args, fmt);
+
+	if (!strcmp("%s", fmt)) {
+		const char *s1 = va_arg(args, const char *);
+		const char *s2 = va_arg(args, const char *);
+		is_equal = (!s1 || !s2) ? (s1 == s2) : !strcmp(s1, s2);
+
+		if (!is_equal) {
+			if (s1 && s2) {
+				int pos;
+				for (pos = 0; s1[pos] == s2[pos] && s1[pos] && s2[pos]; ++pos)
+					/* find differing byte offset */;
+				p_snprintf(buf, sizeof(buf), "'%s' != '%s' (at byte %d)",
+					s1, s2, pos);
+			} else {
+				p_snprintf(buf, sizeof(buf), "'%s' != '%s'", s1, s2);
+			}
+		}
+	}
+	else if (!strcmp(PRIuZ, fmt) || !strcmp(PRIxZ, fmt)) {
+		size_t sz1 = va_arg(args, size_t), sz2 = va_arg(args, size_t);
+		is_equal = (sz1 == sz2);
+		if (!is_equal) {
+			int offset = p_snprintf(buf, sizeof(buf), fmt, sz1);
+			strncat(buf, " != ", sizeof(buf) - offset);
+			p_snprintf(buf + offset + 4, sizeof(buf) - offset - 4, fmt, sz2);
+		}
+	}
+	else if (!strcmp("%p", fmt)) {
+		void *p1 = va_arg(args, void *), *p2 = va_arg(args, void *);
+		is_equal = (p1 == p2);
+		if (!is_equal)
+			p_snprintf(buf, sizeof(buf), "%p != %p", p1, p2);
+	}
+	else {
+		int i1 = va_arg(args, int), i2 = va_arg(args, int);
+		is_equal = (i1 == i2);
+		if (!is_equal) {
+			int offset = p_snprintf(buf, sizeof(buf), fmt, i1);
+			strncat(buf, " != ", sizeof(buf) - offset);
+			p_snprintf(buf + offset + 4, sizeof(buf) - offset - 4, fmt, i2);
 		}
-
-		clar__fail(file, line, err, buf, should_abort);
 	}
-}
 
-void clar__assert_equal_i(
-	int i1,
-	int i2,
-	const char *file,
-	int line,
-	const char *err,
-	int should_abort)
-{
-	if (i1 != i2) {
-		char buf[128];
-		snprint_eq(buf, sizeof(buf), "%d != %d", i1, i2);
+	va_end(args);
+
+	if (!is_equal)
 		clar__fail(file, line, err, buf, should_abort);
-	}
 }
 
 void cl_set_cleanup(void (*cleanup)(void *), void *opaque)
diff --git a/tests-clar/clar.h b/tests-clar/clar.h
index d92318b..e1f244e 100644
--- a/tests-clar/clar.h
+++ b/tests-clar/clar.h
@@ -57,15 +57,17 @@ void cl_fixture_cleanup(const char *fixture_name);
 /**
  * Typed assertion macros
  */
-#define cl_assert_equal_s(s1,s2) clar__assert_equal_s((s1),(s2),__FILE__,__LINE__,"String mismatch: " #s1 " != " #s2, 1)
-#define cl_assert_equal_s_(s1,s2,note) clar__assert_equal_s((s1),(s2),__FILE__,__LINE__,"String mismatch: " #s1 " != " #s2 " (" #note ")", 1)
+#define cl_assert_equal_s(s1,s2) clar__assert_equal(__FILE__,__LINE__,"String mismatch: " #s1 " != " #s2, 1, "%s", (s1), (s2))
+#define cl_assert_equal_s_(s1,s2,note) clar__assert_equal(__FILE__,__LINE__,"String mismatch: " #s1 " != " #s2 " (" #note ")", 1, "%s", (s1), (s2))
 
-#define cl_assert_equal_i(i1,i2) clar__assert_equal_i((i1),(i2),__FILE__,__LINE__,#i1 " != " #i2, 1)
-#define cl_assert_equal_i_(i1,i2,note) clar__assert_equal_i((i1),(i2),__FILE__,__LINE__,#i1 " != " #i2 " (" #note ")", 1)
+#define cl_assert_equal_i(i1,i2) clar__assert_equal(__FILE__,__LINE__,#i1 " != " #i2, 1, "%d", (int)(i1), (int)(i2))
+#define cl_assert_equal_i_(i1,i2,note) clar__assert_equal(__FILE__,__LINE__,#i1 " != " #i2 " (" #note ")", 1, "%d", (i1), (i2))
+#define cl_assert_equal_i_fmt(i1,i2,fmt) clar__assert_equal(__FILE__,__LINE__,#i1 " != " #i2, 1, (fmt), (int)(i1), (int)(i2))
 
-#define cl_assert_equal_b(b1,b2) clar__assert_equal_i(!!(b1),!!(b2),__FILE__,__LINE__,#b1 " != " #b2, 1)
+#define cl_assert_equal_b(b1,b2) clar__assert_equal(__FILE__,__LINE__,#b1 " != " #b2, 1, "%d", (int)((b1) != 0),(int)((b2) != 0))
+
+#define cl_assert_equal_p(p1,p2) clar__assert_equal(__FILE__,__LINE__,"Pointer mismatch: " #p1 " != " #p2, 1, "%p", (p1), (p2))
 
-#define cl_assert_equal_p(p1,p2) cl_assert((p1) == (p2))
 
 void clar__fail(
 	const char *file,
@@ -82,7 +84,12 @@ void clar__assert(
 	const char *description,
 	int should_abort);
 
-void clar__assert_equal_s(const char *,const char *,const char *,int,const char *,int);
-void clar__assert_equal_i(int,int,const char *,int,const char *,int);
+void clar__assert_equal(
+	const char *file,
+	int line,
+	const char *err,
+	int should_abort,
+	const char *fmt,
+	...);
 
 #endif
diff --git a/tests-clar/clar/sandbox.h b/tests-clar/clar/sandbox.h
index 1ca6fca..ee75641 100644
--- a/tests-clar/clar/sandbox.h
+++ b/tests-clar/clar/sandbox.h
@@ -43,10 +43,8 @@ find_tmp_path(char *buffer, size_t length)
 	}
 
 #else
-	DWORD env_len;
-
-	if ((env_len = GetEnvironmentVariable("CLAR_TMP", buffer, length)) > 0 &&
-		env_len < length)
+	DWORD env_len = GetEnvironmentVariable("CLAR_TMP", buffer, (DWORD)length);
+	if (env_len > 0 && env_len < (DWORD)length)
 		return 0;
 
 	if (GetTempPath((DWORD)length, buffer))
diff --git a/tests-clar/diff/submodules.c b/tests-clar/diff/submodules.c
index 94804db..9dcf819 100644
--- a/tests-clar/diff/submodules.c
+++ b/tests-clar/diff/submodules.c
@@ -39,8 +39,9 @@ static void check_diff_patches_at_line(
 
 		cl_git_pass(git_diff_patch_to_str(&patch_text, patch));
 
-		clar__assert_equal_s(expected[d], patch_text, file, line,
-			"expected diff did not match actual diff", 1);
+		clar__assert_equal(
+			file, line, "expected diff did not match actual diff", 1,
+			"%s", expected[d], patch_text);
 		git__free(patch_text);
 	}