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