cbase coverage


File: src/list/private/l_type.c
Date: 2024-07-26 20:57:32
Lines:
28/28
100.0%
Functions:
2/2
100.0%
Branches:
8/8
100.0%

Line Branch Exec Source
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4
5 #include "list.h"
6 #include "l_internal.h"
7 #include "type.h"
8
9 // TODO Use rope concat instead
10 4 rope_t* list_repr(const list_t* self) {
11 4 list_t* item_reprs = list_create(str_type, true);
12 char* item_repr;
13 4 __list_node_t* n = &_head;
14 4 size_t len = 0;
15
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
16 while ((n = n->next) != &_tail) {
16 12 rope_t* rope = type_repr(_type, n->data);
17 12 item_repr = rope_str(rope);
18 12 list_append(item_reprs, item_repr);
19 12 len += strlen(item_repr) + 2;
20 12 rope_destroy(rope);
21 }
22
23 4 char* repr_iter, *repr = (char*)malloc(sizeof(*repr) * (len + 3));
24 4 repr[0] = '[';
25 4 repr_iter = repr + 1;
26 4 list_head(item_reprs);
27
2/2
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 4 times.
16 while (!list_next(item_reprs, (void**)&item_repr)) {
28 int len2;
29
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 4 times.
12 if (list_has_next(item_reprs))
30 8 sprintf(repr_iter, "%s, %n", item_repr, &len2);
31 else
32 4 sprintf(repr_iter, "%s%n", item_repr, &len2);
33 12 repr_iter += len2;
34 }
35 4 sprintf(repr_iter, "]");
36 4 list_destroy(item_reprs);
37 4 return repr;
38 }
39
40 /**
41 * @brief Return a hash of this list using a modified djb2
42 *
43 * @param self The string
44 * @return uint64_t The hash of this string
45 */
46 4 uint64_t list_hash(const list_t* self) {
47 4 uint64_t hash = 5381;
48 4 __list_node_t* n = &_head;
49
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
16 while ((n = n->next) != &_tail)
50 12 hash = ((hash << 5) + hash) + type_hash(_type, n->data);
51
52 4 return hash;
53 }
54
55 /// @brief List type
56 const type_t* list_type = &(type_t){
57 .identifier = "list",
58 .destroy = list_destroy,
59 .copy = list_copy,
60 .repr = list_repr,
61 .hash = list_hash,
62 .cmp = list_cmp
63 };
64