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