libamxc  1.10.3
C Generic Data Containers
test_amxc_string.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_variant.h>
65 #include <amxc/amxc_string.h>
66 #include <amxc/amxc_utils.h>
67 
68 #include "test_amxc_string.h"
69 
70 #include <amxc/amxc_macros.h>
71 char data[] = "abcdefghij";
72 
74  // passing NULL pointers should not lead to segfault
75  assert_int_not_equal(amxc_string_new(NULL, 0), 0);
76  amxc_string_delete(NULL);
77 }
78 
80  // passing NULL pointers should not lead to segfault
81  assert_int_equal(amxc_string_init(NULL, 0), -1);
82  amxc_string_clean(NULL);
83  amxc_string_reset(NULL);
84 }
85 
86 void test_amxc_string_new_delete(UNUSED void** state) {
87  amxc_string_t* string = NULL;
88 
89  assert_int_equal(amxc_string_new(&string, 0), 0);
90  assert_ptr_equal(string->buffer, NULL);
91  assert_int_equal(string->length, 0);
92  assert_int_equal(string->last_used, 0);
93  amxc_string_delete(&string);
94  assert_ptr_equal(string, NULL);
95 
96  assert_int_equal(amxc_string_new(&string, 10), 0);
97  assert_ptr_not_equal(string->buffer, NULL);
98  assert_int_equal(string->length, 10);
99  assert_int_equal(string->last_used, 0);
100  amxc_string_delete(&string);
101  assert_ptr_equal(string, NULL);
102 }
103 
105  amxc_string_t string;
106 
107  assert_int_equal(amxc_string_init(&string, 0), 0);
108  assert_ptr_equal(string.buffer, NULL);
109  assert_int_equal(string.length, 0);
110  assert_int_equal(string.last_used, 0);
111  amxc_string_clean(&string);
112  assert_int_equal(string.length, 0);
113  assert_int_equal(string.last_used, 0);
114 
115  assert_int_equal(amxc_string_init(&string, 10), 0);
116  assert_ptr_not_equal(string.buffer, NULL);
117  assert_int_equal(string.length, 10);
118  assert_int_equal(string.last_used, 0);
119  amxc_string_reset(&string);
120  assert_int_equal(string.length, 10);
121  assert_int_equal(string.last_used, 0);
122  amxc_string_clean(&string);
123  assert_ptr_equal(string.buffer, NULL);
124  assert_int_equal(string.length, 0);
125  assert_int_equal(string.last_used, 0);
126 }
127 
128 void test_amxc_string_grow_null(UNUSED void** state) {
129  assert_int_equal(amxc_string_grow(NULL, 5), -1);
130 }
131 
132 void test_amxc_string_grow(UNUSED void** state) {
133  amxc_string_t string;
134 
135  assert_int_equal(amxc_string_init(&string, 5), 0);
136 
137  assert_int_equal(amxc_string_grow(&string, 0), 0);
138  assert_ptr_not_equal(string.buffer, NULL);
139  assert_int_equal(string.length, 5);
140  assert_int_equal(string.last_used, 0);
141 
142  assert_int_equal(amxc_string_grow(&string, 5), 0);
143  assert_ptr_not_equal(string.buffer, NULL);
144  assert_int_equal(string.length, 10);
145  assert_int_equal(string.last_used, 0);
146 
147  assert_int_equal(amxc_string_grow(&string, 7), 0);
148  assert_ptr_not_equal(string.buffer, NULL);
149  assert_int_equal(string.length, 17);
150  assert_int_equal(string.last_used, 0);
151 
152  amxc_string_clean(&string);
153 }
154 
156  assert_int_equal(amxc_string_shrink(NULL, 5), -1);
157 }
158 
159 void test_amxc_string_shrink(UNUSED void** state) {
160  amxc_string_t string;
161 
162  assert_int_equal(amxc_string_init(&string, 15), 0);
163 
164  assert_int_equal(amxc_string_shrink(&string, 20), -1);
165 
166  assert_int_equal(amxc_string_shrink(&string, 0), 0);
167  assert_ptr_not_equal(string.buffer, NULL);
168  assert_int_equal(string.length, 15);
169  assert_int_equal(string.last_used, 0);
170 
171  assert_int_equal(amxc_string_shrink(&string, 5), 0);
172  assert_ptr_not_equal(string.buffer, NULL);
173  assert_int_equal(string.length, 10);
174  assert_int_equal(string.last_used, 0);
175 
176  assert_int_equal(amxc_string_shrink(&string, 3), 0);
177  assert_ptr_not_equal(string.buffer, NULL);
178  assert_int_equal(string.length, 7);
179  assert_int_equal(string.last_used, 0);
180 
181  assert_int_equal(amxc_string_shrink(&string, 7), 0);
182  assert_ptr_equal(string.buffer, NULL);
183  assert_int_equal(string.length, 0);
184  assert_int_equal(string.last_used, 0);
185 
186  amxc_string_clean(&string);
187 }
188 
189 void test_amxc_string_append(UNUSED void** state) {
190  amxc_string_t string;
191 
192  assert_int_equal(amxc_string_init(&string, 10), 0);
193 
194  assert_int_not_equal(amxc_string_append(NULL, "abcde", 5), 0);
195  assert_int_not_equal(amxc_string_append(&string, NULL, 0), 0);
196  assert_int_not_equal(amxc_string_append(&string, "abcde", 0), 0);
197 
198  assert_int_equal(amxc_string_append(&string, "abcde", 5), 0);
199  assert_int_equal(string.length, 10);
200  assert_int_equal(string.last_used, 5);
201  assert_string_equal(string.buffer, "abcde");
202 
203  assert_int_equal(amxc_string_append(&string, "12345", 5), 0);
204  assert_int_equal(string.length, 11);
205  assert_int_equal(string.last_used, 10);
206  assert_string_equal(string.buffer, "abcde12345");
207 
208  assert_int_equal(amxc_string_append(&string, "abcde", 5), 0);
209  assert_int_equal(string.length, 16);
210  assert_int_equal(string.last_used, 15);
211  assert_string_equal(string.buffer, "abcde12345abcde");
212 
213  assert_int_equal(amxc_string_shrink(&string, 5), 0);
214  assert_int_equal(string.length, 11);
215  assert_int_equal(string.last_used, 10);
216 
217  amxc_string_clean(&string);
218 }
219 
220 void test_amxc_string_prepend(UNUSED void** state) {
221  amxc_string_t string;
222 
223  assert_int_equal(amxc_string_init(&string, 10), 0);
224 
225  assert_int_not_equal(amxc_string_prepend(NULL, "abcde", 5), 0);
226  assert_int_not_equal(amxc_string_prepend(&string, NULL, 0), 0);
227  assert_int_not_equal(amxc_string_prepend(&string, "abcde", 0), 0);
228 
229  assert_int_equal(amxc_string_prepend(&string, "abcde", 5), 0);
230  assert_int_equal(string.length, 10);
231  assert_int_equal(string.last_used, 5);
232  assert_string_equal(string.buffer, "abcde");
233 
234  assert_int_equal(amxc_string_prepend(&string, "12345", 5), 0);
235  assert_int_equal(string.length, 11);
236  assert_int_equal(string.last_used, 10);
237  assert_string_equal(string.buffer, "12345abcde");
238 
239  assert_int_equal(amxc_string_prepend(&string, "abcde", 5), 0);
240  assert_int_equal(string.length, 16);
241  assert_int_equal(string.last_used, 15);
242  assert_string_equal(string.buffer, "abcde12345abcde");
243 
244  assert_int_equal(amxc_string_shrink(&string, 5), 0);
245  assert_int_equal(string.length, 11);
246  assert_int_equal(string.last_used, 10);
247 
248  amxc_string_clean(&string);
249 }
250 
252  amxc_string_t string;
253 
254  assert_int_equal(amxc_string_init(&string, 10), 0);
255 
256  assert_int_equal(amxc_string_buffer_length(NULL), 0);
257  assert_int_equal(amxc_string_buffer_length(&string), 10);
258  assert_int_equal(amxc_string_shrink(&string, 5), 0);
259  assert_int_equal(amxc_string_buffer_length(&string), 5);
260  assert_int_equal(amxc_string_grow(&string, 15), 0);
261  assert_int_equal(amxc_string_buffer_length(&string), 20);
262 
263  amxc_string_clean(&string);
264 }
265 
267  amxc_string_t string;
268 
269  assert_int_equal(amxc_string_init(&string, 10), 0);
270 
271  assert_int_equal(amxc_string_text_length(NULL), 0);
272  assert_int_equal(amxc_string_text_length(&string), 0);
273  assert_int_equal(amxc_string_append(&string, "abcde", 5), 0);
274  assert_int_equal(amxc_string_text_length(&string), 5);
275  assert_int_equal(amxc_string_append(&string, "abcde", 5), 0);
276  assert_int_equal(amxc_string_text_length(&string), 10);
277  assert_int_equal(amxc_string_append(&string, "abcde", 5), 0);
278  assert_int_equal(amxc_string_text_length(&string), 15);
279  assert_int_equal(amxc_string_shrink(&string, 5), 0);
280  assert_int_equal(amxc_string_text_length(&string), 10);
281  assert_int_equal(amxc_string_grow(&string, 15), 0);
282  assert_int_equal(amxc_string_text_length(&string), 10);
283 
284  amxc_string_clean(&string);
285 }
286 
287 void test_amxc_string_set_at(UNUSED void** state) {
288  amxc_string_t string;
289 
290  assert_int_equal(amxc_string_init(&string, 10), 0);
291  assert_int_not_equal(amxc_string_set_at(NULL,
292  0,
293  "hello world",
294  11,
296  assert_int_not_equal(amxc_string_set_at(&string,
297  5,
298  "hello world",
299  11,
301  assert_int_not_equal(amxc_string_set_at(&string,
302  0,
303  "",
304  0,
306  assert_int_equal(amxc_string_set_at(&string,
307  0,
308  "hello world",
309  11,
311  assert_int_equal(string.length, 12);
312  assert_int_equal(string.last_used, 11);
313  assert_int_equal(amxc_string_set_at(&string,
314  6,
315  "Europe",
316  6,
318  assert_int_equal(string.length, 12);
319  assert_int_equal(string.last_used, 12);
320  assert_string_equal(string.buffer, "hello Europe");
321 
322  assert_int_equal(amxc_string_set_at(&string,
323  12,
324  "and the rest of the world",
325  25,
327 
328  assert_int_equal(amxc_string_set_at(&string,
329  6,
330  "Asia",
331  4,
333  assert_int_equal(string.length, 37);
334  assert_int_equal(string.last_used, 37);
335  assert_string_equal(string.buffer, "hello Asiapeand the rest of the world");
336 
337  amxc_string_clean(&string);
338 }
339 
340 void test_amxc_string_remove_at(UNUSED void** state) {
341  amxc_string_t string;
342 
343  assert_int_equal(amxc_string_init(&string, 10), 0);
344  assert_int_not_equal(amxc_string_remove_at(NULL,
345  0,
346  10), 0);
347  assert_int_not_equal(amxc_string_remove_at(&string,
348  0,
349  0), 0);
350  assert_int_not_equal(amxc_string_remove_at(&string,
351  5,
352  10), 0);
353  assert_int_equal(amxc_string_set_at(&string,
354  0,
355  "hello world",
356  11,
358 
359  assert_int_equal(amxc_string_remove_at(&string,
360  5,
361  AMXC_STRING_MAX), 0);
362  assert_int_equal(string.length, 12);
363  assert_int_equal(string.last_used, 5);
364  assert_string_equal(string.buffer, "hello");
365 
366  assert_int_equal(amxc_string_remove_at(&string, 2, 2), 0);
367  assert_int_equal(string.length, 12);
368  assert_int_equal(string.last_used, 3);
369  assert_string_equal(string.buffer, "heo");
370 
371  amxc_string_reset(&string);
372  assert_int_equal(amxc_string_set_at(&string,
373  0,
374  "hello world",
375  11,
377 
378  assert_int_equal(amxc_string_remove_at(&string, 5, 35), 0);
379  assert_int_equal(string.length, 12);
380  assert_int_equal(string.last_used, 5);
381  assert_string_equal(string.buffer, "hello");
382 
383  amxc_string_clean(&string);
384 }
385 
386 void test_amxc_string_get(UNUSED void** state) {
387  amxc_string_t string;
388 
389  assert_int_equal(amxc_string_init(&string, 0), 0);
390  assert_string_equal(amxc_string_get(&string, 0), "");
391  amxc_string_clean(&string);
392 
393  assert_int_equal(amxc_string_init(&string, 10), 0);
394  assert_ptr_equal(amxc_string_get(NULL, 0), NULL);
395  assert_ptr_equal(amxc_string_get(&string, 5), NULL);
396  assert_int_equal(amxc_string_set_at(&string,
397  0,
398  "hello world",
399  11,
401  assert_ptr_equal(amxc_string_get(&string, 12), NULL);
402 
403  assert_string_equal(amxc_string_get(&string, 0), "hello world");
404  assert_string_equal(amxc_string_get(&string, 6), "world");
405 
406  amxc_string_clean(&string);
407 }
408 
409 void test_amxc_string_dup(UNUSED void** state) {
410  amxc_string_t string;
411  char* dup_buffer = NULL;
412 
413  assert_int_equal(amxc_string_init(&string, 10), 0);
414  assert_ptr_equal(amxc_string_dup(NULL, 0, 10), NULL);
415  assert_ptr_equal(amxc_string_dup(&string, 5, 10), NULL);
416  assert_int_equal(amxc_string_set_at(&string,
417  0,
418  "hello world",
419  11,
421 
422  assert_ptr_equal(amxc_string_dup(&string, 15, 15), NULL);
423  assert_ptr_equal(amxc_string_dup(&string, 15, 0), NULL);
424  assert_ptr_equal(amxc_string_dup(&string, 0, 0), NULL);
425 
426  dup_buffer = amxc_string_dup(&string, 0, AMXC_STRING_MAX);
427  assert_ptr_not_equal(dup_buffer, NULL);
428  assert_string_equal(dup_buffer, "hello world");
429  free(dup_buffer);
430 
431  dup_buffer = amxc_string_dup(&string, 0, 25);
432  assert_ptr_not_equal(dup_buffer, NULL);
433  assert_string_equal(dup_buffer, "hello world");
434  free(dup_buffer);
435 
436  dup_buffer = amxc_string_dup(&string, 6, 25);
437  assert_ptr_not_equal(dup_buffer, NULL);
438  assert_string_equal(dup_buffer, "world");
439  free(dup_buffer);
440 
441  dup_buffer = amxc_string_dup(&string, 2, 2);
442  assert_ptr_not_equal(dup_buffer, NULL);
443  assert_string_equal(dup_buffer, "ll");
444  free(dup_buffer);
445 
446  amxc_string_clean(&string);
447 }
448 
449 void test_amxc_string_trim(UNUSED void** state) {
450  amxc_string_t string;
451 
452  amxc_string_triml(NULL, NULL);
453  amxc_string_trimr(NULL, NULL);
454  amxc_string_trim(NULL, NULL);
455 
456  assert_int_equal(amxc_string_init(&string, 10), 0);
457 
458  amxc_string_triml(&string, isdigit);
459  amxc_string_trimr(&string, isspace);
460  assert_int_equal(string.last_used, 0);
461 
462  assert_int_equal(amxc_string_set_at(&string,
463  0,
464  " \t hello world\t ",
465  18,
467 
468  amxc_string_triml(&string, isdigit);
469  assert_int_equal(string.last_used, 18);
470  amxc_string_trimr(&string, isdigit);
471  assert_int_equal(string.last_used, 18);
472 
473  assert_int_equal(string.last_used, 18);
474  amxc_string_triml(&string, NULL);
475  assert_int_equal(string.last_used, 14);
476  assert_string_equal(string.buffer, "hello world\t ");
477 
478  amxc_string_trimr(&string, NULL);
479  assert_int_equal(string.last_used, 11);
480  assert_string_equal(string.buffer, "hello world");
481 
482  amxc_string_trim(&string, NULL);
483  assert_string_equal(string.buffer, "hello world");
484  amxc_string_reset(&string);
485 
486  assert_int_equal(amxc_string_set_at(&string,
487  0,
488  "1234567890",
489  10,
491  amxc_string_triml(&string, isdigit);
492  assert_int_equal(string.last_used, 0);
493  amxc_string_reset(&string);
494 
495  assert_int_equal(amxc_string_set_at(&string,
496  0,
497  "1234567890",
498  10,
500  amxc_string_trimr(&string, isdigit);
501  assert_int_equal(string.last_used, 0);
502  amxc_string_reset(&string);
503 
504  assert_int_equal(amxc_string_set_at(&string,
505  0,
506  " \t hello world\t ",
507  18,
509  amxc_string_trim(&string, NULL);
510  assert_string_equal(string.buffer, "hello world");
511 
512  assert_int_equal(amxc_string_setf(&string, "%s", " "), 0);
513  amxc_string_triml(&string, NULL);
514  assert_string_equal(string.buffer, "");
515 
516  assert_int_equal(amxc_string_setf(&string, "%s", " "), 0);
517  amxc_string_trimr(&string, NULL);
518  assert_string_equal(string.buffer, "");
519 
520  amxc_string_clean(&string);
521 }
522 
524  amxc_string_t string;
525  char* buffer = NULL;
526 
527  assert_ptr_equal(amxc_string_take_buffer(NULL), NULL);
528  assert_int_not_equal(amxc_string_push_buffer(NULL, NULL, 0), NULL);
529 
530  assert_int_equal(amxc_string_init(&string, 0), 0);
531  assert_ptr_equal(amxc_string_take_buffer(&string), NULL);
532  assert_int_equal(amxc_string_append(&string, "Hello World", 11), 0);
533 
534  buffer = amxc_string_take_buffer(&string);
535  assert_ptr_not_equal(buffer, NULL);
536  assert_string_equal(buffer, "Hello World");
537  assert_int_equal(string.length, 0);
538  assert_int_equal(string.last_used, 0);
539  assert_ptr_equal(string.buffer, NULL);
540 
541  assert_int_equal(amxc_string_push_buffer(&string, NULL, 0), 0);
542  assert_int_not_equal(amxc_string_push_buffer(&string, buffer, 0), 0);
543  assert_int_not_equal(amxc_string_push_buffer(&string, buffer, 5), 0);
544  assert_int_equal(amxc_string_push_buffer(&string, buffer, 12), 0);
545 
546  amxc_string_clean(&string);
547 }
548 
549 void test_amxc_string_setf(UNUSED void** state) {
550  amxc_string_t string;
551 
552  assert_int_equal(amxc_string_init(&string, 0), 0);
553  assert_int_not_equal(amxc_string_setf(NULL, NULL), 0);
554  assert_int_not_equal(amxc_string_setf(&string, NULL), 0);
555 
556  assert_int_equal(amxc_string_setf(&string, "Hello world"), 0);
557  assert_int_equal(string.length, 12);
558  assert_int_equal(string.last_used, 11);
559  assert_string_equal(amxc_string_get(&string, 0), "Hello world");
560  assert_int_equal(string.buffer[string.last_used], 0);
561 
562  assert_int_equal(amxc_string_setf(&string, "%s-%d", "acracadabra", 5), 0);
563  assert_string_equal(amxc_string_get(&string, 0), "acracadabra-5");
564  assert_int_equal(string.buffer[string.last_used], 0);
565 
566 
567  assert_int_equal(amxc_string_setf(&string, "%d", 1), 0);
568 
569  amxc_string_clean(&string);
570 }
571 
572 void test_amxc_string_appendf(UNUSED void** state) {
573  amxc_string_t string;
574 
575  assert_int_equal(amxc_string_init(&string, 0), 0);
576  assert_int_not_equal(amxc_string_appendf(NULL, NULL), 0);
577  assert_int_not_equal(amxc_string_appendf(&string, NULL), 0);
578 
579  assert_int_equal(amxc_string_appendf(&string, "Hello world"), 0);
580  assert_int_equal(string.length, 12);
581  assert_int_equal(string.last_used, 11);
582  assert_string_equal(string.buffer, "Hello world");
583 
584  assert_int_equal(amxc_string_appendf(&string, "%s-%d", "acracadabra", 5), 0);
585  assert_string_equal(string.buffer, "Hello worldacracadabra-5");
586 
587  amxc_string_reset(&string);
588 
589  assert_int_equal(amxc_string_appendf(&string, "%d", 1), 0);
590 
591  amxc_string_clean(&string);
592 }
593 
594 void test_amxc_string_prependf(UNUSED void** state) {
595  amxc_string_t string;
596 
597  assert_int_equal(amxc_string_init(&string, 0), 0);
598  assert_int_not_equal(amxc_string_prependf(NULL, NULL), 0);
599  assert_int_not_equal(amxc_string_prependf(&string, NULL), 0);
600 
601  assert_int_equal(amxc_string_prependf(&string, "Hello world"), 0);
602  assert_int_equal(string.length, 12);
603  assert_int_equal(string.last_used, 11);
604  assert_string_equal(string.buffer, "Hello world");
605 
606  assert_int_equal(amxc_string_prependf(&string, "%s-%d", "acracadabra", 5), 0);
607  assert_string_equal(string.buffer, "acracadabra-5Hello world");
608 
609  amxc_string_reset(&string);
610 
611  assert_int_equal(amxc_string_prependf(&string, "%d", 1), 0);
612 
613  amxc_string_clean(&string);
614 }
615 
617  amxc_string_t string;
618 
619  assert_int_equal(amxc_string_init(&string, 0), 0);
620  assert_false(amxc_string_is_numeric(&string));
621  amxc_string_clean(&string);
622 
623  assert_int_equal(amxc_string_init(&string, 0), 0);
624  assert_int_equal(amxc_string_set_at(&string,
625  0,
626  "hello world",
627  11,
629  assert_false(amxc_string_is_numeric(&string));
630  amxc_string_clean(&string);
631 
632  assert_int_equal(amxc_string_init(&string, 0), 0);
633  assert_int_equal(amxc_string_set_at(&string,
634  0,
635  "42world",
636  7,
638  assert_false(amxc_string_is_numeric(&string));
639  amxc_string_clean(&string);
640 
641  assert_int_equal(amxc_string_init(&string, 0), 0);
642  assert_int_equal(amxc_string_set_at(&string,
643  0,
644  "world42",
645  7,
647  assert_false(amxc_string_is_numeric(&string));
648  amxc_string_clean(&string);
649 
650  assert_int_equal(amxc_string_init(&string, 0), 0);
651  assert_int_equal(amxc_string_set_at(&string,
652  0,
653  "42 world",
654  8,
656  assert_false(amxc_string_is_numeric(&string));
657  amxc_string_clean(&string);
658 
659  assert_int_equal(amxc_string_init(&string, 0), 0);
660  assert_int_equal(amxc_string_set_at(&string,
661  0,
662  "42",
663  2,
665  assert_true(amxc_string_is_numeric(&string));
666  amxc_string_clean(&string);
667 
668  assert_int_equal(amxc_string_init(&string, 0), 0);
669  assert_int_equal(amxc_string_set_at(&string,
670  0,
671  "00",
672  2,
674  assert_true(amxc_string_is_numeric(&string));
675  amxc_string_clean(&string);
676 
677  assert_int_equal(amxc_string_init(&string, 0), 0);
678  assert_int_equal(amxc_string_set_at(&string,
679  0,
680  "2",
681  1,
683  assert_true(amxc_string_is_numeric(&string));
684  amxc_string_clean(&string);
685 }
686 
688  amxc_string_t string;
689  setenv("TestEnvVar", "MyValue", 1);
690  setenv("RefVar", "$(TestEnvVar)", 1);
691 
692  assert_int_equal(amxc_string_init(&string, 0), 0);
693 
694  assert_int_equal(amxc_string_appendf(&string, "Resolves Env Var $(TestEnvVar)"), 0);
695  assert_int_equal(amxc_string_resolve_env(&string), 1);
696  assert_string_equal(string.buffer, "Resolves Env Var MyValue");
697  amxc_string_reset(&string);
698 
699  assert_int_equal(amxc_string_appendf(&string, "$(TestEnvVar) $(TestEnvVar) $TestEnvVar TestEnvVar"), 0);
700  assert_int_equal(amxc_string_resolve_env(&string), 2);
701  assert_string_equal(string.buffer, "MyValue MyValue $TestEnvVar TestEnvVar");
702  amxc_string_reset(&string);
703 
704  assert_int_equal(amxc_string_appendf(&string, "$(TestEnvVar) $(RefVar)"), 0);
705  assert_int_equal(amxc_string_resolve_env(&string), 3);
706  assert_string_equal(string.buffer, "MyValue MyValue");
707  amxc_string_reset(&string);
708 
709  assert_int_equal(amxc_string_appendf(&string, "$(Dummy) $(RefVar)"), 0);
710  assert_int_equal(amxc_string_resolve_env(&string), 3);
711  assert_string_equal(string.buffer, " MyValue");
712  amxc_string_reset(&string);
713 
714  assert_int_equal(amxc_string_appendf(&string, "$(Dummy) $(RefVar"), 0);
715  assert_int_equal(amxc_string_resolve_env(&string), 1);
716  assert_string_equal(string.buffer, " $(RefVar");
717  amxc_string_reset(&string);
718 
719  assert_int_equal(amxc_string_appendf(&string, "$(Dummy - $(RefVar"), 0);
720  assert_int_equal(amxc_string_resolve_env(&string), 0);
721  assert_string_equal(string.buffer, "$(Dummy - $(RefVar");
722  amxc_string_reset(&string);
723 
724  assert_int_equal(amxc_string_resolve_env(&string), 0);
725  assert_int_equal(amxc_string_resolve_env(NULL), 0);
726 
727  amxc_string_clean(&string);
728 }
729 
731  amxc_string_t string;
733 
736  amxc_var_add_key(cstring_t, &data, "rw_data_path", "/tmp/");
737  amxc_var_add_key(cstring_t, &data, "name", "a_name");
738  amxc_var_add_key(cstring_t, &data, "ref", "${name}");
739 
740  assert_int_equal(amxc_string_init(&string, 0), 0);
741 
742  assert_int_equal(amxc_string_appendf(&string, "Resolves Var ${rw_data_path}"), 0);
743  assert_int_equal(amxc_string_resolve_var(&string, &data), 1);
744  assert_string_equal(string.buffer, "Resolves Var /tmp/");
745  amxc_string_reset(&string);
746 
747  assert_int_equal(amxc_string_appendf(&string, "${name} ${rw_data_path} $rw_data_path rw_data_path"), 0);
748  assert_int_equal(amxc_string_resolve_var(&string, &data), 2);
749  assert_string_equal(string.buffer, "a_name /tmp/ $rw_data_path rw_data_path");
750  amxc_string_reset(&string);
751 
752  assert_int_equal(amxc_string_appendf(&string, "${name} ${ref}"), 0);
753  assert_int_equal(amxc_string_resolve_var(&string, &data), 3);
754  assert_string_equal(string.buffer, "a_name a_name");
755  amxc_string_reset(&string);
756 
757  assert_int_equal(amxc_string_appendf(&string, "${Dummy} ${ref}"), 0);
758  assert_int_equal(amxc_string_resolve_var(&string, &data), 3);
759  assert_string_equal(string.buffer, " a_name");
760  amxc_string_reset(&string);
761 
762  assert_int_equal(amxc_string_resolve_var(&string, &data), 0);
763  assert_int_equal(amxc_string_appendf(&string, "${Dummy} ${ref}"), 0);
764  assert_int_equal(amxc_string_resolve_var(&string, NULL), 0);
765  assert_int_equal(amxc_string_resolve_var(NULL, &data), 0);
767  assert_int_equal(amxc_string_resolve_var(&string, &data), 0);
768 
770  amxc_string_clean(&string);
771 }
772 
773 void test_amxc_string_resolve(UNUSED void** state) {
774  amxc_string_t string;
776 
777  setenv("TESTENV", "MyValue", 1);
778  setenv("DATAREF", "${name}", 1);
779 
782  amxc_var_add_key(cstring_t, &data, "rw_data_path", "/tmp/");
783  amxc_var_add_key(cstring_t, &data, "name", "a_name");
784  amxc_var_add_key(cstring_t, &data, "ref", "$(TESTENV)");
785 
786  assert_int_equal(amxc_string_init(&string, 0), 0);
787 
788  assert_int_equal(amxc_string_appendf(&string, "TESTENV = $(TESTENV) & name = ${name}"), 0);
789  assert_int_equal(amxc_string_resolve(&string, &data), 2);
790  assert_string_equal(string.buffer, "TESTENV = MyValue & name = a_name");
791  amxc_string_reset(&string);
792 
793  assert_int_equal(amxc_string_appendf(&string, "name = $(DATAREF) & TESTENV = ${ref}"), 0);
794  assert_int_equal(amxc_string_resolve(&string, &data), 4);
795  assert_string_equal(string.buffer, "name = a_name & TESTENV = MyValue");
796  amxc_string_reset(&string);
797 
798  assert_int_equal(amxc_string_appendf(&string, "${ref} ${rw_data_path} $(TESTENV) ${name}"), 0);
799  assert_int_equal(amxc_string_resolve(&string, &data), 5);
800  assert_string_equal(string.buffer, "MyValue /tmp/ MyValue a_name");
801  amxc_string_reset(&string);
802 
803  assert_int_equal(amxc_string_appendf(&string, "$\\{ref\\} \\\" $\\(TESTENV\\) \\' \\\\"), 0);
804  assert_int_equal(amxc_string_resolve(&string, &data), 7);
805  assert_string_equal(string.buffer, "${ref} \" $(TESTENV) ' \\");
806  amxc_string_reset(&string);
807 
808  assert_int_equal(amxc_string_appendf(&string, "\\${ref} \\$(TESTENV)"), 0);
809  assert_int_equal(amxc_string_resolve(&string, &data), 2);
810  assert_string_equal(string.buffer, "${ref} $(TESTENV)");
811  amxc_string_reset(&string);
812 
813  assert_int_equal(amxc_string_appendf(&string, "${ref} \" $(TESTENV) ' \\"), 0);
814  assert_int_equal(amxc_string_esc(&string), 7);
815  assert_string_equal(string.buffer, "$\\{ref\\} \\\" $\\(TESTENV\\) \\' \\\\");
816  amxc_string_reset(&string);
817 
818  assert_int_equal(amxc_string_appendf(&string, "\n\t\\n"), 0);
819  assert_int_equal(amxc_string_esc(&string), 3);
820  assert_string_equal(string.buffer, "\\n\\t\\\\n");
821  assert_int_equal(amxc_string_append(&string, "\\", 1), 0);
822  assert_int_equal(amxc_string_resolve_esc(&string), 3);
823  assert_string_equal(string.buffer, "\n\t\\n\\");
824  amxc_string_reset(&string);
825 
827  amxc_string_clean(&string);
828 }
829 
831  amxc_string_t string;
833 
834  setenv("TESTENV", "MyValue", 1);
835  setenv("DATAREF", "${name}", 1);
836 
839  amxc_var_add_key(cstring_t, &data, "rw_data_path", "/tmp/");
840  amxc_var_add_key(cstring_t, &data, "name", "a_name");
841  amxc_var_add_key(cstring_t, &data, "ref", "$(TESTENV)");
842 
843  assert_int_equal(amxc_string_init(&string, 0), 0);
844 
845  assert_int_equal(amxc_string_set_resolved(&string, "TESTENV = $(TESTENV) & name = ${name}", &data), 2);
846  assert_string_equal(string.buffer, "TESTENV = MyValue & name = a_name");
847 
848  assert_int_equal(amxc_string_set_resolved(&string, "This is text without variables", &data), 0);
849  assert_true(amxc_string_is_empty(&string));
850 
851  assert_int_equal(amxc_string_set_resolved(&string, "This is text without $variables", &data), 0);
852  assert_true(amxc_string_is_empty(&string));
853 
854  assert_int_equal(amxc_string_set_resolved(&string, NULL, &data), 0);
855  assert_int_equal(amxc_string_set_resolved(&string, "", &data), 0);
856  assert_int_equal(amxc_string_set_resolved(NULL, "TESTENV = $(TESTENV) & name = ${name}", &data), 0);
857 
859  amxc_string_clean(&string);
860 }
861 
863  amxc_string_t* string = NULL;
865 
866  setenv("TESTENV", "MyValue", 1);
867  setenv("DATAREF", "${name}", 1);
868 
871  amxc_var_add_key(cstring_t, &data, "rw_data_path", "/tmp/");
872  amxc_var_add_key(cstring_t, &data, "name", "a_name");
873  amxc_var_add_key(cstring_t, &data, "ref", "$(TESTENV)");
874 
875  assert_int_equal(amxc_string_new_resolved(&string, "TESTENV = $(TESTENV) & name = ${name}", &data), 2);
876  assert_ptr_not_equal(string, NULL);
877  assert_string_equal(string->buffer, "TESTENV = MyValue & name = a_name");
878  amxc_string_delete(&string);
879 
880  assert_int_equal(amxc_string_new_resolved(&string, "This is text without variables", &data), 0);
881  assert_ptr_equal(string, NULL);
882 
883  assert_int_equal(amxc_string_new_resolved(&string, "This is text without $variables", &data), 0);
884  assert_ptr_equal(string, NULL);
885 
886  assert_int_equal(amxc_string_new_resolved(&string, NULL, &data), 0);
887  assert_ptr_equal(string, NULL);
888  assert_int_equal(amxc_string_new_resolved(&string, "", &data), 0);
889  assert_ptr_equal(string, NULL);
890  assert_int_equal(amxc_string_new_resolved(NULL, "TESTENV = $(TESTENV) & name = ${name}", &data), -1);
891  assert_ptr_equal(string, NULL);
892 
894 }
895 
896 void test_amxc_llist_add_string(UNUSED void** state) {
897  amxc_llist_t list;
898  amxc_llist_init(&list);
899 
900  assert_ptr_not_equal(amxc_llist_add_string(&list, "text1"), NULL);
901  assert_ptr_not_equal(amxc_llist_add_string(&list, "text2"), NULL);
902  assert_ptr_not_equal(amxc_llist_add_string(&list, "text3"), NULL);
903  assert_ptr_equal(amxc_llist_add_string(&list, NULL), NULL);
904  assert_ptr_equal(amxc_llist_add_string(&list, ""), NULL);
905  assert_int_equal(amxc_llist_size(&list), 3);
906 
907  assert_ptr_equal(amxc_llist_add_string(NULL, "text4"), NULL);
908 
910 }
911 
912 void test_amxc_string_search(UNUSED void** state) {
913  amxc_string_t string;
914  amxc_string_init(&string, 0);
915 
916  assert_int_equal(amxc_string_search(&string, "text", 0), -1);
917  amxc_string_setf(&string, "This is some text");
918  assert_int_equal(amxc_string_search(&string, "text", 0), 13);
919  assert_int_equal(amxc_string_search(&string, "Too long needle, it can not be in the text", 0), -1);
920  amxc_string_appendf(&string, ", adding more text");
921  assert_int_equal(amxc_string_search(&string, "text", 0), 13);
922  assert_int_equal(amxc_string_search(&string, "text", 17), 31);
923  assert_int_equal(amxc_string_search(&string, "NOT IN THERE", 0), -1);
924 
925  assert_int_equal(amxc_string_search(NULL, "text", 0), -1);
926  assert_int_equal(amxc_string_search(&string, "", 0), -1);
927  assert_int_equal(amxc_string_search(&string, NULL, 0), -1);
928  assert_int_equal(amxc_string_search(&string, "more", 1000), -1);
929 
930  amxc_string_clean(&string);
931 }
932 
933 void test_amxc_string_replace(UNUSED void** state) {
934  amxc_string_t string;
935  amxc_string_init(&string, 0);
936 
937  assert_int_equal(amxc_string_replace(&string, "text", "dummy", 10), 0);
938  amxc_string_setf(&string, "This is text and some more text and more text text");
939  assert_int_equal(amxc_string_replace(&string, "text", "dummy", 1), 1);
940  assert_int_equal(amxc_string_replace(&string, "text", "foo", 2), 2);
941  assert_int_equal(amxc_string_replace(&string, "text", "dummy-foo extra text", UINT32_MAX), 1);
942  assert_int_equal(amxc_string_replace(&string, "text", "foo", UINT32_MAX), 1);
943  assert_int_equal(amxc_string_replace(&string, "text", "dummy", UINT32_MAX), 0);
944  assert_string_equal(string.buffer, "This is dummy and some more foo and more foo dummy-foo extra foo");
945 
946  amxc_string_setf(&string, "This is text and some more text and more text text");
947  assert_int_equal(amxc_string_replace(&string, "text", "text", UINT32_MAX), 4);
948  assert_string_equal(string.buffer, "This is text and some more text and more text text");
949 
950  assert_int_equal(amxc_string_replace(NULL, "text", "dummy", 10), 0);
951  assert_int_equal(amxc_string_replace(&string, "", "dummy", 10), 0);
952  assert_int_equal(amxc_string_replace(&string, "text", "", 10), 4);
953  assert_string_equal(string.buffer, "This is and some more and more ");
954 
955  amxc_string_setf(&string, "abbaabbaabbaabbaabbaabba");
956  assert_int_equal(amxc_string_replace(&string, "abba", "12", UINT32_MAX), 6);
957 
958  amxc_string_reset(&string);
959  assert_int_equal(amxc_string_replace(&string, "", "", UINT32_MAX), 0);
960 
961  assert_int_equal(amxc_string_replace(&string, NULL, "more", 10), 0);
962  assert_int_equal(amxc_string_replace(&string, "more", NULL, 10), 0);
963 
964  amxc_string_clean(&string);
965 }
966 
967 void test_amxc_string_copy(UNUSED void** state) {
968  amxc_string_t src;
969  amxc_string_t dst;
970 
971  amxc_string_init(&src, 0);
972  amxc_string_init(&dst, 0);
973 
974  amxc_string_setf(&src, "Hello world, I am so happy");
975  assert_int_equal(amxc_string_copy(&dst, &src), 0);
976 
977  assert_int_equal(dst.length, src.length);
978  assert_int_equal(dst.last_used, src.last_used);
979  assert_string_equal(dst.buffer, src.buffer);
980 
981  amxc_string_clean(&src);
982  assert_int_equal(amxc_string_copy(&dst, &src), 0);
983 
984  assert_int_not_equal(dst.length, src.length);
985  assert_int_equal(dst.last_used, src.last_used);
986  assert_string_equal(dst.buffer, "");
987 
988  amxc_string_clean(&src);
989  amxc_string_clean(&dst);
990 
991  assert_int_not_equal(amxc_string_copy(&dst, NULL), 0);
992  assert_int_not_equal(amxc_string_copy(NULL, &src), 0);
993 }
994 
995 void test_amxc_string_reset(UNUSED void** state) {
996  amxc_string_t string;
997  amxc_string_init(&string, 0);
998  amxc_string_setf(&string, "Hello world, I am so happy");
999 
1000  amxc_string_reset(&string);
1001  assert_int_equal(string.last_used, 0);
1002  assert_string_equal(string.buffer, "");
1003  amxc_string_clean(&string);
1004 }
1005 
1006 void test_amxc_string_set(UNUSED void** state) {
1007  amxc_string_t string;
1008  amxc_string_init(&string, 0);
1009 
1010  assert_int_equal(amxc_string_set(&string, "Hello world, I am so happy"), 26);
1011  assert_string_equal(amxc_string_get(&string, 0), "Hello world, I am so happy");
1012  assert_int_equal(amxc_string_set(&string, NULL), 0);
1013  assert_string_equal(amxc_string_get(&string, 0), "");
1014  assert_int_equal(amxc_string_set(NULL, "Test"), 0);
1015 
1016  amxc_string_clean(&string);
1017 }
1018 
1020  amxc_string_t string;
1021  amxc_ts_t time;
1022  char data[11] = { 0xA0, 0x1B, 0xC2, 0x3D, 0xE4, 0x00, 0xA5, 0x6B, 0xC7, 0x8D, 0xE9 };
1023 
1024  memset(&time, 0, sizeof(amxc_ts_t));
1025  amxc_ts_parse(&time, "2020-02-29T16:16:16.123456-01:15", strlen("2020-02-29T16:16:16.123456-01:15"));
1026 
1027  amxc_string_init(&string, 0);
1028  assert_int_equal(amxc_string_bytes_2_hex_binary(&string, data, 11, NULL), 0);
1029  assert_string_equal(amxc_string_get(&string, 0), "A01BC23DE400A56BC78DE9");
1030  assert_int_equal(amxc_string_text_length(&string), 11 * 2);
1031 
1032  assert_int_equal(amxc_string_bytes_2_hex_binary(&string, (char*) &time, sizeof(amxc_ts_t), NULL), 0);
1033  assert_string_equal(amxc_string_get(&string, 0), "E49F5A5E0000000000CA5B07B5FF0000");
1034  assert_int_equal(amxc_string_text_length(&string), sizeof(amxc_ts_t) * 2);
1035 
1036  amxc_string_clean(&string);
1037 }
1038 
1040  amxc_string_t string;
1041  amxc_ts_t* time = NULL;
1042  char time_str[40];
1043  char* data = NULL;
1044  uint32_t len = 0;
1045  char verify[11] = { 0xA0, 0x1B, 0xC2, 0x3D, 0xE4, 0x00, 0xA5, 0x6B, 0xC7, 0x8D, 0xE9 };
1046 
1047  amxc_string_init(&string, 0);
1048 
1049  amxc_string_setf(&string, "A01BC23DE400A56BC78DE9");
1050  assert_int_equal(amxc_string_hex_binary_2_bytes(&string, &data, &len, NULL), 0);
1051  assert_non_null(data);
1052  assert_int_equal(len, 11);
1053  for(int i = 0; i < 11; i++) {
1054  assert_int_equal(data[i], verify[i]);
1055  }
1056  free(data);
1057 
1058  amxc_string_setf(&string, "a01bc23de400a56bc78de9");
1059  assert_int_equal(amxc_string_hex_binary_2_bytes(&string, &data, &len, NULL), 0);
1060  assert_non_null(data);
1061  assert_int_equal(len, 11);
1062  for(int i = 0; i < 11; i++) {
1063  assert_int_equal(data[i], verify[i]);
1064  }
1065  free(data);
1066 
1067  amxc_string_setf(&string, "E49F5A5E0000000000CA5B07B5FF0000");
1068  assert_int_equal(amxc_string_hex_binary_2_bytes(&string, (char**) &time, &len, NULL), 0);
1069  assert_non_null(time);
1070  assert_int_equal(len, sizeof(amxc_ts_t));
1071  amxc_ts_format(time, time_str, 40);
1072  assert_string_equal(time_str, "2020-02-29T16:16:16.123456-01:15");
1073  free(time);
1074 
1075  amxc_string_setf(&string, "e49f5a5e0000000000ca5b07b5ff0000");
1076  assert_int_equal(amxc_string_hex_binary_2_bytes(&string, (char**) &time, &len, NULL), 0);
1077  assert_non_null(time);
1078  assert_int_equal(len, sizeof(amxc_ts_t));
1079  amxc_ts_format(time, time_str, 40);
1080  assert_string_equal(time_str, "2020-02-29T16:16:16.123456-01:15");
1081  free(time);
1082 
1083  amxc_string_clean(&string);
1084 }
1085 
1087  amxc_string_t string;
1088  char mac_addr[6] = { 0x02, 0x42, 0xac, 0x11, 0x00, 0x02 };
1089 
1090  amxc_string_init(&string, 0);
1091  assert_int_equal(amxc_string_bytes_2_hex_binary(&string, mac_addr, 6, ":"), 0);
1092  assert_string_equal(amxc_string_get(&string, 0), "02:42:AC:11:00:02");
1093  assert_int_equal(amxc_string_text_length(&string), 6 * 2 + 5);
1094 
1095  amxc_string_clean(&string);
1096 }
1097 
1099  amxc_string_t string;
1100  char* data = NULL;
1101  uint32_t len = 0;
1102  char verify[6] = { 0x02, 0x42, 0xac, 0x11, 0x00, 0x02 };
1103 
1104  amxc_string_init(&string, 0);
1105 
1106  amxc_string_setf(&string, "02:42:ac:11:00:02");
1107  assert_int_equal(amxc_string_hex_binary_2_bytes(&string, &data, &len, ":"), 0);
1108  assert_non_null(data);
1109  assert_int_equal(len, 6);
1110  for(int i = 0; i < 6; i++) {
1111  assert_int_equal(data[i], verify[i]);
1112  }
1113  free(data);
1114 
1115  amxc_string_clean(&string);
1116 }
1117 
1119  amxc_string_t string;
1120  char* data = NULL;
1121  uint32_t len = 0;
1122 
1123  amxc_string_init(&string, 0);
1124 
1125  amxc_string_setf(&string, "A01BC23QQ400A56BC78DE9");
1126  assert_int_not_equal(amxc_string_hex_binary_2_bytes(&string, &data, &len, NULL), 0);
1127  assert_null(data);
1128  assert_int_equal(len, 0);
1129 
1130  amxc_string_setf(&string, "A01BC23400A56BC78DE9");
1131  assert_int_not_equal(amxc_string_hex_binary_2_bytes(NULL, &data, &len, NULL), 0);
1132  assert_null(data);
1133  assert_int_equal(len, 0);
1134 
1135  assert_int_not_equal(amxc_string_hex_binary_2_bytes(&string, NULL, &len, NULL), 0);
1136  assert_null(data);
1137  assert_int_equal(len, 0);
1138 
1139  assert_int_not_equal(amxc_string_hex_binary_2_bytes(&string, &data, NULL, NULL), 0);
1140  assert_null(data);
1141  assert_int_equal(len, 0);
1142 
1143  amxc_string_clean(&string);
1144 }
1145 
1147  amxc_string_t string;
1148  char* data = NULL;
1149  uint32_t len = 0;
1150 
1151  amxc_string_init(&string, 0);
1152 
1153  amxc_string_setf(&string, "A");
1154  assert_int_equal(amxc_string_hex_binary_2_bytes(&string, &data, &len, NULL), 0);
1155  assert_non_null(data);
1156  assert_int_equal(len, 1);
1157 
1158  assert_int_equal(data[0], 10);
1159 
1160  free(data);
1161  amxc_string_clean(&string);
1162 }
1163 
1164 void test_amxc_string_toupper(UNUSED void** state) {
1165  amxc_string_t string;
1166 
1167  amxc_string_init(&string, 0);
1168  amxc_string_setf(&string, "This is some text with symbols ;=) and uppercase ABC");
1169 
1170  assert_int_equal(amxc_string_to_upper(&string), 0);
1171  assert_string_equal(amxc_string_get(&string, 0), "THIS IS SOME TEXT WITH SYMBOLS ;=) AND UPPERCASE ABC");
1172 
1173  assert_int_not_equal(amxc_string_to_upper(NULL), 0);
1174 
1175  amxc_string_clean(&string);
1176 }
1177 
1178 void test_amxc_string_tolower(UNUSED void** state) {
1179  amxc_string_t string;
1180 
1181  amxc_string_init(&string, 0);
1182  amxc_string_setf(&string, "THIS IS SOME TEXT WITH SYMBOLS ;=) AND LOWERCASE abc");
1183 
1184  assert_int_equal(amxc_string_to_lower(&string), 0);
1185  assert_string_equal(amxc_string_get(&string, 0), "this is some text with symbols ;=) and lowercase abc");
1186 
1187  assert_int_not_equal(amxc_string_to_lower(NULL), 0);
1188 
1189  amxc_string_clean(&string);
1190 }
1191 
1192 static bool s_accept_all_strings(UNUSED const char* string) {
1193  return true;
1194 }
1195 
1196 static bool s_reject_all_strings(UNUSED const char* string) {
1197  return false;
1198 }
1199 
1200 static bool s_reject_longer_than_5(const char* string) {
1201  return string != NULL && strlen(string) <= 5;
1202 }
1203 
1204 static void s_testhelper_vappendf_checked(const char* expected, amxc_string_is_safe_cb_t is_safe_cb, const char* fmt, ...) {
1205  va_list args;
1206  amxc_string_t str;
1207  int retval = -1;
1208  va_start(args, fmt);
1209 
1210  amxc_string_init(&str, 64);
1211  retval = amxc_string_vappendf_checked(&str, is_safe_cb, fmt, args);
1212  if(expected == NULL) {
1213  assert_int_not_equal(0, retval);
1214  assert_true(amxc_string_is_empty(&str));
1215  } else {
1216  assert_int_equal(0, retval);
1217  assert_string_equal(expected, amxc_string_get(&str, 0));
1218  }
1219 
1220  va_end(args);
1221  amxc_string_clean(&str);
1222 }
1223 
1225  // Case: Normal case
1226  s_testhelper_vappendf_checked("abc 1 2 hi def", s_accept_all_strings, "abc %i %d %s def", 1, 2, "hi");
1227 
1228  // Case: special characters in format string
1229  s_testhelper_vappendf_checked("Hello\t\n\r!", s_accept_all_strings, "Hello\t\n\r%s", "!");
1230 
1231  // Case: "%%""
1232  s_testhelper_vappendf_checked("abc%def", s_accept_all_strings, "abc%%def");
1233 
1234  // Case: No leading text, no trailing text
1235  s_testhelper_vappendf_checked("hi123", s_accept_all_strings, "hi%i", 123);
1236  s_testhelper_vappendf_checked("123hi", s_accept_all_strings, "%ihi", 123);
1238 
1239  // Case: long format placeholders:
1240  s_testhelper_vappendf_checked("hi123", s_accept_all_strings, "hi%ld", 123);
1241  s_testhelper_vappendf_checked("hi123hello", s_accept_all_strings, "hi%ldhello", 123);
1242 
1243  // Case: fixed integer width macro compatibility (without crosscompiling unfortunately)
1245  "int8_t: -128, "
1246  "int16_t: -32768, "
1247  "int32_t: -2147483648, "
1248  "int64_t: -9223372036854775808, "
1249  "uint8_t: 255, "
1250  "uint16_t: 65535, "
1251  "uint32_t: 4294967295, "
1252  "uint64_t: 18446744073709551615",
1254  "int8_t: %" PRId8
1255  ", int16_t: %" PRId16
1256  ", int32_t: %" PRId32
1257  ", int64_t: %" PRId64
1258  ", uint8_t: %" PRIu8
1259  ", uint16_t: %" PRIu16
1260  ", uint32_t: %" PRIu32
1261  ", uint64_t: %" PRIu64,
1262  INT8_MIN,
1263  INT16_MIN,
1264  INT32_MIN,
1265  INT64_MIN,
1266  UINT8_MAX,
1267  UINT16_MAX,
1268  UINT32_MAX,
1269  UINT64_MAX);
1270 
1271  // Case: one string not accepted:
1272  s_testhelper_vappendf_checked(NULL, s_reject_longer_than_5, "short: %i long: %s", 1234, "looong");
1273  s_testhelper_vappendf_checked(NULL, s_reject_longer_than_5, "long: %i short: %s", 123456, "short");
1274 
1275  // Case: char (because C has auto integer promotion for varargs)
1276  s_testhelper_vappendf_checked("hello", s_accept_all_strings, "%c%cllo", 104, 101);
1277 
1278  // Case: unsupported format string placeholders
1280  s_testhelper_vappendf_checked(NULL, s_accept_all_strings, "%.2f", 3.14159265);
1283  s_testhelper_vappendf_checked(NULL, s_accept_all_strings, "%1$.*2$d", 2, 3);
1284 }
1285 
1287  bool retval = -1;
1288  // GIVEN a string
1289  amxc_string_t str;
1290  amxc_string_init(&str, 64);
1291  amxc_string_set(&str, "the");
1292 
1293  // WHEN appending using a formatstring and a checker (which accepts):
1294  retval = amxc_string_appendf_checked(&str, s_accept_all_strings, "quick%ibrown%s", 123, "dog");
1295 
1296  // THEN the string is formatted
1297  assert_string_equal(amxc_string_get(&str, 0), "thequick123browndog");
1298  assert_int_equal(0, retval);
1299 
1300  amxc_string_clean(&str);
1301 }
1302 
1304  int retval = -1;
1305  // GIVEN a string
1306  amxc_string_t str;
1307  amxc_string_init(&str, 64);
1308  amxc_string_set(&str, "the");
1309 
1310  // WHEN appending using a formatstring and a checker (which rejects):
1311  retval = amxc_string_appendf_checked(&str, s_reject_all_strings, "quick%ibrown%s", 123, "dog");
1312 
1313  // THEN the formatting is rejected, and the string is cleared:
1314  assert_string_equal(amxc_string_get(&str, 0), "");
1315  assert_int_not_equal(0, retval);
1316 
1317  amxc_string_clean(&str);
1318 }
#define UNUSED
Definition: amxc_macros.h:70
Ambiorix string API header file.
#define AMXC_STRING_MAX
Definition: amxc_string.h:71
Ambiorix variant API header file.
#define cstring_t
Convenience macro.
Definition: amxc_variant.h:584
size_t amxc_llist_size(const amxc_llist_t *const llist)
Calculates the size of the linked list.
Definition: amxc_llist.c:151
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
amxc_llist_it_t * amxc_llist_add_string(amxc_llist_t *const llist, const char *text)
Adds a string (char*) to a linked list of amxc_string_t structures.
Definition: amxc_utils.c:306
int amxc_string_set_resolved(amxc_string_t *string, const char *text, const amxc_var_t *const data)
Sets the resolved string.
Definition: amxc_utils.c:258
int amxc_string_esc(amxc_string_t *const string)
Add escape characters to a string.
Definition: amxc_utils.c:226
int amxc_string_new_resolved(amxc_string_t **string, const char *text, const amxc_var_t *const data)
Sets the resolved string.
Definition: amxc_utils.c:289
int amxc_string_resolve_var(amxc_string_t *const string, const amxc_var_t *const data)
Resolves variant path variables.
Definition: amxc_utils.c:164
int amxc_string_resolve(amxc_string_t *const string, const amxc_var_t *const data)
Resolves variant paths and environment variables.
Definition: amxc_utils.c:242
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
int amxc_string_resolve_esc(amxc_string_t *const string)
Resolves escaped characters in a string.
Definition: amxc_utils.c:183
int amxc_string_resolve_env(amxc_string_t *const string)
Resolves environment variables.
Definition: amxc_utils.c:148
void amxc_string_delete(amxc_string_t **string)
Frees the previously allocated string.
Definition: amxc_string.c:150
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
int amxc_string_to_lower(amxc_string_t *const string)
Converts all upper case characters to lower case.
Definition: amxc_string.c:856
int amxc_string_bytes_2_hex_binary(amxc_string_t *const string, const char bytes[], const uint32_t len, const char *sep)
Creates a hexbinary string from an array of bytes.
Definition: amxc_string.c:870
int amxc_string_setf(amxc_string_t *const string, const char *fmt,...) __attribute__((format(printf
Sets the content of the string using printf like formatting.
AMXC_INLINE size_t amxc_string_buffer_length(const amxc_string_t *const string)
Gets the current size of the allocate string buffer.
Definition: amxc_string.h:976
int amxc_string_push_buffer(amxc_string_t *const string, char *buffer, size_t length)
Sets the string buffer.
Definition: amxc_string.c:372
size_t amxc_string_set(amxc_string_t *const string, const char *text)
Sets a 0 terminated string in the string buffer.
Definition: amxc_string.c:826
AMXC_INLINE int amxc_string_append(amxc_string_t *const string, const char *const text, const size_t length)
Appends text to the end of the current content of the string buffer.
Definition: amxc_string.h:920
int amxc_string_replace(amxc_string_t *const string, const char *needle, const char *newstr, uint32_t max)
Replaces a number of sub-string occurrences in a string.
Definition: amxc_string.c:789
char * amxc_string_dup(const amxc_string_t *const string, const size_t start, size_t length)
Creates a full or partial copy of the text in the string buffer.
Definition: amxc_string.c:400
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_to_upper(amxc_string_t *const string)
Converts all lower case characters to upper case.
Definition: amxc_string.c:842
int amxc_string_copy(amxc_string_t *const dest, const amxc_string_t *const src)
Copies the content.
Definition: amxc_string.c:215
int amxc_string_new(amxc_string_t **string, const size_t length)
Allocates a string.
Definition: amxc_string.c:118
AMXC_INLINE size_t amxc_string_text_length(const amxc_string_t *const string)
Gets the current size of the used string buffer.
Definition: amxc_string.h:997
bool(* amxc_string_is_safe_cb_t)(const char *replacement)
Checks if given replacement is safe to be included in a bigger string in a particular language.
Definition: amxc_string.h:158
int amxc_string_search(const amxc_string_t *const string, const char *needle, uint32_t start_pos)
Searches a sub-string in a string.
Definition: amxc_string.c:765
AMXC_INLINE int amxc_string_prepend(amxc_string_t *const string, const char *const text, const size_t length)
Inserts text at the beginning of the current content of the string buffer.
Definition: amxc_string.h:953
int int amxc_string_vappendf_checked(amxc_string_t *target_string, amxc_string_is_safe_cb_t is_safe_cb, const char *fmt, va_list args)
va_list version of amxc_string_appendf_checked
Definition: amxc_string.c:600
int amxc_string_appendf_checked(amxc_string_t *string, amxc_string_is_safe_cb_t is_safe_cb, const char *fmt,...) __attribute__((format(printf
Appends a formatted string while performing safety checks on the replacements.
int amxc_string_grow(amxc_string_t *const string, const size_t length)
Grows the string buffer.
Definition: amxc_string.c:240
int amxc_string_shrink(amxc_string_t *const string, const size_t length)
Shrinks the string buffer.
Definition: amxc_string.c:260
void amxc_string_trim(amxc_string_t *const string, amxc_string_is_char_fn_t fn)
Trim.
Definition: amxc_string.c:471
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.
int amxc_string_remove_at(amxc_string_t *const string, const size_t pos, size_t length)
Removes part of the text in the string buffer.
Definition: amxc_string.c:314
int amxc_string_set_at(amxc_string_t *const string, const size_t pos, const char *const text, const size_t length, const amxc_string_flags_t flags)
Set text in the string buffer at a certain position.
Definition: amxc_string.c:276
int amxc_string_hex_binary_2_bytes(const amxc_string_t *const string, char **bytes, uint32_t *len, const char *sep)
Creates an array of bytes from a hex binary string.
Definition: amxc_string.c:901
void amxc_string_triml(amxc_string_t *const string, amxc_string_is_char_fn_t fn)
Trim left.
Definition: amxc_string.c:423
void amxc_string_trimr(amxc_string_t *const string, amxc_string_is_char_fn_t fn)
Trim right.
Definition: amxc_string.c:453
int bool amxc_string_is_numeric(const amxc_string_t *const string)
Checks if a string is fully numeric.
Definition: amxc_string.c:746
AMXC_INLINE bool amxc_string_is_empty(const amxc_string_t *const string)
Checks if the string is empty.
Definition: amxc_string.h:1015
int amxc_string_prependf(amxc_string_t *const string, const char *fmt,...) __attribute__((format(printf
Prepends a formatted string to a string.
void amxc_string_clean(amxc_string_t *const string)
Frees the string buffer and reset length attributes.
Definition: amxc_string.c:189
char * amxc_string_take_buffer(amxc_string_t *const string)
Takes the string buffer.
Definition: amxc_string.c:356
@ amxc_string_overwrite
Definition: amxc_string.h:123
@ amxc_string_no_flags
Definition: amxc_string.h:121
size_t amxc_ts_format(const amxc_ts_t *tsp, char *dst, size_t len)
Transforms unix epoch time to a string.
int amxc_ts_parse(amxc_ts_t *tsp, const char *str, size_t len)
Transforms the given string in to unix epoch time.
#define AMXC_VAR_ID_HTABLE
Ambiorix Hash Table variant id.
Definition: amxc_variant.h:212
#define amxc_var_add_key(type, var, key, data)
Convenience macro for adding a variant to composite variant type.
Definition: amxc_variant.h:627
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
The linked list structure.
Definition: amxc_llist.h:228
The string structure.
Definition: amxc_string.h:103
char * buffer
Definition: amxc_string.h:104
size_t last_used
Definition: amxc_string.h:106
size_t length
Definition: amxc_string.h:105
The variant struct definition.
Definition: amxc_variant.h:861
The timestamp structure (unix epoch time).
void test_amxc_string_setf(UNUSED void **state)
void test_amxc_string_build_hex_binary(UNUSED void **state)
void test_amxc_string_grow_null(UNUSED void **state)
void test_amxc_string_get(UNUSED void **state)
void test_amxc_string_build_byte_array_with_invalid_string(UNUSED void **state)
void test_amxc_string_resolve_var(UNUSED void **state)
void test_amxc_llist_add_string(UNUSED void **state)
void test_amxc_string_trim(UNUSED void **state)
static bool s_reject_all_strings(UNUSED const char *string)
void test_amxc_string_shrink_null(UNUSED void **state)
void test_amxc_string_append(UNUSED void **state)
void test_amxc_string_new_delete_null(UNUSED void **state)
void test_amxc_string_prepend(UNUSED void **state)
void test_amxc_string_init_reset_clean_null(UNUSED void **state)
void test_amxc_string_build_hex_binary_using_separator(UNUSED void **state)
void test_amxc_string_build_byte_array(UNUSED void **state)
void test_amxc_string_prependf(UNUSED void **state)
void test_amxc_string_text_length(UNUSED void **state)
static bool s_reject_longer_than_5(const char *string)
void test_amxc_string_vappendf_checked(UNUSED void **state)
void test_amxc_string_resolve(UNUSED void **state)
void test_amxc_string_init_reset_clean(UNUSED void **state)
void test_amxc_string_dup(UNUSED void **state)
void test_amxc_string_new_resolved(UNUSED void **state)
void test_amxc_string_search(UNUSED void **state)
void test_amxc_string_resolve_env(UNUSED void **state)
void test_amxc_string_replace(UNUSED void **state)
void test_amxc_string_build_byte_array_with_separator(UNUSED void **state)
char data[]
void test_amxc_string_take_push_buffer(UNUSED void **state)
void test_amxc_string_shrink(UNUSED void **state)
void test_amxc_string_grow(UNUSED void **state)
void test_amxc_string_remove_at(UNUSED void **state)
void test_amxc_string_toupper(UNUSED void **state)
void test_amxc_string_is_numeric(UNUSED void **state)
void test_amxc_string_buffer_length(UNUSED void **state)
void test_amxc_string_appendf_checked__rejected(UNUSED void **state)
void test_amxc_string_set(UNUSED void **state)
void test_amxc_string_set_at(UNUSED void **state)
void test_amxc_string_appendf(UNUSED void **state)
void test_amxc_string_set_resolved(UNUSED void **state)
static void s_testhelper_vappendf_checked(const char *expected, amxc_string_is_safe_cb_t is_safe_cb, const char *fmt,...)
void test_amxc_string_new_delete(UNUSED void **state)
void test_amxc_string_appendf_checked__normalcase(UNUSED void **state)
void test_amxc_string_tolower(UNUSED void **state)
void test_amxc_string_reset(UNUSED void **state)
void test_amxc_string_copy(UNUSED void **state)
void test_amxc_string_build_byte_array_from_incomplete_string(UNUSED void **state)
static bool s_accept_all_strings(UNUSED const char *string)