From: fluffymormegil Date: Tue, 12 Oct 2010 23:17:59 +0000 (+0100) Subject: Added noddy test for dice functions X-Git-Tag: v1.0.0~22^2~4 X-Git-Url: http://git.blackswordsonics.com/?a=commitdiff_plain;h=bece1b222982a78edc9924e682d6c5966184d620;p=libmormegil Added noddy test for dice functions --- diff --git a/examples/dice-test.cc b/examples/dice-test.cc new file mode 100644 index 0000000..08c5527 --- /dev/null +++ b/examples/dice-test.cc @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#include + +uint32_t keybuf[8]; +uint32_t nonce[2]; + +int main() +{ + int fd; + fd = open("/dev/urandom", O_RDONLY, 0); + if (fd < 0) + { + perror("S20prng-test: can't open /dev/urandom"); + return 1; + } + + int i = 0; + int testroll; + uint64_t counter = 0; + int rolls[11]; + + read(fd, keybuf, sizeof keybuf); + read(fd, nonce, sizeof nonce); + dice_setstate(keybuf, nonce, &counter, &i); + for (i = 0; i < 11; ++i) + { + rolls[i] = 0; + } + for (i = 0; i < 1000000; ++i) + { + testroll = dice(2, 6); + if ((testroll < 2) || (testroll > 12)) + { + fprintf(stderr, "S20prng-test: FAIL: 2d6 roll came up %d\n", testroll); + return 2; + } + rolls[testroll - 2]++; + } + for (i = 0; i < 11; ++i) + { + printf("%d: %d times\n", i + 2, rolls[i]); + } + return 0; +} +