Statistiques
| Révision :

root / pobysoC-4.0 / src / test-pobyso-evaluate-constant.c @ 143

Historique | Voir | Annoter | Télécharger (3,93 ko)

1 142 storres
/** @file test-pobyso-evaluate-constant.c
2 142 storres
 * Name & purpose
3 142 storres
 *
4 142 storres
 * Two arguments are expected : an functional expression and a
5 142 storres
 * constant expression. The functional expression is evaluated at the value
6 142 storres
 * of the constant expression.
7 142 storres
 * If only the functional expression is literally "NULL" the
8 142 storres
 * pobyso_evaluate_constant is tested with a NULL argument as for the function.
9 142 storres
 *
10 142 storres
 * The second argument is not looked for.
11 142 storres
 *
12 142 storres
 * @author S. T.
13 142 storres
 * @date 2014-12-05
14 142 storres
 *
15 142 storres
 * @details Verbosity manipulation are performed  to make sur  that verbosity
16 142 storres
 *  suppression is correctly managed in pobyso_is_constant_expression()
17 142 storres
 *  function.
18 142 storres
 */
19 142 storres
/******************************************************************************/
20 142 storres
/* Headers, applying the "particular to general" convention.*/
21 142 storres
22 142 storres
#include "pobyso.h"
23 142 storres
24 142 storres
/* includes of local headers */
25 142 storres
26 142 storres
/* includes of project headers */
27 142 storres
28 142 storres
/* includes of system headers */
29 142 storres
30 142 storres
/* Other declarations */
31 142 storres
32 142 storres
/* Internal prototypes */
33 142 storres
34 142 storres
/* Types, constants and macros definitions */
35 142 storres
36 142 storres
/* Global variables */
37 142 storres
38 142 storres
/* Functions */
39 142 storres
40 142 storres
int
41 142 storres
main(int argc, char** argv)
42 142 storres
{
43 142 storres
  pobyso_func_exp_t funcExpSo  = NULL;
44 142 storres
  mpfr_t evaluationMp;
45 142 storres
  mpfr_t argumentMp;
46 142 storres
  sollya_obj_t argumentSo = NULL;
47 142 storres
  int outcome = 1;
48 142 storres
49 142 storres
  sollya_lib_init();
50 142 storres
51 142 storres
  /* Two command line arguments expected. */
52 142 storres
  if (argc < 3)
53 142 storres
  {
54 142 storres
    fprintf(stderr,"\n\n");
55 142 storres
    fprintf(stderr, "Usage: %s funcExp constExpArg\n", argv[0]);
56 142 storres
    fprintf(stderr,
57 142 storres
            "            funcExp: a quoted functional expression for Sollya;\n");
58 142 storres
    fprintf(stderr,
59 142 storres
            "            constExpArg: a (possibly quoted) Sollya constant expression for the argument.");
60 142 storres
    fprintf(stderr,"\n\n");
61 142 storres
    sollya_lib_close();
62 142 storres
    return 1;
63 142 storres
  }
64 142 storres
65 142 storres
  if (! strcmp(argv[1], "NULL")) /* First parameter is the "NULL" string) */
66 142 storres
  {
67 142 storres
    outcome = pobyso_evaluate_constant(NULL, argumentMp, evaluationMp);
68 142 storres
    sollya_lib_close();
69 142 storres
    return outcome;
70 142 storres
  }
71 142 storres
  pobyso_set_canonical_on();
72 142 storres
  argumentSo = pobyso_parse_string(argv[2]);
73 142 storres
  if (argumentSo == NULL)
74 142 storres
  {
75 142 storres
    fprintf(stderr,
76 142 storres
        "\n\n%s: can't parse \"%s\" to a valid expression. Aborting!\n\n",
77 142 storres
        argv[0], argv[2]);
78 142 storres
    sollya_lib_close();
79 142 storres
    return 1;
80 142 storres
  }
81 142 storres
  if (! pobyso_is_constant_expression(argumentSo))
82 142 storres
  {
83 142 storres
    fprintf(stderr,
84 142 storres
        "\n\n%s: can't evaluate \"%s\" to a constant expression. Aborting!\n\n",
85 142 storres
        argv[0], argv[2]);
86 142 storres
    sollya_lib_clear_obj(argumentSo);
87 142 storres
    sollya_lib_close();
88 142 storres
    return 1;
89 142 storres
  }
90 142 storres
  mpfr_init2(argumentMp, 512);
91 142 storres
  if (! sollya_lib_get_constant(argumentMp, argumentSo))
92 142 storres
  {
93 142 storres
    fprintf(stderr,
94 142 storres
        "\n\n%s: can't recover the value of \"%s\". Aborting!\n\n",
95 142 storres
        argv[0], argv[2]);
96 142 storres
    mpfr_clear(argumentMp);
97 142 storres
    sollya_lib_clear_obj(argumentSo);
98 142 storres
    sollya_lib_close();
99 142 storres
    return 1;
100 142 storres
  }
101 142 storres
  if (argc > 3)
102 142 storres
  {
103 142 storres
    fprintf(stdout, "argument: ");
104 142 storres
    mpfr_out_str(stdout, 10, 17, argumentMp, MPFR_RNDN);
105 142 storres
    fprintf(stdout, "\n");
106 142 storres
  }
107 142 storres
  /* Not needed any more. */
108 142 storres
  sollya_lib_clear_obj(argumentSo);
109 142 storres
  funcExpSo = pobyso_parse_string(argv[1]);
110 142 storres
  if (funcExpSo == NULL)
111 142 storres
  {
112 142 storres
    fprintf(stderr,
113 142 storres
        "\n\n%s: could not parse \"%s\" as a valid Sollya expression. Aborting!\n\n",
114 142 storres
        argv[0], argv[1]);
115 142 storres
    mpfr_clear(argumentMp);
116 142 storres
    sollya_lib_close();
117 142 storres
    return 1;
118 142 storres
  }
119 142 storres
  if (argc > 3)
120 142 storres
  {
121 142 storres
    fprintf(stdout, "Functional expression: ");
122 142 storres
    pobyso_autoprint(funcExpSo);
123 142 storres
  }
124 142 storres
  /* Precision does not matter: the callee adjusts it as needed. */
125 142 storres
  mpfr_init(evaluationMp);
126 142 storres
  outcome = pobyso_evaluate_constant(funcExpSo, argumentMp, evaluationMp);
127 142 storres
  if (outcome)
128 142 storres
  {
129 142 storres
    switch(outcome)
130 142 storres
    {
131 142 storres
      case POBYSO_NAN:
132 142 storres
        fprintf(stderr, "%s: evaluated to NaN.\n", argv[0]); break;
133 142 storres
      case POBYSO_UNFAITHFUL:
134 142 storres
        fprintf(stderr, "%s: result is unfaithful.\n", argv[0]); break;
135 142 storres
    } /* End switch. */
136 142 storres
  }
137 142 storres
  fprintf(stdout, "Evaluation: ");
138 142 storres
  mpfr_out_str(stdout, 10, 17, evaluationMp, MPFR_RNDN);
139 142 storres
  fprintf(stdout, "\n");
140 142 storres
  sollya_lib_clear_obj(funcExpSo);
141 142 storres
  mpfr_clear(argumentMp);
142 142 storres
  mpfr_clear(evaluationMp);
143 142 storres
  sollya_lib_close();
144 142 storres
  return outcome;
145 142 storres
} /* End main */