diff --git a/test/test.c b/test/test.c index 67de207..70ea1f9 100644 --- a/test/test.c +++ b/test/test.c @@ -1,280 +1,284 @@ #include "../squiggle.h" -#include #include -#include +#include #include +#include -#define PERCENTAGE_TOLERANCE 1.0/1000.0 -#define PERCENTAGE_TOLERANCE_LOGNORMAL 5.0/1000.0 +#define PERCENTAGE_TOLERANCE 1.0 / 1000.0 +#define PERCENTAGE_TOLERANCE_LOGNORMAL 5.0 / 1000.0 #define MAX_NAME_LENGTH 500 // Structs struct array_expectations { - double* array; - int n; - char* name; - double expected_mean; - double expected_std; - double tolerance; + double* array; + int n; + char* name; + double expected_mean; + double expected_std; + double tolerance; }; -void test_array_expectations(struct array_expectations e){ - double mean = array_mean(e.array, e.n); - double delta_mean = mean - e.expected_mean; +void test_array_expectations(struct array_expectations e) +{ + double mean = array_mean(e.array, e.n); + double delta_mean = mean - e.expected_mean; - double std = array_std(e.array, e.n); - double delta_std = std - e.expected_std; - + double std = array_std(e.array, e.n); + double delta_std = std - e.expected_std; - if(fabs(delta_mean)/fabs(mean) > e.tolerance){ - printf("[-] Mean test for %s NOT passed.\n", e.name); - printf("Mean of %s: %f, vs expected mean: %f\n", e.name, mean, e.expected_mean); - printf("delta: %f, relative delta: %f\n", delta_mean, delta_mean/fabs(mean)); - }else { - printf("[x] Mean test for %s PASSED\n", e.name); - } + if (fabs(delta_mean) / fabs(mean) > e.tolerance) { + printf("[-] Mean test for %s NOT passed.\n", e.name); + printf("Mean of %s: %f, vs expected mean: %f\n", e.name, mean, e.expected_mean); + printf("delta: %f, relative delta: %f\n", delta_mean, delta_mean / fabs(mean)); + } else { + printf("[x] Mean test for %s PASSED\n", e.name); + } - if(fabs(delta_std)/fabs(std) > e.tolerance){ - printf("[-] Std test for %s NOT passed.\n", e.name); - printf("Std of %s: %f, vs expected std: %f\n", e.name, std, e.expected_std); - printf("delta: %f, relative delta: %f\n", delta_std, delta_std/fabs(std)); - }else { - printf("[x] Std test for %s PASSED\n", e.name); - } - - printf("\n"); + if (fabs(delta_std) / fabs(std) > e.tolerance) { + printf("[-] Std test for %s NOT passed.\n", e.name); + printf("Std of %s: %f, vs expected std: %f\n", e.name, std, e.expected_std); + printf("delta: %f, relative delta: %f\n", delta_std, delta_std / fabs(std)); + } else { + printf("[x] Std test for %s PASSED\n", e.name); + } + printf("\n"); } // Test unit uniform -void test_unit_uniform(uint64_t* seed){ - int n = 1000 * 1000; - double* unit_uniform_array = malloc(sizeof(double) * n); - - for(int i=0; i start){ - test_uniform(start, end, seed); - } - } + printf("Testing unit uniform\n"); + test_unit_uniform(seed); - printf("Testing wide uniforms\n"); - for(int i=0; i<100; i++){ - double start = sample_uniform(-1000 * 1000, 1000 * 1000, seed); - double end = sample_uniform(-1000 * 1000, 1000 * 1000, seed); - if ( end > start){ - test_uniform(start, end, seed); - } - } + printf("Testing small uniforms\n"); + for (int i = 0; i < 100; i++) { + double start = sample_uniform(-10, 10, seed); + double end = sample_uniform(-10, 10, seed); + if (end > start) { + test_uniform(start, end, seed); + } + } - printf("Testing unit normal\n"); - test_unit_normal(seed); - - printf("Testing small normals\n"); - for(int i=0; i<100; i++){ - double mean = sample_uniform(-10, 10, seed); - double std = sample_uniform(0, 10, seed); - if ( std > 0){ - test_normal(mean, std, seed); - } - } + printf("Testing wide uniforms\n"); + for (int i = 0; i < 100; i++) { + double start = sample_uniform(-1000 * 1000, 1000 * 1000, seed); + double end = sample_uniform(-1000 * 1000, 1000 * 1000, seed); + if (end > start) { + test_uniform(start, end, seed); + } + } - printf("Testing larger normals\n"); - for(int i=0; i<100; i++){ - double mean = sample_uniform(-1000 * 1000, 1000 * 1000, seed); - double std = sample_uniform(0, 1000 * 1000, seed); - if ( std > 0){ - test_normal(mean, std, seed); - } - } - */ - printf("Testing very small lognormals\n"); - for(int i=0; i<10; i++){ - double mean = sample_uniform(-1, 1, seed); - double std = sample_uniform(0, 1, seed); - if ( std > 0){ - test_lognormal(mean, std, seed); - } - } + printf("Testing unit normal\n"); + test_unit_normal(seed); - printf("Testing small lognormals\n"); - for(int i=0; i<10; i++){ - double mean = sample_uniform(-1, 5, seed); - double std = sample_uniform(0, 5, seed); - if ( std > 0){ - test_lognormal(mean, std, seed); - } - } - /* - printf("Testing beta distribution\n"); - for(int i=0; i<100; i++){ - double a = sample_uniform(0, 1000, seed); - double b = sample_uniform(0, 1000, seed); - if ( (a > 0) && ( b >0)){ - test_beta(a, b, seed); - } - } + printf("Testing small normals\n"); + for (int i = 0; i < 100; i++) { + double mean = sample_uniform(-10, 10, seed); + double std = sample_uniform(0, 10, seed); + if (std > 0) { + test_normal(mean, std, seed); + } + } - printf("Testing larger beta distributions\n"); - for(int i=0; i<100; i++){ - double a = sample_uniform(0, 1000 * 1000, seed); - double b = sample_uniform(0, 1000 * 1000, seed); - if ( (a > 0) && ( b >0)){ - test_beta(a, b, seed); - } - } + printf("Testing larger normals\n"); + for (int i = 0; i < 100; i++) { + double mean = sample_uniform(-1000 * 1000, 1000 * 1000, seed); + double std = sample_uniform(0, 1000 * 1000, seed); + if (std > 0) { + test_normal(mean, std, seed); + } + } - free(seed); - */ + printf("Testing very small lognormals\n"); + for (int i = 0; i < 10; i++) { + double mean = sample_uniform(-1, 1, seed); + double std = sample_uniform(0, 1, seed); + if (std > 0) { + test_lognormal(mean, std, seed); + } + } + + printf("Testing small lognormals\n"); + for (int i = 0; i < 10; i++) { + double mean = sample_uniform(-1, 5, seed); + double std = sample_uniform(0, 5, seed); + if (std > 0) { + test_lognormal(mean, std, seed); + } + } + + printf("Testing beta distribution\n"); + for (int i = 0; i < 100; i++) { + double a = sample_uniform(0, 1000, seed); + double b = sample_uniform(0, 1000, seed); + if ((a > 0) && (b > 0)) { + test_beta(a, b, seed); + } + } + + printf("Testing larger beta distributions\n"); + for (int i = 0; i < 100; i++) { + double a = sample_uniform(0, 1000 * 1000, seed); + double b = sample_uniform(0, 1000 * 1000, seed); + if ((a > 0) && (b > 0)) { + test_beta(a, b, seed); + } + } + + free(seed); } -