GCC Code Coverage Report


Directory: ./
File: sources/Component/component_size.c
Date: 2023-09-29 04:53:15
Exec Total Coverage
Lines: 14 25 56.0%
Branches: 7 12 58.3%

Line Branch Exec Source
1 /*
2 * Filename: sources/Component/size.c
3 * Path: sources/Component
4 * Created Date: Thursday, March 9th 2023, 3:10:24 pm
5 * Author: Thomas
6 *
7 * Copyright (c) 2023 our_rpg
8 */
9
10 #include "components.h"
11 #include "json.h"
12 #include "world_logger.h"
13 #include <stdlib.h>
14 #include <string.h>
15
16 /// @brief Check if component is size type.
17 /// @param component Component to check.
18 /// @return Return 1 if component is C_SIZE, 0 otherwise.
19 static int component_is_size(const component_t *component)
20 {
21 6 if (component->type == C_SIZE)
22 return 1;
23 log_error("Component is not size.");
24 return 0;
25 }
26
27 3 int component_size_constructor(component_t *component, void *data)
28 {
29 int rvalue = 0;
30
31 3 component->type = C_SIZE;
32 3 component->data = malloc(sizeof(ecs_vector2i_t));
33
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!component->data) {
34 log_fatal("Could not allocate memory for size component.");
35 return -1;
36 }
37
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (!data) {
38 data = &(ecs_vector2i_t){0, 0};
39 }
40 3 rvalue = component_size_set(component, data);
41 3 log_info("Size component created.");
42 3 return rvalue;
43 }
44
45 int component_size_constructor_from_json(component_t *component, void *data)
46 {
47 json_object *obj = json_tokener_parse(data);
48 ecs_vector2i_t v = {0};
49
50 v.x = json_object_get_int(json_object_object_get(obj, "width"));
51 v.y = json_object_get_int(json_object_object_get(obj, "height"));
52 return component_size_constructor(component, &v);
53 }
54
55
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 int component_size_set(component_t *component, void *data)
56 {
57
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (!component_is_size(component) || !component->data || !data)
58 return -1;
59 memcpy(component->data, data, sizeof(ecs_vector2i_t));
60 4 return 0;
61 }
62
63
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 void *component_size_get(const component_t *component)
64 {
65 if (!component_is_size(component))
66 return 0;
67 2 return component->data;
68 }
69