2 This file is part of GNUnet.
3 Copyright (C) 2008 GNUnet e.V.
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.
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.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file util/test_container_multihashmap.c
23 * @brief Test for container_multihashmap.c
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
30 #define ABORT() { fprintf(stderr, "Error at %s:%d\n", __FILE__, __LINE__); if (m != NULL) GNUNET_CONTAINER_multihashmap_destroy(m); if (NULL != iter) GNUNET_CONTAINER_multihashmap_iterator_destroy (iter); return 1; }
31 #define CHECK(c) { if (! (c)) ABORT(); }
36 struct GNUNET_CONTAINER_MultiHashMap *m;
37 struct GNUNET_HashCode k1;
38 struct GNUNET_HashCode k2;
39 struct GNUNET_CONTAINER_MultiHashMapIterator *iter = NULL;
40 struct GNUNET_HashCode key_ret;
44 CHECK (NULL != (m = GNUNET_CONTAINER_multihashmap_create (i, GNUNET_NO)));
45 memset (&k1, 0, sizeof (k1));
46 memset (&k2, 1, sizeof (k2));
47 CHECK (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (m, &k1));
48 CHECK (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (m, &k2));
49 CHECK (GNUNET_NO == GNUNET_CONTAINER_multihashmap_remove (m, &k1, NULL));
50 CHECK (GNUNET_NO == GNUNET_CONTAINER_multihashmap_remove (m, &k2, NULL));
51 CHECK (NULL == GNUNET_CONTAINER_multihashmap_get (m, &k1));
52 CHECK (NULL == GNUNET_CONTAINER_multihashmap_get (m, &k2));
53 CHECK (0 == GNUNET_CONTAINER_multihashmap_remove_all (m, &k1));
54 CHECK (0 == GNUNET_CONTAINER_multihashmap_size (m));
55 CHECK (0 == GNUNET_CONTAINER_multihashmap_iterate (m, NULL, NULL));
56 CHECK (0 == GNUNET_CONTAINER_multihashmap_get_multiple (m, &k1, NULL, NULL));
59 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v1",
60 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
61 CHECK (1 == GNUNET_CONTAINER_multihashmap_size (m));
62 ret = GNUNET_CONTAINER_multihashmap_get (m, &k1);
63 GNUNET_assert (ret != NULL);
64 CHECK (0 == strcmp ("v1", ret));
66 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v1",
67 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
68 CHECK (1 == GNUNET_CONTAINER_multihashmap_size (m));
70 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v2",
71 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
73 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v3",
74 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
75 CHECK (3 == GNUNET_CONTAINER_multihashmap_size (m));
76 CHECK (GNUNET_OK == GNUNET_CONTAINER_multihashmap_remove (m, &k1, "v3"));
77 CHECK (2 == GNUNET_CONTAINER_multihashmap_size (m));
78 CHECK (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains (m, &k1));
79 CHECK (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (m, &k2));
80 CHECK (2 == GNUNET_CONTAINER_multihashmap_get_multiple (m, &k1, NULL, NULL));
81 CHECK (0 == GNUNET_CONTAINER_multihashmap_get_multiple (m, &k2, NULL, NULL));
82 CHECK (2 == GNUNET_CONTAINER_multihashmap_iterate (m, NULL, NULL));
83 iter = GNUNET_CONTAINER_multihashmap_iterator_create (m);
84 CHECK (GNUNET_YES == GNUNET_CONTAINER_multihashmap_iterator_next (iter, &key_ret, (const void **)&ret));
85 CHECK (0 == memcmp (&key_ret, &k1, sizeof (key_ret)));
86 CHECK (GNUNET_YES == GNUNET_CONTAINER_multihashmap_iterator_next (iter, &key_ret, (const void **)&ret));
87 CHECK (0 == memcmp (&key_ret, &k1, sizeof (key_ret)));
88 CHECK (GNUNET_NO == GNUNET_CONTAINER_multihashmap_iterator_next (iter, NULL, NULL));
91 CHECK (2 == GNUNET_CONTAINER_multihashmap_remove_all (m, &k1));
92 for (j = 0; j < 1024; j++)
94 GNUNET_CONTAINER_multihashmap_put (m, &k1, "v2",
95 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
96 iter = GNUNET_CONTAINER_multihashmap_iterator_create (m);
97 for (j = 0; j < GNUNET_CONTAINER_multihashmap_size (m); j++)
98 CHECK (GNUNET_YES == GNUNET_CONTAINER_multihashmap_iterator_next (iter, NULL, NULL));
99 CHECK (GNUNET_NO == GNUNET_CONTAINER_multihashmap_iterator_next (iter, NULL, NULL));
100 GNUNET_CONTAINER_multihashmap_iterator_destroy (iter);
102 GNUNET_CONTAINER_multihashmap_destroy (m);
107 main (int argc, char *argv[])
109 int failureCount = 0;
112 GNUNET_log_setup ("test-container-multihashmap", "WARNING", NULL);
113 for (i = 1; i < 255; i++)
114 failureCount += testMap (i);
115 if (failureCount != 0)
120 /* end of test_container_multihashmap.c */