paragraph for gnunet devs that don't know how to use the web
[oweals/gnunet.git] / src / psycutil / test_psyc_env.c
1 /*
2  * This file is part of GNUnet.
3  * Copyright (C) 2013 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  * You should have received a copy of the GNU Affero General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 /**
20  * @author Gabor X Toth
21  *
22  * @file
23  * Tests for the environment library.
24  */
25
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testing_lib.h"
29 #include "gnunet_psyc_util_lib.h"
30
31 struct GNUNET_PSYC_Modifier mods[] = {
32   { .oper = GNUNET_PSYC_OP_SET,
33     .name = "_foo", .value = "foo", .value_size = 3 },
34
35   { .oper = GNUNET_PSYC_OP_ASSIGN,
36     .name = "_foo_bar", .value = "foo bar", .value_size = 7 },
37
38   { .oper = GNUNET_PSYC_OP_AUGMENT,
39     .name = "_foo_bar_baz", .value = "foo bar baz", .value_size = 11 }
40 };
41
42 struct ItCls
43 {
44   size_t n;
45 };
46
47 int
48 iterator (void *cls, enum GNUNET_PSYC_Operator oper,
49           const char *name, const char *value, uint32_t value_size)
50 {
51   struct ItCls *it_cls = cls;
52   struct GNUNET_PSYC_Modifier *m = &mods[it_cls->n++];
53
54   GNUNET_assert (oper == m->oper);
55   GNUNET_assert (value_size == m->value_size);
56   GNUNET_assert (0 == memcmp (name, m->name, strlen (m->name)));
57   GNUNET_assert (0 == memcmp (value, m->value, m->value_size));
58
59   return GNUNET_YES;
60 }
61
62 int
63 main (int argc, char *argv[])
64 {
65   GNUNET_log_setup ("test-env", "WARNING", NULL);
66
67   struct GNUNET_PSYC_Environment *env = GNUNET_PSYC_env_create ();
68   GNUNET_assert (NULL != env);
69   int i, len = 3;
70
71   for (i = 0; i < len; i++)
72   {
73     GNUNET_PSYC_env_add (env, mods[i].oper, mods[i].name,
74                          mods[i].value, mods[i].value_size);
75   }
76
77   struct ItCls it_cls = { .n = 0 };
78   GNUNET_PSYC_env_iterate (env, iterator, &it_cls);
79   GNUNET_assert (len == it_cls.n);
80
81   for (i = 0; i < len; i++)
82   {
83     enum GNUNET_PSYC_Operator oper;
84     const char *name;
85     const void *value;
86     size_t value_size;
87     GNUNET_PSYC_env_shift (env, &oper, &name, &value, &value_size);
88     GNUNET_assert (len - i - 1 == GNUNET_PSYC_env_get_count (env));
89   }
90
91   GNUNET_PSYC_env_destroy (env);
92
93   return 0;
94 }