libamxc  1.10.3
C Generic Data Containers
test_amxc_variant_type.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 <stdarg.h>
58 #include <stddef.h>
59 #include <setjmp.h>
60 #include <cmocka.h>
61 
62 #include <amxc/amxc_variant_type.h>
63 #include <amxc_variant_priv.h>
64 
65 #include "test_amxc_variant_type.h"
66 
67 #include <amxc/amxc_macros.h>
69 {
70  .copy = NULL,
71  .convert_from = NULL,
72  .convert_to = NULL,
73  .compare = NULL,
74  .del = NULL,
75  .name = "dummy1_t"
76 };
77 
79 {
80  .copy = NULL,
81  .convert_from = NULL,
82  .convert_to = NULL,
83  .compare = NULL,
84  .del = NULL,
85  .name = "dummy2_t"
86 };
87 
89 {
90  .copy = NULL,
91  .convert_from = NULL,
92  .convert_to = NULL,
93  .compare = NULL,
94  .del = NULL,
95  .name = "dummy3_t"
96 };
97 
99 {
100  .copy = NULL,
101  .convert_from = NULL,
102  .convert_to = NULL,
103  .compare = NULL,
104  .del = NULL,
105  .name = "dummy4_t"
106 };
107 
108 void test_amxc_var_add_type(UNUSED void** state) {
110 
111  assert_int_equal(amxc_var_add_type(&dummy1, 0), 0);
112  assert_ptr_equal(amxc_array_get_at(types, 0)->data, &dummy1);
113  assert_int_equal(dummy1.type_id, 0);
114  assert_int_equal(amxc_var_add_type(&dummy1, 1), 0);
115  assert_ptr_equal(amxc_array_get_at(types, 0)->data, &dummy1);
116  assert_int_equal(amxc_var_add_type(&dummy1, 0), 0);
117  assert_ptr_equal(amxc_array_get_at(types, 0)->data, &dummy1);
118 
119  assert_int_equal(amxc_var_add_type(&dummy2, -1), AMXC_VAR_ID_CUSTOM_BASE);
120  assert_int_equal(dummy2.type_id, AMXC_VAR_ID_CUSTOM_BASE);
121  assert_ptr_equal(amxc_array_get_at(types, AMXC_VAR_ID_CUSTOM_BASE)->data, &dummy2);
122  assert_int_equal(amxc_var_add_type(&dummy2, 1), AMXC_VAR_ID_CUSTOM_BASE);
123  assert_ptr_equal(amxc_array_get_at(types, AMXC_VAR_ID_CUSTOM_BASE)->data, &dummy2);
125  assert_ptr_equal(amxc_array_get_at(types, AMXC_VAR_ID_CUSTOM_BASE)->data, &dummy2);
126 
128  assert_int_equal(amxc_var_remove_type(&dummy2), 0);
131 
132  assert_int_equal(amxc_var_remove_type(&dummy1), 0);
133  assert_int_equal(amxc_var_remove_type(&dummy2), 0);
134  assert_int_equal(amxc_var_remove_type(&dummy3), 0);
135  assert_int_equal(amxc_var_remove_type(&dummy4), 0);
136 }
137 
138 void test_amxc_var_remove_type(UNUSED void** state) {
140  assert_int_equal(amxc_var_add_type(&dummy1, 0), 0);
141  assert_int_equal(amxc_var_add_type(&dummy2, -1), AMXC_VAR_ID_CUSTOM_BASE);
142 
143  assert_int_equal(amxc_var_remove_type(&dummy1), 0);
144  assert_ptr_equal(amxc_array_get_at(types, 0)->data, NULL);
145  assert_int_equal(dummy1.type_id, AMXC_VAR_ID_MAX);
146  assert_int_equal(amxc_var_remove_type(&dummy1), -1);
147 
148  assert_int_equal(amxc_var_add_type(&dummy2, 0), AMXC_VAR_ID_CUSTOM_BASE);
149  assert_ptr_equal(amxc_array_get_at(types, 0)->data, NULL);
150  assert_int_equal(amxc_var_remove_type(&dummy2), 0);
151  assert_int_equal(dummy2.type_id, AMXC_VAR_ID_MAX);
152  assert_ptr_equal(amxc_array_get_data_at(types, AMXC_VAR_ID_CUSTOM_BASE), NULL);
153  assert_int_equal(amxc_array_size(types), 0);
154  assert_int_equal(amxc_var_remove_type(&dummy2), -1);
155  assert_int_equal(amxc_var_add_type(&dummy1, -1), AMXC_VAR_ID_CUSTOM_BASE);
156  assert_ptr_equal(amxc_array_get_at(types, AMXC_VAR_ID_CUSTOM_BASE)->data, &dummy1);
157  assert_int_equal(amxc_var_remove_type(&dummy1), 0);
158 }
159 
160 void test_amxc_var_get_type(UNUSED void** state) {
161  assert_int_equal(amxc_var_add_type(&dummy1, 0), 0);
162  assert_int_equal(amxc_var_add_type(&dummy2, -1), AMXC_VAR_ID_CUSTOM_BASE);
163 
164  assert_ptr_equal(amxc_var_get_type(0), &dummy1);
165  assert_ptr_equal(amxc_var_get_type(AMXC_VAR_ID_CUSTOM_BASE), &dummy2);
166  assert_ptr_equal(amxc_var_get_type(999), NULL);
167 
168  assert_int_equal(amxc_var_remove_type(&dummy1), 0);
169  assert_int_equal(amxc_var_remove_type(&dummy2), 0);
170 }
171 
174 
175  assert_int_equal(amxc_var_register_type(NULL), AMXC_VAR_ID_MAX);
176 
178  assert_int_equal(dummy1.type_id, AMXC_VAR_ID_CUSTOM_BASE);
179  assert_ptr_equal(amxc_array_get_at(types, AMXC_VAR_ID_CUSTOM_BASE)->data, &dummy1);
180 
181  assert_int_equal(amxc_var_register_type(&dummy2), AMXC_VAR_ID_CUSTOM_BASE + 1);
182  assert_int_equal(dummy2.type_id, AMXC_VAR_ID_CUSTOM_BASE + 1);
183  assert_ptr_equal(amxc_array_get_at(types, AMXC_VAR_ID_CUSTOM_BASE + 1)->data, &dummy2);
184 
185  assert_int_equal(amxc_var_remove_type(&dummy1), 0);
186  assert_int_equal(amxc_var_remove_type(&dummy2), 0);
187 }
188 
191 
192  assert_int_equal(amxc_var_add_type(&dummy1, 0), 0);
194 
195  assert_int_equal(amxc_var_unregister_type(NULL), -1);
196 
197  assert_int_equal(amxc_var_unregister_type(&dummy1), -1);
198  assert_ptr_equal(amxc_array_get_at(types, 0)->data, &dummy1);
199 
200  assert_int_equal(amxc_var_unregister_type(&dummy2), 0);
201  assert_ptr_equal(amxc_array_get_at(types, AMXC_VAR_ID_CUSTOM_BASE)->data, NULL);
202  assert_int_equal(amxc_var_unregister_type(&dummy2), -1);
203 
204  assert_int_equal(amxc_var_remove_type(&dummy1), 0);
205 }
206 
208  assert_int_equal(amxc_var_add_type(&dummy1, 0), 0);
209  assert_int_equal(amxc_var_add_type(&dummy2, -1), AMXC_VAR_ID_CUSTOM_BASE);
210 
211  assert_ptr_equal(amxc_var_get_type_name_from_id(AMXC_VAR_ID_MAX), NULL);
212  assert_ptr_equal(amxc_var_get_type_name_from_id(9999), NULL);
213 
214  assert_string_equal(amxc_var_get_type_name_from_id(0), "dummy1_t");
215  assert_string_equal(amxc_var_get_type_name_from_id(AMXC_VAR_ID_CUSTOM_BASE), "dummy2_t");
216 
217  assert_int_equal(amxc_var_remove_type(&dummy1), 0);
218  assert_int_equal(amxc_var_remove_type(&dummy2), 0);
219 }
220 
222  assert_int_equal(amxc_var_add_type(&dummy1, 0), 0);
223  assert_int_equal(amxc_var_add_type(&dummy2, -1), AMXC_VAR_ID_CUSTOM_BASE);
224 
225  assert_ptr_equal(amxc_var_get_type_id_from_name(NULL), AMXC_VAR_ID_MAX);
226  assert_ptr_equal(amxc_var_get_type_id_from_name("NotValid_t"), AMXC_VAR_ID_MAX);
227  assert_ptr_equal(amxc_var_get_type_id_from_name(""), AMXC_VAR_ID_MAX);
228 
229  assert_int_equal(amxc_var_get_type_id_from_name("dummy1_t"), 0);
230  assert_int_equal(amxc_var_get_type_id_from_name("dummy2_t"), AMXC_VAR_ID_CUSTOM_BASE);
231 
232  assert_int_equal(amxc_var_remove_type(&dummy1), 0);
233  assert_int_equal(amxc_var_remove_type(&dummy2), 0);
234 }
#define UNUSED
Definition: amxc_macros.h:70
amxc_array_t PRIVATE * amxc_variant_get_types_array(void)
uint32_t PRIVATE amxc_var_add_type(amxc_var_type_t *const type, const uint32_t index)
int PRIVATE amxc_var_remove_type(amxc_var_type_t *const type)
Ambiorix ring buffer API header file.
size_t amxc_array_size(const amxc_array_t *const array)
Calculates the number of used items in the array.
Definition: amxc_array.c:415
AMXC_INLINE void * amxc_array_get_data_at(const amxc_array_t *const array, const unsigned int index)
Gets the data pointer of the item at the given index.
Definition: amxc_array.h:711
amxc_array_it_t * amxc_array_get_at(const amxc_array_t *const array, const unsigned int index)
Gets the item iterator for the given index.
Definition: amxc_array.c:504
#define AMXC_VAR_ID_CUSTOM_BASE
Base variant id for custom variants.
Definition: amxc_variant.h:257
#define AMXC_VAR_ID_MAX
Same as AMXC_VAR_ID_INVALID.
Definition: amxc_variant.h:263
uint32_t amxc_var_register_type(amxc_var_type_t *const type)
Register a new variant type.
amxc_var_type_t * amxc_var_get_type(unsigned int type_id)
Get the type definition structure.
uint32_t amxc_var_get_type_id_from_name(const char *const name)
Get the type id.
int amxc_var_unregister_type(amxc_var_type_t *const type)
Unregisters an already registered variant type.
const char * amxc_var_get_type_name_from_id(const uint32_t type_id)
Get the type name.
The array structure.
Definition: amxc_array.h:162
A variant type structure.
amxc_var_copy_fn_t copy
char data[]
static amxc_var_type_t dummy1
void test_amxc_var_remove_type(UNUSED void **state)
void test_amxc_var_get_type_id_from_name(UNUSED void **state)
void test_amxc_var_get_type(UNUSED void **state)
void test_amxc_var_register_type(UNUSED void **state)
void test_amxc_var_unregister_type(UNUSED void **state)
static amxc_var_type_t dummy3
void test_amxc_var_get_type_name_from_id(UNUSED void **state)
void test_amxc_var_add_type(UNUSED void **state)
static amxc_var_type_t dummy4
static amxc_var_type_t dummy2