summaryrefslogtreecommitdiff
path: root/xf86drmRandom.c
diff options
context:
space:
mode:
authorEmil Velikov <emil.l.velikov@gmail.com>2015-03-22 21:38:08 +0000
committerEmil Velikov <emil.l.velikov@gmail.com>2015-04-05 15:33:33 +0100
commitf1f4cabd8e8602355630b2b4b5715fc137b4dc71 (patch)
tree729cbd1bde66975707d7e4fc663a8f0215df0c4c /xf86drmRandom.c
parent7e4f0664acfa79448f6e32c1f1b12b94626777b2 (diff)
tests/random: extract test out of xf86drmRandom.c
With follow up commits we can clear it up and wire to make check v2: - Use xf86drmRandom.h for common struct.(Jan) - Add test to .gitignore. Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com> Reviewed-by: Jan Vesely <jan.vesely@rutgers.edu>
Diffstat (limited to 'xf86drmRandom.c')
-rw-r--r--xf86drmRandom.c78
1 files changed, 4 insertions, 74 deletions
diff --git a/xf86drmRandom.c b/xf86drmRandom.c
index 94922ad7..b3a4be94 100644
--- a/xf86drmRandom.c
+++ b/xf86drmRandom.c
@@ -74,45 +74,17 @@
#include <stdio.h>
#include <stdlib.h>
-#define RANDOM_MAIN 0
-
-#if !RANDOM_MAIN
-# include "xf86drm.h"
-#endif
+#include "xf86drm.h"
+#include "xf86drmRandom.h"
#define RANDOM_MAGIC 0xfeedbeef
#define RANDOM_DEBUG 0
-#if RANDOM_MAIN
-#define RANDOM_ALLOC malloc
-#define RANDOM_FREE free
-#else
-#define RANDOM_ALLOC drmMalloc
-#define RANDOM_FREE drmFree
-#endif
-
-typedef struct RandomState {
- unsigned long magic;
- unsigned long a;
- unsigned long m;
- unsigned long q; /* m div a */
- unsigned long r; /* m mod a */
- unsigned long check;
- unsigned long seed;
-} RandomState;
-
-#if RANDOM_MAIN
-extern void *drmRandomCreate(unsigned long seed);
-extern int drmRandomDestroy(void *state);
-extern unsigned long drmRandom(void *state);
-extern double drmRandomDouble(void *state);
-#endif
-
void *drmRandomCreate(unsigned long seed)
{
RandomState *state;
- state = RANDOM_ALLOC(sizeof(*state));
+ state = drmMalloc(sizeof(*state));
if (!state) return NULL;
state->magic = RANDOM_MAGIC;
#if 0
@@ -140,7 +112,7 @@ void *drmRandomCreate(unsigned long seed)
int drmRandomDestroy(void *state)
{
- RANDOM_FREE(state);
+ drmFree(state);
return 0;
}
@@ -164,45 +136,3 @@ double drmRandomDouble(void *state)
return (double)drmRandom(state)/(double)s->m;
}
-
-#if RANDOM_MAIN
-static void check_period(unsigned long seed)
-{
- unsigned long count = 0;
- unsigned long initial;
- void *state;
-
- state = drmRandomCreate(seed);
- initial = drmRandom(state);
- ++count;
- while (initial != drmRandom(state)) {
- if (!++count) break;
- }
- printf("With seed of %10lu, period = %10lu (0x%08lx)\n",
- seed, count, count);
- drmRandomDestroy(state);
-}
-
-int main(void)
-{
- RandomState *state;
- int i;
- unsigned long rand;
-
- state = drmRandomCreate(1);
- for (i = 0; i < 10000; i++) {
- rand = drmRandom(state);
- }
- printf("After 10000 iterations: %lu (%lu expected): %s\n",
- rand, state->check,
- rand - state->check ? "*INCORRECT*" : "CORRECT");
- drmRandomDestroy(state);
-
- printf("Checking periods...\n");
- check_period(1);
- check_period(2);
- check_period(31415926);
-
- return 0;
-}
-#endif