diff options
Diffstat (limited to 'src/test.c')
-rw-r--r-- | src/test.c | 31 |
1 files changed, 31 insertions, 0 deletions
@@ -879,6 +879,36 @@ err: return 1; } +int test_trapz(char *err) +{ + /* Tests the trapz() function. */ + size_t i; + double y[100], integral, expected; + + init_genrand(0); + + for (i = 0; i < LEN(y); i++) { + y[i] = genrand_real2(); + } + + expected = 0.0; + for (i = 1; i < LEN(y); i++) { + expected += (y[i-1] + y[i])/2; + } + + integral = trapz(y, 1.0, LEN(y)); + + if (!isclose(integral, expected, 0, 1e-9)) { + sprintf(err, "trapz() returned %.5g, but expected %.5g", integral, expected); + goto err; + } + + return 0; + +err: + return 1; +} + struct tests { testFunction *test; char *name; @@ -906,6 +936,7 @@ struct tests { {test_proton_get_range, "test_proton_get_range"}, {test_proton_get_energy, "test_proton_get_energy"}, {test_log_norm, "test_log_norm"}, + {test_trapz, "test_trapz"}, }; int main(int argc, char **argv) |