Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C unit testing with Check #49

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions .github/workflows/python-app.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

name: Python application
name: ONEFlux CI

on:
push:
Expand All @@ -24,11 +21,10 @@ jobs:
- name: Install OneFLUX
run: |
python -m pip install --upgrade pip
pip install setuptools==44.1.1 wheel pytest
pip install setuptools wheel pytest
# pip install flake8 pytest
# if [ -f frozen-requirement.txt ]; then pip install -r frozen-requirements.txt; fi
make
- name: Get data
- name: Download sample data
run: |
mkdir -p ./tests/data/test_input
mkdir -p ./tests/data/test_output
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ buildc:
@echo "\nBuilding C code executables..."
$(MAKE) -C oneflux_steps

# buildc_test:
# $(MAKE) -C oneflux_steps test


.PHONY: init test buildpy buildc
2 changes: 1 addition & 1 deletion oneflux_steps/meteo_proc/src/dataset.c
Original file line number Diff line number Diff line change
Expand Up @@ -4914,7 +4914,7 @@ int compute_datasets(DATASET *const datasets, const int datasets_count) {
New option added 20160616 to take into account the possibility that ERA is
not present in a given year (recent years). In this case the data filled using
MDS are used for the _M and the QC is set as 3
AGGIUNGI COME SOPRA, SE LW_IN_CALC_M � ancora -9999 metter LW_IN_CALC e QC = 3
AGGIUNGI COME SOPRA, SE LW_IN_CALC_M � ancora -9999 metter LW_IN_CALC e QC = 3
*/
if ( IS_INVALID_VALUE(current_dataset->rows[i].value[LW_IN_CALC_M]) ) {
current_dataset->rows[i].value[LW_IN_CALC_M] = current_dataset->rows[i].value[LW_IN_CALC];
Expand Down
3 changes: 3 additions & 0 deletions oneflux_steps/meteo_proc/src/dataset.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,8 @@ void free_datasets(DATASET *datasets, const int datasets_count);
DATASET *get_datasets(int *const datasets_count);
int compute_datasets(DATASET *const datasets, const int datasets_count);

/* int is_valid_era_filename(const char *const filename); */
/* int is_valid_met_filename(const char *const filename); */

/* */
#endif /* DATASET_H */
58 changes: 58 additions & 0 deletions tests/C/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
CC := gcc -O3
# copy file command (verbose, keep file metadata)
COPY = cp -v

SRCDIR := $(shell pwd)/
TGTDIR := ${HOME}/bin/oneflux/

# TEST_DIR := $(join ${SRCDIR}, )


METEO_PROC_DIR := $(join ${SRCDIR}, ../../oneflux_steps/meteo_proc/src/)
METEO_PROC_SRC := dataset.c
METEO_PROC_OBJ := $(METEO_PROC_SRC:.c=.o)
METEO_PROC_BIN := $(join ${TGTDIR}, meteo_proc)
METEO_TGT := test_meteo

TEST_METEO_DIR := ${SRCDIR}
TEST_METEO_SRC := test_meteo.c
TEST_METEO_OBJ := $(TEST_METEO_SRC:.c=.o)
TEST_METEO_BIN := $(join ${TGTDIR}, test_meteo)
TEST_METEO_TGT := test_meteo

COMMON_DIR := $(join ${SRCDIR}, ../../oneflux_steps/common/)
COMMON_SRC := common.c
COMMON_OBJ := $(COMMON_SRC:.c=.o)
COMMON_TGT := common

CHECK_PATH := /rds/project/rds-5mCMIDBOkPU/ma595/FluxNet/ONEFlux/tests/C/check-0.15.2/install/
CHECK_INCLUDE := $(join ${CHECK_PATH}, include)
CHECK_LIB := $(join ${CHECK_PATH}, lib)

${COMMON_TGT}:
@echo "\nBuilding common objects..."
cd ${COMMON_DIR} ;\
${CC} -w -c ${COMMON_SRC}

${METEO_TGT}: ${COMMON_TGT}
@echo "\nBuilding ${METEO_PROC_BIN}..."
cd ${METEO_PROC_DIR} ;\
${CC} -w -c ${METEO_PROC_SRC}

${TEST_METEO_BIN}: ${METEO_TGT}
@echo "\n${TEST_METEO_BIN}"
${CC} -w -c ${TEST_METEO_SRC} ;\
${CC} $(join ${METEO_PROC_DIR}, ${METEO_PROC_OBJ}) $(join ${COMMON_DIR}, ${COMMON_OBJ}) ${TEST_METEO_OBJ} -I ${METEO_PROC_DIR} -I ${CHECK_INCLUDE} -w -lm -L ${CHECK_LIB} -o ${TEST_METEO_BIN} -lcheck

# ${TEST_METEO_BIN}: ${COMMON_TGT}
# @echo "\n${TEST_METEO_BIN}"
# cd ${METEO_PROC_DIR} ;\
# ${CC} -w -c ${METEO_PROC_SRC} ;\
# cd ${TEST_METEO_DIR} ;\
# ${CC} -w -c ${TEST_METEO_SRC} ;\
# ${CC} $(join ${METEO_PROC_DIR}, ${METEO_PROC_OBJ}) $(join ${COMMON_DIR}, ${COMMON_OBJ}) ${TEST_METEO_OBJ} -I ${METEO_PROC_DIR} -I/rds/project/rds-5mCMIDBOkPU/ma595/FluxNet/ONEFlux/tests/C/check-0.15.2/install/include -w -lm -L/rds/project/rds-5mCMIDBOkPU/ma595/FluxNet/ONEFlux/tests/C/check-0.15.2/install/lib -o ${TEST_METEO_BIN} -lcheck

meteo_test: directories ${TEST_METEO_BIN}
@echo "\nBuild finished."

directories: ${TGTDIR}
117 changes: 117 additions & 0 deletions tests/C/test_meteo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#include <check.h>
#include <stdio.h>
#include <stdlib.h>
#include "../../oneflux_steps/meteo_proc/src/dataset.c"

char *qc_auto_files_path = NULL; /* mandatory */
char *era_files_path = NULL; /* mandatory */
char *output_files_path = NULL;

START_TEST(sanity_check) {
fail_unless(5 == 5, "this should succeed");
fail_unless(6 == 5, "this should fail");
}
END_TEST

START_TEST(sanity_check_2) {
fail_unless(5 == 5, "this should succeed");
}
END_TEST

START_TEST(test_import_era_values){

// create dummy input file and check that

// ck_assert_msg();

}
END_TEST

START_TEST(test_is_valid_era_filename) {
char * filename;
filename = "";
char out[1024];
//ck_assert_msg: Fails test if supplied condition evaluates to false and displays user provided message.

// fail_unless(is_valid_era_filename(filename), "this should fail");
sprintf(out, "%s should be returned for this string: '%s'", "False", filename);
ck_assert_msg(!is_valid_era_filename(filename), out);

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

// empty string
filename = "";
sprintf(out, "%s should be returned for this string: %s", "False", filename);
ck_assert_msg(!is_valid_era_filename(filename), out);

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

// // unterminated string (potential bug) no easy way to test this
// START_TEST(test_unterm)
// {
// char *unterm_str = [ 'h', 'e', 'l', ];
// fail_unless(is_valid_era_filename(unterm_str));
// }
// END_TEST

Suite *meteo_suite(void) {
Suite *s;
TCase *tc_ds_sanity, *tc_ds_files;

s = suite_create("Meteo");

/* Dataset sanity case */
tc_ds_sanity = tcase_create("Sanity checks");

// actual tests
tcase_add_test(tc_ds_sanity, sanity_check);
tcase_add_test(tc_ds_sanity, sanity_check_2);

// add the tests to the suite
suite_add_tcase(s, tc_ds_sanity);



/* Dataset file names test case */
tc_ds_files = tcase_create("Dataset files");

// actual tests
tcase_add_test(tc_ds_files, test_is_valid_era_filename);

// add the tests to the suite
suite_add_tcase(s, tc_ds_files);

return s;
}

// boiler plate that should never need
// to be changed
int main(void) {
int num_failed;
Suite *s;
SRunner *sr;

s = meteo_suite();
sr = srunner_create(s);

srunner_run_all(sr, CK_VERBOSE);
num_failed = srunner_ntests_failed(sr);
srunner_free(sr);
return (num_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;

// SRunner *sr = srunner_create(s1);
// suite_add_tcase(s1, tc1_1);
// tcase_add_test(tc1_1, sanity_check);
// tcase_add_test(tc2_1, sanity_check);
// srunner_run_all(sr, CK_ENV);
// nf = srunner_ntests_failed(sr);
// srunner_free(sr);
// return nf == 0 ? 0 : 1;
}