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