Update contrib.texi
[glibc.git] / stdlib / tst-rand48.c
blob52e1b96afe2f1b501b4d4330da969d1ce4f7f109
1 #include <float.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
6 #ifndef DECIMAL_DIG
7 # define DECIMAL_DIG 21
8 #endif
11 int
12 main (void)
14 unsigned short int xs[3] = { 0x0001, 0x0012, 0x0123 };
15 unsigned short int lxs[7];
16 unsigned short int *xsp;
17 int result = 0;
18 long int l;
19 double d;
20 double e;
22 /* Test srand48. */
23 srand48 (0x98765432);
24 /* Get the values of the internal Xi array. */
25 xsp = seed48 (xs);
26 if (xsp[0] != 0x330e || xsp[1] != 0x5432 || xsp[2] != 0x9876)
28 puts ("srand48(0x98765432) didn't set correct value");
29 printf (" expected: { %04hx, %04hx, %04hx }\n", 0x330e, 0x5432, 0x9876);
30 printf (" seen: { %04hx, %04hx, %04hx }\n", xsp[0], xsp[1], xsp[2]);
31 result = 1;
33 /* Put the values back. */
34 memcpy (xs, xsp, sizeof (xs));
35 (void) seed48 (xs);
37 /* See whether the correct values are installed. */
38 l = lrand48 ();
39 if (l != 0x2fed1413l)
41 printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
42 __LINE__ - 4, 0x2fed1413l, l);
43 result = 1;
46 l = mrand48 ();
47 if (l != -0x5d73effdl)
49 printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
50 __LINE__ - 4, -0x5d73effdl, l);
51 result = 1;
54 l = lrand48 ();
55 if (l != 0x585fcfb7l)
57 printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
58 __LINE__ - 4, 0x585fcfb7l, l);
59 result = 1;
62 l = mrand48 ();
63 if (l != -0x61770b8cl)
65 printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
66 __LINE__ - 4, -0x61770b8cl, l);
67 result = 1;
70 /* Test seed48. The previous call should have install the values in
71 the initialization of `xs' above. */
72 xs[0] = 0x1234;
73 xs[1] = 0x5678;
74 xs[2] = 0x9012;
75 xsp = seed48 (xs);
76 if (xsp[0] != 0x62f2 || xsp[1] != 0xf474 || xsp[2] != 0x9e88)
78 puts ("seed48() did not install the values correctly");
79 printf (" expected: { %04hx, %04hx, %04hx }\n", 0x62f2, 0xf474, 0x9e88);
80 printf (" seen: { %04hx, %04hx, %04hx }\n", xsp[0], xsp[1], xsp[2]);
81 result = 1;
84 /* Test lrand48 and mrand48. We continue from the seed established
85 above. */
86 l = lrand48 ();
87 if (l != 0x017e48b5l)
89 printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
90 __LINE__ - 4, 0x017e48b5l, l);
91 result = 1;
94 l = mrand48 ();
95 if (l != -0x1485e05dl)
97 printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
98 __LINE__ - 4, -0x1485e05dl, l);
99 result = 1;
102 l = lrand48 ();
103 if (l != 0x6b6a3f95l)
105 printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
106 __LINE__ - 4, 0x6b6a3f95l, l);
107 result = 1;
110 l = mrand48 ();
111 if (l != 0x175c0d6fl)
113 printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
114 __LINE__ - 4, 0x175c0d6fl, l);
115 result = 1;
118 /* Test lcong48. */
119 lxs[0] = 0x4567;
120 lxs[1] = 0x6789;
121 lxs[2] = 0x8901;
122 lxs[3] = 0x0123;
123 lxs[4] = 0x2345;
124 lxs[5] = 0x1111;
125 lxs[6] = 0x2222;
126 lcong48 (lxs);
128 /* See whether the correct values are installed. */
129 l = lrand48 ();
130 if (l != 0x6df63d66l)
132 printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
133 __LINE__ - 4, 0x6df63d66l, l);
134 result = 1;
137 l = mrand48 ();
138 if (l != 0x2f92c8e1l)
140 printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
141 __LINE__ - 4, 0x2f92c8e1l, l);
142 result = 1;
145 l = lrand48 ();
146 if (l != 0x3b4869ffl)
148 printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
149 __LINE__ - 4, 0x3b4869ffl, l);
150 result = 1;
153 l = mrand48 ();
154 if (l != 0x5cd4cc3el)
156 printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
157 __LINE__ - 4, 0x5cd4cc3el, l);
158 result = 1;
161 /* Check whether srand48() restores the A and C parameters. */
162 srand48 (0x98765432);
164 /* See whether the correct values are installed. */
165 l = lrand48 ();
166 if (l != 0x2fed1413l)
168 printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
169 __LINE__ - 4, 0x2fed1413l, l);
170 result = 1;
173 l = mrand48 ();
174 if (l != -0x5d73effdl)
176 printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
177 __LINE__ - 4, -0x5d73effdl, l);
178 result = 1;
181 l = lrand48 ();
182 if (l != 0x585fcfb7l)
184 printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
185 __LINE__ - 4, 0x585fcfb7l, l);
186 result = 1;
189 l = mrand48 ();
190 if (l != -0x61770b8cl)
192 printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
193 __LINE__ - 4, -0x61770b8cl, l);
194 result = 1;
197 /* And again to see whether seed48() does the same. */
198 lcong48 (lxs);
200 /* See whether lxs wasn't modified. */
201 l = lrand48 ();
202 if (l != 0x6df63d66l)
204 printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
205 __LINE__ - 4, 0x6df63d66l, l);
206 result = 1;
209 /* Test seed48. The previous call should have install the values in
210 the initialization of `xs' above. */
211 xs[0] = 0x1234;
212 xs[1] = 0x5678;
213 xs[2] = 0x9012;
214 xsp = seed48 (xs);
215 if (xsp[0] != 0x0637 || xsp[1] != 0x7acd || xsp[2] != 0xdbec)
217 puts ("seed48() did not install the values correctly");
218 printf (" expected: { %04hx, %04hx, %04hx }\n", 0x0637, 0x7acd, 0xdbec);
219 printf (" seen: { %04hx, %04hx, %04hx }\n", xsp[0], xsp[1], xsp[2]);
220 result = 1;
223 /* Test lrand48 and mrand48. We continue from the seed established
224 above. */
225 l = lrand48 ();
226 if (l != 0x017e48b5l)
228 printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
229 __LINE__ - 4, 0x017e48b5l, l);
230 result = 1;
233 l = mrand48 ();
234 if (l != -0x1485e05dl)
236 printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
237 __LINE__ - 4, -0x1485e05dl, l);
238 result = 1;
241 l = lrand48 ();
242 if (l != 0x6b6a3f95l)
244 printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
245 __LINE__ - 4, 0x6b6a3f95l, l);
246 result = 1;
249 l = mrand48 ();
250 if (l != 0x175c0d6fl)
252 printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
253 __LINE__ - 4, 0x175c0d6fl, l);
254 result = 1;
257 /* Test drand48. */
258 d = drand48 ();
259 if (d != 0.0908832261858485424)
261 printf ("drand48() in line %d failed: expected %.*g, seen %.*g\n",
262 __LINE__ - 4, DECIMAL_DIG, 0.0908832261858485424,
263 DECIMAL_DIG, d);
264 result = 1;
267 d = drand48 ();
268 if (d != 0.943149381730059133133)
270 printf ("drand48() in line %d failed: expected %.*g, seen %.*g\n",
271 __LINE__ - 4, DECIMAL_DIG, 0.943149381730059133133,
272 DECIMAL_DIG, d);
273 result = 1;
276 /* Now the functions which get the Xis passed. */
277 xs[0] = 0x3849;
278 xs[1] = 0x5061;
279 xs[2] = 0x7283;
281 l = nrand48 (xs);
282 if (l != 0x1efe61a1l)
284 printf ("nrand48() in line %d failed: expected %lx, seen %lx\n",
285 __LINE__ - 4, 0x1efe61a1l, l);
286 result = 1;
289 l = jrand48 (xs);
290 if (l != -0xa973860l)
292 printf ("jrand48() in line %d failed: expected %lx, seen %lx\n",
293 __LINE__ - 4, -0xa973860l, l);
294 result = 1;
297 l = nrand48 (xs);
298 if (l != 0x2a5e57fel)
300 printf ("nrand48() in line %d failed: expected %lx, seen %lx\n",
301 __LINE__ - 4, 0x2a5e57fel, l);
302 result = 1;
305 l = jrand48 (xs);
306 if (l != 0x71a779a8l)
308 printf ("jrand48() in line %d failed: expected %lx, seen %lx\n",
309 __LINE__ - 4, 0x71a779a8l, l);
310 result = 1;
313 /* Test whether the global A and C are used. */
314 lcong48 (lxs);
316 l = nrand48 (xs);
317 if (l != 0x32beee9fl)
319 printf ("nrand48() in line %d failed: expected %lx, seen %lx\n",
320 __LINE__ - 4, 0x32beee9fl, l);
321 result = 1;
324 l = jrand48 (xs);
325 if (l != 0x7bddf3bal)
327 printf ("jrand48() in line %d failed: expected %lx, seen %lx\n",
328 __LINE__ - 4, 0x7bddf3bal, l);
329 result = 1;
332 l = nrand48 (xs);
333 if (l != 0x85bdf28l)
335 printf ("nrand48() in line %d failed: expected %lx, seen %lx\n",
336 __LINE__ - 4, 0x85bdf28l, l);
337 result = 1;
340 l = jrand48 (xs);
341 if (l != 0x7b433e47l)
343 printf ("jrand48() in line %d failed: expected %lx, seen %lx\n",
344 __LINE__ - 4, 0x7b433e47l, l);
345 result = 1;
348 /* Test erand48. Also compare with the drand48 results. */
349 (void) seed48 (xs);
351 d = drand48 ();
352 e = erand48 (xs);
353 if (d != e)
355 printf ("\
356 drand48() and erand48 in lines %d and %d produce different results\n",
357 __LINE__ - 6, __LINE__ - 5);
358 printf (" drand48() = %g, erand48() = %g\n", d, e);
359 result = 1;
361 else if (e != 0.640650904452755298735)
363 printf ("erand48() in line %d failed: expected %.*g, seen %.*g\n",
364 __LINE__ - 4, DECIMAL_DIG, 0.640650904452755298735,
365 DECIMAL_DIG, e);
366 result = 1;
370 d = drand48 ();
371 e = erand48 (xs);
372 if (d != e)
374 printf ("\
375 drand48() and erand48 in lines %d and %d produce different results\n",
376 __LINE__ - 6, __LINE__ - 5);
377 printf (" drand48() = %g, erand48() = %g\n", d, e);
378 result = 1;
380 else if (e != 0.115372323508150742555)
382 printf ("erand48() in line %d failed: expected %.*g, seen %.*g\n",
383 __LINE__ - 4, DECIMAL_DIG, 0.0115372323508150742555,
384 DECIMAL_DIG, e);
385 result = 1;
389 return result;