0d3a0daaede3c495fd767e42e53260d02b487bf4
[platform/upstream/gmp.git] / mini-gmp / tests / t-add.c
1 /*
2
3 Copyright 2012, 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 http://www.gnu.org/licenses/.  */
19
20 #include <stdlib.h>
21 #include <stdio.h>
22
23 #include "testutils.h"
24
25 #define MAXBITS 400
26 #define COUNT 10000
27
28 static void
29 dump (const char *label, const mpz_t x)
30 {
31   char *buf = mpz_get_str (NULL, 16, x);
32   fprintf (stderr, "%s: %s\n", label, buf);
33   testfree (buf);
34 }
35
36 void
37 testmain (int argc, char **argv)
38 {
39   unsigned i;
40   mpz_t a, b, res, ref;
41
42   mpz_init (a);
43   mpz_init (b);
44   mpz_init (res);
45   mpz_init (ref);
46
47   for (i = 0; i < COUNT; i++)
48     {
49       mini_random_op3 (OP_ADD, MAXBITS, a, b, ref);
50       mpz_add (res, a, b);
51       if (mpz_cmp (res, ref))
52         {
53           fprintf (stderr, "mpz_add failed:\n");
54           dump ("a", a);
55           dump ("b", b);
56           dump ("r", res);
57           dump ("ref", ref);
58           abort ();
59         }
60     }
61   mpz_clear (a);
62   mpz_clear (b);
63   mpz_clear (res);
64   mpz_clear (ref);
65 }