cbase coverage


File: src/type/private/t_base.c
Date: 2024-07-26 20:57:32
Lines:
29/29
100.0%
Functions:
6/6
100.0%
Branches:
10/10
100.0%

Line Branch Exec Source
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <stddef.h>
4
5 #include "type.h"
6
7 /**
8 * @brief Returns the identifier of specific type
9 *
10 * @param t The type
11 * @return const char* The identifier of the type
12 */
13 5 const char* type_identifier(const type_t* t) {
14 5 return t->identifier;
15 }
16
17 /**
18 * @brief Destroys an object of this type
19 *
20 * @param t The type
21 * @param self The object to destroy
22 */
23 1630 void type_destroy(const type_t* t, void* self) {
24
2/2
✓ Branch 0 taken 1193 times.
✓ Branch 1 taken 437 times.
1630 if (t->destroy != NULL)
25 1193 t->destroy(self);
26 1630 }
27
28 /**
29 * @brief Returns the comparison of two object of this type
30 *
31 * @param t The type
32 * @param self The object to copy
33 * @return void* The copy of the object
34 */
35 35 void* type_copy(const type_t* t, const void* self) {
36
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 34 times.
35 if (t->copy == NULL)
37 1 return type_copy(ptr_type, self);
38 34 return t->copy(self);
39 }
40
41 /**
42 * @brief Returns a representation of an object of this type
43 *
44 * @param t The type
45 * @param self The object to represent
46 * @return rope_t* Representation of the object
47 *
48 * @note The returned rope must be destroyed after use
49 * @note Returns NULL if no representation function is defined
50 */
51 58 rope_t* type_repr(const type_t* t, const void* self) {
52
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 56 times.
58 if (t->repr == NULL)
53 2 return type_repr(ptr_type, self);
54 56 return t->repr(self);
55 }
56
57 /**
58 * @brief Returns a hash of an object of this type
59 *
60 * @param t The type
61 * @param self The object to hash
62 * @return uint64_t The hash of the object
63 *
64 * @note Returns pointer hash if no hashing function is defined
65 */
66 59 uint64_t type_hash(const type_t* t, const void* self) {
67
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 58 times.
59 if (t->hash == NULL)
68 1 return type_hash(ptr_type, self);
69 58 return t->hash(self);
70 }
71
72 /**
73 * @brief Returns the comparison of two object of this type
74 *
75 * @param t The type
76 * @param self The first object to compare
77 * @param other The second object to compare
78 * @return int
79 * >0: self > other
80 * <0: self < other
81 * =0: self = other
82 *
83 * @note Returns pointer comparison if no comparison function is defined
84 */
85 1360 int type_cmp(const type_t* t, const void* self, const void* other) {
86
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1359 times.
1360 if (t->cmp == NULL)
87 1 return type_cmp(ptr_type, self, other);
88 1359 return t->cmp(self, other);
89 }
90
91 /**
92 * @brief Prints an object of this type
93 *
94 * @param f File to write to
95 * @param t The type
96 * @param self The object to print
97 */
98 2 void __type_print(FILE* f, const type_t* t, const void* self) {
99 2 rope_t* rope = type_repr(t, self);
100 2 char* repr = rope_str(rope);
101 2 fprintf(f, "%s\n", repr);
102 2 free(repr);
103 2 rope_destroy(rope);
104 2 }
105