libamxc  1.10.3
C Generic Data Containers
test_amxc_string_join.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 <ctype.h>
60 #include <string.h>
61 #include <setjmp.h>
62 #include <cmocka.h>
63 
64 #include <amxc/amxc_string_split.h>
65 #include <amxc/amxc_string_join.h>
66 #include <amxc/amxc_utils.h>
67 
68 #include "test_amxc_string_join.h"
69 
70 #include <amxc/amxc_macros.h>
73  amxc_string_t string;
74  amxc_var_t* sub_var = NULL;
75 
77  amxc_string_init(&string, 0);
78 
80  amxc_var_add(cstring_t, &var, "text1");
81  amxc_var_add(uint32_t, &var, 123);
82  sub_var = amxc_var_add(amxc_llist_t, &var, NULL);
83  amxc_var_add(csv_string_t, sub_var, "a,b");
84  amxc_var_add(ssv_string_t, sub_var, "a b");
85 
86  assert_int_equal(amxc_string_csv_join_var(&string, &var), 0);
87  assert_string_equal(amxc_string_get(&string, 0), "text1,123,[a,b,a b]");
88  amxc_string_reset(&string);
89 
90  assert_int_equal(amxc_string_ssv_join_var(&string, &var), 0);
91  assert_string_equal(amxc_string_get(&string, 0), "text1 123 [a,b,a b]");
92 
94  amxc_string_clean(&string);
95 }
96 
99  amxc_string_t string;
100 
101  amxc_var_init(&var);
102  amxc_string_init(&string, 0);
103 
104  amxc_var_set(cstring_t, &var, "text1");
105  assert_int_not_equal(amxc_string_csv_join_var(&string, &var), 0);
106  assert_true(amxc_string_is_empty(&string));
107 
108  amxc_var_set(bool, &var, true);
109  assert_int_not_equal(amxc_string_csv_join_var(&string, &var), 0);
110  assert_true(amxc_string_is_empty(&string));
111 
113  amxc_string_clean(&string);
114 }
115 
116 void test_join_adds_to_string(UNUSED void** state) {
117  amxc_var_t var;
118  amxc_string_t string;
119 
120  amxc_var_init(&var);
121  amxc_string_init(&string, 0);
122  amxc_string_appendf(&string, "The list is added after this: ");
123 
125  amxc_var_add(cstring_t, &var, "text1");
126  amxc_var_add(uint32_t, &var, 123);
127  amxc_var_add(amxc_llist_t, &var, NULL);
128 
129  assert_int_equal(amxc_string_csv_join_var(&string, &var), 0);
130  assert_string_equal(amxc_string_get(&string, 0), "The list is added after this: text1,123,[]");
131  amxc_string_reset(&string);
132 
133  amxc_string_appendf(&string, "The list is added after this: ");
134  assert_int_equal(amxc_string_ssv_join_var(&string, &var), 0);
135  assert_string_equal(amxc_string_get(&string, 0), "The list is added after this: text1 123 []");
136 
138  amxc_string_clean(&string);
139 }
140 
142  amxc_llist_t list;
143  amxc_string_t string;
144 
145  amxc_llist_init(&list);
146  amxc_string_init(&string, 0);
147  amxc_string_appendf(&string, "text1,text2,text3,[text4,text5],text6");
148 
149  assert_int_equal(amxc_string_split_to_llist(&string, &list, ','), 0);
150  amxc_string_clean(&string);
151  assert_int_equal(amxc_string_join_llist(&string, &list, ','), 0);
152  assert_string_equal(amxc_string_get(&string, 0), "text1,text2,text3,[text4,text5],text6");
153 
154  amxc_string_clean(&string);
155  assert_int_equal(amxc_string_join_llist(&string, &list, ' '), 0);
156  assert_string_equal(amxc_string_get(&string, 0), "text1 text2 text3 [text4,text5] text6");
157 
159  amxc_string_clean(&string);
160 }
161 
163  amxc_llist_t list;
164  amxc_string_t string;
165 
166  amxc_llist_init(&list);
167  amxc_string_init(&string, 0);
168  amxc_string_appendf(&string, "text1,text2,text3,[text4,text5],text6");
169 
170  assert_int_equal(amxc_string_split_to_llist(&string, &list, ','), 0);
171  amxc_string_clean(&string);
172  assert_int_not_equal(amxc_string_join_llist(&string, &list, '['), 0);
173  assert_true(amxc_string_is_empty(&string));
174  assert_int_not_equal(amxc_string_join_llist(&string, &list, ']'), 0);
175  assert_true(amxc_string_is_empty(&string));
176  assert_int_not_equal(amxc_string_join_llist(&string, &list, 'a'), 0);
177  assert_true(amxc_string_is_empty(&string));
178  assert_int_not_equal(amxc_string_join_llist(&string, &list, '9'), 0);
179  assert_true(amxc_string_is_empty(&string));
180 
181  assert_int_equal(amxc_string_join_llist(&string, &list, ':'), 0);
182  assert_string_equal(amxc_string_get(&string, 0), "text1:text2:text3:[text4,text5]:text6");
183 
185  amxc_string_clean(&string);
186 }
187 
189  amxc_llist_t list;
190  amxc_var_t var;
191  amxc_string_t string;
192 
193  amxc_llist_init(&list);
194  amxc_var_init(&var);
195  amxc_string_init(&string, 0);
196 
197  assert_int_not_equal(amxc_string_csv_join_var(NULL, &var), 0);
198  assert_int_not_equal(amxc_string_csv_join_var(&string, NULL), 0);
199  assert_int_not_equal(amxc_string_ssv_join_var(NULL, &var), 0);
200  assert_int_not_equal(amxc_string_ssv_join_var(&string, NULL), 0);
201 
202  assert_int_not_equal(amxc_string_join_llist(NULL, &list, '/'), 0);
203  assert_int_not_equal(amxc_string_join_llist(&string, NULL, '/'), 0);
204 
206  amxc_string_clean(&string);
208 }
#define UNUSED
Definition: amxc_macros.h:70
#define csv_string_t
Convenience macro.
Definition: amxc_variant.h:589
#define ssv_string_t
Convenience macro.
Definition: amxc_variant.h:594
#define cstring_t
Convenience macro.
Definition: amxc_variant.h:584
int amxc_llist_init(amxc_llist_t *const llist)
Initializes a linked list.
Definition: amxc_llist.c:111
void amxc_llist_clean(amxc_llist_t *const llist, amxc_llist_it_delete_t func)
Removes all items from the linked list.
Definition: amxc_llist.c:124
int amxc_string_join_llist(amxc_string_t *string, const amxc_llist_t *list, char separator)
Joins a list of amxc_string_t values into a single string with a separator.
int amxc_string_csv_join_var(amxc_string_t *string, const amxc_var_t *const var)
Joins a variant containing a list of variants into a single string using ',' as separator.
int amxc_string_ssv_join_var(amxc_string_t *string, const amxc_var_t *const var)
Joins a variant containing a list of variants into a single string using ' ' as separator.
amxc_string_split_status_t amxc_string_split_to_llist(const amxc_string_t *const string, amxc_llist_t *list, const char separator)
Simple split function using a single character separator.
void amxc_string_list_it_free(amxc_llist_it_t *it)
Helper function to delete an item in a linked list.
Definition: amxc_utils.c:327
const char * amxc_string_get(const amxc_string_t *const string, const size_t offset)
Gets the content of the string buffer.
Definition: amxc_string.c:339
void amxc_string_reset(amxc_string_t *const string)
Resets the buffer, reset the content to all 0.
Definition: amxc_string.c:203
int amxc_string_init(amxc_string_t *const string, const size_t length)
Initializes a string.
Definition: amxc_string.c:163
int amxc_string_appendf(amxc_string_t *const string, const char *fmt,...) __attribute__((format(printf
Appends a formatted string to a string.
AMXC_INLINE bool amxc_string_is_empty(const amxc_string_t *const string)
Checks if the string is empty.
Definition: amxc_string.h:1015
void amxc_string_clean(amxc_string_t *const string)
Frees the string buffer and reset length attributes.
Definition: amxc_string.c:189
#define AMXC_VAR_ID_LIST
Ambiorix Linked List variant id.
Definition: amxc_variant.h:206
#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
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
#define amxc_var_set(type, var, data)
Convenience macro for setting a value in a variant.
Definition: amxc_variant.h:609
The linked list structure.
Definition: amxc_llist.h:228
The string structure.
Definition: amxc_string.h:103
The variant struct definition.
Definition: amxc_variant.h:861
void test_join_adds_to_string(UNUSED void **state)
void test_split_and_join_to_list_provides_similar_string(UNUSED void **state)
void test_join_fails_on_wrong_variant_type(UNUSED void **state)
void test_can_join_variant_list_to_string(UNUSED void **state)
void test_join_does_input_argument_validation(UNUSED void **state)
void test_join_fails_with_invalid_separators(UNUSED void **state)
static amxc_var_t * var
Definition: test_issue_58.c:77