Home | History | Annotate | Line # | Download | only in mpn
t-scan.c revision 1.1
      1  1.1  mrg /* Test mpn_scan0 and mpn_scan1.
      2  1.1  mrg 
      3  1.1  mrg Copyright 2002 Free Software Foundation, Inc.
      4  1.1  mrg 
      5  1.1  mrg This file is part of the GNU MP Library.
      6  1.1  mrg 
      7  1.1  mrg The GNU MP Library is free software; you can redistribute it and/or modify
      8  1.1  mrg it under the terms of the GNU Lesser General Public License as published by
      9  1.1  mrg the Free Software Foundation; either version 3 of the License, or (at your
     10  1.1  mrg option) any later version.
     11  1.1  mrg 
     12  1.1  mrg The GNU MP Library is distributed in the hope that it will be useful, but
     13  1.1  mrg WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     14  1.1  mrg or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
     15  1.1  mrg License for more details.
     16  1.1  mrg 
     17  1.1  mrg You should have received a copy of the GNU Lesser General Public License
     18  1.1  mrg along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
     19  1.1  mrg 
     20  1.1  mrg #include <stdio.h>
     21  1.1  mrg #include <stdlib.h>
     22  1.1  mrg 
     23  1.1  mrg #include "gmp.h"
     24  1.1  mrg #include "gmp-impl.h"
     25  1.1  mrg 
     26  1.1  mrg #include "tests.h"
     27  1.1  mrg 
     28  1.1  mrg 
     29  1.1  mrg #define SIZE  ((mp_size_t) 3)
     30  1.1  mrg mp_limb_t  x[SIZE+1];
     31  1.1  mrg 
     32  1.1  mrg void
     33  1.1  mrg check (void)
     34  1.1  mrg {
     35  1.1  mrg   unsigned long  i, got, want;
     36  1.1  mrg 
     37  1.1  mrg   x[SIZE] = 1;
     38  1.1  mrg   for (i = 0; i < SIZE*GMP_NUMB_BITS; i++)
     39  1.1  mrg     {
     40  1.1  mrg       got = refmpn_scan1 (x, i);
     41  1.1  mrg       want = mpn_scan1 (x, i);
     42  1.1  mrg       if (got != want)
     43  1.1  mrg         {
     44  1.1  mrg           printf ("mpn_scan1\n");
     45  1.1  mrg           printf ("  i     %lu\n", i);
     46  1.1  mrg           printf ("  got   %lu\n", got);
     47  1.1  mrg           printf ("  want  %lu\n", want);
     48  1.1  mrg           mpn_trace ("  x    ", x, SIZE);
     49  1.1  mrg           abort ();
     50  1.1  mrg         }
     51  1.1  mrg     }
     52  1.1  mrg 
     53  1.1  mrg   x[SIZE] = 0;
     54  1.1  mrg   for (i = 0; i < SIZE*GMP_NUMB_BITS; i++)
     55  1.1  mrg     {
     56  1.1  mrg       got = refmpn_scan0 (x, i);
     57  1.1  mrg       want = mpn_scan0 (x, i);
     58  1.1  mrg       if (got != want)
     59  1.1  mrg         {
     60  1.1  mrg           printf ("mpn_scan0\n");
     61  1.1  mrg           printf ("  i     %lu\n", i);
     62  1.1  mrg           printf ("  got   %lu\n", got);
     63  1.1  mrg           printf ("  want  %lu\n", want);
     64  1.1  mrg           mpn_trace ("  x    ", x, SIZE);
     65  1.1  mrg           abort ();
     66  1.1  mrg         }
     67  1.1  mrg     }
     68  1.1  mrg }
     69  1.1  mrg 
     70  1.1  mrg void
     71  1.1  mrg check_twobits (void)
     72  1.1  mrg {
     73  1.1  mrg #define TWOBITS(a, b) \
     74  1.1  mrg   ((CNST_LIMB(1) << (a)) | (CNST_LIMB(1) << (b)))
     75  1.1  mrg 
     76  1.1  mrg   refmpn_zero (x, SIZE);
     77  1.1  mrg   x[0] = TWOBITS (1, 0);
     78  1.1  mrg   check ();
     79  1.1  mrg 
     80  1.1  mrg   refmpn_zero (x, SIZE);
     81  1.1  mrg   x[0] = TWOBITS (GMP_NUMB_BITS-1, 1);
     82  1.1  mrg   check ();
     83  1.1  mrg 
     84  1.1  mrg   refmpn_zero (x, SIZE);
     85  1.1  mrg   x[0] = CNST_LIMB(1);
     86  1.1  mrg   x[1] = CNST_LIMB(1);
     87  1.1  mrg   check ();
     88  1.1  mrg 
     89  1.1  mrg   refmpn_zero (x, SIZE);
     90  1.1  mrg   x[0] = CNST_LIMB(1) << (GMP_NUMB_BITS-1);
     91  1.1  mrg   x[1] = CNST_LIMB(1);
     92  1.1  mrg   check ();
     93  1.1  mrg 
     94  1.1  mrg   refmpn_zero (x, SIZE);
     95  1.1  mrg   x[1] = TWOBITS (1, 0);
     96  1.1  mrg   check ();
     97  1.1  mrg 
     98  1.1  mrg   refmpn_zero (x, SIZE);
     99  1.1  mrg   x[1] = CNST_LIMB(1);
    100  1.1  mrg   x[2] = CNST_LIMB(1);
    101  1.1  mrg   check ();
    102  1.1  mrg }
    103  1.1  mrg 
    104  1.1  mrg /* This is unused, it takes too long, especially on 64-bit systems. */
    105  1.1  mrg void
    106  1.1  mrg check_twobits_exhaustive (void)
    107  1.1  mrg {
    108  1.1  mrg   unsigned long  i, j;
    109  1.1  mrg 
    110  1.1  mrg   for (i = 0; i < GMP_NUMB_BITS * SIZE; i++)
    111  1.1  mrg     {
    112  1.1  mrg       for (j = 0; j < GMP_NUMB_BITS * SIZE; j++)
    113  1.1  mrg         {
    114  1.1  mrg           refmpn_zero (x, SIZE);
    115  1.1  mrg           refmpn_setbit (x, i);
    116  1.1  mrg           refmpn_setbit (x, j);
    117  1.1  mrg           check ();
    118  1.1  mrg         }
    119  1.1  mrg     }
    120  1.1  mrg }
    121  1.1  mrg 
    122  1.1  mrg void
    123  1.1  mrg check_rand (void)
    124  1.1  mrg {
    125  1.1  mrg   int  i;
    126  1.1  mrg 
    127  1.1  mrg   for (i = 0; i < 100; i++)
    128  1.1  mrg     {
    129  1.1  mrg       refmpn_random2 (x, SIZE);
    130  1.1  mrg       check ();
    131  1.1  mrg     }
    132  1.1  mrg }
    133  1.1  mrg 
    134  1.1  mrg int
    135  1.1  mrg main (void)
    136  1.1  mrg {
    137  1.1  mrg   mp_trace_base = -16;
    138  1.1  mrg   tests_start ();
    139  1.1  mrg 
    140  1.1  mrg   check_twobits ();
    141  1.1  mrg   check_rand ();
    142  1.1  mrg 
    143  1.1  mrg   tests_end ();
    144  1.1  mrg   exit (0);
    145  1.1  mrg }
    146