libamxc  1.10.3
C Generic Data Containers
test_2_key_cb_func.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 <sys/time.h>
56 #include <sys/resource.h>
57 
58 #include <string.h>
59 #include <stdlib.h>
60 #include <stdio.h>
61 #include <stdarg.h>
62 #include <stddef.h>
63 #include <setjmp.h>
64 #include <inttypes.h>
65 #include <limits.h>
66 #include <unistd.h>
67 #include <fcntl.h>
68 #include <cmocka.h>
69 
70 #include <amxc/amxc_variant.h>
71 #include <amxc_variant_priv.h>
72 
73 #include "test_variant_htable.h"
74 
75 #include <amxc/amxc_macros.h>
76 void test_variant_htable_get_key(UNUSED void** state) {
78  amxc_var_t string;
79  amxc_var_t* key_part = NULL;
80  const amxc_var_t* key_part_const = NULL;
81 
83  amxc_var_init(&string);
84  amxc_var_set(cstring_t, &string, "Key1:Hello,Key2:world,Key3:and,Key4:anyone,Key5:else,Key6:in,Key7:the,Key8:universe");
85  assert_int_equal(amxc_var_convert(&var, &string, AMXC_VAR_ID_HTABLE), 0);
86  assert_int_equal(amxc_var_type_of(&var), AMXC_VAR_ID_HTABLE);
87  assert_int_equal(amxc_htable_size(&var.data.vm), 8);
88 
89  key_part = amxc_var_get_key(&var, "Key7", AMXC_VAR_FLAG_COPY);
90  assert_int_equal(amxc_var_type_of(key_part), AMXC_VAR_ID_CSTRING);
91  assert_string_equal(key_part->data.s, "the");
92  amxc_var_delete(&key_part);
93  assert_ptr_equal(key_part, NULL);
94 
95  key_part_const = amxc_var_get_key(&var, "Key6", AMXC_VAR_FLAG_DEFAULT);
96  assert_int_equal(amxc_var_type_of(key_part_const), AMXC_VAR_ID_CSTRING);
97  assert_string_equal(key_part_const->data.s, "in");
98 
99  key_part = amxc_var_get_key(&var, NULL, AMXC_VAR_FLAG_COPY);
100  assert_ptr_equal(key_part, NULL);
101 
102  key_part = amxc_var_get_key(&var, "", AMXC_VAR_FLAG_DEFAULT);
103  assert_ptr_equal(key_part, NULL);
104 
105  key_part = amxc_var_get_key(&var, "non-existing-key", AMXC_VAR_FLAG_COPY);
106  assert_ptr_equal(key_part, AMXC_VAR_ID_NULL);
107 
109  key_part = amxc_var_get_key(&var, "key7", AMXC_VAR_FLAG_COPY);
110  assert_ptr_equal(key_part, AMXC_VAR_ID_NULL);
111 
112  amxc_var_clean(&string);
114 }
115 
117  amxc_var_t var;
118  amxc_var_t string;
119  amxc_var_t* key_part = NULL;
120  amxc_var_t* key_part_copy = NULL;
121 
122  amxc_var_init(&var);
123  amxc_var_init(&string);
124  amxc_var_set(cstring_t, &string, "Key1:Hello,Key2:world,Key3:and,Key4:anyone,Key5:else,Key6:in,Key7:the,Key8:universe");
125  assert_int_equal(amxc_var_convert(&var, &string, AMXC_VAR_ID_HTABLE), 0);
126 
127  assert_int_equal(amxc_var_type_of(&var), AMXC_VAR_ID_HTABLE);
128  assert_int_equal(amxc_htable_size(&var.data.vm), 8);
129 
130  assert_int_equal(amxc_var_new(&key_part), 0);
131  amxc_var_set(cstring_t, key_part, "some text");
132  assert_int_equal(amxc_var_set_key(&var, "Key8", key_part, AMXC_VAR_FLAG_COPY | AMXC_VAR_FLAG_UPDATE), 0);
133  assert_int_equal(amxc_htable_size(&var.data.vm), 8);
134 
135  assert_int_equal(amxc_var_set_key(&var, "Key7", key_part, AMXC_VAR_FLAG_UPDATE), 0);
136  assert_int_equal(amxc_htable_size(&var.data.vm), 8);
137 
138  key_part_copy = amxc_var_get_key(&var, "Key8", AMXC_VAR_FLAG_COPY);
139  assert_int_equal(amxc_var_type_of(key_part), AMXC_VAR_ID_CSTRING);
140  assert_string_equal(key_part_copy->data.s, "some text");
141  amxc_var_delete(&key_part_copy);
142  assert_int_equal(amxc_htable_size(&var.data.vm), 8);
143 
144  key_part_copy = amxc_var_get_key(&var, "Key7", AMXC_VAR_FLAG_DEFAULT);
145  assert_int_equal(amxc_var_type_of(key_part_copy), AMXC_VAR_ID_CSTRING);
146  assert_string_equal(key_part_copy->data.s, "some text");
147  assert_ptr_equal(key_part, key_part_copy);
148 
149  assert_int_equal(amxc_var_new(&key_part), 0);
150  amxc_var_set(uint64_t, key_part, 123);
151  assert_int_equal(amxc_var_set_key(&var, "Key9", key_part, AMXC_VAR_FLAG_COPY), 0);
152  assert_int_equal(amxc_htable_size(&var.data.vm), 9);
153  amxc_var_delete(&key_part);
154  assert_int_equal(amxc_htable_size(&var.data.vm), 9);
155 
156  assert_int_equal(amxc_var_new(&key_part), 0);
157  amxc_var_set(uint64_t, key_part, 456);
158  assert_int_equal(amxc_var_set_key(&var, "Key10", key_part, AMXC_VAR_FLAG_DEFAULT), 0);
159  assert_int_equal(amxc_htable_size(&var.data.vm), 10);
160  amxc_var_delete(&key_part);
161  assert_int_equal(amxc_htable_size(&var.data.vm), 9);
162 
163  assert_int_equal(amxc_var_new(&key_part), 0);
164  amxc_var_set(uint64_t, key_part, 456);
165  assert_int_equal(amxc_var_set_key(&var, "Key10", key_part, AMXC_VAR_FLAG_DEFAULT), 0);
166  assert_int_equal(amxc_htable_size(&var.data.vm), 10);
167 
168  key_part_copy = amxc_var_get_key(&var, "Key9", AMXC_VAR_FLAG_COPY);
169  assert_int_equal(amxc_var_type_of(key_part_copy), AMXC_VAR_ID_UINT64);
170  assert_int_equal(key_part_copy->data.ui64, 123);
171  amxc_var_delete(&key_part_copy);
172 
173  key_part_copy = amxc_var_get_key(&var, "Key9", AMXC_VAR_FLAG_DEFAULT);
174  assert_int_equal(amxc_htable_size(&var.data.vm), 10);
175  amxc_var_delete(&key_part_copy);
176  assert_int_equal(amxc_htable_size(&var.data.vm), 9);
177 
178  assert_int_equal(amxc_var_new(&key_part), 0);
179  amxc_var_set(uint64_t, key_part, 123);
180  assert_int_equal(amxc_var_set_key(&var, "Key9", key_part, AMXC_VAR_FLAG_COPY), 0);
181 
182  assert_int_not_equal(amxc_var_set_key(&var, "Key8", NULL, AMXC_VAR_FLAG_COPY), 0);
183  assert_int_not_equal(amxc_var_set_key(&var, "", key_part, AMXC_VAR_FLAG_COPY), 0);
184  assert_int_not_equal(amxc_var_set_key(&var, NULL, key_part, AMXC_VAR_FLAG_COPY), 0);
185  assert_int_not_equal(amxc_var_set_key(NULL, "Key8", key_part, AMXC_VAR_FLAG_COPY), 0);
186 
187  assert_int_not_equal(amxc_var_set_key(&var, "Key8", NULL, AMXC_VAR_FLAG_DEFAULT), 0);
188  assert_int_not_equal(amxc_var_set_key(&var, "", key_part, AMXC_VAR_FLAG_DEFAULT), 0);
189  assert_int_not_equal(amxc_var_set_key(&var, NULL, key_part, AMXC_VAR_FLAG_DEFAULT), 0);
190  assert_int_not_equal(amxc_var_set_key(NULL, "Key8", key_part, AMXC_VAR_FLAG_DEFAULT), 0);
191 
193  assert_int_not_equal(amxc_var_set_key(&var, "Key8", key_part, AMXC_VAR_FLAG_COPY), 0);
194 
195  amxc_var_delete(&key_part);
196  amxc_var_clean(&string);
198 }
#define UNUSED
Definition: amxc_macros.h:70
Ambiorix variant API header file.
#define cstring_t
Convenience macro.
Definition: amxc_variant.h:584
AMXC_INLINE size_t amxc_htable_size(const amxc_htable_t *const htable)
Calculates the size of the hash table.
Definition: amxc_htable.h:334
#define AMXC_VAR_FLAG_DEFAULT
The default flag, do not copy, use variant as is.
Definition: amxc_variant.h:392
#define AMXC_VAR_FLAG_COPY
Copy the variant, creates a new variant, leaves the source variant untouched.
Definition: amxc_variant.h:398
#define AMXC_VAR_FLAG_UPDATE
Replaces the value of the variant, leaves the source variant untouched.
Definition: amxc_variant.h:404
#define AMXC_VAR_ID_NULL
Null variant type id (aka void)
Definition: amxc_variant.h:128
#define AMXC_VAR_ID_CSTRING
C-string variant id (aka char *), null terminated string.
Definition: amxc_variant.h:134
#define AMXC_VAR_ID_UINT64
Unsigned 64 bit integer variant id.
Definition: amxc_variant.h:182
#define AMXC_VAR_ID_BOOL
Boolean variant id.
Definition: amxc_variant.h:200
#define AMXC_VAR_ID_HTABLE
Ambiorix Hash Table variant id.
Definition: amxc_variant.h:212
uint32_t amxc_var_type_of(const amxc_var_t *const var)
Gets the variant type id of a variant.
Definition: amxc_variant.c:668
int amxc_var_set_type(amxc_var_t *const var, const uint32_t type)
Change the variant data type.
Definition: amxc_variant.c:261
amxc_var_t * amxc_var_get_key(const amxc_var_t *const var, const char *const key, const int flags)
Get a reference to a part of composed variant using a key.
Definition: amxc_variant.c:449
int amxc_var_new(amxc_var_t **var)
Allocates a variant and initializes it to the null variant type.
Definition: amxc_variant.c:194
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
void amxc_var_delete(amxc_var_t **var)
Frees the previously allocated variant.
Definition: amxc_variant.c:207
int amxc_var_set_key(amxc_var_t *const var, const char *const key, amxc_var_t *data, const int flags)
Sets a part of composed variant using a key.
Definition: amxc_variant.c:468
#define amxc_var_set(type, var, data)
Convenience macro for setting a value in a variant.
Definition: amxc_variant.h:609
int amxc_var_convert(amxc_var_t *const dest, const amxc_var_t *src, const uint32_t type_id)
Converts one variant (source) to another variant(destination) using the specified variant type id.
Definition: amxc_variant.c:333
The variant struct definition.
Definition: amxc_variant.h:861
void * data
Definition: amxc_variant.h:883
void test_variant_htable_get_key(UNUSED void **state)
void test_variant_htable_set_key(UNUSED void **state)
static amxc_var_t * var
Definition: test_issue_58.c:77