forked from siriobalmelli/nonlibc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lifo_test.c
66 lines (57 loc) · 1.32 KB
/
lifo_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <nonlibc.h>
#include <ndebug.h>
#include <lifo.h>
#include <time.h> /* clock() */
#include <stdlib.h> /* getenv() */
/* test_many()
*/
int test_many(LIFO_MEM_TYPE numiter)
{
int err_cnt = 0;
struct lifo *stk = NULL;
NB_die_if(!(stk = lifo_new()),
"");
/* push to stack sequentially */
for (LIFO_MEM_TYPE i=0; i < numiter; i++) {
LIFO_MEM_TYPE ret = lifo_push(&stk, i);
NB_die_if(ret != i,
"ret %zu != i %zu",
ret, i);
}
/* pop off stack; verify sequence */
LIFO_MEM_TYPE pop, remain;
while ( (remain = lifo_pop(stk, &pop)) != LIFO_ERR) {
NB_die_if(--numiter != remain,
"--numiter %zu != remain %zu",
numiter, remain);
NB_die_if(pop != remain,
"pop %zu != remain %zu",
pop, remain);
}
die:
lifo_free(stk);
return err_cnt;
}
/* main()
*/
int main()
{
int err_cnt = 0;
/* do MUCH less work if VALGRIND environment variable is set */
int shifts = 4;
if (!(getenv("VALGRIND")))
shifts = 20;
LIFO_MEM_TYPE numiter = 128;
for (int i=0; i < shifts; i++, numiter <<= 1) {
nlc_timing_start(elapsed);
err_cnt += test_many(numiter);
/* TODO: 'errno' is SOMETIMES set here, but ONLY on BSD;
It is NEVER set just before the 'return'
statement in test_many() ????
*/
nlc_timing_stop(elapsed);
NB_inf("%zu stack pushes in %fs",
numiter, nlc_timing_cpu(elapsed));
}
return err_cnt;
}