From bb92a32d1770f97ac7503b6c3346bc36081279d9 Mon Sep 17 00:00:00 2001 From: tlatorre Date: Thu, 5 Jul 2018 15:11:58 -0400 Subject: add a simple test for the various muon functions --- test.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 53 insertions(+), 11 deletions(-) (limited to 'test.c') diff --git a/test.c b/test.c index 77beba7..1491711 100644 --- a/test.c +++ b/test.c @@ -2,6 +2,9 @@ #include #include #include "refractive_index.h" +#include "muon.h" + +typedef int testFunction(char *err); /* Table of some of the tabulated values of the refractive index of water as a * function of wavelength and temperature. In all cases, I just used the values @@ -106,6 +109,36 @@ int isclose(double a, double b, double rel_tol, double abs_tol) return fabs(a-b) <= fmax(rel_tol*fmax(fabs(a),fabs(b)),abs_tol); } +int test_muon_get_range(char *err) +{ + /* A very simple test to make sure we read in the PDG table correctly. */ + double value; + + value = get_range(1.0,1.0); + + if (!isclose(value, 1.863e-3, 1e-5, 0)) { + sprintf(err, "range = %.5f, but expected %.5f", value, 1.863e-3); + return 1; + } + + return 0; +} + +int test_muon_get_dEdx(char *err) +{ + /* A very simple test to make sure we read in the PDG table correctly. */ + double value; + + value = get_dEdx(1.0,1.0); + + if (!isclose(value, 6.097, 1e-5, 0)) { + sprintf(err, "dE/dx = %.5f, but expected %.5f", value, 6.097); + return 1; + } + + return 0; +} + int test_refractive_index(char *err) { /* Tests the get_index() function. */ @@ -158,23 +191,32 @@ int test_solid_angle(char *err) return 0; } +struct tests { + testFunction *test; + char *name; +} tests[] = { + {test_solid_angle, "test_solid_angle"}, + {test_refractive_index, "test_refractive_index"}, + {test_muon_get_dEdx, "test_muon_get_dEdx"}, + {test_muon_get_range, "test_muon_get_range"} +}; + int main(int argc, char **argv) { + int i; char err[256]; int retval = 0; + struct tests test; - if (!test_solid_angle(err)) { - printf("[\033[92mok\033[0m] test_solid_angle\n"); - } else { - printf("[\033[91mfail\033[0m] test_solid_angle: %s\n", err); - retval = 1; - } + for (i = 0; i < sizeof(tests)/sizeof(struct tests); i++) { + test = tests[i]; - if (!test_refractive_index(err)) { - printf("[\033[92mok\033[0m] test_refractive_index\n"); - } else { - printf("[\033[91mfail\033[0m] test_refractive_index: %s\n", err); - retval = 1; + if (!test.test(err)) { + printf("[\033[92mok\033[0m] %s\n", test.name); + } else { + printf("[\033[91mfail\033[0m] %s: %s\n", test.name, err); + retval = 1; + } } return retval; -- cgit