GCC Code Coverage Report


Directory: src/gate/
File: src/gate/comparers.c
Date: 2025-09-14 13:10:38
Exec Total Coverage
Lines: 79 83 95.2%
Functions: 24 24 100.0%
Branches: 72 76 94.7%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28
29 #include "gate/comparers.h"
30 #include "gate/mathematics.h"
31 #include "gate/strings.h"
32 #include "gate/guids.h"
33 #include "gate/times.h"
34 #include "gate/results.h"
35 #include "gate/memalloc.h"
36
37 #define GATE_COMPARE_CALC(tp, item1, item2) (int)(*((tp const*)item1)) - (int)(*((tp const*)item2))
38 #define GATE_COMPARE_OP(tp, item1, item2) (*((tp const*)item1) == *((tp const*)item2) ) ? 0 : ( (*((tp const*)item1) < *((tp const*)item2)) ? -1 : +1 )
39
40 3 int gate_compare_bool(void const* item1, void const* item2)
41 {
42
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if ((*(gate_bool_t const*)item1) == (*(gate_bool_t const*)item2))
43 {
44 1 return 0;
45 }
46
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 else if ((*(gate_bool_t const*)item1) == false)
47 {
48 1 return -1;
49 }
50 else
51 {
52 1 return 1;
53 }
54 }
55 3 int gate_compare_int8(void const* item1, void const* item2) { return GATE_COMPARE_CALC(gate_int8_t, item1, item2); }
56 3 int gate_compare_uint8(void const* item1, void const* item2) { return GATE_COMPARE_CALC(gate_uint8_t, item1, item2); }
57 59 int gate_compare_int16(void const* item1, void const* item2) { return GATE_COMPARE_CALC(gate_int16_t, item1, item2); }
58 4541055 int gate_compare_uint16(void const* item1, void const* item2) { return GATE_COMPARE_CALC(gate_uint16_t, item1, item2); }
59
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
3 int gate_compare_int32(void const* item1, void const* item2) { return GATE_COMPARE_OP(gate_int32_t, item1, item2); }
60
4/4
✓ Branch 0 taken 3857 times.
✓ Branch 1 taken 4095 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 3856 times.
7952 int gate_compare_uint32(void const* item1, void const* item2) { return GATE_COMPARE_OP(gate_uint32_t, item1, item2); }
61
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
3 int gate_compare_int64(void const* item1, void const* item2) { return GATE_COMPARE_OP(gate_int64_t, item1, item2); }
62
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
3 int gate_compare_uint64(void const* item1, void const* item2) { return GATE_COMPARE_OP(gate_uint64_t, item1, item2); }
63
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
3 int gate_compare_intptr(void const* item1, void const* item2) { return GATE_COMPARE_OP(gate_intptr_t, item1, item2); }
64
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
17 int gate_compare_uintptr(void const* item1, void const* item2) { return GATE_COMPARE_OP(gate_uintptr_t, item1, item2); }
65
66 3 int gate_compare_real32(void const* item1, void const* item2)
67 {
68 3 gate_real32_t diff = *((gate_real32_t const*)item1) - *((gate_real32_t const*)item2);
69
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
3 if (gate_math_iszerof(diff))
70 {
71 1 return 0;
72 }
73 else
74 {
75
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 return diff < 0.0f ? -1 : 1;
76 }
77 }
78 3 int gate_compare_real64(void const* item1, void const* item2)
79 {
80 3 gate_real64_t diff = *((gate_real64_t const*)item1) - *((gate_real64_t const*)item2);
81
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
3 if (gate_math_iszero(diff))
82 {
83 1 return 0;
84 }
85 else
86 {
87
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 return diff < 0.0 ? -1 : 1;
88 }
89 }
90 3 int gate_compare_cstring(void const* item1, void const* item2)
91 {
92 3 return gate_str_comp((char const*)item1, (char const*)item2);
93 }
94 3 int gate_compare_wstring(void const* item1, void const* item2)
95 {
96 if (sizeof(wchar_t) == sizeof(gate_char16_t))
97 {
98 return gate_str16_comp((gate_char16_t const*)(wchar_t const*)item1, (gate_char16_t const*)(wchar_t const*)item2);
99 }
100 else
101 {
102 3 return gate_str32_comp((gate_char32_t const*)(wchar_t const*)item1, (gate_char32_t const*)(wchar_t const*)item2);
103 }
104 }
105 2336 int gate_compare_string(void const* item1, void const* item2)
106 {
107 2336 return gate_string_comp((gate_string_t const*)item1, (gate_string_t const*)item2);
108 }
109 17 int gate_compare_string_ic(void const* item1, void const* item2)
110 {
111 17 return gate_string_comp_ic((gate_string_t const*)item1, (gate_string_t const*)item2);
112 }
113 3 int gate_compare_guid(void const* item1, void const* item2)
114 {
115 3 return gate_mem_compare(item1, item2, sizeof(gate_guid_t));
116 }
117 15 int gate_compare_date(void const* item1, void const* item2)
118 {
119 15 gate_date_t const* d1 = (gate_date_t const*)item1;
120 15 gate_date_t const* d2 = (gate_date_t const*)item2;
121
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 14 times.
15 if (d1->year < d2->year) return -1;
122
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 13 times.
14 if (d1->year > d2->year) return +1;
123
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 12 times.
13 if (d1->month < d2->month) return -1;
124
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 11 times.
12 if (d1->month > d2->month) return +1;
125
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 9 times.
11 if (d1->day < d2->day) return -1;
126
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7 times.
9 if (d1->day > d2->day) return +1;
127 7 return 0;
128 }
129 16 int gate_compare_daytime(void const* item1, void const* item2)
130 {
131 16 gate_daytime_t const* t1 = (gate_daytime_t const*)item1;
132 16 gate_daytime_t const* t2 = (gate_daytime_t const*)item2;
133
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 15 times.
16 if (t1->hour < t2->hour) return -1;
134
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 14 times.
15 if (t1->hour > t2->hour) return +1;
135
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 13 times.
14 if (t1->minute < t2->minute) return -1;
136
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 12 times.
13 if (t1->minute > t2->minute) return +1;
137
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 11 times.
12 if (t1->second < t2->second) return -1;
138
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10 times.
11 if (t1->second > t2->second) return +1;
139
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
10 if (t1->microsecond < t2->microsecond) return -1;
140
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 if (t1->microsecond > t2->microsecond) return +1;
141 6 return 0;
142 }
143 6 int gate_compare_datetime(void const* item1, void const* item2)
144 {
145 6 gate_datetime_t const* dt1 = (gate_datetime_t const*)item1;
146 6 gate_datetime_t const* dt2 = (gate_datetime_t const*)item2;
147 6 int ret = gate_compare_date(&dt1->date, &dt2->date);
148
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if (ret == 0)
149 {
150 4 ret = gate_compare_daytime(&dt1->time, &dt2->time);
151 }
152 6 return ret;
153 }
154 6 int gate_compare_time(void const* item1, void const* item2)
155 {
156 6 gate_time_t const* t1 = (gate_time_t const*)item1;
157 6 gate_time_t const* t2 = (gate_time_t const*)item2;
158
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if (t1->timestamp == t2->timestamp)
159 {
160 4 return ((int)t1->bias) - ((int)t2->bias);
161 }
162 else
163 {
164
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 return (t1->timestamp < t2->timestamp) ? -1 : +1;
165 }
166 }
167 4 int gate_compare_ptr(void const* item1, void const* item2)
168 {
169 4 gate_ptr_t const* t1 = (gate_ptr_t const*)item1;
170 4 gate_ptr_t const* t2 = (gate_ptr_t const*)item2;
171
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (*t1 == *t2)
172 {
173 4 return 0;
174 }
175 else if (*t1 < *t2)
176 {
177 return -1;
178 }
179 else
180 {
181 return 1;
182 }
183 }
184
185
186 2142 int gate_compare_types(void const* item1, void const* item2, gate_size_t size, gate_comparer_t comparer)
187 {
188
1/2
✓ Branch 0 taken 2142 times.
✗ Branch 1 not taken.
2142 if (comparer)
189 {
190 2142 return comparer(item1, item2);
191 }
192 else
193 {
194 return gate_mem_compare(item1, item2, size);
195 }
196 }
197