forked from personal/squiggle.c
start adding some testing
This commit is contained in:
parent
91e237464e
commit
98e058bd88
53
test/makefile
Normal file
53
test/makefile
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
# Interface:
|
||||||
|
# make
|
||||||
|
# make build
|
||||||
|
# make format
|
||||||
|
# make run
|
||||||
|
|
||||||
|
# Compiler
|
||||||
|
CC=gcc
|
||||||
|
# CC=tcc # <= faster compilation
|
||||||
|
|
||||||
|
# Main file
|
||||||
|
SRC=test.c ../squiggle.c
|
||||||
|
OUTPUT=test
|
||||||
|
|
||||||
|
## Dependencies
|
||||||
|
MATH=-lm
|
||||||
|
|
||||||
|
## Flags
|
||||||
|
DEBUG= #'-g'
|
||||||
|
STANDARD=-std=c99
|
||||||
|
WARNINGS=-Wall
|
||||||
|
OPTIMIZED=-O3 #-Ofast
|
||||||
|
# OPENMP=-fopenmp
|
||||||
|
|
||||||
|
## Formatter
|
||||||
|
STYLE_BLUEPRINT=webkit
|
||||||
|
FORMATTER=clang-format -i -style=$(STYLE_BLUEPRINT)
|
||||||
|
|
||||||
|
## make build
|
||||||
|
build: $(SRC)
|
||||||
|
$(CC) $(OPTIMIZED) $(DEBUG) $(SRC) $(MATH) -o $(OUTPUT)
|
||||||
|
|
||||||
|
format: $(SRC)
|
||||||
|
$(FORMATTER) $(SRC)
|
||||||
|
|
||||||
|
run: $(SRC) $(OUTPUT)
|
||||||
|
./$(OUTPUT)
|
||||||
|
|
||||||
|
time-linux:
|
||||||
|
@echo "Requires /bin/time, found on GNU/Linux systems" && echo
|
||||||
|
|
||||||
|
@echo "Running 100x and taking avg time $(OUTPUT)"
|
||||||
|
@t=$$(/usr/bin/time -f "%e" -p bash -c 'for i in {1..100}; do $(OUTPUT); done' 2>&1 >/dev/null | grep real | awk '{print $$2}' ); echo "scale=2; 1000 * $$t / 100" | bc | sed "s|^|Time using 1 thread: |" | sed 's|$$|ms|' && echo
|
||||||
|
|
||||||
|
## Profiling
|
||||||
|
|
||||||
|
profile-linux:
|
||||||
|
echo "Requires perf, which depends on the kernel version, and might be in linux-tools package or similar"
|
||||||
|
echo "Must be run as sudo"
|
||||||
|
$(CC) $(SRC) $(MATH) -o $(OUTPUT)
|
||||||
|
sudo perf record ./$(OUTPUT)
|
||||||
|
sudo perf report
|
||||||
|
rm perf.data
|
93
test/test.c
Normal file
93
test/test.c
Normal file
|
@ -0,0 +1,93 @@
|
||||||
|
#include "../squiggle.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define N 10 * 1000 * 1000
|
||||||
|
|
||||||
|
void test_unit_uniform(uint32_t* seed){
|
||||||
|
float* unit_uniform_array = malloc(sizeof(float) * N);
|
||||||
|
|
||||||
|
for(int i=0; i<N; i++){
|
||||||
|
unit_uniform_array[i] = sample_unit_uniform(seed);
|
||||||
|
}
|
||||||
|
|
||||||
|
float mean = array_mean(unit_uniform_array, N);
|
||||||
|
float expected_mean = 0.5;
|
||||||
|
float delta_mean = mean - expected_mean;
|
||||||
|
|
||||||
|
float std = array_std(unit_uniform_array, N);
|
||||||
|
float expected_std = sqrt(1.0/12.0);
|
||||||
|
float delta_std = std - expected_std;
|
||||||
|
|
||||||
|
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){
|
||||||
|
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){
|
||||||
|
printf("[-] Std test for unit uniform NOT passed.\n");
|
||||||
|
}else {
|
||||||
|
printf("[x] Std test for unit uniform PASSED\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_uniform(float start, float end, uint32_t* seed){
|
||||||
|
float* uniform_array = malloc(sizeof(float) * N);
|
||||||
|
|
||||||
|
for(int i=0; i<N; i++){
|
||||||
|
uniform_array[i] = sample_uniform(start, end, seed);
|
||||||
|
}
|
||||||
|
|
||||||
|
float mean = array_mean(uniform_array, N);
|
||||||
|
float expected_mean = (start + end) / 2;
|
||||||
|
float delta_mean = mean - expected_mean;
|
||||||
|
|
||||||
|
float std = array_std(uniform_array, N);
|
||||||
|
float expected_std = sqrt(1.0/12.0) * fabs(end-start);
|
||||||
|
float delta_std = std - expected_std;
|
||||||
|
|
||||||
|
|
||||||
|
float width = fabs(end - start);
|
||||||
|
if(fabs(delta_mean) > width * 1.0/1000.0){
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(fabs(delta_std) > width * 1.0/1000.0){
|
||||||
|
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);
|
||||||
|
}else {
|
||||||
|
printf("[x] Std test for unit uniform PASSED\n");
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
// set randomness seed
|
||||||
|
uint32_t* seed = malloc(sizeof(uint32_t));
|
||||||
|
*seed = 1000; // xorshift can't start with a seed of 0
|
||||||
|
|
||||||
|
test_unit_uniform(seed);
|
||||||
|
|
||||||
|
for(int i=0; i<100; i++){
|
||||||
|
float start = sample_uniform(-10, 10, seed);
|
||||||
|
float end = sample_uniform(-10, 10, seed);
|
||||||
|
if ( end > start){
|
||||||
|
test_uniform(start, end, seed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(seed);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user