forked from personal/squiggle.c
fix floats.h bug, fix std bug, add tests for std.
This commit is contained in:
parent
6e228dcc6b
commit
f65699a688
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -1,4 +1,4 @@
|
|||
#include <double.h>
|
||||
#include <float.h>
|
||||
#include <limits.h>
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
|
@ -161,8 +161,7 @@ double array_std(double* array, int length)
|
|||
double mean = array_mean(array, length);
|
||||
double std = 0.0;
|
||||
for (int i = 0; i < length; i++) {
|
||||
std += (array[i] - mean);
|
||||
std *= std;
|
||||
std += (array[i] - mean) * (array[i] - mean);
|
||||
}
|
||||
std = sqrt(std / length);
|
||||
return std;
|
||||
|
|
|
@ -36,6 +36,9 @@ format: $(SRC)
|
|||
run: $(SRC) $(OUTPUT)
|
||||
./$(OUTPUT)
|
||||
|
||||
verify: $(SRC) $(OUTPUT)
|
||||
./$(OUTPUT) | grep "NOT passed" || true
|
||||
|
||||
time-linux:
|
||||
@echo "Requires /bin/time, found on GNU/Linux systems" && echo
|
||||
|
||||
|
|
31
test/test.c
31
test/test.c
|
@ -4,7 +4,8 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define N 100
|
||||
#define N 1000 * 1000
|
||||
#define PERCENTAGE_TOLERANCE 1.0/1000.0
|
||||
|
||||
void test_unit_uniform(uint64_t* seed){
|
||||
double* unit_uniform_array = malloc(sizeof(double) * N);
|
||||
|
@ -24,13 +25,13 @@ void test_unit_uniform(uint64_t* seed){
|
|||
printf("Mean of unit uniform: %f, vs expected mean: %f, delta: %f\n", mean, expected_mean, delta_mean);
|
||||
printf("Std of unit uniform: %f, vs expected std: %f, delta: %f\n", std, expected_std, delta_std);
|
||||
|
||||
if(fabs(delta_mean) > 1.0/1000.0){
|
||||
if(fabs(delta_mean) > PERCENTAGE_TOLERANCE){
|
||||
printf("[-] Mean test for unit uniform NOT passed.\n");
|
||||
}else {
|
||||
printf("[x] Mean test for unit uniform PASSED\n");
|
||||
}
|
||||
|
||||
if(fabs(delta_std) > 1.0/1000.0){
|
||||
if(fabs(delta_std) > PERCENTAGE_TOLERANCE){
|
||||
printf("[-] Std test for unit uniform NOT passed.\n");
|
||||
}else {
|
||||
printf("[x] Std test for unit uniform PASSED\n");
|
||||
|
@ -56,22 +57,22 @@ void test_uniform(double start, double end, uint64_t* seed){
|
|||
double delta_std = std - expected_std;
|
||||
|
||||
double width = fabs(end - start);
|
||||
if(fabs(delta_mean) > width * 1.0/1000.0){
|
||||
if(fabs(delta_mean) > 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 unit uniform PASSED\n");
|
||||
printf("[x] Mean test for [%.1f, %.1f] uniform PASSED\n", start, end);
|
||||
}
|
||||
|
||||
if(fabs(delta_std) > width * 1.0/1000.0){
|
||||
if(fabs(delta_std) > 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<N; i++){
|
||||
for(int i=0; i<10; i++){
|
||||
|
||||
printf("%.1f, ", uniform_array[i]);
|
||||
}
|
||||
}else {
|
||||
printf("[x] Std test for unit uniform PASSED\n");
|
||||
printf("[x] Std test for [%.1f, %.1f] uniform PASSED\n", start, end);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
|
@ -82,15 +83,27 @@ int main(){
|
|||
uint64_t* seed = malloc(sizeof(uint64_t));
|
||||
*seed = 1000; // xorshift can't start with a seed of 0
|
||||
|
||||
printf("Testing unit uniform\n");
|
||||
test_unit_uniform(seed);
|
||||
|
||||
for(int i=0; i<1; i++){
|
||||
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 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);
|
||||
}
|
||||
}
|
||||
|
||||
free(seed);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user