@@ -57,6 +57,8 @@ extern int snprintf(char *buf, int size, const char *fmt, ...);
extern int vsnprintf(char *buf, int size, const char *fmt, va_list va);
extern long atol(const char *ptr);
+void report_prefix_push(const char *prefix);
+void report_prefix_pop(void);
void report(const char *msg_fmt, bool pass, ...);
void report_xfail(const char *msg_fmt, bool xfail, bool pass, ...);
int report_summary(void);
@@ -13,6 +13,27 @@
#include "libcflat.h"
static unsigned int tests, failures, xfailures;
+static char prefixes[256];
+
+void report_prefix_push(const char *prefix)
+{
+ strcat(prefixes, prefix);
+ strcat(prefixes, ": ");
+}
+
+void report_prefix_pop(void)
+{
+ char *p, *q;
+
+ if (!*prefixes)
+ return;
+
+ for (p = prefixes, q = strstr(p, ": ") + 2;
+ *q;
+ p = q, q = strstr(p, ": ") + 2)
+ ;
+ *p = '\0';
+}
void va_report_xfail(const char *msg_fmt, bool xfail, bool cond, va_list va)
{
@@ -22,6 +43,7 @@ void va_report_xfail(const char *msg_fmt, bool xfail, bool cond, va_list va)
tests++;
printf("%s: ", cond ? pass : fail);
+ puts(prefixes);
vsnprintf(buf, sizeof(buf), msg_fmt, va);
puts(buf);
puts("\n");
@@ -45,6 +45,23 @@ char *strchr(const char *s, int c)
return (char *)s;
}
+char *strstr(const char *s1, const char *s2)
+{
+ size_t l1, l2;
+
+ l2 = strlen(s2);
+ if (!l2)
+ return (char *)s1;
+ l1 = strlen(s1);
+ while (l1 >= l2) {
+ l1--;
+ if (!memcmp(s1, s2, l2))
+ return (char *)s1;
+ s1++;
+ }
+ return NULL;
+}
+
void *memset(void *s, int c, size_t n)
{
size_t i;
@@ -6,6 +6,7 @@ extern char *strcat(char *dest, const char *src);
extern char *strcpy(char *dest, const char *src);
extern int strcmp(const char *a, const char *b);
extern char *strchr(const char *s, int c);
+char *strstr(const char *haystack, const char *needle);
extern void *memset(void *s, int c, size_t n);
extern void *memcpy(void *dest, const void *src, size_t n);
extern int memcmp(const void *s1, const void *s2, size_t n);
Add some methods to report in order to manage output prefixes. Also add strstr to string, as it was useful for implementing report_prefix_pop. Prefixes can be useful, as test code frequently has the following pattern main() { foreach(subtest) run(subtest) } But, if we want output lines to include 'subtest: ', then we need report("%s: ...", cond, subtest, ...) for each report line. With push/pop we can now just do subtest() { report_prefix_push(subtest); report(...); ... report(...); report_prefix_pop(); } Signed-off-by: Andrew Jones <drjones@redhat.com> --- lib/libcflat.h | 2 ++ lib/report.c | 22 ++++++++++++++++++++++ lib/string.c | 17 +++++++++++++++++ lib/string.h | 1 + 4 files changed, 42 insertions(+)