From 9ebf0384a53d0dcca149a45b830729dce7685717 Mon Sep 17 00:00:00 2001 From: Avril Date: Thu, 4 Nov 2021 19:44:44 +0000 Subject: [PATCH] C API basic test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for rngxx's current commit: Blessing − 吉 --- .gitignore | 1 + Makefile | 8 ++++---- include/rngxx.h | 2 +- src/rng/crand.cpp | 2 +- src/test/main.c | 22 ++++++++++++++++++++++ 5 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 src/test/main.c diff --git a/.gitignore b/.gitignore index 304249c..14b9e02 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,6 @@ obj/ vgcore.* *-release *-debug +*-test *.so* *.a diff --git a/Makefile b/Makefile index 3403ed9..12fec0d 100644 --- a/Makefile +++ b/Makefile @@ -4,15 +4,15 @@ PROJECT=rngxx AUTHOR=Avril (Flanchan) VERSION_MAJOR=0 -VERSION_MINOR=0 +VERSION_MINOR=1 VERSION=$(VERSION_MAJOR).$(VERSION_MINOR) ifeq ($(PREFIX),) PREFIX := /usr/local endif -SRC_C = $(wildcard src/*.c) -SRC_CXX = $(wildcard src/*.cpp) +SRC_C = $(wildcard src/*.c) $(wildcard src/rng/*.c) +SRC_CXX = $(wildcard src/*.cpp) $(wildcard src/rng/*.cpp) INCLUDE=include INCLUDE_INTERNAL=src/internal @@ -165,5 +165,5 @@ uninstall: -rmdir $(DESTDIR)$(PREFIX)/include/$(PROJECT) $(PROJECT)-test: lib$(PROJECT).so - $(CXX) $(CXXFLAGS) src/test/*.cpp -o $@ -l:$< -lfmt -Wl,-flto -Wl,-O3 $(LDFLAGS) + $(CC) $(CFLAGS) src/test/*.c -o $@ -l:$< -lfmt -Wl,-flto -Wl,-O3 $(LDFLAGS) -valgrind ./$@ diff --git a/include/rngxx.h b/include/rngxx.h index c843fbb..6b51739 100644 --- a/include/rngxx.h +++ b/include/rngxx.h @@ -73,7 +73,7 @@ typedef __typeof( ((struct rng_next_opt*)NULL)->ty ) rng_next_tyq; // Qualified // -- C API functions -- (C++ NO compat) int rng_raw(rng_t* engine, void* restrict output, struct rng_next_opt opt); void rng_next_bin(rng_t* engine, unsigned char* restrict output, size_t n); -//TODO: Specialised `rng_next` functions + void* rng_next_ty(rng_t* engine, void* restrict output, enum rng_next_type ty, enum rng_next_flag flags); void* rng_next_tyb(rng_t* engine, void* restrict output, enum rng_next_type ty, enum rng_next_flag flags, const void* pmin, const void* pmax); diff --git a/src/rng/crand.cpp b/src/rng/crand.cpp index 1f06bba..54825a0 100644 --- a/src/rng/crand.cpp +++ b/src/rng/crand.cpp @@ -8,7 +8,7 @@ #include #include -#include +#include #include "crand.h" // Note: the output of drand48 is uniform between INT32_MIN..=INT32_MAX (inclusive) diff --git a/src/test/main.c b/src/test/main.c new file mode 100644 index 0000000..4184cca --- /dev/null +++ b/src/test/main.c @@ -0,0 +1,22 @@ +#include +#include + +#include + +static int next(rng_t* rng, const int* min, const int* max) +{ + int output; + if(rng_next_tyb(rng, &output, RNG_TY_INT32, RNG_TYQ_SIGNED, min, max)) return output; + + fprintf(stderr, "next() failed\n"); + exit(-1); +} + +#define TREF(x) ( (const __typeof(x)[]){ (x) } ) +int main() +{ + rng_t* engine = rng_new(RNG_KIND_CRAND, (const u64[]){ 1000 }); + printf("%d %d %d\n", next(engine, NULL, TREF(100)), next(engine, TREF(10), TREF(20)), next(engine, NULL, NULL)); + rng_free(engine); + return 0; +}