|
1 | 1 | #include "assertion_handler.h"
|
2 | 2 |
|
3 |
| -#include <assert.h> |
4 |
| -#include <jansson.h> |
5 |
| - |
6 |
| -struct assertion_handler_s { |
7 |
| - TAILQ_HEAD(, assertion_e) assertion_q; |
8 |
| - TAILQ_HEAD(, value_e) value_q; |
9 |
| - pthread_mutex_t assertion_mutex; |
10 |
| - pthread_mutex_t value_mutex; |
| 3 | +/** |
| 4 | + * Queue for store assertions |
| 5 | + */ |
| 6 | +struct assertion_e { |
| 7 | + TAILQ_ENTRY(assertion_e) tailq; |
| 8 | + char *assertion; |
11 | 9 | };
|
12 | 10 |
|
13 | 11 | /**
|
14 |
| - * Creates a new assertion_handler |
15 |
| - * @return assertion_handler The new assertion handler |
| 12 | + * Initializes the handler |
| 13 | + * @param assertion_handler Handler that will be used |
| 14 | + * @return Error code |
16 | 15 | */
|
17 |
| -struct assertion_handler_s *assertion_handler_new() { |
| 16 | +struct assertion_handler_s *assertion_handler_create() { |
18 | 17 | struct assertion_handler_s *assertion_handler =
|
19 |
| - (struct assertion_handler_s *)malloc(sizeof(struct assertion_handler_s)); |
20 |
| - TAILQ_INIT(&assertion_handler->assertion_q); |
21 |
| - TAILQ_INIT(&assertion_handler->value_q); |
| 18 | + (struct assertion_handler_s *)calloc(1, |
| 19 | + sizeof(struct assertion_handler_s)); |
22 | 20 |
|
23 |
| - if ((pthread_mutex_init(&assertion_handler->assertion_mutex, NULL) || |
24 |
| - pthread_mutex_init(&assertion_handler->value_mutex, NULL)) != 0) { |
25 |
| - printf("\n mutex init failed\n"); |
| 21 | + if (pthread_mutex_init(&assertion_handler->mutex, NULL) != 0) { |
| 22 | + return NULL; |
26 | 23 | }
|
27 | 24 |
|
| 25 | + TAILQ_INIT(&assertion_handler->assertion_q); |
| 26 | + |
28 | 27 | return assertion_handler;
|
29 | 28 | }
|
30 | 29 |
|
31 | 30 | /**
|
32 |
| - * Inserts an assertion at the end of the assertion queue |
33 |
| - * @param assertion_handler assertion_handler_s to add the assertion |
34 |
| - * @param assertion New assertion to add |
| 31 | + * Adds an assertion to a given handler |
| 32 | + * @param assertion_handler Handler that will be used |
| 33 | + * @param message_assertion Message to assert |
| 34 | + * @return Error code |
35 | 35 | */
|
36 |
| -void assertion_handler_push_assertion( |
37 |
| - struct assertion_handler_s *assertion_handler, |
38 |
| - struct assertion_e *assertion) { |
| 36 | +int assertion_handler_add(struct assertion_handler_s *assertion_handler, |
| 37 | + char *message_assertion) { |
39 | 38 |
|
40 |
| - pthread_mutex_lock(&assertion_handler->assertion_mutex); |
41 |
| - TAILQ_INSERT_TAIL(&assertion_handler->assertion_q, assertion, tailq); |
42 |
| - pthread_mutex_unlock(&assertion_handler->assertion_mutex); |
43 |
| -} |
| 39 | + if (NULL == message_assertion) { |
| 40 | + return 1; |
| 41 | + } |
44 | 42 |
|
45 |
| -/** |
46 |
| - * Inserts a new value at the end of the value queue |
47 |
| - * @param assertion_handler assertion_handler_s to add the assertion |
48 |
| - * @param value New value to add |
49 |
| - */ |
50 |
| -void assertion_handler_push_value(struct assertion_handler_s *assertion_handler, |
51 |
| - struct value_e *value) { |
| 43 | + struct assertion_e *assertion = |
| 44 | + (struct assertion_e *)calloc(1, sizeof(struct assertion_e)); |
52 | 45 |
|
53 |
| - pthread_mutex_lock(&assertion_handler->value_mutex); |
54 |
| - TAILQ_INSERT_TAIL(&assertion_handler->value_q, value, tailq); |
55 |
| - pthread_mutex_unlock(&assertion_handler->value_mutex); |
| 46 | + assertion->assertion = strdup(message_assertion); |
| 47 | + |
| 48 | + if (strlen(message_assertion) > 0) { |
| 49 | + pthread_mutex_lock(&assertion_handler->mutex); |
| 50 | + TAILQ_INSERT_TAIL(&assertion_handler->assertion_q, assertion, tailq); |
| 51 | + pthread_mutex_unlock(&assertion_handler->mutex); |
| 52 | + } |
| 53 | + |
| 54 | + return 0; |
56 | 55 | }
|
57 | 56 |
|
58 | 57 | /**
|
59 |
| - * Iterates the assertions and check for matchs between assertions and values |
60 |
| - * @param assertion_handler assertion_handler_s to use |
| 58 | + * Assert every element in the queue |
| 59 | + * @param assertion_handler Handler that will be used |
| 60 | + * @return Error code |
61 | 61 | */
|
62 | 62 | int assertion_handler_assert(struct assertion_handler_s *assertion_handler) {
|
63 |
| - uint found = 0; |
64 |
| - int err = 0; |
65 |
| - |
66 |
| - pthread_mutex_lock(&assertion_handler->value_mutex); |
67 |
| - pthread_mutex_lock(&assertion_handler->assertion_mutex); |
68 |
| - |
69 | 63 | struct assertion_e *assertion;
|
70 |
| - TAILQ_FOREACH(assertion, &assertion_handler->assertion_q, tailq) { |
71 |
| - struct value_e *value; |
72 |
| - |
73 |
| - json_error_t assertion_jerr; |
74 |
| - char *assertion_message = NULL; |
75 |
| - |
76 |
| - json_t *assertion_json = json_loads(assertion->str, 0, &assertion_jerr); |
77 |
| - json_unpack_ex(assertion_json, &assertion_jerr, JSON_STRICT, "{s:s}", |
78 |
| - "message", &assertion_message); |
79 | 64 |
|
80 |
| - found = 0; |
81 |
| - |
82 |
| - TAILQ_FOREACH(value, &assertion_handler->value_q, tailq) { |
83 |
| - json_error_t value_jerr; |
84 |
| - char *value_message = NULL; |
85 |
| - |
86 |
| - json_t *value_json = json_loads(value->str, 0, &value_jerr); |
87 |
| - json_unpack_ex(value_json, &value_jerr, JSON_STRICT, "{s:s}", |
88 |
| - "message", &value_message); |
89 |
| - |
90 |
| - if (assertion_message == NULL || value_message == NULL) { |
91 |
| - return 1; |
92 |
| - } |
93 |
| - |
94 |
| - // printf("%s:%s -> %d\n", assertion_message, value_message, |
95 |
| - // strcmp(assertion_message, value_message) == 0); |
96 |
| - |
97 |
| - if (strcmp(assertion_message, value_message) == 0) { |
98 |
| - found++; |
99 |
| - json_decref(value_json); |
100 |
| - break; |
101 |
| - } |
102 |
| - |
103 |
| - json_decref(value_json); |
104 |
| - } |
105 |
| - |
106 |
| - json_decref(assertion_json); |
107 |
| - |
108 |
| - if (!found) { |
109 |
| - err++; |
110 |
| - break; |
111 |
| - } |
| 65 | + //// NOTE Debug block |
| 66 | + pthread_mutex_lock(&assertion_handler->mutex); |
| 67 | + TAILQ_FOREACH(assertion, &assertion_handler->assertion_q, tailq) { |
| 68 | + printf("ASSERTION: %s\n", assertion->assertion); |
| 69 | + pthread_mutex_unlock(&assertion_handler->mutex); |
112 | 70 | }
|
| 71 | + //// |
113 | 72 |
|
114 |
| - pthread_mutex_unlock(&assertion_handler->assertion_mutex); |
115 |
| - pthread_mutex_unlock(&assertion_handler->value_mutex); |
| 73 | + // TODO Assert here |
116 | 74 |
|
117 |
| - return err; |
| 75 | + return 0; |
118 | 76 | }
|
119 | 77 |
|
120 |
| -void assertion_handler_destroy(struct assertion_handler_s *assertion_handler) { |
121 |
| - pthread_mutex_lock(&assertion_handler->value_mutex); |
122 |
| - pthread_mutex_lock(&assertion_handler->assertion_mutex); |
123 |
| - |
124 |
| - struct assertion_e *assertion = NULL; |
125 |
| - while (!TAILQ_EMPTY(&assertion_handler->assertion_q)) { |
126 |
| - assertion = TAILQ_FIRST(&assertion_handler->assertion_q); |
127 |
| - TAILQ_REMOVE(&assertion_handler->assertion_q, assertion, tailq); |
128 |
| - free(assertion->str); |
129 |
| - free(assertion); |
130 |
| - } |
131 |
| - |
132 |
| - struct value_e *value; |
133 |
| - while (!TAILQ_EMPTY(&assertion_handler->value_q)) { |
134 |
| - value = TAILQ_FIRST(&assertion_handler->value_q); |
135 |
| - TAILQ_REMOVE(&assertion_handler->value_q, value, tailq); |
136 |
| - free(value->str); |
137 |
| - free(value); |
138 |
| - } |
139 |
| - |
140 |
| - pthread_mutex_unlock(&assertion_handler->assertion_mutex); |
141 |
| - pthread_mutex_unlock(&assertion_handler->value_mutex); |
142 |
| - |
143 |
| - pthread_mutex_destroy(&assertion_handler->value_mutex); |
144 |
| - pthread_mutex_destroy(&assertion_handler->assertion_mutex); |
| 78 | +/** |
| 79 | + * Removes every element in the queue and free the handler |
| 80 | + * @param assertion_handler Handler that will be used |
| 81 | + * @return Error code |
| 82 | + */ |
| 83 | +int assertion_handler_destroy(struct assertion_handler_s *assertion_handler) { |
| 84 | + // TODO Free elements |
145 | 85 |
|
| 86 | + pthread_mutex_destroy(&assertion_handler->mutex); |
146 | 87 | free(assertion_handler);
|
| 88 | + return 0; |
147 | 89 | }
|
0 commit comments