aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortlatorre <tlatorre@uchicago.edu>2018-07-04 12:22:21 -0400
committertlatorre <tlatorre@uchicago.edu>2018-07-04 12:22:21 -0400
commit9646e43e6016536911f0703dc7e3af7a0eae56db (patch)
tree99c744bb1e589a68b66b8fd54860b0c189aef677
parent6b8e727b10ff7a1ae219987fd4ff40f359a0c655 (diff)
downloadsddm-9646e43e6016536911f0703dc7e3af7a0eae56db.tar.gz
sddm-9646e43e6016536911f0703dc7e3af7a0eae56db.tar.bz2
sddm-9646e43e6016536911f0703dc7e3af7a0eae56db.zip
add a test of the get_solid_angle function
-rw-r--r--.gitignore1
-rw-r--r--Makefile2
-rw-r--r--solid_angle.c2
-rw-r--r--test.c44
4 files changed, 48 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore
index 9e63049..7d3fd46 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
*.swp
calculate_limits
*.o
+test
diff --git a/Makefile b/Makefile
index 6148848..98f700d 100644
--- a/Makefile
+++ b/Makefile
@@ -5,6 +5,8 @@ calculate_limits: calculate_limits.c
solid_angle.o: solid_angle.c
+test: test.c solid_angle.c
+
clean:
rm calculate_limits
diff --git a/solid_angle.c b/solid_angle.c
index 5fc6033..cbc71f1 100644
--- a/solid_angle.c
+++ b/solid_angle.c
@@ -15,7 +15,7 @@ double get_solid_angle(double *pos, double *pmt, double *n, double r)
dir[1] = pos[1] - pmt[1];
dir[2] = pos[2] - pmt[2];
- L = dir[0]*n[0] + dir[1]*n[1] + dir[2]*n[2];
+ L = fabs(dir[0]*n[0] + dir[1]*n[1] + dir[2]*n[2]);
R = sqrt(dir[0]*dir[0] + dir[1]*dir[1] + dir[2]*dir[2]);
r0 = sqrt(R*R - L*L);
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 <math.h>
+#include <stdio.h>
+
+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;
+ }
+}