(root)/
glibc-2.38/
stdlib/
tst-rand48.c
       1  #include <float.h>
       2  #include <stdio.h>
       3  #include <stdlib.h>
       4  #include <string.h>
       5  
       6  #ifndef DECIMAL_DIG
       7  # define DECIMAL_DIG	21
       8  #endif
       9  
      10  
      11  static int
      12  do_test (void)
      13  {
      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;
      21  
      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)
      27      {
      28        puts ("srand48(0x98765432) didn't set correct value");
      29        printf ("  expected: { %04x, %04x, %04x }\n", 0x330e, 0x5432, 0x9876);
      30        printf ("  seen:     { %04hx, %04hx, %04hx }\n", xsp[0], xsp[1], xsp[2]);
      31        result = 1;
      32      }
      33    /* Put the values back.  */
      34    memcpy (xs, xsp, sizeof (xs));
      35    (void) seed48 (xs);
      36  
      37    /* See whether the correct values are installed.  */
      38    l = lrand48 ();
      39    if (l != 0x2fed1413l)
      40      {
      41        printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
      42  	      __LINE__ - 4, 0x2fed1413l, l);
      43        result = 1;
      44      }
      45  
      46    l = mrand48 ();
      47    if (l != -0x5d73effdl)
      48      {
      49        printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
      50  	      __LINE__ - 4, -0x5d73effdl, l);
      51        result = 1;
      52      }
      53  
      54    l = lrand48 ();
      55    if (l != 0x585fcfb7l)
      56      {
      57        printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
      58  	      __LINE__ - 4, 0x585fcfb7l, l);
      59        result = 1;
      60      }
      61  
      62    l = mrand48 ();
      63    if (l != -0x61770b8cl)
      64      {
      65        printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
      66  	      __LINE__ - 4, -0x61770b8cl, l);
      67        result = 1;
      68      }
      69  
      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)
      77      {
      78        puts ("seed48() did not install the values correctly");
      79        printf ("  expected: { %04x, %04x, %04x }\n", 0x62f2, 0xf474, 0x9e88);
      80        printf ("  seen:     { %04hx, %04hx, %04hx }\n", xsp[0], xsp[1], xsp[2]);
      81        result = 1;
      82      }
      83  
      84    /* Test lrand48 and mrand48.  We continue from the seed established
      85       above.  */
      86    l = lrand48 ();
      87    if (l != 0x017e48b5l)
      88      {
      89        printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
      90  	      __LINE__ - 4, 0x017e48b5l, l);
      91        result = 1;
      92      }
      93  
      94    l = mrand48 ();
      95    if (l != -0x1485e05dl)
      96      {
      97        printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
      98  	      __LINE__ - 4, -0x1485e05dl, l);
      99        result = 1;
     100      }
     101  
     102    l = lrand48 ();
     103    if (l != 0x6b6a3f95l)
     104      {
     105        printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
     106  	      __LINE__ - 4, 0x6b6a3f95l, l);
     107        result = 1;
     108      }
     109  
     110    l = mrand48 ();
     111    if (l != 0x175c0d6fl)
     112      {
     113        printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
     114  	      __LINE__ - 4, 0x175c0d6fl, l);
     115        result = 1;
     116      }
     117  
     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);
     127  
     128    /* See whether the correct values are installed.  */
     129    l = lrand48 ();
     130    if (l != 0x6df63d66l)
     131      {
     132        printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
     133  	      __LINE__ - 4, 0x6df63d66l, l);
     134        result = 1;
     135      }
     136  
     137    l = mrand48 ();
     138    if (l != 0x2f92c8e1l)
     139      {
     140        printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
     141  	      __LINE__ - 4, 0x2f92c8e1l, l);
     142        result = 1;
     143      }
     144  
     145    l = lrand48 ();
     146    if (l != 0x3b4869ffl)
     147      {
     148        printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
     149  	      __LINE__ - 4, 0x3b4869ffl, l);
     150        result = 1;
     151      }
     152  
     153    l = mrand48 ();
     154    if (l != 0x5cd4cc3el)
     155      {
     156        printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
     157  	      __LINE__ - 4, 0x5cd4cc3el, l);
     158        result = 1;
     159      }
     160  
     161    /* Check whether srand48() restores the A and C parameters.  */
     162    srand48 (0x98765432);
     163  
     164    /* See whether the correct values are installed.  */
     165    l = lrand48 ();
     166    if (l != 0x2fed1413l)
     167      {
     168        printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
     169  	      __LINE__ - 4, 0x2fed1413l, l);
     170        result = 1;
     171      }
     172  
     173    l = mrand48 ();
     174    if (l != -0x5d73effdl)
     175      {
     176        printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
     177  	      __LINE__ - 4, -0x5d73effdl, l);
     178        result = 1;
     179      }
     180  
     181    l = lrand48 ();
     182    if (l != 0x585fcfb7l)
     183      {
     184        printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
     185  	      __LINE__ - 4, 0x585fcfb7l, l);
     186        result = 1;
     187      }
     188  
     189    l = mrand48 ();
     190    if (l != -0x61770b8cl)
     191      {
     192        printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
     193  	      __LINE__ - 4, -0x61770b8cl, l);
     194        result = 1;
     195      }
     196  
     197    /* And again to see whether seed48() does the same.  */
     198    lcong48 (lxs);
     199  
     200    /* See whether lxs wasn't modified.  */
     201    l = lrand48 ();
     202    if (l != 0x6df63d66l)
     203      {
     204        printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
     205  	      __LINE__ - 4, 0x6df63d66l, l);
     206        result = 1;
     207      }
     208  
     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)
     216      {
     217        puts ("seed48() did not install the values correctly");
     218        printf ("  expected: { %04x, %04x, %04x }\n", 0x0637, 0x7acd, 0xdbec);
     219        printf ("  seen:     { %04hx, %04hx, %04hx }\n", xsp[0], xsp[1], xsp[2]);
     220        result = 1;
     221      }
     222  
     223    /* Test lrand48 and mrand48.  We continue from the seed established
     224       above.  */
     225    l = lrand48 ();
     226    if (l != 0x017e48b5l)
     227      {
     228        printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
     229  	      __LINE__ - 4, 0x017e48b5l, l);
     230        result = 1;
     231      }
     232  
     233    l = mrand48 ();
     234    if (l != -0x1485e05dl)
     235      {
     236        printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
     237  	      __LINE__ - 4, -0x1485e05dl, l);
     238        result = 1;
     239      }
     240  
     241    l = lrand48 ();
     242    if (l != 0x6b6a3f95l)
     243      {
     244        printf ("lrand48() in line %d failed: expected %lx, seen %lx\n",
     245  	      __LINE__ - 4, 0x6b6a3f95l, l);
     246        result = 1;
     247      }
     248  
     249    l = mrand48 ();
     250    if (l != 0x175c0d6fl)
     251      {
     252        printf ("mrand48() in line %d failed: expected %lx, seen %lx\n",
     253  	      __LINE__ - 4, 0x175c0d6fl, l);
     254        result = 1;
     255      }
     256  
     257    /* Test drand48.  */
     258    d = drand48 ();
     259    if (d != 0.0908832261858485424)
     260      {
     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;
     265      }
     266  
     267    d = drand48 ();
     268    if (d != 0.943149381730059133133)
     269      {
     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;
     274      }
     275  
     276    /* Now the functions which get the Xis passed.  */
     277    xs[0] = 0x3849;
     278    xs[1] = 0x5061;
     279    xs[2] = 0x7283;
     280  
     281    l = nrand48 (xs);
     282    if (l != 0x1efe61a1l)
     283      {
     284        printf ("nrand48() in line %d failed: expected %lx, seen %lx\n",
     285  	      __LINE__ - 4, 0x1efe61a1l, l);
     286        result = 1;
     287      }
     288  
     289    l = jrand48 (xs);
     290    if (l != -0xa973860l)
     291      {
     292        printf ("jrand48() in line %d failed: expected %lx, seen %lx\n",
     293  	      __LINE__ - 4, -0xa973860l, l);
     294        result = 1;
     295      }
     296  
     297    l = nrand48 (xs);
     298    if (l != 0x2a5e57fel)
     299      {
     300        printf ("nrand48() in line %d failed: expected %lx, seen %lx\n",
     301  	      __LINE__ - 4, 0x2a5e57fel, l);
     302        result = 1;
     303      }
     304  
     305    l = jrand48 (xs);
     306    if (l != 0x71a779a8l)
     307      {
     308        printf ("jrand48() in line %d failed: expected %lx, seen %lx\n",
     309  	      __LINE__ - 4, 0x71a779a8l, l);
     310        result = 1;
     311      }
     312  
     313    /* Test whether the global A and C are used.  */
     314    lcong48 (lxs);
     315  
     316    l = nrand48 (xs);
     317    if (l != 0x32beee9fl)
     318      {
     319        printf ("nrand48() in line %d failed: expected %lx, seen %lx\n",
     320  	      __LINE__ - 4, 0x32beee9fl, l);
     321        result = 1;
     322      }
     323  
     324    l = jrand48 (xs);
     325    if (l != 0x7bddf3bal)
     326      {
     327        printf ("jrand48() in line %d failed: expected %lx, seen %lx\n",
     328  	      __LINE__ - 4, 0x7bddf3bal, l);
     329        result = 1;
     330      }
     331  
     332    l = nrand48 (xs);
     333    if (l != 0x85bdf28l)
     334      {
     335        printf ("nrand48() in line %d failed: expected %lx, seen %lx\n",
     336  	      __LINE__ - 4, 0x85bdf28l, l);
     337        result = 1;
     338      }
     339  
     340    l = jrand48 (xs);
     341    if (l != 0x7b433e47l)
     342      {
     343        printf ("jrand48() in line %d failed: expected %lx, seen %lx\n",
     344  	      __LINE__ - 4, 0x7b433e47l, l);
     345        result = 1;
     346      }
     347  
     348    /* Test erand48.  Also compare with the drand48 results.  */
     349    (void) seed48 (xs);
     350  
     351    d = drand48 ();
     352    e = erand48 (xs);
     353    if (d != e)
     354      {
     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;
     360      }
     361    else if (e != 0.640650904452755298735)
     362      {
     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;
     367  
     368      }
     369  
     370    d = drand48 ();
     371    e = erand48 (xs);
     372    if (d != e)
     373      {
     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;
     379      }
     380    else if (e != 0.115372323508150742555)
     381      {
     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;
     386  
     387      }
     388  
     389    return result;
     390  }
     391  
     392  #define TEST_FUNCTION do_test ()
     393  #include "../test-skeleton.c"