tst-rand48.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. #include <float.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #ifndef DECIMAL_DIG
  6. # define DECIMAL_DIG 21
  7. #endif
  8. static int
  9. do_test (void)
  10. {
  11. unsigned short int xs[3] = { 0x0001, 0x0012, 0x0123 };
  12. unsigned short int lxs[7];
  13. unsigned short int *xsp;
  14. int result = 0;
  15. long int l;
  16. double d;
  17. double e;
  18. /* Test srand48. */
  19. srand48 (0x98765432);
  20. /* Get the values of the internal Xi array. */
  21. xsp = seed48 (xs);
  22. if (xsp[0] != 0x330e || xsp[1] != 0x5432 || xsp[2] != 0x9876)
  23. {
  24. puts ("srand48(0x98765432) didn't set correct value");
  25. printf (" expected: { %04hx, %04hx, %04hx }\n", 0x330e, 0x5432, 0x9876);
  26. printf (" seen: { %04hx, %04hx, %04hx }\n", xsp[0], xsp[1], xsp[2]);
  27. result = 1;
  28. }
  29. /* Put the values back. */
  30. memcpy (xs, xsp, sizeof (xs));
  31. (void) seed48 (xs);
  32. /* See whether the correct values are installed. */
  33. l = lrand48 ();
  34. if (l != 0x2fed1413l)
  35. {
  36. printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
  37. __LINE__ - 4, 0x2fed1413l, l);
  38. result = 1;
  39. }
  40. l = mrand48 ();
  41. if (l != -0x5d73effdl)
  42. {
  43. printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
  44. __LINE__ - 4, -0x5d73effdl, l);
  45. result = 1;
  46. }
  47. l = lrand48 ();
  48. if (l != 0x585fcfb7l)
  49. {
  50. printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
  51. __LINE__ - 4, 0x585fcfb7l, l);
  52. result = 1;
  53. }
  54. l = mrand48 ();
  55. if (l != -0x61770b8cl)
  56. {
  57. printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
  58. __LINE__ - 4, -0x61770b8cl, l);
  59. result = 1;
  60. }
  61. /* Test seed48. The previous call should have install the values in
  62. the initialization of `xs' above. */
  63. xs[0] = 0x1234;
  64. xs[1] = 0x5678;
  65. xs[2] = 0x9012;
  66. xsp = seed48 (xs);
  67. if (xsp[0] != 0x62f2 || xsp[1] != 0xf474 || xsp[2] != 0x9e88)
  68. {
  69. puts ("seed48() did not install the values correctly");
  70. printf (" expected: { %04hx, %04hx, %04hx }\n", 0x62f2, 0xf474, 0x9e88);
  71. printf (" seen: { %04hx, %04hx, %04hx }\n", xsp[0], xsp[1], xsp[2]);
  72. result = 1;
  73. }
  74. /* Test lrand48 and mrand48. We continue from the seed established
  75. above. */
  76. l = lrand48 ();
  77. if (l != 0x017e48b5l)
  78. {
  79. printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
  80. __LINE__ - 4, 0x017e48b5l, l);
  81. result = 1;
  82. }
  83. l = mrand48 ();
  84. if (l != -0x1485e05dl)
  85. {
  86. printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
  87. __LINE__ - 4, -0x1485e05dl, l);
  88. result = 1;
  89. }
  90. l = lrand48 ();
  91. if (l != 0x6b6a3f95l)
  92. {
  93. printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
  94. __LINE__ - 4, 0x6b6a3f95l, l);
  95. result = 1;
  96. }
  97. l = mrand48 ();
  98. if (l != 0x175c0d6fl)
  99. {
  100. printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
  101. __LINE__ - 4, 0x175c0d6fl, l);
  102. result = 1;
  103. }
  104. /* Test lcong48. */
  105. lxs[0] = 0x4567;
  106. lxs[1] = 0x6789;
  107. lxs[2] = 0x8901;
  108. lxs[3] = 0x0123;
  109. lxs[4] = 0x2345;
  110. lxs[5] = 0x1111;
  111. lxs[6] = 0x2222;
  112. lcong48 (lxs);
  113. /* See whether the correct values are installed. */
  114. l = lrand48 ();
  115. if (l != 0x6df63d66l)
  116. {
  117. printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
  118. __LINE__ - 4, 0x6df63d66l, l);
  119. result = 1;
  120. }
  121. l = mrand48 ();
  122. if (l != 0x2f92c8e1l)
  123. {
  124. printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
  125. __LINE__ - 4, 0x2f92c8e1l, l);
  126. result = 1;
  127. }
  128. l = lrand48 ();
  129. if (l != 0x3b4869ffl)
  130. {
  131. printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
  132. __LINE__ - 4, 0x3b4869ffl, l);
  133. result = 1;
  134. }
  135. l = mrand48 ();
  136. if (l != 0x5cd4cc3el)
  137. {
  138. printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
  139. __LINE__ - 4, 0x5cd4cc3el, l);
  140. result = 1;
  141. }
  142. /* Check whether srand48() restores the A and C parameters. */
  143. srand48 (0x98765432);
  144. /* See whether the correct values are installed. */
  145. l = lrand48 ();
  146. if (l != 0x2fed1413l)
  147. {
  148. printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
  149. __LINE__ - 4, 0x2fed1413l, l);
  150. result = 1;
  151. }
  152. l = mrand48 ();
  153. if (l != -0x5d73effdl)
  154. {
  155. printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
  156. __LINE__ - 4, -0x5d73effdl, l);
  157. result = 1;
  158. }
  159. l = lrand48 ();
  160. if (l != 0x585fcfb7l)
  161. {
  162. printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
  163. __LINE__ - 4, 0x585fcfb7l, l);
  164. result = 1;
  165. }
  166. l = mrand48 ();
  167. if (l != -0x61770b8cl)
  168. {
  169. printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
  170. __LINE__ - 4, -0x61770b8cl, l);
  171. result = 1;
  172. }
  173. /* And again to see whether seed48() does the same. */
  174. lcong48 (lxs);
  175. /* See whether lxs wasn't modified. */
  176. l = lrand48 ();
  177. if (l != 0x6df63d66l)
  178. {
  179. printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
  180. __LINE__ - 4, 0x6df63d66l, l);
  181. result = 1;
  182. }
  183. /* Test seed48. The previous call should have install the values in
  184. the initialization of `xs' above. */
  185. xs[0] = 0x1234;
  186. xs[1] = 0x5678;
  187. xs[2] = 0x9012;
  188. xsp = seed48 (xs);
  189. if (xsp[0] != 0x0637 || xsp[1] != 0x7acd || xsp[2] != 0xdbec)
  190. {
  191. puts ("seed48() did not install the values correctly");
  192. printf (" expected: { %04hx, %04hx, %04hx }\n", 0x0637, 0x7acd, 0xdbec);
  193. printf (" seen: { %04hx, %04hx, %04hx }\n", xsp[0], xsp[1], xsp[2]);
  194. result = 1;
  195. }
  196. /* Test lrand48 and mrand48. We continue from the seed established
  197. above. */
  198. l = lrand48 ();
  199. if (l != 0x017e48b5l)
  200. {
  201. printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
  202. __LINE__ - 4, 0x017e48b5l, l);
  203. result = 1;
  204. }
  205. l = mrand48 ();
  206. if (l != -0x1485e05dl)
  207. {
  208. printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
  209. __LINE__ - 4, -0x1485e05dl, l);
  210. result = 1;
  211. }
  212. l = lrand48 ();
  213. if (l != 0x6b6a3f95l)
  214. {
  215. printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
  216. __LINE__ - 4, 0x6b6a3f95l, l);
  217. result = 1;
  218. }
  219. l = mrand48 ();
  220. if (l != 0x175c0d6fl)
  221. {
  222. printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
  223. __LINE__ - 4, 0x175c0d6fl, l);
  224. result = 1;
  225. }
  226. /* Test drand48. */
  227. d = drand48 ();
  228. if (d != 0.0908832261858485424)
  229. {
  230. printf ("drand48() in line %d failed: expected %.*g, seen %.*g\n",
  231. __LINE__ - 4, DECIMAL_DIG, 0.0908832261858485424,
  232. DECIMAL_DIG, d);
  233. result = 1;
  234. }
  235. d = drand48 ();
  236. if (d != 0.943149381730059133133)
  237. {
  238. printf ("drand48() in line %d failed: expected %.*g, seen %.*g\n",
  239. __LINE__ - 4, DECIMAL_DIG, 0.943149381730059133133,
  240. DECIMAL_DIG, d);
  241. result = 1;
  242. }
  243. /* Now the functions which get the Xis passed. */
  244. xs[0] = 0x3849;
  245. xs[1] = 0x5061;
  246. xs[2] = 0x7283;
  247. l = nrand48 (xs);
  248. if (l != 0x1efe61a1l)
  249. {
  250. printf ("nrand48() in line %d failed: expected %lx, seen %lx\n",
  251. __LINE__ - 4, 0x1efe61a1l, l);
  252. result = 1;
  253. }
  254. l = jrand48 (xs);
  255. if (l != -0xa973860l)
  256. {
  257. printf ("jrand48() in line %d failed: expected %lx, seen %lx\n",
  258. __LINE__ - 4, -0xa973860l, l);
  259. result = 1;
  260. }
  261. l = nrand48 (xs);
  262. if (l != 0x2a5e57fel)
  263. {
  264. printf ("nrand48() in line %d failed: expected %lx, seen %lx\n",
  265. __LINE__ - 4, 0x2a5e57fel, l);
  266. result = 1;
  267. }
  268. l = jrand48 (xs);
  269. if (l != 0x71a779a8l)
  270. {
  271. printf ("jrand48() in line %d failed: expected %lx, seen %lx\n",
  272. __LINE__ - 4, 0x71a779a8l, l);
  273. result = 1;
  274. }
  275. /* Test whether the global A and C are used. */
  276. lcong48 (lxs);
  277. l = nrand48 (xs);
  278. if (l != 0x32beee9fl)
  279. {
  280. printf ("nrand48() in line %d failed: expected %lx, seen %lx\n",
  281. __LINE__ - 4, 0x32beee9fl, l);
  282. result = 1;
  283. }
  284. l = jrand48 (xs);
  285. if (l != 0x7bddf3bal)
  286. {
  287. printf ("jrand48() in line %d failed: expected %lx, seen %lx\n",
  288. __LINE__ - 4, 0x7bddf3bal, l);
  289. result = 1;
  290. }
  291. l = nrand48 (xs);
  292. if (l != 0x85bdf28l)
  293. {
  294. printf ("nrand48() in line %d failed: expected %lx, seen %lx\n",
  295. __LINE__ - 4, 0x85bdf28l, l);
  296. result = 1;
  297. }
  298. l = jrand48 (xs);
  299. if (l != 0x7b433e47l)
  300. {
  301. printf ("jrand48() in line %d failed: expected %lx, seen %lx\n",
  302. __LINE__ - 4, 0x7b433e47l, l);
  303. result = 1;
  304. }
  305. /* Test erand48. Also compare with the drand48 results. */
  306. (void) seed48 (xs);
  307. d = drand48 ();
  308. e = erand48 (xs);
  309. if (d != e)
  310. {
  311. printf ("\
  312. drand48() and erand48 in lines %d and %d produce different results\n",
  313. __LINE__ - 6, __LINE__ - 5);
  314. printf (" drand48() = %g, erand48() = %g\n", d, e);
  315. result = 1;
  316. }
  317. else if (e != 0.640650904452755298735)
  318. {
  319. printf ("erand48() in line %d failed: expected %.*g, seen %.*g\n",
  320. __LINE__ - 4, DECIMAL_DIG, 0.640650904452755298735,
  321. DECIMAL_DIG, e);
  322. result = 1;
  323. }
  324. d = drand48 ();
  325. e = erand48 (xs);
  326. if (d != e)
  327. {
  328. printf ("\
  329. drand48() and erand48 in lines %d and %d produce different results\n",
  330. __LINE__ - 6, __LINE__ - 5);
  331. printf (" drand48() = %g, erand48() = %g\n", d, e);
  332. result = 1;
  333. }
  334. else if (e != 0.115372323508150742555)
  335. {
  336. printf ("erand48() in line %d failed: expected %.*g, seen %.*g\n",
  337. __LINE__ - 4, DECIMAL_DIG, 0.0115372323508150742555,
  338. DECIMAL_DIG, e);
  339. result = 1;
  340. }
  341. return result;
  342. }
  343. #define TEST_FUNCTION do_test ()
  344. #include "../test-skeleton.c"