diff mbox

[08/10] report: add prefix support

Message ID 1418400377-17388-9-git-send-email-drjones@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Andrew Jones Dec. 12, 2014, 4:06 p.m. UTC
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(+)
diff mbox

Patch

diff --git a/lib/libcflat.h b/lib/libcflat.h
index 7db29a4f4f3cb..9747ccdbc9f1d 100644
--- a/lib/libcflat.h
+++ b/lib/libcflat.h
@@ -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);
diff --git a/lib/report.c b/lib/report.c
index 7a9f270b1fafb..dc30250c676d3 100644
--- a/lib/report.c
+++ b/lib/report.c
@@ -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");
diff --git a/lib/string.c b/lib/string.c
index 026f50252287c..e7bcfe945fcfa 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -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;
diff --git a/lib/string.h b/lib/string.h
index dbab368b1b9e4..7820db86ee4e0 100644
--- a/lib/string.h
+++ b/lib/string.h
@@ -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);