glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / util / test_common_allocation.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2001, 2002, 2003, 2005, 2006, 2017 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14 */
15
16 /**
17  * @file util/test_common_allocation.c
18  * @brief testcase for common_allocation.c
19  */
20 #include "platform.h"
21 #include "gnunet_util_lib.h"
22
23
24 static int
25 check ()
26 {
27 #define MAX_TESTVAL 1024
28   char *ptrs[MAX_TESTVAL];
29   unsigned int **a2;
30   char ***a3;
31   int i;
32   int j;
33   int k;
34   unsigned int ui;
35
36   /* GNUNET_malloc/GNUNET_free test */
37   k = 352;                      /* random start value */
38   for (i = 1; i < MAX_TESTVAL; i++)
39   {
40     ptrs[i] = GNUNET_malloc (i);
41     for (j = 0; j < i; j++)
42       ptrs[i][j] = k++;
43   }
44
45   for (i = MAX_TESTVAL - 1; i >= 1; i--)
46   {
47     for (j = i - 1; j >= 0; j--)
48       if (ptrs[i][j] != (char) --k)
49         return 1;
50     GNUNET_free (ptrs[i]);
51   }
52
53   /* GNUNET_free_non_null test */
54   GNUNET_free_non_null (NULL);
55   GNUNET_free_non_null (GNUNET_malloc (4));
56
57   /* GNUNET_strdup tests */
58   ptrs[0] = GNUNET_strdup ("bar");
59   if (0 != strcmp (ptrs[0], "bar"))
60     return 3;
61   /* now realloc */
62   ptrs[0] = GNUNET_realloc (ptrs[0], 12);
63   strcpy (ptrs[0], "Hello World");
64
65   GNUNET_free (ptrs[0]);
66   GNUNET_asprintf (&ptrs[0], "%s %s", "Hello", "World");
67   GNUNET_assert (strlen (ptrs[0]) == 11);
68   GNUNET_free (ptrs[0]);
69
70   /* GNUNET_array_grow tests */
71   ptrs[0] = NULL;
72   ui = 0;
73   GNUNET_array_grow (ptrs[0], ui, 42);
74   if (ui != 42)
75     return 4;
76   GNUNET_array_grow (ptrs[0], ui, 22);
77   if (ui != 22)
78     return 5;
79   for (j = 0; j < 22; j++)
80     ptrs[0][j] = j;
81   GNUNET_array_grow (ptrs[0], ui, 32);
82   for (j = 0; j < 22; j++)
83     if (ptrs[0][j] != j)
84       return 6;
85   for (j = 22; j < 32; j++)
86     if (ptrs[0][j] != 0)
87       return 7;
88   GNUNET_array_grow (ptrs[0], ui, 0);
89   if (i != 0)
90     return 8;
91   if (ptrs[0] != NULL)
92     return 9;
93
94   /* GNUNET_new_array_2d tests */
95   a2 = GNUNET_new_array_2d (17, 22, unsigned int);
96   for (i = 0; i < 17; i++)
97   {
98     for (j = 0; j < 22; j++)
99     {
100       if (0 != a2[i][j])
101       {
102         GNUNET_free (a2);
103         return 10;
104       }
105       a2[i][j] = i * 100 + j;
106     }
107   }
108   GNUNET_free (a2);
109
110   /* GNUNET_new_array_3d tests */
111   a3 = GNUNET_new_array_3d (2, 3, 4, char);
112   for (i = 0; i < 2; i++)
113   {
114     for (j = 0; j < 3; j++)
115     {
116       for (k = 0; k < 4; k++)
117       {
118         if (0 != a3[i][j][k])
119         {
120           GNUNET_free (a3);
121           return 11;
122         }
123         a3[i][j][k] = i * 100 + j * 10 + k;
124       }
125     }
126   }
127   GNUNET_free (a3);
128   return 0;
129 }
130
131
132 int
133 main (int argc, char *argv[])
134 {
135   int ret;
136
137   GNUNET_log_setup ("test-common-allocation",
138                     "WARNING",
139                     NULL);
140   ret = check ();
141   if (ret != 0)
142     FPRINTF (stderr,
143              "ERROR %d.\n",
144              ret);
145   return ret;
146 }
147
148 /* end of test_common_allocation.c */