aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEmil Velikov2015-03-22 16:38:08 -0500
committerEmil Velikov2015-04-05 09:33:33 -0500
commitf1f4cabd8e8602355630b2b4b5715fc137b4dc71 (patch)
tree729cbd1bde66975707d7e4fc663a8f0215df0c4c /xf86drmRandom.c
parent7e4f0664acfa79448f6e32c1f1b12b94626777b2 (diff)
downloadexternal-libdrm-f1f4cabd8e8602355630b2b4b5715fc137b4dc71.tar.gz
external-libdrm-f1f4cabd8e8602355630b2b4b5715fc137b4dc71.tar.xz
external-libdrm-f1f4cabd8e8602355630b2b4b5715fc137b4dc71.zip
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 @@
74#include <stdio.h> 74#include <stdio.h>
75#include <stdlib.h> 75#include <stdlib.h>
76 76
77#define RANDOM_MAIN 0 77#include "xf86drm.h"
78 78#include "xf86drmRandom.h"
79#if !RANDOM_MAIN
80# include "xf86drm.h"
81#endif
82 79
83#define RANDOM_MAGIC 0xfeedbeef 80#define RANDOM_MAGIC 0xfeedbeef
84#define RANDOM_DEBUG 0 81#define RANDOM_DEBUG 0
85 82
86#if RANDOM_MAIN
87#define RANDOM_ALLOC malloc
88#define RANDOM_FREE free
89#else
90#define RANDOM_ALLOC drmMalloc
91#define RANDOM_FREE drmFree
92#endif
93
94typedef struct RandomState {
95 unsigned long magic;
96 unsigned long a;
97 unsigned long m;
98 unsigned long q; /* m div a */
99 unsigned long r; /* m mod a */
100 unsigned long check;
101 unsigned long seed;
102} RandomState;
103
104#if RANDOM_MAIN
105extern void *drmRandomCreate(unsigned long seed);
106extern int drmRandomDestroy(void *state);
107extern unsigned long drmRandom(void *state);
108extern double drmRandomDouble(void *state);
109#endif
110
111void *drmRandomCreate(unsigned long seed) 83void *drmRandomCreate(unsigned long seed)
112{ 84{
113 RandomState *state; 85 RandomState *state;
114 86
115 state = RANDOM_ALLOC(sizeof(*state)); 87 state = drmMalloc(sizeof(*state));
116 if (!state) return NULL; 88 if (!state) return NULL;
117 state->magic = RANDOM_MAGIC; 89 state->magic = RANDOM_MAGIC;
118#if 0 90#if 0
@@ -140,7 +112,7 @@ void *drmRandomCreate(unsigned long seed)
140 112
141int drmRandomDestroy(void *state) 113int drmRandomDestroy(void *state)
142{ 114{
143 RANDOM_FREE(state); 115 drmFree(state);
144 return 0; 116 return 0;
145} 117}
146 118
@@ -164,45 +136,3 @@ double drmRandomDouble(void *state)
164 136
165 return (double)drmRandom(state)/(double)s->m; 137 return (double)drmRandom(state)/(double)s->m;
166} 138}
167
168#if RANDOM_MAIN
169static void check_period(unsigned long seed)
170{
171 unsigned long count = 0;
172 unsigned long initial;
173 void *state;
174
175 state = drmRandomCreate(seed);
176 initial = drmRandom(state);
177 ++count;
178 while (initial != drmRandom(state)) {
179 if (!++count) break;
180 }
181 printf("With seed of %10lu, period = %10lu (0x%08lx)\n",
182 seed, count, count);
183 drmRandomDestroy(state);
184}
185
186int main(void)
187{
188 RandomState *state;
189 int i;
190 unsigned long rand;
191
192 state = drmRandomCreate(1);
193 for (i = 0; i < 10000; i++) {
194 rand = drmRandom(state);
195 }
196 printf("After 10000 iterations: %lu (%lu expected): %s\n",
197 rand, state->check,
198 rand - state->check ? "*INCORRECT*" : "CORRECT");
199 drmRandomDestroy(state);
200
201 printf("Checking periods...\n");
202 check_period(1);
203 check_period(2);
204 check_period(31415926);
205
206 return 0;
207}
208#endif