libamxc  1.10.3
C Generic Data Containers
test_amxc_variant_macros.c
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
13 ** 2. Redistributions in binary form must reproduce the above copyright notice,
14 ** this list of conditions and the following disclaimer in the documentation
15 ** and/or other materials provided with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 
55 #include <stdlib.h>
56 #include <stdio.h>
57 #include <string.h>
58 #include <stdarg.h>
59 #include <stddef.h>
60 #include <setjmp.h>
61 #include <cmocka.h>
62 
63 #include <amxc/amxc_variant.h>
64 #include <amxc_variant_priv.h>
65 
67 
68 #include <amxc/amxc_macros.h>
69 
70 void test_amxc_var_get_macros(UNUSED void** state) {
72  amxc_var_t* part;
74 
76  part = amxc_var_add_key(amxc_llist_t, &var, "list", NULL);
77  amxc_var_add(uint32_t, part, 100);
78  amxc_var_add(bool, part, true);
79  amxc_var_add(cstring_t, part, "yes");
80 
81  part = amxc_var_add_key(amxc_htable_t, &var, "table", NULL);
82  amxc_var_add_key(uint32_t, part, "test1", 100);
83  amxc_var_add_key(bool, part, "test2", true);
84  amxc_var_add_key(cstring_t, part, "test3", "yes");
85 
86  assert_true(GETP_BOOL(&var, "list.0"));
87  assert_int_equal(GETP_UINT32(&var, "list.0"), 100);
88  assert_int_equal(GETP_INT32(&var, "list.0"), 100);
89  assert_true(GETP_BOOL(&var, "list.1"));
90  assert_true(GETP_BOOL(&var, "list.2"));
91  assert_string_equal(GETP_CHAR(&var, "list.2"), "yes");
92 
93  part = GETP_ARG(&var, "list.0");
94  assert_int_equal(GETP_UINT32(part, NULL), 100);
95  assert_int_equal(GETP_INT32(part, NULL), 100);
96  assert_int_equal(GET_UINT32(part, NULL), 100);
97  assert_int_equal(GET_INT32(part, NULL), 100);
98 
99  part = GETP_ARG(&var, "list.2");
100  assert_true(GETP_BOOL(part, NULL));
101  assert_true(GET_BOOL(part, NULL));
102  assert_string_equal(GET_CHAR(part, NULL), "yes");
103  assert_string_equal(GETP_CHAR(part, NULL), "yes");
104 
105  part = GETP_ARG(&var, "table.test1");
106  assert_int_equal(GETP_UINT32(part, NULL), 100);
107  assert_int_equal(GETP_INT32(part, NULL), 100);
108  assert_int_equal(GET_UINT32(part, NULL), 100);
109  assert_int_equal(GET_INT32(part, NULL), 100);
110 
111  part = GETP_ARG(&var, "table.test3");
112  assert_true(GETP_BOOL(part, NULL));
113  assert_true(GET_BOOL(part, NULL));
114  assert_string_equal(GET_CHAR(part, NULL), "yes");
115  assert_string_equal(GETP_CHAR(part, NULL), "yes");
116 
118 }
119 
121  amxc_var_t var;
122  uint32_t count = 0;
123  amxc_var_init(&var);
124 
126  amxc_var_add_key(uint32_t, &var, "key1", 100);
127  amxc_var_add_key(uint32_t, &var, "key2", 200);
128  amxc_var_add_key(bool, &var, "key3", true);
129  amxc_var_add_key(cstring_t, &var, "key4", "text");
130 
132  const char* key = amxc_var_key(data);
133  assert_non_null(key);
134  printf("%s = ", key);
135  fflush(stdout);
136  amxc_var_dump(data, 1);
137  assert_ptr_equal(amxc_var_get_parent(data), &var);
138  count++;
139  }
140  assert_int_equal(count, 4);
141 
142  printf("\n");
143 
144  count = 0;
146  const char* key = amxc_var_key(data);
147  assert_non_null(key);
148  printf("%s = ", key);
149  fflush(stdout);
150  amxc_var_dump(data, 1);
151  assert_ptr_equal(amxc_var_get_parent(data), &var);
152  count++;
153  }
154  assert_int_equal(count, 4);
155 
157 }
158 
160  amxc_var_t var;
161  uint32_t count = 0;
162  amxc_var_init(&var);
163 
165  amxc_var_add(uint32_t, &var, 100);
166  amxc_var_add(uint32_t, &var, 200);
167  amxc_var_add(bool, &var, true);
168  amxc_var_add(cstring_t, &var, "text");
169 
171  const char* key = amxc_var_key(data);
172  assert_null(key);
173  fflush(stdout);
174  amxc_var_dump(data, 1);
175  assert_ptr_equal(amxc_var_get_parent(data), &var);
176  count++;
177  }
178  assert_int_equal(count, 4);
179 
180  printf("\n");
181 
182  count = 0;
184  const char* key = amxc_var_key(data);
185  assert_null(key);
186  fflush(stdout);
187  amxc_var_dump(data, 1);
188  assert_ptr_equal(amxc_var_get_parent(data), &var);
189  count++;
190  }
191  assert_int_equal(count, 4);
192 
194 }
195 
197  amxc_var_t var;
198  uint32_t count = 0;
199  amxc_var_init(&var);
200 
202 
204  fflush(stdout);
205  amxc_var_dump(data, 1);
206  assert_ptr_equal(amxc_var_get_parent(data), &var);
207  count++;
208  }
209  assert_int_equal(count, 0);
210 
212  const char* key = amxc_var_key(data);
213  assert_non_null(key);
214  printf("%s = ", key);
215  fflush(stdout);
216  amxc_var_dump(data, 1);
217  assert_ptr_equal(amxc_var_get_parent(data), &var);
218  count++;
219  }
220  assert_int_equal(count, 0);
221 
222  assert_null(amxc_var_get_parent(&var));
223 
225 }
#define UNUSED
Definition: amxc_macros.h:70
Ambiorix variant API header file.
#define cstring_t
Convenience macro.
Definition: amxc_variant.h:584
#define AMXC_VAR_ID_CSTRING
C-string variant id (aka char *), null terminated string.
Definition: amxc_variant.h:134
#define AMXC_VAR_ID_LIST
Ambiorix Linked List variant id.
Definition: amxc_variant.h:206
#define AMXC_VAR_ID_HTABLE
Ambiorix Hash Table variant id.
Definition: amxc_variant.h:212
#define GET_INT32(a, n)
Convenience macro for getting a int32_t out of a composite variant by key.
Definition: amxc_variant.h:476
#define GETP_INT32(a, p)
Convenience macro for getting a int32 out of a composite variant by path.
Definition: amxc_variant.h:578
#define GETP_BOOL(a, p)
Convenience macro for getting a bool out of a composite variant by path.
Definition: amxc_variant.h:548
int amxc_var_dump(const amxc_var_t *const var, int fd)
Dumps the content of the variant in a human readable manner.
#define GET_CHAR(a, n)
Convenience macro for getting a char* out of a composite variant by key.
Definition: amxc_variant.h:456
#define GET_BOOL(a, n)
Convenience macro for getting a bool out of a composite variant by key.
Definition: amxc_variant.h:446
#define GETP_CHAR(a, p)
Convenience macro for getting a string out of a composite variant by path.
Definition: amxc_variant.h:558
#define GETP_UINT32(a, p)
Convenience macro for getting a uint32 out of a composite variant by path.
Definition: amxc_variant.h:568
#define GETP_ARG(a, p)
Convenience macro for getting variant out of a composite variant by path.
Definition: amxc_variant.h:538
#define GET_UINT32(a, n)
Convenience macro for getting a uint32_t out of a composite variant by key.
Definition: amxc_variant.h:466
#define amxc_var_add_key(type, var, key, data)
Convenience macro for adding a variant to composite variant type.
Definition: amxc_variant.h:627
#define amxc_var_add(type, var, data)
Convenience macro for adding a variant to composite variant type.
Definition: amxc_variant.h:618
int amxc_var_set_type(amxc_var_t *const var, const uint32_t type)
Change the variant data type.
Definition: amxc_variant.c:261
#define amxc_var_for_each_reverse(var, var_list)
When the variant is a htable or list variant, iterates over the variants in reverse order.
Definition: amxc_variant.h:843
const char * amxc_var_key(const amxc_var_t *const var)
Gets the key, with which the variant is stored in a htable variant.
Definition: amxc_variant.c:821
int amxc_var_init(amxc_var_t *const var)
Initializes a variant.
Definition: amxc_variant.c:223
void amxc_var_clean(amxc_var_t *const var)
Clean-up and reset variant.
Definition: amxc_variant.c:237
amxc_var_t * amxc_var_get_parent(const amxc_var_t *const var)
Gets the containing variant.
Definition: amxc_variant.c:802
#define amxc_var_for_each(var, var_list)
When the variant is a htable or list variant, iterates over the variants in the htable or list.
Definition: amxc_variant.h:819
The hash table structure.
Definition: amxc_htable.h:175
The linked list structure.
Definition: amxc_llist.h:228
The variant struct definition.
Definition: amxc_variant.h:861
char data[]
void test_amxc_var_for_each_htable(UNUSED void **state)
void test_amxc_var_for_each_list(UNUSED void **state)
void test_amxc_var_get_macros(UNUSED void **state)
void test_amxc_var_for_each_non_composite(UNUSED void **state)
static amxc_var_t * var
Definition: test_issue_58.c:77