File Coverage

src/xh_sort.c
Criterion Covered Total %
statement 10 11 90.9
branch 3 4 75.0
condition n/a
subroutine n/a
pod n/a
total 13 15 86.6


line stmt bran cond sub pod time code
1             #include "xh_config.h"
2             #include "xh_core.h"
3              
4             static int
5 23           xh_sort_hash_cmp(const void *p1, const void *p2)
6             {
7 23           return xh_strcmp(((xh_sort_hash_t *) p1)->key, ((xh_sort_hash_t *) p2)->key);
8             }
9              
10             xh_sort_hash_t *
11 8           xh_sort_hash(HV *hash, size_t len)
12             {
13             xh_sort_hash_t *sorted_hash;
14             size_t i;
15              
16 8           sorted_hash = malloc(sizeof(xh_sort_hash_t) * len);
17 8 50         if (sorted_hash == NULL) {
18 0           croak("Memory allocation error");
19             }
20              
21 8           hv_iterinit(hash);
22              
23 30 100         for (i = 0; i < len; i++) {
24 22           sorted_hash[i].value = hv_iternextsv(hash, (char **) &sorted_hash[i].key, &sorted_hash[i].key_len);
25             }
26              
27 8           qsort(sorted_hash, len, sizeof(xh_sort_hash_t), xh_sort_hash_cmp);
28              
29 8           return sorted_hash;
30             }