From 9646e43e6016536911f0703dc7e3af7a0eae56db Mon Sep 17 00:00:00 2001 From: tlatorre Date: Wed, 4 Jul 2018 12:22:21 -0400 Subject: add a test of the get_solid_angle function --- test.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 test.c (limited to 'test.c') diff --git a/test.c b/test.c new file mode 100644 index 0000000..62529ea --- /dev/null +++ b/test.c @@ -0,0 +1,44 @@ +#include "solid_angle.h" +#include +#include + +int isclose(double a, double b, double rel_tol, double abs_tol) +{ + /* Returns 1 if a and b are "close". This algorithm is taken from Python's + * math.isclose() function. + * + * See https://www.python.org/dev/peps/pep-0485/. */ + return fabs(a-b) <= fmax(rel_tol*fmax(fabs(a),fabs(b)),abs_tol); +} + +int test_solid_angle(char *err) +{ + /* Tests the get_solid_angle() function. */ + double pmt[3] = {0,0,0}; + double pos[3] = {0,0,1}; + double n[3] = {0,0,1}; + double r = 1.0; + double solid_angle; + + solid_angle = get_solid_angle(pos,pmt,n,r); + + if (!isclose(solid_angle, 2*M_PI*(1-1/sqrt(2)), 1e-9, 0)) { + sprintf(err, "solid angle = %.2f, but expected %.2f", solid_angle, 2*M_PI*(1-1/sqrt(2))); + return 1; + } + + return 0; +} + +int main(int argc, char **argv) +{ + char err[256]; + + if (!test_solid_angle(err)) { + printf("[\033[92mok\033[0m] test_solid_angle\n"); + return 0; + } else { + printf("[\033[91mfail\033[0m] test_solid_angle: %s\n", err); + return 1; + } +} -- cgit