Added SILC Thread Queue API
[silc.git] / lib / silcutil / tests / test_silcstack.c
1 /* SilcStack tests */
2
3 #include "silc.h"
4
5 #define NUM_ALLS 300
6
7 int main(int argc, char **argv)
8 {
9   SilcBool success = FALSE;
10   SilcStack stack, child, child2;
11   void *ptr, *ptr2;
12   int i;
13
14   if (argc > 1 && !strcmp(argv[1], "-d")) {
15     silc_log_debug(TRUE);
16     silc_log_debug_hexdump(TRUE);
17     silc_log_quick(TRUE);
18     silc_log_set_debug_string("*stack*,*errno*");
19   }
20
21   SILC_LOG_DEBUG(("Allocating stack of default size (1024 bytes)"));
22   stack = silc_stack_alloc(0, NULL);
23   if (!stack)
24     goto err;
25   silc_stack_stats(stack);
26
27   SILC_LOG_DEBUG(("Allocating 2048 bytes from stack"));
28   ptr = silc_smalloc(stack, 2048);
29   if (!ptr)
30     goto err;
31   silc_stack_stats(stack);
32
33   SILC_LOG_DEBUG(("Freeing the stack"));
34   silc_stack_free(stack);
35
36   SILC_LOG_DEBUG(("Allocating stack of default size (1024 bytes)"));
37   stack = silc_stack_alloc(0, NULL);
38   if (!stack)
39     goto err;
40   silc_stack_stats(stack);
41
42   SILC_LOG_DEBUG(("Pushing and allocating %d times", NUM_ALLS));
43   if (!silc_stack_push(stack, NULL))
44     goto err;
45   for (i = 0; i < NUM_ALLS; i++) {
46     ptr2 = silc_smalloc(stack, (i + 1) * 7);
47     if (!ptr2)
48       goto err;
49   }
50   silc_stack_stats(stack);
51   silc_stack_pop(stack);
52   SILC_LOG_DEBUG(("Popping"));
53   silc_stack_stats(stack);
54
55   SILC_LOG_DEBUG(("Pushing and allocating %d times", NUM_ALLS));
56   if (!silc_stack_push(stack, NULL))
57     goto err;
58   for (i = 0; i < NUM_ALLS; i++) {
59     ptr2 = silc_smalloc(stack, (i + 1) * 7);
60     if (!ptr2)
61       goto err;
62   }
63   silc_stack_stats(stack);
64   silc_stack_pop(stack);
65   SILC_LOG_DEBUG(("Popping"));
66   silc_stack_stats(stack);
67
68   SILC_LOG_DEBUG(("Pushing %d times", NUM_ALLS / 2));
69   for (i = 0; i < NUM_ALLS / 2; i++) {
70     if (!silc_stack_push(stack, NULL))
71       goto err;
72     ptr2 = silc_smalloc(stack, (i + 1) * 7);
73     if (!ptr2)
74       goto err;
75   }
76   silc_stack_stats(stack);
77   SILC_LOG_DEBUG(("Popping %d times", NUM_ALLS / 2));
78   for (i = 0; i < NUM_ALLS / 2; i++)
79     silc_stack_pop(stack);
80   silc_stack_stats(stack);
81
82   SILC_LOG_DEBUG(("Pushing and reallocating %d times", NUM_ALLS / 10));
83   ptr2 = NULL;
84   if (!silc_stack_push(stack, NULL))
85     goto err;
86   for (i = 0; i < NUM_ALLS / 10; i++) {
87     ptr2 = silc_srealloc(stack, (i * 7), ptr2, (i + 1) * 7);
88     if (!ptr2)
89       goto err;
90   }
91   silc_stack_stats(stack);
92   silc_stack_pop(stack);
93   SILC_LOG_DEBUG(("Popping"));
94   silc_stack_stats(stack);
95
96   SILC_LOG_DEBUG(("Creating child stack"));
97   child = silc_stack_alloc(8190, stack);
98   if (!child)
99     goto err;
100   SILC_LOG_DEBUG(("Pushing %d times", NUM_ALLS / 2));
101   for (i = 0; i < NUM_ALLS / 2; i++) {
102     if (!silc_stack_push(child, NULL))
103       goto err;
104     ptr2 = silc_smalloc(child, (i + 1) * 7);
105     if (!ptr2)
106       goto err;
107   }
108   silc_stack_stats(child);
109   SILC_LOG_DEBUG(("Popping %d times", NUM_ALLS / 2));
110   for (i = 0; i < NUM_ALLS / 2; i++)
111     silc_stack_pop(child);
112   silc_stack_stats(child);
113
114   SILC_LOG_DEBUG(("Pushing and reallocating %d times", NUM_ALLS / 10));
115   ptr2 = NULL;
116   if (!silc_stack_push(child, NULL))
117     goto err;
118   for (i = 0; i < NUM_ALLS / 10; i++) {
119     ptr2 = silc_srealloc(child, (i * 7), ptr2, (i + 1) * 7);
120     if (!ptr2)
121       goto err;
122   }
123   ptr = silc_smalloc(child, 100000);
124   silc_stack_stats(child);
125   silc_stack_pop(child);
126   SILC_LOG_DEBUG(("Popping"));
127   silc_stack_stats(child);
128   silc_stack_stats(stack);
129   silc_stack_free(child);
130   silc_stack_stats(stack);
131
132   SILC_LOG_DEBUG(("Creating child stack"));
133   child = silc_stack_alloc(8192, stack);
134   if (!child)
135     goto err;
136   SILC_LOG_DEBUG(("Pushing %d times", NUM_ALLS / 10));
137   for (i = 0; i < NUM_ALLS / 10; i++) {
138     if (!silc_stack_push(child, NULL))
139       goto err;
140     ptr2 = silc_smalloc(child, (i + 1) * 7);
141     if (!ptr2)
142       goto err;
143   }
144   silc_stack_stats(child);
145   SILC_LOG_DEBUG(("Popping %d times", NUM_ALLS / 10));
146   for (i = 0; i < NUM_ALLS / 10; i++)
147     silc_stack_pop(child);
148   silc_stack_stats(child);
149
150   SILC_LOG_DEBUG(("Pushing and reallocating %d times", NUM_ALLS / 10));
151   ptr2 = NULL;
152   if (!silc_stack_push(child, NULL))
153     goto err;
154   for (i = 0; i < NUM_ALLS / 10; i++) {
155     ptr2 = silc_srealloc(child, (i * 7), ptr2, (i + 1) * 7);
156     if (!ptr2)
157       goto err;
158   }
159   SILC_LOG_DEBUG(("Allocate child from child"));
160   child2 = silc_stack_alloc(0, child);
161   ptr = silc_smalloc(child2, 500000);
162   silc_stack_stats(child2);
163   silc_stack_free(child2);
164   silc_stack_stats(child);
165   silc_stack_pop(child);
166   SILC_LOG_DEBUG(("Popping"));
167   silc_stack_stats(child);
168   silc_stack_stats(stack);
169   silc_stack_free(child);
170   silc_stack_stats(stack);
171
172   SILC_LOG_DEBUG(("Purge stack"));
173   silc_stack_purge(stack);
174   silc_stack_stats(stack);
175
176   SILC_LOG_DEBUG(("Current alignment: %d", silc_stack_get_alignment(stack)));
177   SILC_LOG_DEBUG(("Set alignemtn to 16"));
178   silc_stack_set_alignment(stack, 16);
179   SILC_LOG_DEBUG(("Current alignment: %d", silc_stack_get_alignment(stack)));
180   SILC_LOG_DEBUG(("Allocate 1 byte"));
181   ptr = silc_smalloc(stack, 1);
182   SILC_LOG_DEBUG(("Allocate 1 byte, check alignment"));
183   ptr2 = silc_smalloc(stack, 1);
184   if (ptr2 - ptr < 16) {
185     SILC_LOG_DEBUG(("Bad alignment"));
186     goto err;
187   }
188   SILC_LOG_DEBUG(("Alignment (ptr, ptr2) is %d", ptr2 - ptr));
189   SILC_LOG_DEBUG(("Allocate 1 byte, check alignment"));
190   ptr2 = silc_smalloc(stack, 1);
191   if (ptr2 - ptr < 32) {
192     SILC_LOG_DEBUG(("Bad alignment"));
193     goto err;
194   }
195   SILC_LOG_DEBUG(("Alignment (ptr, ptr2) is %d", ptr2 - ptr));
196
197   SILC_LOG_DEBUG(("Freeing the stack"));
198   silc_stack_free(stack);
199
200   success = TRUE;
201
202  err:
203   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
204   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
205
206   return success;
207 }