1/* check_data.c -- Check computed data against reference result.
2
3Copyright (C) 2012, 2013, 2014, 2022 INRIA
4
5This file is part of GNU MPC.
6
7GNU MPC is free software; you can redistribute it and/or modify it under
8the terms of the GNU Lesser General Public License as published by the
9Free Software Foundation; either version 3 of the License, or (at your
10option) any later version.
11
12GNU MPC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15more details.
16
17You should have received a copy of the GNU Lesser General Public License
18along with this program. If not, see http://www.gnu.org/licenses/ .
19*/
20
21#include "mpc-tests.h"
22
23#define MPC_INEX_CMP(c, r, i)                                 \
24  (((r) == TERNARY_NOT_CHECKED || (r) == MPC_INEX_RE(c))      \
25   && ((i) == TERNARY_NOT_CHECKED || (i) == MPC_INEX_IM (c)))
26
27static int
28check_param  (mpc_operand_t* got, mpc_operand_t* expected, mpc_param_t t)
29{
30  switch (t)
31    {
32    case NATIVE_INT:
33      return got->i == expected->i;
34    case NATIVE_UL:
35      return got->ui == expected->ui;
36    case NATIVE_L:
37      return got->si == expected->si;
38    case NATIVE_D:
39      return got->d == expected->d;
40
41    case GMP_Z:
42      return mpz_cmp (got->mpz, expected->mpz);
43    case GMP_Q:
44      return mpq_cmp (got->mpq, expected->mpq);
45    case GMP_F:
46      return mpf_cmp (got->mpf, expected->mpf);
47
48    case MPFR_INEX:
49      return expected->mpfr_inex == TERNARY_NOT_CHECKED
50        || got->mpfr_inex == expected->mpfr_inex;
51
52    case MPFR:
53      return tpl_check_mpfr_data (got->mpfr,
54                                  expected->mpfr_data);
55
56    case MPC_INEX:
57      return MPC_INEX_CMP (got->mpc_inex,
58                           expected->mpc_inex_data.real,
59                           expected->mpc_inex_data.imag);
60
61    case MPC:
62      return tpl_check_mpc_data (got->mpc,
63                                 expected->mpc_data);
64
65    case MPCC_INEX:
66      return got->mpcc_inex == expected->mpcc_inex;
67
68    default:
69      fprintf (stderr, "check_data: unsupported type.\n");
70      exit (1);
71    }
72}
73
74void
75check_data (mpc_datafile_context_t* dc, mpc_fun_param_t* params, int reused_op)
76{
77  int out, i;
78  int total = params->nbout + params->nbin;
79
80  for (out = 0; out < params->nbout; out++)
81    {
82      if (!check_param (&(params->P[out]), &(params->P[total + out]),
83                        params->T[out]))
84        {
85          printf ("%s() failed", params->name);
86          if (dc != NULL)
87            printf (" (line %lu, file %s)",
88                    dc->test_line_number, dc->pathname);
89          else
90            printf (" with random parameter%c",
91                    params->nbin > 2 ? 's' : '\0');
92
93          if (reused_op != 0)
94            printf (" when reusing input parameter op%d as output parameter",
95                    reused_op - params->nbout);
96          printf ("\n");
97
98          for (i = 0; i < params->nbin; i++)
99            {
100              printf ("op%d", i + 1);
101              print_parameter (params, params->nbout + i);
102            }
103
104          for (i = 0; i < params->nbout; i++)
105            {
106              if ((params->T[i] == MPFR_INEX && params->T[out] != MPFR_INEX)
107                  || (params->T[i] == MPC_INEX && params->T[out] != MPC_INEX))
108                continue; /* don't print inexact flag if it is correct */
109
110              printf ("     got%c",
111                      (total + i > params->nbout ? ' ' : i + '0'));
112              print_parameter (params, i);
113              printf ("expected%c",
114                      (total + i > params->nbout ? ' ' : i + '0'));
115              print_parameter (params, total + i);
116            }
117          printf ("\n");
118          exit (1);
119        }
120    }
121}
122