Home | History | Annotate | Line # | Download | only in tests
      1 /* Test file for mpfr_grandom
      2 
      3 Copyright 2011-2023 Free Software Foundation, Inc.
      4 Contributed by the AriC and Caramba projects, INRIA.
      5 
      6 This file is part of the GNU MPFR Library.
      7 
      8 The GNU MPFR Library is free software; you can redistribute it and/or modify
      9 it under the terms of the GNU Lesser General Public License as published by
     10 the Free Software Foundation; either version 3 of the License, or (at your
     11 option) any later version.
     12 
     13 The GNU MPFR Library is distributed in the hope that it will be useful, but
     14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     15 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
     16 License for more details.
     17 
     18 You should have received a copy of the GNU Lesser General Public License
     19 along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
     20 https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
     21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
     22 
     23 #define _MPFR_NO_DEPRECATED_GRANDOM
     24 #include "mpfr-test.h"
     25 
     26 static void
     27 test_special (mpfr_prec_t p)
     28 {
     29   mpfr_t x;
     30   int inexact;
     31 
     32   mpfr_init2 (x, p);
     33 
     34   inexact = mpfr_grandom (x, NULL, RANDS, MPFR_RNDN);
     35   if (((unsigned int) inexact & 3) == 0)
     36     {
     37       printf ("Error: mpfr_grandom() returns a zero ternary value.\n");
     38       exit (1);
     39     }
     40   if (((unsigned int) inexact & (3 << 2)) != 0)
     41     {
     42       printf ("Error: the second ternary value of mpfr_grandom(x, NULL, ...)"
     43               " must be 0.\n");
     44       exit (1);
     45     }
     46 
     47   mpfr_clear (x);
     48 }
     49 
     50 
     51 static void
     52 test_grandom (long nbtests, mpfr_prec_t prec, mpfr_rnd_t rnd,
     53               int verbose)
     54 {
     55   mpfr_t *t;
     56   int i, inexact;
     57 
     58   nbtests = (nbtests & 1) ? (nbtests + 1) : nbtests;
     59   t = (mpfr_t *) tests_allocate (nbtests * sizeof (mpfr_t));
     60 
     61   for (i = 0; i < nbtests; ++i)
     62     mpfr_init2 (t[i], prec);
     63 
     64   for (i = 0; i < nbtests; i += 2)
     65     {
     66       inexact = mpfr_grandom (t[i], t[i + 1], RANDS, MPFR_RNDN);
     67       if (((unsigned int) inexact & 3) == 0 ||
     68           ((unsigned int) inexact & (3 << 2)) == 0)
     69         {
     70           /* one call in the loop pretended to return an exact number! */
     71           printf ("Error: mpfr_grandom() returns a zero ternary value.\n");
     72           exit (1);
     73         }
     74     }
     75 
     76 #if defined(HAVE_STDARG) && !defined(MPFR_USE_MINI_GMP)
     77   if (verbose)
     78     {
     79       mpfr_t av, va, tmp;
     80 
     81       mpfr_init2 (av, prec);
     82       mpfr_init2 (va, prec);
     83       mpfr_init2 (tmp, prec);
     84 
     85       mpfr_set_ui (av, 0, MPFR_RNDN);
     86       mpfr_set_ui (va, 0, MPFR_RNDN);
     87       for (i = 0; i < nbtests; ++i)
     88         {
     89           mpfr_add (av, av, t[i], MPFR_RNDN);
     90           mpfr_sqr (tmp, t[i], MPFR_RNDN);
     91           mpfr_add (va, va, tmp, MPFR_RNDN);
     92         }
     93       mpfr_div_ui (av, av, nbtests, MPFR_RNDN);
     94       mpfr_div_ui (va, va, nbtests, MPFR_RNDN);
     95       mpfr_sqr (tmp, av, MPFR_RNDN);
     96       mpfr_sub (va, va, av, MPFR_RNDN);
     97 
     98       mpfr_printf ("Average = %.5Rf\nVariance = %.5Rf\n", av, va);
     99       mpfr_clear (av);
    100       mpfr_clear (va);
    101       mpfr_clear (tmp);
    102     }
    103 #endif /* HAVE_STDARG */
    104 
    105   for (i = 0; i < nbtests; ++i)
    106     mpfr_clear (t[i]);
    107   tests_free (t, nbtests * sizeof (mpfr_t));
    108   return;
    109 }
    110 
    111 
    112 int
    113 main (int argc, char *argv[])
    114 {
    115   long nbtests;
    116   int verbose;
    117   int i;
    118 
    119   tests_start_mpfr ();
    120 
    121   verbose = 0;
    122   nbtests = 10;
    123   if (argc > 1)
    124     {
    125       long a = atol (argv[1]);
    126       verbose = 1;
    127       if (a != 0)
    128         nbtests = a;
    129     }
    130 
    131   test_grandom (nbtests, 420, MPFR_RNDN, verbose);
    132   test_special (2);
    133   test_special (42000);
    134   /* the following should exercise the case "Extend by 32 bits" in grandom.c */
    135   for (i = 0; i < 10000; i++)
    136     test_special (1);
    137 
    138   tests_end_mpfr ();
    139   return 0;
    140 }
    141