GCC Code Coverage Report


Directory: src/gate/
File: src/gate/comparers.c
Date: 2026-03-20 22:56:14
Exec Total Coverage
Lines: 80 85 94.1%
Functions: 24 24 100.0%
Branches: 73 78 93.6%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright (c) 2018-2026, 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 6257155 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 130637 times.
✓ Branch 1 taken 69638 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 130636 times.
200275 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 31 times.
✓ Branch 1 taken 26 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 30 times.
57 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 static gate_bool_t const wchar_is_16bit = sizeof(wchar_t) == sizeof(gate_char16_t);
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (wchar_is_16bit)
98 {
99 return gate_str16_comp((gate_char16_t const*)(wchar_t const*)item1, (gate_char16_t const*)(wchar_t const*)item2);
100 }
101 else
102 {
103 3 return gate_str32_comp((gate_char32_t const*)(wchar_t const*)item1, (gate_char32_t const*)(wchar_t const*)item2);
104 }
105 }
106 8294 int gate_compare_string(void const* item1, void const* item2)
107 {
108 8294 return gate_string_comp((gate_string_t const*)item1, (gate_string_t const*)item2);
109 }
110 21 int gate_compare_string_ic(void const* item1, void const* item2)
111 {
112 21 return gate_string_comp_ic((gate_string_t const*)item1, (gate_string_t const*)item2);
113 }
114 3 int gate_compare_guid(void const* item1, void const* item2)
115 {
116 3 return gate_mem_compare(item1, item2, sizeof(gate_guid_t));
117 }
118 15 int gate_compare_date(void const* item1, void const* item2)
119 {
120 15 gate_date_t const* d1 = (gate_date_t const*)item1;
121 15 gate_date_t const* d2 = (gate_date_t const*)item2;
122
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 14 times.
15 if (d1->year < d2->year) return -1;
123
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 13 times.
14 if (d1->year > d2->year) return +1;
124
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 12 times.
13 if (d1->month < d2->month) return -1;
125
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 11 times.
12 if (d1->month > d2->month) return +1;
126
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 9 times.
11 if (d1->day < d2->day) return -1;
127
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7 times.
9 if (d1->day > d2->day) return +1;
128 7 return 0;
129 }
130 16 int gate_compare_daytime(void const* item1, void const* item2)
131 {
132 16 gate_daytime_t const* t1 = (gate_daytime_t const*)item1;
133 16 gate_daytime_t const* t2 = (gate_daytime_t const*)item2;
134
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 15 times.
16 if (t1->hour < t2->hour) return -1;
135
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 14 times.
15 if (t1->hour > t2->hour) return +1;
136
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 13 times.
14 if (t1->minute < t2->minute) return -1;
137
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 12 times.
13 if (t1->minute > t2->minute) return +1;
138
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 11 times.
12 if (t1->second < t2->second) return -1;
139
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10 times.
11 if (t1->second > t2->second) return +1;
140
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
10 if (t1->microsecond < t2->microsecond) return -1;
141
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 if (t1->microsecond > t2->microsecond) return +1;
142 6 return 0;
143 }
144 6 int gate_compare_datetime(void const* item1, void const* item2)
145 {
146 6 gate_datetime_t const* dt1 = (gate_datetime_t const*)item1;
147 6 gate_datetime_t const* dt2 = (gate_datetime_t const*)item2;
148 6 int ret = gate_compare_date(&dt1->date, &dt2->date);
149
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if (ret == 0)
150 {
151 4 ret = gate_compare_daytime(&dt1->time, &dt2->time);
152 }
153 6 return ret;
154 }
155 6 int gate_compare_time(void const* item1, void const* item2)
156 {
157 6 gate_time_t const* t1 = (gate_time_t const*)item1;
158 6 gate_time_t const* t2 = (gate_time_t const*)item2;
159
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if (t1->timestamp == t2->timestamp)
160 {
161 4 return ((int)t1->bias) - ((int)t2->bias);
162 }
163 else
164 {
165
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 return (t1->timestamp < t2->timestamp) ? -1 : +1;
166 }
167 }
168 4 int gate_compare_ptr(void const* item1, void const* item2)
169 {
170 4 gate_ptr_t const* t1 = (gate_ptr_t const*)item1;
171 4 gate_ptr_t const* t2 = (gate_ptr_t const*)item2;
172
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (*t1 == *t2)
173 {
174 4 return 0;
175 }
176 else if (*t1 < *t2)
177 {
178 return -1;
179 }
180 else
181 {
182 return 1;
183 }
184 }
185
186
187 2772 int gate_compare_types(void const* item1, void const* item2, gate_size_t size, gate_comparer_t comparer)
188 {
189
1/2
✓ Branch 0 taken 2772 times.
✗ Branch 1 not taken.
2772 if (comparer)
190 {
191 2772 return comparer(item1, item2);
192 }
193 else
194 {
195 return gate_mem_compare(item1, item2, size);
196 }
197 }
198