Skip to content

Commit

Permalink
C unit testing with utest.h library
Browse files Browse the repository at this point in the history
  • Loading branch information
ARibecaJob committed Apr 7, 2023
1 parent 62dbfea commit 90d3ab7
Show file tree
Hide file tree
Showing 4 changed files with 1,711 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tests/C/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# enables most optimization flags
CC := gcc -O3

TEST01_SRC := test_01.c
TEST01_OBJ := $(TEST01_SRC:.c=.o)
TEST01_BIN := $(join ${TGTDIR}, test_01)

${TEST01_BIN}:
@echo "Building ${TEST01_BIN}..."
${CC} -w -c ${TEST01_SRC} ;\
${CC} ${TEST01_OBJ} -w -lm -o ${TEST01_BIN}

clean:
@echo "Cleaning up..."
rm -vf ${TEST01_OBJ} ; rm -vf ${TEST01_BIN}
@echo "Done cleaning up"
70 changes: 70 additions & 0 deletions tests/C/test_01.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
test_01.c
check library output for reference:
Running suite(s): Meteo
66%: Checks: 3, Failures: 1, Errors: 0
test_01.c:13:F:Sanity checks:sanity_check:0: this should fail
test_01.c:18:P:Sanity checks:sanity_check_2:0: Passed
test_01.c:53:P:Dataset files:test_is_valid_era_filename:0: Passed
*/

#include "utest_oneflux.h"
#include "../../oneflux_steps/common/common.c"
#include "../../oneflux_steps/meteo_proc/src/dataset.c"

/*
dataset.c needs some external variables
*/
char *qc_auto_files_path = NULL; /* mandatory */
char *era_files_path = NULL; /* mandatory */
char *output_files_path = NULL; /* mandatory */

UTEST(sanity_check, 1)
{
FAIL_UNLESS(5 == 5, "this should succeed");
FAIL_UNLESS(6 == 5, "this should fail");
}

UTEST(sanity_check, 2)
{
FAIL_UNLESS(5 == 5, "this should succeed");
}

UTEST(test_is_valid_era_filename, 1)
{
char* filename;

filename = "";

/* CK_ASSERT_MSG: Fails test if supplied condition evaluates to false and displays user provided message. */

CK_ASSERT_MSG(!is_valid_era_filename(filename), "False should be returned for this string: '%s'", filename);

filename = " - _9999.csv";
CK_ASSERT_MSG(is_valid_era_filename(filename), "True should be returned for this string: %s", filename);

filename = "";
CK_ASSERT_MSG(!is_valid_era_filename(filename), "False should be returned for this string: %s", filename);

// example of a correct string
filename = "US-ARc_2009.csv";
CK_ASSERT_MSG(is_valid_era_filename(filename), "True should be returned for this string: %s", filename);
}

/*
PLEASE NOTE:
we can use 'UTEST_MAIN' instead of call 'UTEST_STATE' and 'main'
but in this way we can do our own stuff like load files and so on...
*/

UTEST_STATE();

int main(int argc, const char *const argv[])
{
/* do your own stuff here! */

return utest_main(argc, argv);
}
Loading

0 comments on commit 90d3ab7

Please sign in to comment.