Skip to content

Commit 04f2574

Browse files
committed
Add assertion handler
1 parent 9b767ec commit 04f2574

File tree

2 files changed

+69
-139
lines changed

2 files changed

+69
-139
lines changed

tests/assertion_handler.c

Lines changed: 56 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,147 +1,89 @@
11
#include "assertion_handler.h"
22

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;
119
};
1210

1311
/**
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
1615
*/
17-
struct assertion_handler_s *assertion_handler_new() {
16+
struct assertion_handler_s *assertion_handler_create() {
1817
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));
2220

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;
2623
}
2724

25+
TAILQ_INIT(&assertion_handler->assertion_q);
26+
2827
return assertion_handler;
2928
}
3029

3130
/**
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
3535
*/
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) {
3938

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+
}
4442

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));
5245

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;
5655
}
5756

5857
/**
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
6161
*/
6262
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-
6963
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);
7964

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);
11270
}
71+
////
11372

114-
pthread_mutex_unlock(&assertion_handler->assertion_mutex);
115-
pthread_mutex_unlock(&assertion_handler->value_mutex);
73+
// TODO Assert here
11674

117-
return err;
75+
return 0;
11876
}
11977

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
14585

86+
pthread_mutex_destroy(&assertion_handler->mutex);
14687
free(assertion_handler);
88+
return 0;
14789
}

tests/assertion_handler.h

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,19 @@
1-
#include "stdio.h"
2-
#include "stdlib.h"
3-
4-
#include <string.h>
51
#include <pthread.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <string.h>
65
#include <sys/queue.h>
76

8-
// Public structs
9-
struct assertion_e {
10-
TAILQ_ENTRY(assertion_e) tailq;
11-
char *str;
7+
/**
8+
* assertion_handler_s is used to store assertions and check them later.
9+
*/
10+
struct assertion_handler_s {
11+
TAILQ_HEAD(, assertion_e) assertion_q;
12+
pthread_mutex_t mutex; // Mutex used to keep the handle memory safe
1213
};
1314

14-
struct value_e {
15-
TAILQ_ENTRY(value_e) tailq;
16-
char *str;
17-
size_t len;
18-
};
19-
20-
// Private structs
21-
struct assertion_handler_s;
22-
23-
// Functions
24-
struct assertion_handler_s *assertion_handler_new();
25-
void assertion_handler_push_assertion(
26-
struct assertion_handler_s *assertion_handler,
27-
struct assertion_e *assertion);
28-
void assertion_handler_push_value(struct assertion_handler_s *assertion_handler,
29-
struct value_e *value);
15+
struct assertion_handler_s *assertion_handler_create();
16+
int assertion_handler_add(struct assertion_handler_s *assertion_handler,
17+
char *message_assertion);
3018
int assertion_handler_assert(struct assertion_handler_s *assertion_handler);
31-
void assertion_handler_destroy(struct assertion_handler_s *assertion_handler);
19+
int assertion_handler_destroy(struct assertion_handler_s *assertion_handler);

0 commit comments

Comments
 (0)