c-template
returns_test.c
Go to the documentation of this file.
1
#include <string.h>
2
#include <stdlib.h>
3
#include <stdarg.h>
4
#include <stddef.h>
5
#include <setjmp.h>
6
#include <cmocka.h>
7
#include <assert.h>
8
#include "../../include/utils/errors.h"
9
#include "../../include/utils/returns.h"
10
11
#pragma GCC diagnostic ignored "-Wunused-parameter"
12
#pragma GCC diagnostic ignored "-Wunused-variable"
13
void
test_cg_return_no_error
(
void
**state) {
14
// ensures that no error passed in results in a NULL err
15
int
a = 10;
16
cg_return
*ret_val =
new_cg_return
(&a, NULL);
17
assert(ret_val != NULL);
18
assert(ret_val->
value
!= NULL);
19
assert(ret_val->
err
== NULL);
20
int
*aa = (
int
*)ret_val->
value
;
21
assert(*aa == a);
22
free_cg_return
(ret_val);
23
}
24
25
#pragma GCC diagnostic ignored "-Wunused-parameter"
26
#pragma GCC diagnostic ignored "-Wunused-variable"
27
void
test_cg_return_error
(
void
**state) {
28
char
*msg =
"thisisanerrormessage"
;
29
// ensures that the correct error is passed through
30
cg_error
*err =
new_cg_error
(msg);
31
assert(err != NULL);
32
cg_return
*ret_val =
new_cg_return
(NULL, err);
33
assert(ret_val != NULL);
34
assert(
35
strcmp(msg, ret_val->
err
->
message
) == 0
36
);
37
free_cg_return
(ret_val);
38
}
39
40
41
int
main
(
void
) {
42
const
struct
CMUnitTest tests[] = {
43
cmocka_unit_test(
test_cg_return_no_error
),
44
cmocka_unit_test(
test_cg_return_error
),
45
};
46
return
cmocka_run_group_tests(tests, NULL, NULL);
47
}
cg_return::value
void * value
Definition:
returns.h:16
main
int main(void)
Definition:
returns_test.c:41
cg_return::err
cg_error * err
Definition:
returns.h:19
new_cg_error
cg_error * new_cg_error(char *message)
returns a cg_error struct containing message
Definition:
errors.c:7
new_cg_return
cg_return * new_cg_return(void *value, cg_error *err)
creates a new cg_return struct
Definition:
returns.c:5
cg_error::message
char * message
Definition:
errors.h:15
cg_return
allows returning both a value and error message from a function call useful as it makes it easier to ...
Definition:
returns.h:14
cg_error
Definition:
errors.h:14
free_cg_return
void free_cg_return(cg_return *ret)
frees up resources associated with an instance of cg_return
Definition:
returns.c:24
test_cg_return_error
void test_cg_return_error(void **state)
Definition:
returns_test.c:27
test_cg_return_no_error
void test_cg_return_no_error(void **state)
Definition:
returns_test.c:13
src
utils
returns_test.c
Generated by
1.8.17