diff --git a/nifticdf/CMakeLists.txt b/nifticdf/CMakeLists.txt index b7d48480..8af8e395 100644 --- a/nifticdf/CMakeLists.txt +++ b/nifticdf/CMakeLists.txt @@ -33,6 +33,11 @@ endif() if(NIFTI_BUILD_TESTING AND NIFTI_BUILD_APPLICATIONS) + add_executable(${NIFTI_PACKAGE_PREFIX}nifticdf_range_test nifticdf_range_test.c) + target_link_libraries(${NIFTI_PACKAGE_PREFIX}nifticdf_range_test PRIVATE ${NIFTI_CDFLIB_NAME}) + add_test( NAME ${NIFTI_PACKAGE_PREFIX}nifticdf_range_test + COMMAND $ ) + foreach(DISTRIBUTION CORREL TTEST FTEST ZSCORE CHISQ BETA BINOM GAMMA POISSON NORMAL FTEST_NONC CHISQ_NONC LOGISTIC LAPLACE UNIFORM TTEST_NONC WEIBULL CHI INVGAUSS EXTVAL PVAL LOGPVAL LOG10PVAL ) add_test( NAME ${NIFTI_PACKAGE_PREFIX}nifti_stats_${DISTRIBUTION}_test COMMAND $ 0:4:1 ${DISTRIBUTION}) add_test( NAME q${NIFTI_PACKAGE_PREFIX}nifti_stats_${DISTRIBUTION}_test COMMAND $ -q 0:4:1 ${DISTRIBUTION}) diff --git a/nifticdf/nifticdf.c b/nifticdf/nifticdf.c index bdab933f..d9dba0b9 100644 --- a/nifticdf/nifticdf.c +++ b/nifticdf/nifticdf.c @@ -1892,7 +1892,7 @@ static double T5,T6,T7,T8,T9,T10,T12,T13; /* Check arguments */ - if(!(*which < 1 && *which > 4)) goto S30; + if(!(*which < 1 || *which > 4)) goto S30; if(!(*which < 1)) goto S10; *bound = 1.0e0; goto S20; diff --git a/nifticdf/nifticdf_range_test.c b/nifticdf/nifticdf_range_test.c new file mode 100644 index 00000000..63e607d6 --- /dev/null +++ b/nifticdf/nifticdf_range_test.c @@ -0,0 +1,50 @@ +/* cdfbin must reject an out-of-range "which" selector the way its ten + siblings in nifticdf.c do: *status = -1 and *bound = the limit. */ + +#include +#include "nifticdf.h" + +static int check(int which, double expected_bound) +{ + int status = 999; + double p = 0.5, q = 0.5, s = 2.0, xn = 5.0, pr = 0.5, ompr = 0.5; + double bound = -999.0; + + cdfbin(&which, &p, &q, &s, &xn, &pr, &ompr, &status, &bound); + + if( status != -1 || bound != expected_bound ) { + fprintf(stderr, "** cdfbin(which=%d): status=%d bound=%g," + " expected status=-1 bound=%g\n", + which, status, bound, expected_bound); + return 1; + } + printf("cdfbin(which=%d): status=%d bound=%g\n", which, status, bound); + return 0; +} + +int main(void) +{ + int errs = 0; + + errs += check(9, 4.0); /* above the legal range */ + errs += check(0, 1.0); /* below the legal range */ + + /* a legal selector must still compute, not report a range error */ + { + int which = 1, status = 999; + double p = 0.0, q = 0.0, s = 2.0, xn = 5.0, pr = 0.5, ompr = 0.5; + double bound = -999.0; + + cdfbin(&which, &p, &q, &s, &xn, &pr, &ompr, &status, &bound); + if( status != 0 ) { + fprintf(stderr, "** cdfbin(which=1): status=%d, expected 0\n", status); + errs++; + } else { + printf("cdfbin(which=1): status=0 p=%g\n", p); + } + } + + if( errs ) { fprintf(stderr, "** %d failure(s)\n", errs); return 1; } + printf("nifticdf range test passed\n"); + return 0; +}