1/* Test mpz_hamdist. 2 3Copyright 2001, 2002 Free Software Foundation, Inc. 4 5This file is part of the GNU MP Library test suite. 6 7The GNU MP Library test suite is free software; you can redistribute it 8and/or modify it under the terms of the GNU General Public License as 9published by the Free Software Foundation; either version 3 of the License, 10or (at your option) any later version. 11 12The GNU MP Library test suite is distributed in the hope that it will be 13useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 15Public License for more details. 16 17You should have received a copy of the GNU General Public License along with 18the GNU MP Library test suite. If not, see https://www.gnu.org/licenses/. */ 19 20#include <stdio.h> 21#include <stdlib.h> 22#include "gmp-impl.h" 23#include "tests.h" 24 25 26void 27check_twobits (void) 28{ 29 unsigned long i, j, got, want; 30 mpz_t x, y; 31 32 mpz_init (x); 33 mpz_init (y); 34 for (i = 0; i < 5 * GMP_NUMB_BITS; i++) 35 { 36 for (j = 0; j < 5 * GMP_NUMB_BITS; j++) 37 { 38 mpz_set_ui (x, 0L); 39 mpz_setbit (x, i); 40 mpz_set_ui (y, 0L); 41 mpz_setbit (y, j); 42 43 want = 2 * (i != j); 44 got = mpz_hamdist (x, y); 45 if (got != want) 46 { 47 printf ("mpz_hamdist wrong on 2 bits pos/pos\n"); 48 wrong: 49 printf (" i %lu\n", i); 50 printf (" j %lu\n", j); 51 printf (" got %lu\n", got); 52 printf (" want %lu\n", want); 53 mpz_trace (" x ", x); 54 mpz_trace (" y ", y); 55 abort(); 56 } 57 58 mpz_neg (x, x); 59 mpz_neg (y, y); 60 want = ABS ((long) (i-j)); 61 got = mpz_hamdist (x, y); 62 if (got != want) 63 { 64 printf ("mpz_hamdist wrong on 2 bits neg/neg\n"); 65 goto wrong; 66 } 67 } 68 69 } 70 mpz_clear (x); 71 mpz_clear (y); 72} 73 74 75void 76check_rand (void) 77{ 78 gmp_randstate_ptr rands = RANDS; 79 unsigned long got, want; 80 int i; 81 mpz_t x, y; 82 83 mpz_init (x); 84 mpz_init (y); 85 86 for (i = 0; i < 2000; i++) 87 { 88 mpz_erandomb (x, rands, 6 * GMP_NUMB_BITS); 89 mpz_negrandom (x, rands); 90 mpz_mul_2exp (x, x, urandom() % (4 * GMP_NUMB_BITS)); 91 92 mpz_erandomb (y, rands, 6 * GMP_NUMB_BITS); 93 mpz_negrandom (y, rands); 94 mpz_mul_2exp (y, y, urandom() % (4 * GMP_NUMB_BITS)); 95 96 want = refmpz_hamdist (x, y); 97 got = mpz_hamdist (x, y); 98 if (got != want) 99 { 100 printf ("mpz_hamdist wrong on random\n"); 101 printf (" got %lu\n", got); 102 printf (" want %lu\n", want); 103 mpz_trace (" x ", x); 104 mpz_trace (" y ", y); 105 abort(); 106 } 107 } 108 mpz_clear (x); 109 mpz_clear (y); 110} 111 112int 113main (void) 114{ 115 tests_start (); 116 mp_trace_base = -16; 117 118 check_twobits (); 119 check_rand (); 120 121 tests_end (); 122 exit (0); 123} 124