diff --git a/test/makefile b/test/makefile new file mode 100644 index 0000000..e4139a0 --- /dev/null +++ b/test/makefile @@ -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 diff --git a/test/test b/test/test new file mode 100755 index 0000000..932cb62 Binary files /dev/null and b/test/test differ diff --git a/test/test.c b/test/test.c new file mode 100644 index 0000000..7c5943d --- /dev/null +++ b/test/test.c @@ -0,0 +1,93 @@ +#include "../squiggle.h" +#include +#include +#include +#include + +#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 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 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); +} +