GCC Code Coverage Report


Directory: src/gate/
File: src/gate/tests/gatecore_test/test_utilities.c
Date: 2025-09-14 13:10:38
Exec Total Coverage
Lines: 107 107 100.0%
Functions: 6 6 100.0%
Branches: 56 112 50.0%

Line Branch Exec Source
1 #include "gate/tests.h"
2 #include "test_utilities.h"
3 #include "gate/utilities.h"
4 #include "gate/mathematics.h"
5 #include "gate/streams.h"
6
7 1 static gate_bool_t test_map_utils()
8 {
9 1 gate_map_t m = GATE_INIT_EMPTY;
10 gate_map_iterator_t iter;
11 1 gate_string_t const* ptr_str = NULL;
12 1 gate_arraylist_t lk = NULL;
13 1 gate_arraylist_t lv = NULL;
14
15
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_REQUIRE(gate_util_stringmap_create(&m) != NULL);
16
17 1 iter = gate_util_stringmap_add(&m, "hello", "world");
18
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 GATE_TEST_CHECK(iter != NULL);
19 1 iter = gate_util_stringmap_add(&m, "hello2", "world2");
20
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 GATE_TEST_CHECK(iter != NULL);
21
22 1 ptr_str = gate_util_stringmap_get(&m, "hello");
23
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 GATE_TEST_CHECK(ptr_str != NULL);
24
25 1 lk = gate_util_map_export_keys(&m);
26
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 GATE_TEST_CHECK(lk != NULL);
27 1 lv = gate_util_map_export_values(&m);
28
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 GATE_TEST_CHECK(lv != NULL);
29
30 1 gate_arraylist_release(lk);
31 1 gate_arraylist_release(lv);
32
33 1 gate_map_destroy(&m);
34 1 return true;
35 }
36
37 1 static gate_bool_t test_set_utils()
38 {
39 1 gate_set_t s = GATE_INIT_EMPTY;
40 static gate_string_t const b = GATE_STRING_INIT_EMPTY;
41
42
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_REQUIRE(NULL != gate_util_stringset_create(&s));
43
44
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK(gate_util_stringset_add(&s, "a"));
45
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK(gate_util_stringset_add_string(&s, &b));
46
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK(gate_util_stringset_contains(&s, "a"));
47
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK(gate_util_stringset_contains_string(&s, &b));
48
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK(gate_util_stringset_remove(&s, "a"));
49
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK(gate_util_stringset_remove_string(&s, &b));
50
51 1 gate_set_destroy(&s);
52 1 return true;
53 }
54
55 1 static gate_bool_t test_array_utils()
56 {
57 1 gate_arraylist_t l = gate_util_stringarray_create_duplicate(2);
58
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 GATE_TEST_REQUIRE(l != NULL);
59
60
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK(NULL != gate_util_stringarray_add_str(l, "test"));
61
62 1 gate_arraylist_release(l);
63 1 return true;
64 }
65
66 1 static gate_bool_t test_string_utils()
67 {
68 static gate_string_t const str_true = GATE_STRING_INIT_STATIC("true");
69 static gate_string_t const str_false = GATE_STRING_INIT_STATIC("false");
70 static gate_string_t const source = GATE_STRING_INIT_STATIC("a,b,c");
71 static gate_string_t const sep = GATE_STRING_INIT_STATIC(",");
72 static gate_string_t const str_num = GATE_STRING_INIT_STATIC("123.456");
73
74 char buffer[1024];
75 1 gate_string_t tmp = GATE_STRING_INIT_EMPTY;
76
77 1 gate_arraylist_t l = NULL;
78
79
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK_EQUAL(gate_util_string_eval_bool(&str_false), false);
80
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK_EQUAL(gate_util_string_eval_bool(NULL), false);
81
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK_EQUAL(gate_util_string_eval_bool(&str_true), true);
82
83 1 l = gate_util_string_split(&source, &sep, 2);
84
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 GATE_TEST_REQUIRE(l != NULL);
85
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK_EQUAL(gate_arraylist_length(l), 2);
86 1 gate_arraylist_release(l);
87
88 1 l = gate_util_string_split(&source, &sep, 3);
89
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 GATE_TEST_REQUIRE(l != NULL);
90
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK_EQUAL(gate_arraylist_length(l), 3);
91 1 gate_arraylist_release(l);
92
93
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK(gate_util_print_byte_size(123, buffer, sizeof(buffer)) > 0);
94
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK(gate_util_print_byte_size(12345, buffer, sizeof(buffer)) > 0);
95
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK(gate_util_print_byte_size(12345678, buffer, sizeof(buffer)) > 0);
96
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK(gate_util_print_byte_size(12345678901, buffer, sizeof(buffer)) > 0);
97
98
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK(gate_util_print_duration(12345678901, buffer, sizeof(buffer), true) > 0);
99
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK(gate_util_print_duration(12345678901, buffer, sizeof(buffer), false) > 0);
100
101
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK(NULL != gate_util_uint_to_string(&tmp, 123456));
102 1 gate_string_release(&tmp);
103
104
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK(NULL != gate_util_real_to_string(&tmp, 123456.789, 6, 3, 3));
105 1 gate_string_release(&tmp);
106
107
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK_EQUAL(gate_util_string_to_int(&str_num), 123);
108
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK_EQUAL(gate_util_string_to_uint(&str_num), 123);
109
1/2
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 GATE_TEST_CHECK(gate_math_iszero(gate_util_string_to_real(&str_num) - 123.456));
110
111 1 return true;
112 }
113
114 1 gate_bool_t test_stream_utils()
115 {
116 static char const* const data_ptr = "Hello World";
117 static gate_size_t const data_len = 11;
118
119 {
120 /* use memstream as input */
121 gate_memstream_impl_t src_impl;
122 1 gate_memstream_t* src = gate_memstream_create_static_unmanaged_readonly(&src_impl, data_ptr, data_len, data_len);
123 1 char const* ptr_src = NULL;
124 1 gate_size_t ptr_src_len = 0;
125
126
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 GATE_TEST_REQUIRE(src != NULL);
127
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK_OK(gate_util_buffer_load(&ptr_src, &ptr_src_len, (gate_stream_t*)src, NULL));
128
129
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 GATE_TEST_CHECK_EQUAL(ptr_src, &data_ptr[0]);
130
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 GATE_TEST_CHECK_EQUAL(ptr_src_len, data_len);
131
132 1 gate_object_release(src);
133 }
134
135 {
136 /* use stringstream as input */
137 1 gate_stringstream_t* src = gate_stringstream_create(16);
138 1 char const* ptr_src = NULL;
139 1 gate_size_t ptr_src_len = 0;
140
141
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 GATE_TEST_REQUIRE(src != NULL);
142
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK_OK(gate_stream_write_block((gate_stream_t*)src, data_ptr, data_len, NULL));
143
144
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK_OK(gate_util_buffer_load(&ptr_src, &ptr_src_len, (gate_stream_t*)src, NULL));
145
146
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 GATE_TEST_CHECK(ptr_src != NULL);
147
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 GATE_TEST_CHECK_EQUAL(ptr_src_len, data_len);
148
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK(gate_mem_compare(ptr_src, data_ptr, data_len) == 0);
149
150 1 gate_object_release(src);
151 }
152
153 {
154 /* use a non-direct-access stream object */
155 1 gate_controlstream_t* src = gate_memfilestream_create(16);
156 1 char const* ptr_src = NULL;
157 1 gate_size_t ptr_src_len = 0;
158 1 gate_memstream_t* help_buffer = NULL;
159
160
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 GATE_TEST_REQUIRE(src != NULL);
161
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK_OK(gate_stream_write_block((gate_stream_t*)src, data_ptr, data_len, NULL));
162
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK_OK(gate_stream_seek(src, 0, GATE_STREAM_SEEK_BEGIN, NULL));
163
164
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK_OK(gate_util_buffer_load(&ptr_src, &ptr_src_len, (gate_stream_t*)src, &help_buffer));
165
166
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 GATE_TEST_CHECK(ptr_src != NULL);
167
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 GATE_TEST_CHECK_EQUAL(ptr_src_len, data_len);
168
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK(gate_mem_compare(ptr_src, data_ptr, data_len) == 0);
169
170 1 gate_object_release(src);
171
172
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 GATE_TEST_REQUIRE(help_buffer != NULL);
173 1 gate_object_release(help_buffer);
174 }
175
176 1 return true;
177 }
178
179 1 GATE_TEST_FUNCTION(test_utilities)
180 {
181 1 GATE_TEST_UNIT_BEGIN(test_utilities);
182
183
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK(test_map_utils());
184
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK(test_set_utils());
185
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK(test_array_utils());
186
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK(test_string_utils());
187
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 GATE_TEST_CHECK(test_stream_utils());
188
189 1 GATE_TEST_UNIT_END;
190 }
191