summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpaul <paul>2005-04-13 03:31:35 +0000
committerpaul <paul>2005-04-13 03:31:35 +0000
commit4dcadf7efd58e9d91a52c5f06c02cb40bbf21823 (patch)
tree5968d1e9773c79cfe0db364ce91314087cb57f6e
parent5d6e26910b6790e7ea759893c16e9f0e380fcc1e (diff)
2004-05-13 Paul Jakma <paul@dishone.st>
* test-buffer.c: Andrew's buffer tester * test-memory.c: basic memory tester * Makefile.am: Add new tests
-rw-r--r--tests/.cvsignore3
-rw-r--r--tests/Makefile.am7
-rw-r--r--tests/test-buffer.c35
-rw-r--r--tests/test-memory.c103
4 files changed, 146 insertions, 2 deletions
diff --git a/tests/.cvsignore b/tests/.cvsignore
index 9e398765..4c4801e3 100644
--- a/tests/.cvsignore
+++ b/tests/.cvsignore
@@ -11,3 +11,6 @@ TAGS
testsig
.arch-inventory
.arch-ids
+testbuffer
+testmemory
+testsig
diff --git a/tests/Makefile.am b/tests/Makefile.am
index d8c73eba..c3fac7c0 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,9 +1,12 @@
INCLUDES = @INCLUDES@ -I.. -I$(top_srcdir) -I$(top_srcdir)/lib
DEFS = @DEFS@ $(LOCAL_OPTS) -DSYSCONFDIR=\"$(sysconfdir)/\"
-noinst_PROGRAMS = testsig
+noinst_PROGRAMS = testsig testbuffer testmemory
testsig_SOURCES = test-sig.c
+testbuffer_SOURCES = test-buffer.c
+testmemory_SOURCES = test-memory.c
testsig_LDADD = ../lib/libzebra.la @LIBCAP@
-
+testbuffer_LDADD = ../lib/libzebra.la @LIBCAP@
+testmemory_LDADD = ../lib/libzebra.la @LIBCAP@
diff --git a/tests/test-buffer.c b/tests/test-buffer.c
new file mode 100644
index 00000000..11e36294
--- /dev/null
+++ b/tests/test-buffer.c
@@ -0,0 +1,35 @@
+#include <zebra.h>
+#include <buffer.h>
+
+struct thread_master *master;
+
+int
+main(int argc, char **argv)
+{
+ struct buffer *b1, *b2;
+ int n;
+ char junk[3];
+ char c = 'a';
+
+ if ((argc != 2) || (sscanf(argv[1], "%d%1s", &n, junk) != 1))
+ {
+ fprintf(stderr, "Usage: %s <number of chars to simulate>\n", *argv);
+ return 1;
+ }
+
+ b1 = buffer_new(0);
+ b2 = buffer_new(1024);
+
+ while (n-- > 0)
+ {
+ buffer_put(b1, &c, 1);
+ buffer_put(b2, &c, 1);
+ if (c++ == 'z')
+ c = 'a';
+ buffer_reset(b1);
+ buffer_reset(b2);
+ }
+ buffer_free(b1);
+ buffer_free(b2);
+ return 0;
+}
diff --git a/tests/test-memory.c b/tests/test-memory.c
new file mode 100644
index 00000000..2971160b
--- /dev/null
+++ b/tests/test-memory.c
@@ -0,0 +1,103 @@
+#include <zebra.h>
+#include <memory.h>
+
+/* Memory torture tests
+ *
+ * Tests below are generic but comments are focused on interaction with
+ * Paul's proposed memory 'quick' cache, which may never be included in
+ * CVS
+ */
+
+struct thread_master *master;
+
+#if 0 /* set to 1 to use system alloc directly */
+#undef XMALLOC
+#undef XCALLOC
+#undef XREALLOC
+#undef XFREE
+#define XMALLOC(T,S) malloc((S))
+#define XCALLOC(T,S) calloc(1, (S))
+#define XREALLOC(T,P,S) realloc((P),(S))
+#define XFREE(T,P) free((P))
+#endif
+
+#define TIMES 10
+
+int
+main(int argc, char **argv)
+{
+ void *a[10];
+ int i;
+
+ printf ("malloc x, malloc x, free, malloc x, free free\n\n");
+ /* simple case, test cache */
+ for (i = 0; i < TIMES; i++)
+ {
+ a[0] = XMALLOC (MTYPE_VTY, 1024);
+ memset (a[0], 1, 1024);
+ a[1] = XMALLOC (MTYPE_VTY, 1024);
+ memset (a[1], 1, 1024);
+ XFREE(MTYPE_VTY, a[0]); /* should go to cache */
+ a[0] = XMALLOC (MTYPE_VTY, 1024); /* should be satisfied from cache */
+ XFREE(MTYPE_VTY, a[0]);
+ XFREE(MTYPE_VTY, a[1]);
+ }
+
+ printf ("malloc x, malloc y, free x, malloc y, free free\n\n");
+ /* cache should go invalid, valid, invalid, etc.. */
+ for (i = 0; i < TIMES; i++)
+ {
+ a[0] = XMALLOC (MTYPE_VTY, 512);
+ memset (a[0], 1, 512);
+ a[1] = XMALLOC (MTYPE_VTY, 1024); /* invalidate cache */
+ memset (a[1], 1, 1024);
+ XFREE(MTYPE_VTY, a[0]);
+ a[0] = XMALLOC (MTYPE_VTY, 1024);
+ XFREE(MTYPE_VTY, a[0]);
+ XFREE(MTYPE_VTY, a[1]);
+ /* cache should become valid again on next request */
+ }
+
+ printf ("calloc\n\n");
+ /* test calloc */
+ for (i = 0; i < TIMES; i++)
+ {
+ a[0] = XCALLOC (MTYPE_VTY, 1024);
+ memset (a[0], 1, 1024);
+ a[1] = XCALLOC (MTYPE_VTY, 512); /* invalidate cache */
+ memset (a[1], 1, 512);
+ XFREE(MTYPE_VTY, a[1]);
+ XFREE(MTYPE_VTY, a[0]);
+ /* alloc == 0, cache can become valid again on next request */
+ }
+
+ printf ("calloc and realloc\n\n");
+ /* check calloc + realloc */
+ for (i = 0; i < TIMES; i++)
+ {
+ printf ("calloc a0 1024\n");
+ a[0] = XCALLOC (MTYPE_VTY, 1024);
+ memset (a[0], 1, 1024/2);
+
+ printf ("calloc 1 1024\n");
+ a[1] = XCALLOC (MTYPE_VTY, 1024);
+ memset (a[1], 1, 1024/2);
+
+ printf ("realloc 0 1024\n");
+ a[3] = XREALLOC (MTYPE_VTY, a[0], 2048); /* invalidate cache */
+ if (a[3] != NULL)
+ a[0] = a[3];
+ memset (a[0], 1, 1024);
+
+ printf ("calloc 2 512\n");
+ a[2] = XCALLOC (MTYPE_VTY, 512);
+ memset (a[2], 1, 512);
+
+ printf ("free 1 0 2\n");
+ XFREE(MTYPE_VTY, a[1]);
+ XFREE(MTYPE_VTY, a[0]);
+ XFREE(MTYPE_VTY, a[2]);
+ /* alloc == 0, cache valid next request */
+ }
+ return 0;
+}