summaryrefslogtreecommitdiff
path: root/none
diff options
context:
space:
mode:
authorbart <bart@a5019735-40e9-0310-863c-91ae7b9d1cf9>2008-04-21 17:41:32 +0000
committerbart <bart@a5019735-40e9-0310-863c-91ae7b9d1cf9>2008-04-21 17:41:32 +0000
commitbee023b2712a1cd943a53f8485e348554f9b8dcf (patch)
tree76274f00473410a8745079129c8ae5438c789a45 /none
parent545380e43e6f1669af91e55bf9840a950db6469f (diff)
Made mallinfo() regression test more extensive, based on a contribution by Eugene Toder.
git-svn-id: svn://svn.valgrind.org/valgrind/trunk@7902 a5019735-40e9-0310-863c-91ae7b9d1cf9
Diffstat (limited to 'none')
-rw-r--r--none/tests/mallinfo.c117
-rw-r--r--none/tests/mallinfo.stderr.exp2
-rw-r--r--none/tests/mallinfo.vgtest1
3 files changed, 103 insertions, 17 deletions
diff --git a/none/tests/mallinfo.c b/none/tests/mallinfo.c
index 5dd62649..6b5ec876 100644
--- a/none/tests/mallinfo.c
+++ b/none/tests/mallinfo.c
@@ -1,28 +1,113 @@
-#include <assert.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
+#include <unistd.h> // getopt()
-int main(int argc, char** argv)
+static int s_quiet = 0;
+
+static size_t check(size_t min, size_t max)
{
struct mallinfo mi;
- int allocated, allocated1, allocated2;
- void* p;
+ size_t used;
- fprintf(stderr,
- "Test of mallinfo(). May not trigger any assertion failures.\n");
-
- mi = mallinfo();
- assert(mi.arena == mi.uordblks + mi.fordblks);
- allocated1 = mi.arena + mi.hblkhd;
- allocated = 128*4096;
- p = malloc(allocated);
mi = mallinfo();
- assert(mi.arena == mi.uordblks + mi.fordblks);
- allocated2 = mi.arena + mi.hblkhd;
- assert(allocated2 - allocated1 >= allocated);
- free(p);
+
+ if (! s_quiet)
+ {
+ printf("arena = %d\n", mi.arena); /* non-mmapped space allocated from system */
+ printf("ordblks = %d\n", mi.ordblks); /* number of free chunks */
+ printf("smblks = %d\n", mi.smblks); /* number of fastbin blocks */
+ printf("hblks = %d\n", mi.hblks); /* number of mmapped regions */
+ printf("hblkhd = %d\n", mi.hblkhd); /* space in mmapped regions */
+ printf("usmblks = %d\n", mi.usmblks); /* maximum total allocated space */
+ printf("fsmblks = %d\n", mi.fsmblks); /* space available in freed fastbin blocks */
+ printf("uordblks = %d\n", mi.uordblks); /* total allocated space */
+ printf("fordblks = %d\n", mi.fordblks); /* total free space */
+ printf("keepcost = %d\n", mi.keepcost); /* top-most, releasable (via malloc_trim) space */
+ printf("(min = %zu, max = %zu)\n", min, max);
+ }
+
+ // size checks
+ used = mi.uordblks + mi.hblkhd;
+ if (used < min)
+ exit(1);
+
+ if (used > max)
+ exit(2);
+
+ // used should be reasonably close to min
+ // define "reasonably" as within 20%
+ if (used/5*4 > min)
+ exit(3);
+
+ // sanity checks
+ if ((mi.ordblks == 0) != (mi.fordblks == 0))
+ exit(10);
+
+ if ((mi.smblks == 0) != (mi.fsmblks == 0))
+ exit(11);
+
+ if ((mi.hblks == 0) != (mi.hblkhd == 0))
+ exit(12);
+
+ if (mi.keepcost > mi.fordblks)
+ exit(13);
+
+ if (mi.fsmblks > mi.fordblks)
+ exit(14);
+
+ // arena should be reasonably close to fordblks + uordblks
+ if (mi.arena < mi.fordblks + mi.uordblks)
+ exit(15);
+
+ if (mi.arena/5*4 > mi.fordblks + mi.uordblks)
+ exit(16);
+
+ return used;
+}
+
+int main(int argc, char** argv)
+{
+ void* ptr[40];
+ int i;
+ size_t min, max;
+ int optchar;
+
+ while ((optchar = getopt(argc, argv, "q")) != EOF)
+ {
+ switch (optchar)
+ {
+ case 'q':
+ s_quiet = 1;
+ break;
+ default:
+ fprintf(stderr, "Usage: %s [-q].\n", argv[0]);
+ return 1;
+ }
+ }
+
+ min = 0;
+ for (i = 1; i <= 40; i++)
+ {
+ int size = i * i * 8;
+ min += size;
+ ptr[i - 1] = malloc(size);
+ };
+
+ max = check(min, (size_t)-1);
+
+ for (i = 1; i <= 20; i++)
+ {
+ int size = i * i * 8;
+ min -= size;
+ max -= size;
+ free(ptr[i - 1]);
+ };
+
+ check(min, max);
+
+ fprintf(stderr, "Success.\n");
return 0;
}
diff --git a/none/tests/mallinfo.stderr.exp b/none/tests/mallinfo.stderr.exp
index c1f4963a..330c29d3 100644
--- a/none/tests/mallinfo.stderr.exp
+++ b/none/tests/mallinfo.stderr.exp
@@ -1,3 +1,3 @@
-Test of mallinfo(). May not trigger any assertion failures.
+Success.
diff --git a/none/tests/mallinfo.vgtest b/none/tests/mallinfo.vgtest
index a918f725..0580988a 100644
--- a/none/tests/mallinfo.vgtest
+++ b/none/tests/mallinfo.vgtest
@@ -1 +1,2 @@
prog: mallinfo
+args: -q