aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_common_allocation.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/test_common_allocation.c')
-rw-r--r--src/util/test_common_allocation.c112
1 files changed, 112 insertions, 0 deletions
diff --git a/src/util/test_common_allocation.c b/src/util/test_common_allocation.c
new file mode 100644
index 000000000..76c7dc8a3
--- /dev/null
+++ b/src/util/test_common_allocation.c
@@ -0,0 +1,112 @@
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 2, 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
28static int
29check ()
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
100int
101main (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 */