All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC][PATCH] Basic unit testing support for GRUB
@ 2009-11-13 17:45 BVK
  2009-11-16 21:21 ` Robert Millan
  2009-11-25 16:34 ` Felix Zielcke
  0 siblings, 2 replies; 7+ messages in thread
From: BVK @ 2009-11-13 17:45 UTC (permalink / raw)
  To: The development of GRUB 2

[-- Attachment #1: Type: text/plain, Size: 577 bytes --]

Hi,


Attached is the (experimental branch) patch for unit testing that I
have in mind for GRUB.  Please let me know your comments.

It provides "make check" target which would build and execute test
programs from "tests" directory.  We can write small programs to test
specific functions in GRUB.  For example, attached patch has sample
program comparing grub_sprintf behavior against standard sprintf
output (from glibc).

Note that it currently has few gotchas, like using TARGET_CC instead
of BUILD_CC, no support for XPASS, XFAIL tests, etc.



regards,
-- 
bvk.chaitanya

[-- Attachment #2: unit-testing-support.patch --]
[-- Type: application/octet-stream, Size: 6127 bytes --]

=== modified file 'Makefile.in'
--- old/Makefile.in	2009-11-09 22:48:53 +0000
+++ new/Makefile.in	2009-11-13 17:03:17 +0000
@@ -125,6 +125,7 @@
 SCRIPTS = $(bin_SCRIPTS) $(sbin_SCRIPTS) $(grub-mkconfig_SCRIPTS) \
 	$(lib_SCRIPTS)
 INFOS = $(info_INFOS)
+TESTS = $(check_PROGRAMS) $(check_SCRIPTS)
 
 CLEANFILES =
 MOSTLYCLEANFILES =
@@ -151,6 +152,9 @@
 include $(srcdir)/conf/$(target_cpu)-$(platform).mk
 endif
 
+# for tests
+include $(srcdir)/conf/tests.mk
+
 # For external modules.
 -include $(wildcard $(GRUB_CONTRIB)/*/conf/common.mk)
 
@@ -428,7 +432,15 @@
 	@echo "$(distdir).tar.gz is ready for distribution" | \
 	  sed 'h;s/./=/g;p;x;p;x'
 
-check:
+check: $(TESTS)
+	@list='$(TESTS)'; \
+	for file in $$list; do \
+	  if $(builddir)/$$file; then \
+	    echo "$$file: PASS"; \
+	  else \
+	    echo "$$file: FAIL"; \
+	  fi; \
+	done
 
 .SUFFIX:
 .SUFFIX: .c .o .S .d

=== added file 'conf/tests.rmk'
--- old/conf/tests.rmk	1970-01-01 00:00:00 +0000
+++ new/conf/tests.rmk	2009-11-13 17:31:12 +0000
@@ -0,0 +1,6 @@
+# -*- makefile -*-
+
+check_PROGRAMS += grub_sprintf
+grub_sprintf_SOURCES = tests/grub_sprintf.c tests/misc.c kern/misc.c
+grub_sprintf_CFLAGS = -Wno-error -Wno-format
+

=== added directory 'tests'
=== added file 'tests/grub_sprintf.c'
--- old/tests/grub_sprintf.c	1970-01-01 00:00:00 +0000
+++ new/tests/grub_sprintf.c	2009-11-13 17:34:00 +0000
@@ -0,0 +1,116 @@
+#include <stdio.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <grub/misc.h>
+#include "test.h"
+
+#define TEST(type,fmt,val)				\
+  do {							\
+    int r1, r2;						\
+    char b1[1024];					\
+    char b2[1024];					\
+							\
+    b1[0] = b2[0] = '\0';				\
+    r1 = sprintf(b1, fmt, val);				\
+    r2 = grub_sprintf(b2, fmt, val);			\
+							\
+    if ((strcmp(b1, b2) != 0) || (r1 != r2)) {		\
+      printf("for (\"%s\","type") "			\
+	     "result should be (\"%s\",%d) "		\
+	     "but got (\"%s\",%d)\n",			\
+	     fmt, val, b1, r1, b2, r2);			\
+      exit(1);						\
+    }							\
+							\
+  } while (0)
+
+#define D(fmt,v) TEST("%20d",fmt,v)
+#define U(fmt,v) TEST("%20u",fmt,v)
+#define x(fmt,v) TEST("%20x",fmt,v)
+#define X(fmt,v) TEST("%20X",fmt,v)
+#define P(fmt,v) TEST("%20p",fmt,v)
+#define S(fmt,s) TEST("%20s",fmt,s)
+
+static void
+sprintf_checks(void)
+{
+  D("%d",   -1); D("%d",   0); D("%d", 1); 
+  D("%5d",  -1); D("%5d",  0); D("%5d", 1);
+  D("%-5d", -1); D("%-5d", 0); D("%-5d", 1);
+  D("%.5d", -1); D("%.5d", 0); D("%.5d", 1);
+  D("%5.0d", -1); D("%5.0d", 0); D("%5.0d", 1);
+  D("%-5.0d", -1); D("%-5.0d", 0); D("%-5.0d", 1);
+
+  U("%d",   -1); U("%d",   0); U("%d", 1); 
+  U("%5d",  -1); U("%5d",  0); U("%5d", 1);
+  U("%-5d", -1); U("%-5d", 0); U("%-5d", 1);
+  U("%.5d", -1); U("%.5d", 0); U("%.5d", 1);
+  U("%5.0d", -1); U("%5.0d", 0); U("%5.0d", 1);
+  U("%-5.0d", -1); U("%-5.0d", 0); U("%-5.0d", 1);
+
+  x("%d",   -1); x("%d",   0); x("%d", 1); 
+  x("%5d",  -1); x("%5d",  0); x("%5d", 1);
+  x("%-5d", -1); x("%-5d", 0); x("%-5d", 1);
+  x("%.5d", -1); x("%.5d", 0); x("%.5d", 1);
+  x("%5.0d", -1); x("%5.0d", 0); x("%5.0d", 1);
+  x("%-5.0d", -1); x("%-5.0d", 0); x("%-5.0d", 1);
+
+  X("%d",   -1); X("%d",   0); X("%d", 1); 
+  X("%5d",  -1); X("%5d",  0); X("%5d", 1);
+  X("%-5d", -1); X("%-5d", 0); X("%-5d", 1);
+  X("%.5d", -1); X("%.5d", 0); X("%.5d", 1);
+  X("%5.0d", -1); X("%5.0d", 0); X("%5.0d", 1);
+  X("%-5.0d", -1); X("%-5.0d", 0); X("%-5.0d", 1);
+
+  P("%p", NULL);
+  P("%p", sprintf_checks);
+
+  S("%s", (char*)NULL);
+  S("%s", "abcd");
+  S("%10s", "abcd");
+  S("%10.5s", "abcdefgh");
+  S("%10.5s", "ab");
+  S("%2.5s", "a");
+  S("%2.5s", "abcdefgh");
+
+  D("%4.2d",1);
+  D("%4.2d",12);
+  D("%4.2d",123);
+  D("%4.2d",1234);
+  D("%4.2d",12345);
+  D("%3.3d",12);
+  D("%3.3d",123);
+  D("%3.3d",1234);
+  D("%2.4d",12345);
+  D("%2.4d",1234);
+  D("%2.4d",123);
+  D("%2.4d",12);
+  D("%2.4d",1);
+  D("%.0d",0);
+  D("%.0d",1);
+
+  S("%4.2s","1");
+  S("%4.2s","12");
+  S("%4.2s","123");
+  S("%4.2s","1234");
+  S("%4.2s","12345");
+  S("%3.3s","12");
+  S("%3.3s","123");
+  S("%3.3s","1234");
+  S("%2.4s","12345");
+  S("%2.4s","1234");
+  S("%2.4s","123");
+  S("%2.4s","12");
+  S("%2.4s","1");
+
+  // add few more here, if necessary
+}
+
+int
+main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))
+{
+  sprintf_checks();
+  return 0;
+}

=== added file 'tests/misc.c'
--- old/tests/misc.c	1970-01-01 00:00:00 +0000
+++ new/tests/misc.c	2009-11-13 17:00:55 +0000
@@ -0,0 +1,56 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+
+#include "test.h"
+
+char *
+grub_env_get (const char *name __attribute__ ((unused)))
+{
+  return NULL;
+}
+
+grub_err_t
+grub_error (grub_err_t n, const char *fmt, ...)
+{
+  va_list ap;
+
+  va_start (ap, fmt);
+  vfprintf (stderr, fmt, ap);
+  va_end (ap);
+
+  return n;
+}
+
+void *
+grub_malloc (grub_size_t size)
+{
+  return malloc (size);
+}
+
+void
+grub_refresh (void)
+{
+  fflush (stdout);
+}
+
+void
+grub_putchar (int c)
+{
+  putchar (c);
+}
+
+int
+grub_getkey (void)
+{
+  return -1;
+}
+
+void
+grub_exit (void)
+{
+  exit (1);
+}
+
+struct grub_handler_class grub_term_input_class;
+struct grub_handler_class grub_term_output_class;

=== added file 'tests/test.h'
--- old/tests/test.h	1970-01-01 00:00:00 +0000
+++ new/tests/test.h	2009-11-13 17:35:28 +0000
@@ -0,0 +1,22 @@
+#ifndef GRUB_TEST_HEADER
+#define GRUB_TEST_HEADER
+
+#include <grub/err.h>
+#include <grub/types.h>
+#include <grub/handler.h>
+
+// Below functions are necessary for successfully linking tests.  We
+// define dummy versions to keep the linker from complaining.
+
+char *     grub_env_get (const char *name __attribute__ ((unused)));
+grub_err_t grub_error   (grub_err_t n, const char *fmt, ...);
+void *     grub_malloc  (grub_size_t size);
+void       grub_refresh (void);
+void       grub_putchar (int c);
+int        grub_getkey  (void);
+void       grub_exit    (void);
+
+extern struct grub_handler_class grub_term_input_class;
+extern struct grub_handler_class grub_term_output_class;
+
+#endif /* ! GRUB_TEST_HEADER */


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2009-12-04  7:15 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-11-13 17:45 [RFC][PATCH] Basic unit testing support for GRUB BVK
2009-11-16 21:21 ` Robert Millan
2009-11-23 15:16   ` BVK
2009-11-23 15:54     ` BVK
2009-11-25 16:34 ` Felix Zielcke
2009-12-03  3:38   ` BVK
2009-12-04  7:15     ` BVK

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.