Created SILC Runtime Toolkit git repository Part II.
[runtime.git] / lib / silcutil / tests / test_silcstack.c
1 /* SilcStack tests */
2
3 #include "silcruntime.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 #ifdef SILC_DIST_INPLACE
26   silc_stack_stats(stack);
27 #endif /* SILC_DIST_INPLACE */
28
29   SILC_LOG_DEBUG(("Allocating 2048 bytes from stack"));
30   ptr = silc_smalloc(stack, 2048);
31   if (!ptr)
32     goto err;
33 #ifdef SILC_DIST_INPLACE
34   silc_stack_stats(stack);
35 #endif /* SILC_DIST_INPLACE */
36
37   SILC_LOG_DEBUG(("Freeing the stack"));
38   silc_stack_free(stack);
39
40   SILC_LOG_DEBUG(("Allocating stack of default size (1024 bytes)"));
41   stack = silc_stack_alloc(0, NULL);
42   if (!stack)
43     goto err;
44 #ifdef SILC_DIST_INPLACE
45   silc_stack_stats(stack);
46 #endif /* SILC_DIST_INPLACE */
47
48   SILC_LOG_DEBUG(("Pushing and allocating %d times", NUM_ALLS));
49   if (!silc_stack_push(stack, NULL))
50     goto err;
51   for (i = 0; i < NUM_ALLS; i++) {
52     ptr2 = silc_smalloc(stack, (i + 1) * 7);
53     if (!ptr2)
54       goto err;
55   }
56 #ifdef SILC_DIST_INPLACE
57   silc_stack_stats(stack);
58 #endif /* SILC_DIST_INPLACE */
59   silc_stack_pop(stack);
60   SILC_LOG_DEBUG(("Popping"));
61 #ifdef SILC_DIST_INPLACE
62   silc_stack_stats(stack);
63 #endif /* SILC_DIST_INPLACE */
64
65   SILC_LOG_DEBUG(("Pushing and allocating %d times", NUM_ALLS));
66   if (!silc_stack_push(stack, NULL))
67     goto err;
68   for (i = 0; i < NUM_ALLS; i++) {
69     ptr2 = silc_smalloc(stack, (i + 1) * 7);
70     if (!ptr2)
71       goto err;
72   }
73 #ifdef SILC_DIST_INPLACE
74   silc_stack_stats(stack);
75 #endif /* SILC_DIST_INPLACE */
76   silc_stack_pop(stack);
77   SILC_LOG_DEBUG(("Popping"));
78 #ifdef SILC_DIST_INPLACE
79   silc_stack_stats(stack);
80 #endif /* SILC_DIST_INPLACE */
81
82   SILC_LOG_DEBUG(("Pushing %d times", NUM_ALLS / 2));
83   for (i = 0; i < NUM_ALLS / 2; i++) {
84     if (!silc_stack_push(stack, NULL))
85       goto err;
86     ptr2 = silc_smalloc(stack, (i + 1) * 7);
87     if (!ptr2)
88       goto err;
89   }
90 #ifdef SILC_DIST_INPLACE
91   silc_stack_stats(stack);
92 #endif /* SILC_DIST_INPLACE */
93   SILC_LOG_DEBUG(("Popping %d times", NUM_ALLS / 2));
94   for (i = 0; i < NUM_ALLS / 2; i++)
95     silc_stack_pop(stack);
96 #ifdef SILC_DIST_INPLACE
97   silc_stack_stats(stack);
98 #endif /* SILC_DIST_INPLACE */
99
100   SILC_LOG_DEBUG(("Pushing and reallocating %d times", NUM_ALLS / 10));
101   ptr2 = NULL;
102   if (!silc_stack_push(stack, NULL))
103     goto err;
104   for (i = 0; i < NUM_ALLS / 10; i++) {
105     ptr2 = silc_srealloc(stack, (i * 7), ptr2, (i + 1) * 7);
106     if (!ptr2)
107       goto err;
108   }
109 #ifdef SILC_DIST_INPLACE
110   silc_stack_stats(stack);
111 #endif /* SILC_DIST_INPLACE */
112   silc_stack_pop(stack);
113   SILC_LOG_DEBUG(("Popping"));
114 #ifdef SILC_DIST_INPLACE
115   silc_stack_stats(stack);
116 #endif /* SILC_DIST_INPLACE */
117
118   SILC_LOG_DEBUG(("Creating child stack"));
119   child = silc_stack_alloc(8190, stack);
120   if (!child)
121     goto err;
122   SILC_LOG_DEBUG(("Pushing %d times", NUM_ALLS / 2));
123   for (i = 0; i < NUM_ALLS / 2; i++) {
124     if (!silc_stack_push(child, NULL))
125       goto err;
126     ptr2 = silc_smalloc(child, (i + 1) * 7);
127     if (!ptr2)
128       goto err;
129   }
130 #ifdef SILC_DIST_INPLACE
131   silc_stack_stats(child);
132 #endif /* SILC_DIST_INPLACE */
133   SILC_LOG_DEBUG(("Popping %d times", NUM_ALLS / 2));
134   for (i = 0; i < NUM_ALLS / 2; i++)
135     silc_stack_pop(child);
136 #ifdef SILC_DIST_INPLACE
137   silc_stack_stats(child);
138 #endif /* SILC_DIST_INPLACE */
139
140   SILC_LOG_DEBUG(("Pushing and reallocating %d times", NUM_ALLS / 10));
141   ptr2 = NULL;
142   if (!silc_stack_push(child, NULL))
143     goto err;
144   for (i = 0; i < NUM_ALLS / 10; i++) {
145     ptr2 = silc_srealloc(child, (i * 7), ptr2, (i + 1) * 7);
146     if (!ptr2)
147       goto err;
148   }
149   ptr = silc_smalloc(child, 100000);
150 #ifdef SILC_DIST_INPLACE
151   silc_stack_stats(child);
152 #endif /* SILC_DIST_INPLACE */
153   silc_stack_pop(child);
154   SILC_LOG_DEBUG(("Popping"));
155 #ifdef SILC_DIST_INPLACE
156   silc_stack_stats(child);
157 #endif /* SILC_DIST_INPLACE */
158 #ifdef SILC_DIST_INPLACE
159   silc_stack_stats(stack);
160 #endif /* SILC_DIST_INPLACE */
161   silc_stack_free(child);
162 #ifdef SILC_DIST_INPLACE
163   silc_stack_stats(stack);
164 #endif /* SILC_DIST_INPLACE */
165
166   SILC_LOG_DEBUG(("Creating child stack"));
167   child = silc_stack_alloc(8192, stack);
168   if (!child)
169     goto err;
170   SILC_LOG_DEBUG(("Pushing %d times", NUM_ALLS / 10));
171   for (i = 0; i < NUM_ALLS / 10; i++) {
172     if (!silc_stack_push(child, NULL))
173       goto err;
174     ptr2 = silc_smalloc(child, (i + 1) * 7);
175     if (!ptr2)
176       goto err;
177   }
178 #ifdef SILC_DIST_INPLACE
179   silc_stack_stats(child);
180 #endif /* SILC_DIST_INPLACE */
181   SILC_LOG_DEBUG(("Popping %d times", NUM_ALLS / 10));
182   for (i = 0; i < NUM_ALLS / 10; i++)
183     silc_stack_pop(child);
184 #ifdef SILC_DIST_INPLACE
185   silc_stack_stats(child);
186 #endif /* SILC_DIST_INPLACE */
187
188   SILC_LOG_DEBUG(("Pushing and reallocating %d times", NUM_ALLS / 10));
189   ptr2 = NULL;
190   if (!silc_stack_push(child, NULL))
191     goto err;
192   for (i = 0; i < NUM_ALLS / 10; i++) {
193     ptr2 = silc_srealloc(child, (i * 7), ptr2, (i + 1) * 7);
194     if (!ptr2)
195       goto err;
196   }
197   SILC_LOG_DEBUG(("Allocate child from child"));
198   child2 = silc_stack_alloc(0, child);
199   ptr = silc_smalloc(child2, 500000);
200 #ifdef SILC_DIST_INPLACE
201   silc_stack_stats(child2);
202 #endif /* SILC_DIST_INPLACE */
203   silc_stack_free(child2);
204 #ifdef SILC_DIST_INPLACE
205   silc_stack_stats(child2);
206 #endif /* SILC_DIST_INPLACE */
207   silc_stack_pop(child);
208   SILC_LOG_DEBUG(("Popping"));
209 #ifdef SILC_DIST_INPLACE
210   silc_stack_stats(child);
211 #endif /* SILC_DIST_INPLACE */
212 #ifdef SILC_DIST_INPLACE
213   silc_stack_stats(stack);
214 #endif /* SILC_DIST_INPLACE */
215   silc_stack_free(child);
216 #ifdef SILC_DIST_INPLACE
217   silc_stack_stats(stack);
218 #endif /* SILC_DIST_INPLACE */
219
220   SILC_LOG_DEBUG(("Purge stack"));
221   silc_stack_purge(stack);
222 #ifdef SILC_DIST_INPLACE
223   silc_stack_stats(stack);
224 #endif /* SILC_DIST_INPLACE */
225
226   SILC_LOG_DEBUG(("Current alignment: %d", silc_stack_get_alignment(stack)));
227   SILC_LOG_DEBUG(("Set alignemtn to 16"));
228   silc_stack_set_alignment(stack, 16);
229   SILC_LOG_DEBUG(("Current alignment: %d", silc_stack_get_alignment(stack)));
230   SILC_LOG_DEBUG(("Allocate 1 byte"));
231   ptr = silc_smalloc(stack, 1);
232   SILC_LOG_DEBUG(("Allocate 1 byte, check alignment"));
233   ptr2 = silc_smalloc(stack, 1);
234   if (ptr2 - ptr < 16) {
235     SILC_LOG_DEBUG(("Bad alignment"));
236     goto err;
237   }
238   SILC_LOG_DEBUG(("Alignment (ptr, ptr2) is %d", ptr2 - ptr));
239   SILC_LOG_DEBUG(("Allocate 1 byte, check alignment"));
240   ptr2 = silc_smalloc(stack, 1);
241   if (ptr2 - ptr < 32) {
242     SILC_LOG_DEBUG(("Bad alignment"));
243     goto err;
244   }
245   SILC_LOG_DEBUG(("Alignment (ptr, ptr2) is %d", ptr2 - ptr));
246
247   SILC_LOG_DEBUG(("Freeing the stack"));
248   silc_stack_free(stack);
249
250   success = TRUE;
251
252  err:
253   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
254   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
255
256   return !success;
257 }