indent
[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
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., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 /**
22  * @author Gabor X Toth
23  *
24  * @file
25  * Tests for the environment library.
26  */
27
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_psyc_util_lib.h"
32
33 struct GNUNET_PSYC_Modifier mods[] = {
34   { .oper = GNUNET_PSYC_OP_SET,
35     .name = "_foo", .value = "foo", .value_size = 3 },
36
37   { .oper = GNUNET_PSYC_OP_ASSIGN,
38     .name = "_foo_bar", .value = "foo bar", .value_size = 7 },
39
40   { .oper = GNUNET_PSYC_OP_AUGMENT,
41     .name = "_foo_bar_baz", .value = "foo bar baz", .value_size = 11 }
42 };
43
44 struct ItCls
45 {
46   size_t n;
47 };
48
49 int
50 iterator (void *cls, enum GNUNET_PSYC_Operator oper,
51           const char *name, const char *value, uint32_t value_size)
52 {
53   struct ItCls *it_cls = cls;
54   struct GNUNET_PSYC_Modifier *m = &mods[it_cls->n++];
55
56   GNUNET_assert (oper == m->oper);
57   GNUNET_assert (value_size == m->value_size);
58   GNUNET_assert (0 == memcmp (name, m->name, strlen (m->name)));
59   GNUNET_assert (0 == memcmp (value, m->value, m->value_size));
60
61   return GNUNET_YES;
62 }
63
64 int
65 main (int argc, char *argv[])
66 {
67   GNUNET_log_setup ("test-env", "WARNING", NULL);
68
69   struct GNUNET_PSYC_Environment *env = GNUNET_PSYC_env_create ();
70   GNUNET_assert (NULL != env);
71   int i, len = 3;
72
73   for (i = 0; i < len; i++)
74   {
75     GNUNET_PSYC_env_add (env, mods[i].oper, mods[i].name,
76                          mods[i].value, mods[i].value_size);
77   }
78
79   struct ItCls it_cls = { .n = 0 };
80   GNUNET_PSYC_env_iterate (env, iterator, &it_cls);
81   GNUNET_assert (len == it_cls.n);
82
83   for (i = 0; i < len; i++)
84   {
85     enum GNUNET_PSYC_Operator oper;
86     const char *name;
87     const void *value;
88     size_t value_size;
89     GNUNET_PSYC_env_shift (env, &oper, &name, &value, &value_size);
90     GNUNET_assert (len - i - 1 == GNUNET_PSYC_env_get_count (env));
91   }
92
93   GNUNET_PSYC_env_destroy (env);
94
95   return 0;
96 }