| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | ||
| 3 | * | ||
| 4 | * Copyright 2021 Mike Becker, Olaf Wintermann 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 | ||
| 10 | * notice, this list of conditions and the following disclaimer. | ||
| 11 | * | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in the | ||
| 14 | * documentation and/or other materials provided with the distribution. | ||
| 15 | * | ||
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 26 | * POSSIBILITY OF SUCH DAMAGE. | ||
| 27 | */ | ||
| 28 | |||
| 29 | #include "cx/compare.h" | ||
| 30 | |||
| 31 | #include <math.h> | ||
| 32 | #include <string.h> | ||
| 33 | |||
| 34 | 1615200 | int cx_vcmp_int(int a, int b) { | |
| 35 |
2/2✓ Branch 0 (2→3) taken 11508 times.
✓ Branch 1 (2→4) taken 1603692 times.
|
1615200 | if (a == b) { |
| 36 | 11508 | return 0; | |
| 37 | } else { | ||
| 38 |
2/2✓ Branch 0 (4→5) taken 88411 times.
✓ Branch 1 (4→6) taken 1515281 times.
|
1603692 | return a < b ? -1 : 1; |
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | 1615200 | int cx_cmp_int(const void *i1, const void *i2) { | |
| 43 | 1615200 | int a = *((const int *) i1); | |
| 44 | 1615200 | int b = *((const int *) i2); | |
| 45 | 1615200 | return cx_vcmp_int(a, b); | |
| 46 | } | ||
| 47 | |||
| 48 | 8 | int cx_vcmp_longint(long int a, long int b) { | |
| 49 |
2/2✓ Branch 0 (2→3) taken 2 times.
✓ Branch 1 (2→4) taken 6 times.
|
8 | if (a == b) { |
| 50 | 2 | return 0; | |
| 51 | } else { | ||
| 52 |
2/2✓ Branch 0 (4→5) taken 3 times.
✓ Branch 1 (4→6) taken 3 times.
|
6 | return a < b ? -1 : 1; |
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | 8 | int cx_cmp_longint(const void *i1, const void *i2) { | |
| 57 | 8 | long int a = *((const long int *) i1); | |
| 58 | 8 | long int b = *((const long int *) i2); | |
| 59 | 8 | return cx_vcmp_longint(a, b); | |
| 60 | } | ||
| 61 | |||
| 62 | 8 | int cx_vcmp_longlong(long long a, long long b) { | |
| 63 |
2/2✓ Branch 0 (2→3) taken 2 times.
✓ Branch 1 (2→4) taken 6 times.
|
8 | if (a == b) { |
| 64 | 2 | return 0; | |
| 65 | } else { | ||
| 66 |
2/2✓ Branch 0 (4→5) taken 3 times.
✓ Branch 1 (4→6) taken 3 times.
|
6 | return a < b ? -1 : 1; |
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | 8 | int cx_cmp_longlong(const void *i1, const void *i2) { | |
| 71 | 8 | long long a = *((const long long *) i1); | |
| 72 | 8 | long long b = *((const long long *) i2); | |
| 73 | 8 | return cx_vcmp_longlong(a, b); | |
| 74 | } | ||
| 75 | |||
| 76 | 8 | int cx_vcmp_int16(int16_t a, int16_t b) { | |
| 77 |
2/2✓ Branch 0 (2→3) taken 2 times.
✓ Branch 1 (2→4) taken 6 times.
|
8 | if (a == b) { |
| 78 | 2 | return 0; | |
| 79 | } else { | ||
| 80 |
2/2✓ Branch 0 (4→5) taken 3 times.
✓ Branch 1 (4→6) taken 3 times.
|
6 | return a < b ? -1 : 1; |
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | 8 | int cx_cmp_int16(const void *i1, const void *i2) { | |
| 85 | 8 | int16_t a = *((const int16_t *) i1); | |
| 86 | 8 | int16_t b = *((const int16_t *) i2); | |
| 87 | 8 | return cx_vcmp_int16(a, b); | |
| 88 | } | ||
| 89 | |||
| 90 | 8 | int cx_vcmp_int32(int32_t a, int32_t b) { | |
| 91 |
2/2✓ Branch 0 (2→3) taken 2 times.
✓ Branch 1 (2→4) taken 6 times.
|
8 | if (a == b) { |
| 92 | 2 | return 0; | |
| 93 | } else { | ||
| 94 |
2/2✓ Branch 0 (4→5) taken 3 times.
✓ Branch 1 (4→6) taken 3 times.
|
6 | return a < b ? -1 : 1; |
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | 8 | int cx_cmp_int32(const void *i1, const void *i2) { | |
| 99 | 8 | int32_t a = *((const int32_t *) i1); | |
| 100 | 8 | int32_t b = *((const int32_t *) i2); | |
| 101 | 8 | return cx_vcmp_int32(a, b); | |
| 102 | } | ||
| 103 | |||
| 104 | 435 | int cx_vcmp_int64(int64_t a, int64_t b) { | |
| 105 |
2/2✓ Branch 0 (2→3) taken 415 times.
✓ Branch 1 (2→4) taken 20 times.
|
435 | if (a == b) { |
| 106 | 415 | return 0; | |
| 107 | } else { | ||
| 108 |
2/2✓ Branch 0 (4→5) taken 10 times.
✓ Branch 1 (4→6) taken 10 times.
|
20 | return a < b ? -1 : 1; |
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | 8 | int cx_cmp_int64(const void *i1, const void *i2) { | |
| 113 | 8 | int64_t a = *((const int64_t *) i1); | |
| 114 | 8 | int64_t b = *((const int64_t *) i2); | |
| 115 | 8 | return cx_vcmp_int64(a, b); | |
| 116 | } | ||
| 117 | |||
| 118 | 6 | int cx_vcmp_uint(unsigned int a, unsigned int b) { | |
| 119 |
2/2✓ Branch 0 (2→3) taken 2 times.
✓ Branch 1 (2→4) taken 4 times.
|
6 | if (a == b) { |
| 120 | 2 | return 0; | |
| 121 | } else { | ||
| 122 |
2/2✓ Branch 0 (4→5) taken 2 times.
✓ Branch 1 (4→6) taken 2 times.
|
4 | return a < b ? -1 : 1; |
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | 6 | int cx_cmp_uint(const void *i1, const void *i2) { | |
| 127 | 6 | unsigned int a = *((const unsigned int *) i1); | |
| 128 | 6 | unsigned int b = *((const unsigned int *) i2); | |
| 129 | 6 | return cx_vcmp_uint(a, b); | |
| 130 | } | ||
| 131 | |||
| 132 | 6 | int cx_vcmp_ulongint(unsigned long int a, unsigned long int b) { | |
| 133 |
2/2✓ Branch 0 (2→3) taken 2 times.
✓ Branch 1 (2→4) taken 4 times.
|
6 | if (a == b) { |
| 134 | 2 | return 0; | |
| 135 | } else { | ||
| 136 |
2/2✓ Branch 0 (4→5) taken 2 times.
✓ Branch 1 (4→6) taken 2 times.
|
4 | return a < b ? -1 : 1; |
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | 6 | int cx_cmp_ulongint(const void *i1, const void *i2) { | |
| 141 | 6 | unsigned long int a = *((const unsigned long int *) i1); | |
| 142 | 6 | unsigned long int b = *((const unsigned long int *) i2); | |
| 143 | 6 | return cx_vcmp_ulongint(a, b); | |
| 144 | } | ||
| 145 | |||
| 146 | 6 | int cx_vcmp_ulonglong(unsigned long long a, unsigned long long b) { | |
| 147 |
2/2✓ Branch 0 (2→3) taken 2 times.
✓ Branch 1 (2→4) taken 4 times.
|
6 | if (a == b) { |
| 148 | 2 | return 0; | |
| 149 | } else { | ||
| 150 |
2/2✓ Branch 0 (4→5) taken 2 times.
✓ Branch 1 (4→6) taken 2 times.
|
4 | return a < b ? -1 : 1; |
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | 6 | int cx_cmp_ulonglong(const void *i1, const void *i2) { | |
| 155 | 6 | unsigned long long a = *((const unsigned long long *) i1); | |
| 156 | 6 | unsigned long long b = *((const unsigned long long *) i2); | |
| 157 | 6 | return cx_vcmp_ulonglong(a, b); | |
| 158 | } | ||
| 159 | |||
| 160 | 6 | int cx_vcmp_uint16(uint16_t a, uint16_t b) { | |
| 161 |
2/2✓ Branch 0 (2→3) taken 2 times.
✓ Branch 1 (2→4) taken 4 times.
|
6 | if (a == b) { |
| 162 | 2 | return 0; | |
| 163 | } else { | ||
| 164 |
2/2✓ Branch 0 (4→5) taken 2 times.
✓ Branch 1 (4→6) taken 2 times.
|
4 | return a < b ? -1 : 1; |
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | 6 | int cx_cmp_uint16(const void *i1, const void *i2) { | |
| 169 | 6 | uint16_t a = *((const uint16_t *) i1); | |
| 170 | 6 | uint16_t b = *((const uint16_t *) i2); | |
| 171 | 6 | return cx_vcmp_uint16(a, b); | |
| 172 | } | ||
| 173 | |||
| 174 | 6 | int cx_vcmp_uint32(uint32_t a, uint32_t b) { | |
| 175 |
2/2✓ Branch 0 (2→3) taken 2 times.
✓ Branch 1 (2→4) taken 4 times.
|
6 | if (a == b) { |
| 176 | 2 | return 0; | |
| 177 | } else { | ||
| 178 |
2/2✓ Branch 0 (4→5) taken 2 times.
✓ Branch 1 (4→6) taken 2 times.
|
4 | return a < b ? -1 : 1; |
| 179 | } | ||
| 180 | } | ||
| 181 | |||
| 182 | 6 | int cx_cmp_uint32(const void *i1, const void *i2) { | |
| 183 | 6 | uint32_t a = *((const uint32_t *) i1); | |
| 184 | 6 | uint32_t b = *((const uint32_t *) i2); | |
| 185 | 6 | return cx_vcmp_uint32(a, b); | |
| 186 | } | ||
| 187 | |||
| 188 | 3400 | int cx_vcmp_uint64(uint64_t a, uint64_t b) { | |
| 189 |
2/2✓ Branch 0 (2→3) taken 738 times.
✓ Branch 1 (2→4) taken 2662 times.
|
3400 | if (a == b) { |
| 190 | 738 | return 0; | |
| 191 | } else { | ||
| 192 |
2/2✓ Branch 0 (4→5) taken 2157 times.
✓ Branch 1 (4→6) taken 505 times.
|
2662 | return a < b ? -1 : 1; |
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | 6 | int cx_cmp_uint64(const void *i1, const void *i2) { | |
| 197 | 6 | uint64_t a = *((const uint64_t *) i1); | |
| 198 | 6 | uint64_t b = *((const uint64_t *) i2); | |
| 199 | 6 | return cx_vcmp_uint64(a, b); | |
| 200 | } | ||
| 201 | |||
| 202 | 742 | int cx_vcmp_size(size_t a, size_t b) { | |
| 203 |
2/2✓ Branch 0 (2→3) taken 737 times.
✓ Branch 1 (2→4) taken 5 times.
|
742 | if (a == b) { |
| 204 | 737 | return 0; | |
| 205 | } else { | ||
| 206 |
2/2✓ Branch 0 (4→5) taken 3 times.
✓ Branch 1 (4→6) taken 2 times.
|
5 | return a < b ? -1 : 1; |
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | 6 | int cx_cmp_size(const void *i1, const void *i2) { | |
| 211 | 6 | size_t a = *((const size_t *) i1); | |
| 212 | 6 | size_t b = *((const size_t *) i2); | |
| 213 | 6 | return cx_vcmp_size(a, b); | |
| 214 | } | ||
| 215 | |||
| 216 | 18 | int cx_vcmp_float(float a, float b) { | |
| 217 |
2/2✓ Branch 0 (2→3) taken 12 times.
✓ Branch 1 (2→4) taken 6 times.
|
18 | if (fabsf(a - b) < 1e-6f) { |
| 218 | 12 | return 0; | |
| 219 | } else { | ||
| 220 |
2/2✓ Branch 0 (4→5) taken 3 times.
✓ Branch 1 (4→6) taken 3 times.
|
6 | return a < b ? -1 : 1; |
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | 8 | int cx_cmp_float(const void *f1, const void *f2) { | |
| 225 | 8 | float a = *((const float *) f1); | |
| 226 | 8 | float b = *((const float *) f2); | |
| 227 | 8 | return cx_vcmp_float(a, b); | |
| 228 | } | ||
| 229 | |||
| 230 | 193 | int cx_vcmp_double(double a, double b) { | |
| 231 |
2/2✓ Branch 0 (2→3) taken 63 times.
✓ Branch 1 (2→4) taken 130 times.
|
193 | if (fabs(a - b) < 1e-14) { |
| 232 | 63 | return 0; | |
| 233 | } else { | ||
| 234 |
2/2✓ Branch 0 (4→5) taken 65 times.
✓ Branch 1 (4→6) taken 65 times.
|
130 | return a < b ? -1 : 1; |
| 235 | } | ||
| 236 | } | ||
| 237 | |||
| 238 | 8 | int cx_cmp_double( | |
| 239 | const void *d1, | ||
| 240 | const void *d2 | ||
| 241 | ) { | ||
| 242 | 8 | double a = *((const double *) d1); | |
| 243 | 8 | double b = *((const double *) d2); | |
| 244 | 8 | return cx_vcmp_double(a, b); | |
| 245 | } | ||
| 246 | |||
| 247 | 8 | int cx_vcmp_intptr(intptr_t p1, intptr_t p2) { | |
| 248 |
2/2✓ Branch 0 (2→3) taken 2 times.
✓ Branch 1 (2→4) taken 6 times.
|
8 | if (p1 == p2) { |
| 249 | 2 | return 0; | |
| 250 | } else { | ||
| 251 |
2/2✓ Branch 0 (4→5) taken 3 times.
✓ Branch 1 (4→6) taken 3 times.
|
6 | return p1 < p2 ? -1 : 1; |
| 252 | } | ||
| 253 | } | ||
| 254 | |||
| 255 | 8 | int cx_cmp_intptr( | |
| 256 | const void *ptr1, | ||
| 257 | const void *ptr2 | ||
| 258 | ) { | ||
| 259 | 8 | intptr_t p1 = *(const intptr_t *) ptr1; | |
| 260 | 8 | intptr_t p2 = *(const intptr_t *) ptr2; | |
| 261 | 8 | return cx_vcmp_intptr(p1, p2); | |
| 262 | } | ||
| 263 | |||
| 264 | 6 | int cx_vcmp_uintptr(uintptr_t p1, uintptr_t p2) { | |
| 265 |
2/2✓ Branch 0 (2→3) taken 2 times.
✓ Branch 1 (2→4) taken 4 times.
|
6 | if (p1 == p2) { |
| 266 | 2 | return 0; | |
| 267 | } else { | ||
| 268 |
2/2✓ Branch 0 (4→5) taken 2 times.
✓ Branch 1 (4→6) taken 2 times.
|
4 | return p1 < p2 ? -1 : 1; |
| 269 | } | ||
| 270 | } | ||
| 271 | |||
| 272 | 6 | int cx_cmp_uintptr( | |
| 273 | const void *ptr1, | ||
| 274 | const void *ptr2 | ||
| 275 | ) { | ||
| 276 | 6 | uintptr_t p1 = *(const uintptr_t *) ptr1; | |
| 277 | 6 | uintptr_t p2 = *(const uintptr_t *) ptr2; | |
| 278 | 6 | return cx_vcmp_uintptr(p1, p2); | |
| 279 | } | ||
| 280 | |||
| 281 | 1142 | int cx_cmp_ptr( | |
| 282 | const void *ptr1, | ||
| 283 | const void *ptr2 | ||
| 284 | ) { | ||
| 285 | 1142 | uintptr_t p1 = (uintptr_t) ptr1; | |
| 286 | 1142 | uintptr_t p2 = (uintptr_t) ptr2; | |
| 287 |
2/2✓ Branch 0 (2→3) taken 24 times.
✓ Branch 1 (2→4) taken 1118 times.
|
1142 | if (p1 == p2) { |
| 288 | 24 | return 0; | |
| 289 | } else { | ||
| 290 |
2/2✓ Branch 0 (4→5) taken 374 times.
✓ Branch 1 (4→6) taken 744 times.
|
1118 | return p1 < p2 ? -1 : 1; |
| 291 | } | ||
| 292 | } | ||
| 293 | |||
| 294 | 228444 | int cx_cmp_wrap( | |
| 295 | const void *ptr1, | ||
| 296 | const void *ptr2, | ||
| 297 | void *w | ||
| 298 | ) { | ||
| 299 | 228444 | cx_compare_func_wrapper *wrapper = w; | |
| 300 | 228444 | return wrapper->cmp(ptr1, ptr2); | |
| 301 | } | ||
| 302 |