t/stest: non-zero exit value on failure
[fio.git] / t / stest.c
CommitLineData
fbc2792b
JA
1#include <stdio.h>
2#include <stdlib.h>
3#include <assert.h>
4
5#include "../smalloc.h"
6#include "../flist.h"
71471cb1 7#include "../arch/arch.h"
9077ee4a 8#include "debug.h"
fbc2792b
JA
9
10#define MAGIC1 0xa9b1c8d2
11#define MAGIC2 0xf0a1e9b3
12
13#define LOOPS 32
6cc38ac0
VF
14#define MAXSMALLOC 120*1024*1024UL
15#define LARGESMALLOC 128*1024U
fbc2792b
JA
16
17struct elem {
18 unsigned int magic1;
19 struct flist_head list;
20 unsigned int magic2;
6cc38ac0 21 unsigned int size;
fbc2792b
JA
22};
23
ca0a0b52 24static FLIST_HEAD(list);
fbc2792b
JA
25
26static int do_rand_allocs(void)
27{
94a30f0a 28 unsigned int size, nr, rounds = 0, ret = 0;
fbc2792b
JA
29 unsigned long total;
30 struct elem *e;
6cc38ac0 31 bool error;
fbc2792b
JA
32
33 while (rounds++ < LOOPS) {
34#ifdef STEST_SEED
35 srand(MAGIC1);
36#endif
6cc38ac0 37 error = false;
fbc2792b 38 nr = total = 0;
6cc38ac0 39 while (total < MAXSMALLOC) {
fbc2792b
JA
40 size = 8 * sizeof(struct elem) + (int) (999.0 * (rand() / (RAND_MAX + 1.0)));
41 e = smalloc(size);
42 if (!e) {
43 printf("fail at %lu, size %u\n", total, size);
94a30f0a 44 ret++;
fbc2792b
JA
45 break;
46 }
47 e->magic1 = MAGIC1;
48 e->magic2 = MAGIC2;
6cc38ac0 49 e->size = size;
fbc2792b
JA
50 total += size;
51 flist_add_tail(&e->list, &list);
52 nr++;
53 }
54
55 printf("Got items: %u\n", nr);
56
57 while (!flist_empty(&list)) {
58 e = flist_entry(list.next, struct elem, list);
59 assert(e->magic1 == MAGIC1);
60 assert(e->magic2 == MAGIC2);
6cc38ac0 61 total -= e->size;
fbc2792b
JA
62 flist_del(&e->list);
63 sfree(e);
6cc38ac0
VF
64
65 if (!error) {
66 e = smalloc(LARGESMALLOC);
67 if (!e) {
68 error = true;
94a30f0a 69 ret++;
6cc38ac0
VF
70 printf("failure allocating %u bytes at %lu allocated during sfree phase\n",
71 LARGESMALLOC, total);
72 }
73 else
74 sfree(e);
75 }
fbc2792b
JA
76 }
77 }
78
94a30f0a 79 return ret;
fbc2792b
JA
80}
81
fbc2792b
JA
82int main(int argc, char *argv[])
83{
94a30f0a
VF
84 int ret;
85
71471cb1 86 arch_init(argv);
fbc2792b 87 sinit();
9077ee4a 88 debug_init();
fbc2792b 89
94a30f0a
VF
90 ret = do_rand_allocs();
91 smalloc_debug(0); /* TODO: check that free and total blocks
92 ** match */
fbc2792b 93
fbc2792b 94 scleanup();
94a30f0a 95 return ret;
fbc2792b 96}