From 95afb7ea1a389d35e8764a25d4ace911acaa41e9 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sun, 23 Jul 2023 14:00:14 +0200 Subject: [PATCH] add tests for normal & beta. --- test/test | Bin 22312 -> 26568 bytes test/test.c | 200 ++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 154 insertions(+), 46 deletions(-) diff --git a/test/test b/test/test index f65f3069c9223a0b49ed614b1a88d3b1ae73a1b0..e3f2968890748fde6f259d81e042eadd5d25640d 100755 GIT binary patch delta 7807 zcmb7J4OmoF`oDAeaGYRffEgGNm@Cc@q9ST2ZlMDVHrf!FXp*9%3y6sykAUH34g04x zN2RFq-(7dJ*4mzb+2hk=;qJQaN2B0IyIJa*QBhDX?KNCQv{p&??(dv?FJ30re|w&1 z?t9+f`*F_uzUQ3#F)zQW>wHhwYKtAPaICpx!s&faWq#_nPLNrZa%P#=frQhpi(b%N z>^e-Zj+J?pYV9H+$}N`VxfLU67ccX@G9{1eVq+9lkD}_SAWtofCqdoeG+Uvp9ZD^9 zdT16M`8i*rI=DF&8R9os{7)nw*#1Dd`>vweb;pao?g?A|LQC7WFl)G98AvZX;Z9iP zjw=LjRdBsemaFdhZ)F4VD7|eGGnTJE1X%YkO^MNhAsK z;ZK{R>?IuM`BAW*mri(^JM`_$K+4GkJSaIQm5@p18~%Ga&SZCU++|bh5Kw0bpAy1b zL-^G({ zEbRxycI+DfFUs;kQ;gf8SG9gRto1Ck22JKN$V}vSv1a2!3~(c zfc-QnHv@bDtQ??C9z7t-l>ki_gDgmK2z#R1bn)YA)717YvDGGjMknG#4roS=*pYND z&m+iRVn}%|HEi6+=(u#?{E-8HZ+PHrxQ2ZE=mW>f1OE*JchG^Aas6!IpD5X-0~ZVr zJa8RJ*cI_S02=9V44v5UzymTK8sRIDs-?U54-EJImUvu`Z86h9nWK!O;vGt;_FvGz z*U*ugJ8aW}%PQKdvrKk5T9e&HHpV3^`8ovywnY0G$G1u#@?D*FZ*-saH5w9;WVZEwSvo|c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3ZVF?WjoA`yMJUGK4;HqsI+nFHCL4_6LQOzp zywaE)rEAEA?8C-uBz1_VohJt3}0qQI>W;Lc|$CFrO9W}f$1E$ k*yM}Sft^t-^Y*Kwlyc3|ZEW@J*H{-W(ppI-wx&Mie?USi2><{9 diff --git a/test/test.c b/test/test.c index c4d571f..aa0b4ad 100644 --- a/test/test.c +++ b/test/test.c @@ -4,43 +4,69 @@ #include #include -#define N 1000 * 1000 -#define PERCENTAGE_TOLERANCE 1.0/1000.0 +#define N 1000 * 1000 +#define PERCENTAGE_TOLERANCE_UNIFORM 1.0/1000.0 +#define PERCENTAGE_TOLERANCE_NORMAL 5.0/1000.0 +#define MAX_NAME_LENGTH 500 -void test_unit_uniform(uint64_t* seed){ - double* unit_uniform_array = malloc(sizeof(double) * N); - - for(int i=0; i PERCENTAGE_TOLERANCE){ - printf("[-] Mean test for unit uniform NOT passed.\n"); +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; + + + if(fabs(delta_mean) > e.tolerance){ + printf("[-] Mean test for %s NOT passed.\n", e.name); + printf("Mean of %s: %f, vs expected mean: %f, delta: %f\n", e.name, mean, e.expected_mean, delta_mean); }else { - printf("[x] Mean test for unit uniform PASSED\n"); + printf("[x] Mean test for %s PASSED\n", e.name); } - if(fabs(delta_std) > PERCENTAGE_TOLERANCE){ - printf("[-] Std test for unit uniform NOT passed.\n"); + if(fabs(delta_std) > e.tolerance){ + printf("[-] Std test for %s NOT passed.\n", e.name); + printf("Std of %s: %f, vs expected std: %f, delta: %f\n", e.name, std, e.expected_std, delta_std); }else { - printf("[x] Std test for unit uniform PASSED\n"); + printf("[x] Std test for %s PASSED\n", e.name); } printf("\n"); } +// Test unit uniform +void test_unit_uniform(uint64_t* seed){ + double* unit_uniform_array = malloc(sizeof(double) * N); + + for(int i=0; i width * PERCENTAGE_TOLERANCE){ - printf("[-] Mean test for [%.1f, %.1f] uniform NOT passed.\n", start, end); - printf("Mean of [%.1f, %.1f] uniform: %f, vs expected mean: %f, delta: %f\n", start, end, mean, expected_mean, mean - expected_mean); - }else { - printf("[x] Mean test for [%.1f, %.1f] uniform PASSED\n", start, end); + for(int i=0; i width * PERCENTAGE_TOLERANCE){ - printf("[-] Std test for [%.1f, %.1f] uniform NOT passed.\n", start, end); - printf("Std of [%.1f, %.1f] uniform: %f, vs expected std: %f, delta: %f\n", start, end, std, expected_std, std - expected_std); - for(int i=0; i<10; i++){ + struct array_expectations expectations = { + .array = unit_normal_array, + .n = N, + .name = "unit normal", + .expected_mean = 0, + .expected_std = 1, + .tolerance = 1 * PERCENTAGE_TOLERANCE_NORMAL, + }; - printf("%.1f, ", uniform_array[i]); - } - }else { - printf("[x] Std test for [%.1f, %.1f] uniform PASSED\n", start, end); + test_array_expectations(expectations); +} + +// Test normal +void test_normal(double mean, double std, uint64_t* seed){ + double* normal_array = malloc(sizeof(double) * N); + + for(int i=0; i 0){ + test_normal(mean, std, 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 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); + } + } + free(seed); }