speedup mechanism to manipulate gnunet time
[oweals/gnunet.git] / src / util / test_common_allocation.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2005, 2006 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file util/test_common_allocation.c
23  * @brief testcase for common_allocation.c
24  */
25 #include "platform.h"
26 #include "gnunet_common.h"
27
28 static int
29 check ()
30 {
31 #define MAX_TESTVAL 1024
32   char *ptrs[MAX_TESTVAL];
33   int i;
34   int j;
35   int k;
36   unsigned int ui;
37
38   /* GNUNET_malloc/GNUNET_free test */
39   k = 352;                      /* random start value */
40   for (i = 1; i < MAX_TESTVAL; i++)
41   {
42     ptrs[i] = GNUNET_malloc (i);
43     for (j = 0; j < i; j++)
44       ptrs[i][j] = k++;
45   }
46
47   for (i = MAX_TESTVAL - 1; i >= 1; i--)
48   {
49     for (j = i - 1; j >= 0; j--)
50       if (ptrs[i][j] != (char) --k)
51         return 1;
52     GNUNET_free (ptrs[i]);
53   }
54
55   /* GNUNET_free_non_null test */
56   GNUNET_free_non_null (NULL);
57   GNUNET_free_non_null (GNUNET_malloc (4));
58
59   /* GNUNET_strdup tests */
60   ptrs[0] = GNUNET_strdup ("bar");
61   if (0 != strcmp (ptrs[0], "bar"))
62     return 3;
63   /* now realloc */
64   ptrs[0] = GNUNET_realloc (ptrs[0], 12);
65   strcpy (ptrs[0], "Hello World");
66
67   GNUNET_free (ptrs[0]);
68   GNUNET_asprintf (&ptrs[0], "%s %s", "Hello", "World");
69   GNUNET_assert (strlen (ptrs[0]) == 11);
70   GNUNET_free (ptrs[0]);
71
72   /* GNUNET_array_grow tests */
73   ptrs[0] = NULL;
74   ui = 0;
75   GNUNET_array_grow (ptrs[0], ui, 42);
76   if (ui != 42)
77     return 4;
78   GNUNET_array_grow (ptrs[0], ui, 22);
79   if (ui != 22)
80     return 5;
81   for (j = 0; j < 22; j++)
82     ptrs[0][j] = j;
83   GNUNET_array_grow (ptrs[0], ui, 32);
84   for (j = 0; j < 22; j++)
85     if (ptrs[0][j] != j)
86       return 6;
87   for (j = 22; j < 32; j++)
88     if (ptrs[0][j] != 0)
89       return 7;
90   GNUNET_array_grow (ptrs[0], ui, 0);
91   if (i != 0)
92     return 8;
93   if (ptrs[0] != NULL)
94     return 9;
95
96
97   return 0;
98 }
99
100 int
101 main (int argc, char *argv[])
102 {
103   int ret;
104
105   GNUNET_log_setup ("test-common-allocation", "WARNING", NULL);
106   ret = check ();
107   if (ret != 0)
108     FPRINTF (stderr, "ERROR %d.\n", ret);
109   return ret;
110 }
111
112 /* end of test_common_allocation.c */