aboutsummaryrefslogtreecommitdiff
path: root/t/unit-tests/clar/clar.c
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2025-12-06 12:47:32 +0100
committerJunio C Hamano <gitster@pobox.com>2025-12-07 07:25:15 +0900
commitd5e4aef3586c07c31e3e4d76ce7fdf0f9843314f (patch)
tree5477b82e8ace04f4ea6c450afc19adeb8f465f5a /t/unit-tests/clar/clar.c
parent9a2fb147f2c61d0cab52c883e7e26f5b7948e3ed (diff)
downloadgit-d5e4aef3586c07c31e3e4d76ce7fdf0f9843314f.tar.xz
t/unit-tests: update clar to 39f11fe
Update clar to commit 39f11fe (Merge pull request #131 from pks-gitlab/pks-integer-double-evaluation, 2025-12-05). This commit includes the following changes relevant to Git: - There are now typesafe integer comparison functions. Furthermore, the range of comparison functions has been included to also have relative comparisons, like "greater than". - There is a new `cl_failf()` macro that allows the caller to specify an error message with formatting directives. - The TAP format has been fixed to correctly terminate YAML blocks with "...\n" instead of "---\n". Note that we already had a `cl_failf()` function declared in our own sources. This function is equivalent to the upstreamed function, so we can simply drop it now. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 't/unit-tests/clar/clar.c')
-rw-r--r--t/unit-tests/clar/clar.c146
1 files changed, 141 insertions, 5 deletions
diff --git a/t/unit-tests/clar/clar.c b/t/unit-tests/clar/clar.c
index d6176e50b2..e959a5ae02 100644
--- a/t/unit-tests/clar/clar.c
+++ b/t/unit-tests/clar/clar.c
@@ -24,6 +24,14 @@
#include <sys/types.h>
#include <sys/stat.h>
+#ifndef va_copy
+# ifdef __va_copy
+# define va_copy(dst, src) __va_copy(dst, src)
+# else
+# define va_copy(dst, src) ((dst) = (src))
+# endif
+#endif
+
#if defined(__UCLIBC__) && ! defined(__UCLIBC_HAS_WCHAR__)
/*
* uClibc can optionally be built without wchar support, in which case
@@ -76,8 +84,10 @@
# define S_ISDIR(x) ((x & _S_IFDIR) != 0)
# endif
# define p_snprintf(buf,sz,fmt,...) _snprintf_s(buf,sz,_TRUNCATE,fmt,__VA_ARGS__)
+# define p_vsnprintf _vsnprintf
# else
# define p_snprintf snprintf
+# define p_vsnprintf vsnprintf
# endif
# define localtime_r(timer, buf) (localtime_s(buf, timer) == 0 ? buf : NULL)
@@ -86,6 +96,7 @@
# include <unistd.h>
# define _MAIN_CC
# define p_snprintf snprintf
+# define p_vsnprintf vsnprintf
typedef struct stat STAT_T;
#endif
@@ -699,13 +710,14 @@ void clar__skip(void)
abort_test();
}
-void clar__fail(
+static void clar__failv(
const char *file,
const char *function,
size_t line,
+ int should_abort,
const char *error_msg,
const char *description,
- int should_abort)
+ va_list args)
{
struct clar_error *error;
@@ -725,9 +737,19 @@ void clar__fail(
error->line_number = _clar.invoke_line ? _clar.invoke_line : line;
error->error_msg = error_msg;
- if (description != NULL &&
- (error->description = strdup(description)) == NULL)
- clar_abort("Failed to allocate description.\n");
+ if (description != NULL) {
+ va_list args_copy;
+ int len;
+
+ va_copy(args_copy, args);
+ if ((len = p_vsnprintf(NULL, 0, description, args_copy)) < 0)
+ clar_abort("Failed to compute description.");
+ va_end(args_copy);
+
+ if ((error->description = calloc(1, len + 1)) == NULL)
+ clar_abort("Failed to allocate buffer.");
+ p_vsnprintf(error->description, len + 1, description, args);
+ }
_clar.total_errors++;
_clar.last_report->status = CL_TEST_FAILURE;
@@ -736,6 +758,34 @@ void clar__fail(
abort_test();
}
+void clar__failf(
+ const char *file,
+ const char *function,
+ size_t line,
+ int should_abort,
+ const char *error_msg,
+ const char *description,
+ ...)
+{
+ va_list args;
+ va_start(args, description);
+ clar__failv(file, function, line, should_abort, error_msg,
+ description, args);
+ va_end(args);
+}
+
+void clar__fail(
+ const char *file,
+ const char *function,
+ size_t line,
+ const char *error_msg,
+ const char *description,
+ int should_abort)
+{
+ clar__failf(file, function, line, should_abort, error_msg,
+ description ? "%s" : NULL, description);
+}
+
void clar__assert(
int condition,
const char *file,
@@ -889,6 +939,92 @@ void clar__assert_equal(
clar__fail(file, function, line, err, buf, should_abort);
}
+void clar__assert_compare_i(
+ const char *file,
+ const char *func,
+ size_t line,
+ int should_abort,
+ enum clar_comparison cmp,
+ intmax_t value1,
+ intmax_t value2,
+ const char *error,
+ const char *description,
+ ...)
+{
+ int fulfilled;
+ switch (cmp) {
+ case CLAR_COMPARISON_EQ:
+ fulfilled = value1 == value2;
+ break;
+ case CLAR_COMPARISON_LT:
+ fulfilled = value1 < value2;
+ break;
+ case CLAR_COMPARISON_LE:
+ fulfilled = value1 <= value2;
+ break;
+ case CLAR_COMPARISON_GT:
+ fulfilled = value1 > value2;
+ break;
+ case CLAR_COMPARISON_GE:
+ fulfilled = value1 >= value2;
+ break;
+ default:
+ cl_assert(0);
+ return;
+ }
+
+ if (!fulfilled) {
+ va_list args;
+ va_start(args, description);
+ clar__failv(file, func, line, should_abort, error,
+ description, args);
+ va_end(args);
+ }
+}
+
+void clar__assert_compare_u(
+ const char *file,
+ const char *func,
+ size_t line,
+ int should_abort,
+ enum clar_comparison cmp,
+ uintmax_t value1,
+ uintmax_t value2,
+ const char *error,
+ const char *description,
+ ...)
+{
+ int fulfilled;
+ switch (cmp) {
+ case CLAR_COMPARISON_EQ:
+ fulfilled = value1 == value2;
+ break;
+ case CLAR_COMPARISON_LT:
+ fulfilled = value1 < value2;
+ break;
+ case CLAR_COMPARISON_LE:
+ fulfilled = value1 <= value2;
+ break;
+ case CLAR_COMPARISON_GT:
+ fulfilled = value1 > value2;
+ break;
+ case CLAR_COMPARISON_GE:
+ fulfilled = value1 >= value2;
+ break;
+ default:
+ cl_assert(0);
+ return;
+ }
+
+ if (!fulfilled) {
+ va_list args;
+ va_start(args, description);
+ clar__failv(file, func, line, should_abort, error,
+ description, args);
+ va_end(args);
+ }
+}
+
void cl_set_cleanup(void (*cleanup)(void *), void *opaque)
{
_clar.local_cleanup = cleanup;