libamxc  1.10.3
C Generic Data Containers
test_amxc_variant_type.c File Reference
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <amxc/amxc_variant_type.h>
#include <amxc_variant_priv.h>
#include "test_amxc_variant_type.h"
#include <amxc/amxc_macros.h>

Go to the source code of this file.

Functions

void test_amxc_var_add_type (UNUSED void **state)
 
void test_amxc_var_remove_type (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)
 
void test_amxc_var_get_type_name_from_id (UNUSED void **state)
 
void test_amxc_var_get_type_id_from_name (UNUSED void **state)
 

Variables

static amxc_var_type_t dummy1
 
static amxc_var_type_t dummy2
 
static amxc_var_type_t dummy3
 
static amxc_var_type_t dummy4
 

Function Documentation

◆ test_amxc_var_add_type()

void test_amxc_var_add_type ( UNUSED void **  state)

Definition at line 108 of file test_amxc_variant_type.c.

108  {
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 }
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)
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
The array structure.
Definition: amxc_array.h:162
char data[]
static amxc_var_type_t dummy1
static amxc_var_type_t dummy3
static amxc_var_type_t dummy4
static amxc_var_type_t dummy2

◆ test_amxc_var_get_type()

void test_amxc_var_get_type ( UNUSED void **  state)

Definition at line 160 of file test_amxc_variant_type.c.

160  {
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 }
amxc_var_type_t * amxc_var_get_type(unsigned int type_id)
Get the type definition structure.

◆ test_amxc_var_get_type_id_from_name()

void test_amxc_var_get_type_id_from_name ( UNUSED void **  state)

Definition at line 221 of file test_amxc_variant_type.c.

221  {
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 }
uint32_t amxc_var_get_type_id_from_name(const char *const name)
Get the type id.

◆ test_amxc_var_get_type_name_from_id()

void test_amxc_var_get_type_name_from_id ( UNUSED void **  state)

Definition at line 207 of file test_amxc_variant_type.c.

207  {
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 }
const char * amxc_var_get_type_name_from_id(const uint32_t type_id)
Get the type name.

◆ test_amxc_var_register_type()

void test_amxc_var_register_type ( UNUSED void **  state)

Definition at line 172 of file test_amxc_variant_type.c.

172  {
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 }
uint32_t amxc_var_register_type(amxc_var_type_t *const type)
Register a new variant type.

◆ test_amxc_var_remove_type()

void test_amxc_var_remove_type ( UNUSED void **  state)

Definition at line 138 of file test_amxc_variant_type.c.

138  {
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 }
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

◆ test_amxc_var_unregister_type()

void test_amxc_var_unregister_type ( UNUSED void **  state)

Definition at line 189 of file test_amxc_variant_type.c.

189  {
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 }
int amxc_var_unregister_type(amxc_var_type_t *const type)
Unregisters an already registered variant type.

Variable Documentation

◆ dummy1

amxc_var_type_t dummy1
static
Initial value:
=
{
.copy = NULL,
.convert_from = NULL,
.convert_to = NULL,
.compare = NULL,
.del = NULL,
.name = "dummy1_t"
}

Definition at line 68 of file test_amxc_variant_type.c.

◆ dummy2

amxc_var_type_t dummy2
static
Initial value:
=
{
.copy = NULL,
.convert_from = NULL,
.convert_to = NULL,
.compare = NULL,
.del = NULL,
.name = "dummy2_t"
}

Definition at line 78 of file test_amxc_variant_type.c.

◆ dummy3

amxc_var_type_t dummy3
static
Initial value:
=
{
.copy = NULL,
.convert_from = NULL,
.convert_to = NULL,
.compare = NULL,
.del = NULL,
.name = "dummy3_t"
}

Definition at line 88 of file test_amxc_variant_type.c.

◆ dummy4

amxc_var_type_t dummy4
static
Initial value:
=
{
.copy = NULL,
.convert_from = NULL,
.convert_to = NULL,
.compare = NULL,
.del = NULL,
.name = "dummy4_t"
}

Definition at line 98 of file test_amxc_variant_type.c.