Back to index

glibc  2.9
tst-rand48.c
Go to the documentation of this file.
00001 #include <float.h>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <string.h>
00005 
00006 #ifndef DECIMAL_DIG
00007 # define DECIMAL_DIG 21
00008 #endif
00009 
00010 
00011 int
00012 main (void)
00013 {
00014   unsigned short int xs[3] = { 0x0001, 0x0012, 0x0123 };
00015   unsigned short int lxs[7];
00016   unsigned short int *xsp;
00017   int result = 0;
00018   long int l;
00019   double d;
00020   double e;
00021 
00022   /* Test srand48.  */
00023   srand48 (0x98765432);
00024   /* Get the values of the internal Xi array.  */
00025   xsp = seed48 (xs);
00026   if (xsp[0] != 0x330e || xsp[1] != 0x5432 || xsp[2] != 0x9876)
00027     {
00028       puts ("srand48(0x98765432) didn't set correct value");
00029       printf ("  expected: { %04hx, %04hx, %04hx }\n", 0x330e, 0x5432, 0x9876);
00030       printf ("  seen:     { %04hx, %04hx, %04hx }\n", xsp[0], xsp[1], xsp[2]);
00031       result = 1;
00032     }
00033   /* Put the values back.  */
00034   memcpy (xs, xsp, sizeof (xs));
00035   (void) seed48 (xs);
00036 
00037   /* See whether the correct values are installed.  */
00038   l = lrand48 ();
00039   if (l != 0x2fed1413l)
00040     {
00041       printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
00042              __LINE__ - 4, 0x2fed1413l, l);
00043       result = 1;
00044     }
00045 
00046   l = mrand48 ();
00047   if (l != -0x5d73effdl)
00048     {
00049       printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
00050              __LINE__ - 4, -0x5d73effdl, l);
00051       result = 1;
00052     }
00053 
00054   l = lrand48 ();
00055   if (l != 0x585fcfb7l)
00056     {
00057       printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
00058              __LINE__ - 4, 0x585fcfb7l, l);
00059       result = 1;
00060     }
00061 
00062   l = mrand48 ();
00063   if (l != -0x61770b8cl)
00064     {
00065       printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
00066              __LINE__ - 4, -0x61770b8cl, l);
00067       result = 1;
00068     }
00069 
00070   /* Test seed48.  The previous call should have install the values in
00071      the initialization of `xs' above.  */
00072   xs[0] = 0x1234;
00073   xs[1] = 0x5678;
00074   xs[2] = 0x9012;
00075   xsp = seed48 (xs);
00076   if (xsp[0] != 0x62f2 || xsp[1] != 0xf474 || xsp[2] != 0x9e88)
00077     {
00078       puts ("seed48() did not install the values correctly");
00079       printf ("  expected: { %04hx, %04hx, %04hx }\n", 0x62f2, 0xf474, 0x9e88);
00080       printf ("  seen:     { %04hx, %04hx, %04hx }\n", xsp[0], xsp[1], xsp[2]);
00081       result = 1;
00082     }
00083 
00084   /* Test lrand48 and mrand48.  We continue from the seed established
00085      above.  */
00086   l = lrand48 ();
00087   if (l != 0x017e48b5l)
00088     {
00089       printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
00090              __LINE__ - 4, 0x017e48b5l, l);
00091       result = 1;
00092     }
00093 
00094   l = mrand48 ();
00095   if (l != -0x1485e05dl)
00096     {
00097       printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
00098              __LINE__ - 4, -0x1485e05dl, l);
00099       result = 1;
00100     }
00101 
00102   l = lrand48 ();
00103   if (l != 0x6b6a3f95l)
00104     {
00105       printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
00106              __LINE__ - 4, 0x6b6a3f95l, l);
00107       result = 1;
00108     }
00109 
00110   l = mrand48 ();
00111   if (l != 0x175c0d6fl)
00112     {
00113       printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
00114              __LINE__ - 4, 0x175c0d6fl, l);
00115       result = 1;
00116     }
00117 
00118   /* Test lcong48.  */
00119   lxs[0] = 0x4567;
00120   lxs[1] = 0x6789;
00121   lxs[2] = 0x8901;
00122   lxs[3] = 0x0123;
00123   lxs[4] = 0x2345;
00124   lxs[5] = 0x1111;
00125   lxs[6] = 0x2222;
00126   lcong48 (lxs);
00127 
00128   /* See whether the correct values are installed.  */
00129   l = lrand48 ();
00130   if (l != 0x6df63d66l)
00131     {
00132       printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
00133              __LINE__ - 4, 0x6df63d66l, l);
00134       result = 1;
00135     }
00136 
00137   l = mrand48 ();
00138   if (l != 0x2f92c8e1l)
00139     {
00140       printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
00141              __LINE__ - 4, 0x2f92c8e1l, l);
00142       result = 1;
00143     }
00144 
00145   l = lrand48 ();
00146   if (l != 0x3b4869ffl)
00147     {
00148       printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
00149              __LINE__ - 4, 0x3b4869ffl, l);
00150       result = 1;
00151     }
00152 
00153   l = mrand48 ();
00154   if (l != 0x5cd4cc3el)
00155     {
00156       printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
00157              __LINE__ - 4, 0x5cd4cc3el, l);
00158       result = 1;
00159     }
00160 
00161   /* Check whether srand48() restores the A and C parameters.  */
00162   srand48 (0x98765432);
00163 
00164   /* See whether the correct values are installed.  */
00165   l = lrand48 ();
00166   if (l != 0x2fed1413l)
00167     {
00168       printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
00169              __LINE__ - 4, 0x2fed1413l, l);
00170       result = 1;
00171     }
00172 
00173   l = mrand48 ();
00174   if (l != -0x5d73effdl)
00175     {
00176       printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
00177              __LINE__ - 4, -0x5d73effdl, l);
00178       result = 1;
00179     }
00180 
00181   l = lrand48 ();
00182   if (l != 0x585fcfb7l)
00183     {
00184       printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
00185              __LINE__ - 4, 0x585fcfb7l, l);
00186       result = 1;
00187     }
00188 
00189   l = mrand48 ();
00190   if (l != -0x61770b8cl)
00191     {
00192       printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
00193              __LINE__ - 4, -0x61770b8cl, l);
00194       result = 1;
00195     }
00196 
00197   /* And again to see whether seed48() does the same.  */
00198   lcong48 (lxs);
00199 
00200   /* See whether lxs wasn't modified.  */
00201   l = lrand48 ();
00202   if (l != 0x6df63d66l)
00203     {
00204       printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
00205              __LINE__ - 4, 0x6df63d66l, l);
00206       result = 1;
00207     }
00208 
00209   /* Test seed48.  The previous call should have install the values in
00210      the initialization of `xs' above.  */
00211   xs[0] = 0x1234;
00212   xs[1] = 0x5678;
00213   xs[2] = 0x9012;
00214   xsp = seed48 (xs);
00215   if (xsp[0] != 0x0637 || xsp[1] != 0x7acd || xsp[2] != 0xdbec)
00216     {
00217       puts ("seed48() did not install the values correctly");
00218       printf ("  expected: { %04hx, %04hx, %04hx }\n", 0x0637, 0x7acd, 0xdbec);
00219       printf ("  seen:     { %04hx, %04hx, %04hx }\n", xsp[0], xsp[1], xsp[2]);
00220       result = 1;
00221     }
00222 
00223   /* Test lrand48 and mrand48.  We continue from the seed established
00224      above.  */
00225   l = lrand48 ();
00226   if (l != 0x017e48b5l)
00227     {
00228       printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
00229              __LINE__ - 4, 0x017e48b5l, l);
00230       result = 1;
00231     }
00232 
00233   l = mrand48 ();
00234   if (l != -0x1485e05dl)
00235     {
00236       printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
00237              __LINE__ - 4, -0x1485e05dl, l);
00238       result = 1;
00239     }
00240 
00241   l = lrand48 ();
00242   if (l != 0x6b6a3f95l)
00243     {
00244       printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
00245              __LINE__ - 4, 0x6b6a3f95l, l);
00246       result = 1;
00247     }
00248 
00249   l = mrand48 ();
00250   if (l != 0x175c0d6fl)
00251     {
00252       printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
00253              __LINE__ - 4, 0x175c0d6fl, l);
00254       result = 1;
00255     }
00256 
00257   /* Test drand48.  */
00258   d = drand48 ();
00259   if (d != 0.0908832261858485424)
00260     {
00261       printf ("drand48() in line %d failed: expected %.*g, seen %.*g\n",
00262              __LINE__ - 4, DECIMAL_DIG, 0.0908832261858485424,
00263              DECIMAL_DIG, d);
00264       result = 1;
00265     }
00266 
00267   d = drand48 ();
00268   if (d != 0.943149381730059133133)
00269     {
00270       printf ("drand48() in line %d failed: expected %.*g, seen %.*g\n",
00271              __LINE__ - 4, DECIMAL_DIG, 0.943149381730059133133,
00272              DECIMAL_DIG, d);
00273       result = 1;
00274     }
00275 
00276   /* Now the functions which get the Xis passed.  */
00277   xs[0] = 0x3849;
00278   xs[1] = 0x5061;
00279   xs[2] = 0x7283;
00280 
00281   l = nrand48 (xs);
00282   if (l != 0x1efe61a1l)
00283     {
00284       printf ("nrand48() in line %d failed: expected %lx, seen %lx\n",
00285              __LINE__ - 4, 0x1efe61a1l, l);
00286       result = 1;
00287     }
00288 
00289   l = jrand48 (xs);
00290   if (l != -0xa973860l)
00291     {
00292       printf ("jrand48() in line %d failed: expected %lx, seen %lx\n",
00293              __LINE__ - 4, -0xa973860l, l);
00294       result = 1;
00295     }
00296 
00297   l = nrand48 (xs);
00298   if (l != 0x2a5e57fel)
00299     {
00300       printf ("nrand48() in line %d failed: expected %lx, seen %lx\n",
00301              __LINE__ - 4, 0x2a5e57fel, l);
00302       result = 1;
00303     }
00304 
00305   l = jrand48 (xs);
00306   if (l != 0x71a779a8l)
00307     {
00308       printf ("jrand48() in line %d failed: expected %lx, seen %lx\n",
00309              __LINE__ - 4, 0x71a779a8l, l);
00310       result = 1;
00311     }
00312 
00313   /* Test whether the global A and C are used.  */
00314   lcong48 (lxs);
00315 
00316   l = nrand48 (xs);
00317   if (l != 0x32beee9fl)
00318     {
00319       printf ("nrand48() in line %d failed: expected %lx, seen %lx\n",
00320              __LINE__ - 4, 0x32beee9fl, l);
00321       result = 1;
00322     }
00323 
00324   l = jrand48 (xs);
00325   if (l != 0x7bddf3bal)
00326     {
00327       printf ("jrand48() in line %d failed: expected %lx, seen %lx\n",
00328              __LINE__ - 4, 0x7bddf3bal, l);
00329       result = 1;
00330     }
00331 
00332   l = nrand48 (xs);
00333   if (l != 0x85bdf28l)
00334     {
00335       printf ("nrand48() in line %d failed: expected %lx, seen %lx\n",
00336              __LINE__ - 4, 0x85bdf28l, l);
00337       result = 1;
00338     }
00339 
00340   l = jrand48 (xs);
00341   if (l != 0x7b433e47l)
00342     {
00343       printf ("jrand48() in line %d failed: expected %lx, seen %lx\n",
00344              __LINE__ - 4, 0x7b433e47l, l);
00345       result = 1;
00346     }
00347 
00348   /* Test erand48.  Also compare with the drand48 results.  */
00349   (void) seed48 (xs);
00350 
00351   d = drand48 ();
00352   e = erand48 (xs);
00353   if (d != e)
00354     {
00355       printf ("\
00356 drand48() and erand48 in lines %d and %d produce different results\n",
00357              __LINE__ - 6, __LINE__ - 5);
00358       printf ("  drand48() = %g, erand48() = %g\n", d, e);
00359       result = 1;
00360     }
00361   else if (e != 0.640650904452755298735)
00362     {
00363       printf ("erand48() in line %d failed: expected %.*g, seen %.*g\n",
00364              __LINE__ - 4, DECIMAL_DIG, 0.640650904452755298735,
00365              DECIMAL_DIG, e);
00366       result = 1;
00367 
00368     }
00369 
00370   d = drand48 ();
00371   e = erand48 (xs);
00372   if (d != e)
00373     {
00374       printf ("\
00375 drand48() and erand48 in lines %d and %d produce different results\n",
00376              __LINE__ - 6, __LINE__ - 5);
00377       printf ("  drand48() = %g, erand48() = %g\n", d, e);
00378       result = 1;
00379     }
00380   else if (e != 0.115372323508150742555)
00381     {
00382       printf ("erand48() in line %d failed: expected %.*g, seen %.*g\n",
00383              __LINE__ - 4, DECIMAL_DIG, 0.0115372323508150742555,
00384              DECIMAL_DIG, e);
00385       result = 1;
00386 
00387     }
00388 
00389   return result;
00390 }