Révision 137

pobysoC-4.0/src/test-pobyso-subpoly.c (revision 137)
22 22
/* Other declarations */
23 23

  
24 24
/* Internal prototypes */
25
long int
25
int
26 26
read_long_decimal(long int* value, char* string);
27
long int
28
read_unsigned_long_decimal(long int* value, char* string);
27
int
28
read_unsigned_long_decimal(long unsigned int* value, char* string);
29 29

  
30 30
/* Types, constants and macros definitions */
31 31

  
32 32
/* Global variables */
33 33

  
34 34
/* Functions */
35
long int
35
int
36 36
read_long_decimal(long int* returnValue, char* string)
37 37
{
38 38
  char* endptr[1];
......
49 49
    *returnValue = convertedValue;
50 50
    return 0;
51 51
  }
52
  return 0;
53 52
} /* End read_long_decimal. */
54 53

  
55
long int
56
read_unsigned_long_decimal(long int* returnValue, char* string)
54
int
55
read_unsigned_long_decimal(long unsigned int* returnValue, char* string)
57 56
{
58 57
  char* endptr[1];
59 58
  long int convertedValue = 0;
pobysoC-4.0/src/test-pobyso-is-int.c (revision 137)
1
/** @file test-pobyso-is-int.c
2
 * Name & purpose
3
 *
4
 * @author
5
 * @date
6
 *
7
 * @details Verbosity manipulation are performed  to make sur  that verbosity
8
 *  suppression is correctly managed in pobyso_is_constant_expression()
9
 *  function.
10
 */
11
/******************************************************************************/
12
/* Headers, applying the "particular to general" convention.*/
13

  
14
#include "pobyso.h"
15

  
16
/* includes of local headers */
17

  
18
/* includes of project headers */
19

  
20
/* includes of system headers */
21
#include <stdlib.h>
22
/* Other declarations */
23

  
24
/* Internal prototypes */
25
int
26
read_long_decimal(long int* value, char* string);
27
int
28
read_unsigned_long_decimal(long unsigned int* value, char* string);
29

  
30
/* Types, constants and macros definitions */
31

  
32
/* Global variables */
33

  
34
/* Functions */
35
int
36
read_long_decimal(long int* returnValue, char* string)
37
{
38
  char* endptr[1];
39
  long int convertedValue = 0;
40

  
41
  convertedValue = strtol(string, endptr, 10);
42
  /* For a completely safe conversion *endptr must point to 0 value char. */
43
  if (**endptr != '\0')
44
  {
45
    return 1;
46
  }
47
  else
48
  {
49
    *returnValue = convertedValue;
50
    return 0;
51
  }
52
} /* End read_long_decimal. */
53

  
54
int
55
read_unsigned_long_decimal(long unsigned int* returnValue, char* string)
56
{
57
  char* endptr[1];
58
  long int convertedValue = 0;
59

  
60
  /* Negative sign -> failure. */
61
  if (*string == '-')
62
  {
63
    return 1;
64
  }
65
  convertedValue = strtoul(string, endptr, 10);
66
  /* For a completely safe conversion *endptr must point to 0 value char. */
67
  if (**endptr != '\0')
68
  {
69
    return 1;
70
  }
71
  else
72
  {
73
    *returnValue = convertedValue;
74
    return 0;
75
  }
76
} /* End read_unsigned_long_decimal. */
77

  
78
int
79
main(int argc, char** argv)
80
{
81
  pobyso_func_exp_t expressionSo = NULL;
82

  
83
  if (argc < 2)
84
  {
85
    fprintf(stderr,
86
            "Usage: %s quotedExpression \n",
87
            argv[0]);
88
    return 1;
89
  }
90

  
91
  pobyso_set_canonical_on();
92
  expressionSo = pobyso_parse_string(argv[1]);
93
  if (expressionSo != NULL)
94
  {
95
    fprintf(stdout, "Sollya expression: ");
96
    pobyso_autoprint(expressionSo);
97
  }
98
  else
99
  {
100
    fprintf(stdout, "NULL expression");
101
  }
102
  if (pobyso_is_int(expressionSo))
103
  {
104
    return 0;
105
  }
106
  else
107
  {
108
    return 1;
109
  }
110
} /* End main */
0 111

  
pobysoC-4.0/src/pobyso.c (revision 137)
1 1
/** @file pobyso.c
2
 * Name & purpose
2
 * Integration of Sollya to C programs
3 3
 *
4 4
 * @author S.T.
5 5
 * @date 2011-10-12
6 6
 *
7
 *
7
 * @todo write pobyso_is_monomial function <br>
8
 *       write pobyso_is_free_var_int_poson_power function
8 9
 */
9

  
10 10
/******************************************************************************/
11 11
/* Headers, applying the "particular to general" convention.*/
12 12

  
......
34 34

  
35 35
/* @see pobyso.h#pobyso_autoprint */
36 36
void
37
pobyso_autoprint(sollya_obj_t solObj)
37
pobyso_autoprint(sollya_obj_t objSo)
38 38
{
39
  sollya_lib_autoprint(solObj, NULL);
39
  sollya_lib_autoprint(objSo, NULL);
40 40
} /* End pobyso_autoprint. */
41 41

  
42 42
/* @see pobyso.h#pobyso_get_verbosity */
......
52 52
  return verbosity;
53 53
} /* End pobyso_get_verbosity. */
54 54

  
55
/* @see pobyso.h#pobyso_is_constant_expression*/
55
/** @see pobyso.h#pobyso_is_constant_expression
56
 * Strategy: rely on sollya_lib_get_constant. It return 1, when the
57
 * expression is constant.
58
 */
56 59
int
57 60
pobyso_is_constant_expression(sollya_obj_t obj_to_test)
58 61
{
......
63 66
  /* Test argument. */
64 67
  if (obj_to_test == NULL)
65 68
  {
66
    pobyso_error_message("pobyso_parse_string",
69
    pobyso_error_message("pobyso_is_constant_expression",
67 70
                        "NULL_POINTER_ARGUMENT",
68 71
                        "The expression is a NULL pointer");
69 72
    return 0;
......
77 80
  }
78 81
  mpfr_init2(dummy,64);
79 82
  /* Call to previous Sollya function resets verbosity. */
83
  /* Todo: change verbosity suppression strategy with a message call back. */
80 84
  pobyso_set_verbosity_off();
81 85
  test = sollya_lib_get_constant(dummy, obj_to_test);
82 86
  mpfr_clear(dummy);
......
84 88
  return test;
85 89
} /* End pobyso_is_constant_expression. */
86 90

  
91
/** @see pobyso.h#pobyso_is_free_var_int_poson_power. */
92
int
93
pobyso_is_free_var_posze_int_power(sollya_obj_t objToTestSo)
94
{
95
  int arity;
96
  sollya_base_function_t headTypeSo;
97
  sollya_obj_t sub1So           = NULL;
98
  sollya_obj_t sub2So           = NULL;
99

  
100
  /* Argument check. */
101
  if (objToTestSo == NULL)
102
  {
103
    pobyso_error_message("pobyso_is_free_var_int_poson_power",
104
                        "NULL_POINTER_ARGUMENT",
105
                        "The expression is a NULL pointer");
106
    return 0;
107
  }
108
  if (! sollya_lib_obj_is_function(objToTestSo))
109
  {
110
    return 0;
111
  }
112
  if (!sollya_lib_get_head_function(&headTypeSo, objToTestSo)) return 0;
113
  if (! sollya_lib_get_subfunctions(objToTestSo,
114
                                    &arity,
115
                                    &sub1So,
116
                                    &sub2So,
117
                                    NULL)) return 0;
118
  /* Power function: arity == 2. */
119
  if (arity != 2) return 0;
120
  if (! pobyso_is_constant_expression(sub1So)) return 0;
121
  return 1;
122
} /* End pobyso_is_free_var_posze_int_power. */
123

  
124

  
125
/** @see pobyso.h#pobyso_is_monomial. */
126
int
127
pobyso_is_int(pobyso_func_exp_t exprSo)
128
{
129
  mpfr_t float1M;
130
  mpfr_t float2M;
131
  mpfr_t tempFloat1M;
132
  mpfr_t tempFloat2M;
133
  mpfr_prec_t prec;
134
  int64_t asInt;
135
  sollya_obj_t newConstantSo = NULL;
136
  /* Arguments check. */
137
  if (exprSo == NULL)
138
  {
139
    pobyso_error_message("pobyso_is_free_var_posze_int_power",
140
                        "NULL_POINTER_ARGUMENT",
141
                        "The expression is a NULL pointer");
142
    return 0;
143
  }
144
  //fprintf(stdout, "Not NULL.\n"); pobyso_autoprint(exprSo);
145
  if (! pobyso_is_constant_expression(exprSo)) return 0;
146
  if (! sollya_lib_get_constant_as_int64(&asInt, exprSo)) return 0;
147
  if (asInt == INT64_MIN || asInt == INT64_MAX) return 0;
148
  /* Some constant integer expression can't have their precision computed
149
   * (e.g. cos(pi). */
150
  if (! sollya_lib_get_prec_of_constant(&prec, exprSo))
151
  {
152
    mpfr_init2(tempFloat1M, 165);
153
    mpfr_init2(tempFloat2M, 165);
154
    mpfr_abs(tempFloat1M, tempFloat1M, MPFR_RNDN);
155
    mpfr_log2(tempFloat2M, tempFloat1M, MPFR_RNDU);
156
    mpfr_rint_ceil(tempFloat1M, tempFloat2M, MPFR_RNDU);
157
    prec = mpfr_get_si(tempFloat1M, MPFR_RNDN) + 10;
158
    if (prec < 1024) prec = 1024;
159
    mpfr_clear(tempFloat1M);
160
    mpfr_clear(tempFloat2M);
161
    mpfr_init2(float1M, prec);
162
    if (!sollya_lib_get_constant(float1M, exprSo))
163
    {
164
      mpfr_clear(float1M);
165
      return 0;
166
    }
167
  }
168
  else /* Precision could be given. */
169
  {
170
    mpfr_init2(float1M, prec);
171
    if (! sollya_lib_get_constant(float1M, exprSo))
172
    {
173
      mpfr_clear(float1M);
174
      return 0;
175
    }
176
  }
177
  if (mpfr_nan_p(float1M) || mpfr_inf_p(float1M))
178
  {
179
    mpfr_clear(float1M);
180
    return 0;
181
  }
182
  if ((newConstantSo = sollya_lib_constant_from_int64(asInt)) == NULL)
183
  {
184
    mpfr_clear(float1M);
185
    return 0;
186
  }
187
  sollya_lib_get_prec_of_constant(&prec, newConstantSo);
188
  mpfr_init2(float2M, prec);
189
  sollya_lib_get_constant(float2M, newConstantSo);
190
  if (mpfr_cmp(float1M, float2M) == 0)
191
  {
192
    mpfr_clear(float1M);
193
    mpfr_clear(float2M);
194
    sollya_lib_clear_obj(newConstantSo);
195
    return 1;
196
  }
197
  else
198
  {
199
    pobyso_autoprint(exprSo);
200
    pobyso_autoprint(newConstantSo);
201
    mpfr_clear(float1M);
202
    mpfr_clear(float2M);
203
    sollya_lib_clear_obj(newConstantSo);
204
    return 0;
205
  }
206
} /* End pobyso_is_int. */
207

  
208
/** @see pobyso.h#pobyso_is_monomial.
209
 * Strategy: check that the object is a functional expression and
210
 * if so check that it is made of cte * free_var ^ some_power where :
211
 * - cte is a constant expression (a per pobyso_is_constant_experession;
212
 * - some_power is a positive or null power. t*/
213
int
214
pobyso_is_monomial(sollya_obj_t objSo)
215
{
216
  return 0;
217
} /* End pobyso_is_monomial. */
218

  
87 219
/** @see pobyso.h#pobyso_new_monomial. */
88 220
pobyso_func_exp_t
89 221
pobyso_new_monomial(pobyso_func_exp_t coefficientSo, long degree)
pobysoC-4.0/src/test-pobyso-subpoly.sh (revision 137)
4 4
TEST_BIN=./test-pobyso-subpoly
5 5

  
6 6
echo
7

  
8
bla() {
7
## Functions
8
ok_if_fail() {
9 9
  echo "$@"
10 10
  eval "$TEST_BIN $ARGUMENTS"
11
if [ $? -ne 0 ] ; then echo "Failed for $ARGUMENTS" ; exit 1 ; fi
11
if [ $? -eq 0 ] ; then 
12
  echo "Succeeded (but should not have) for $ARGUMENTS"
13
  exit 1
14
fi
15
echo "Correctly failing$WHY."
12 16
echo
13 17
}
14 18
##
19
ok_if_ok() {
20
  echo "$@"
21
  eval "$TEST_BIN $ARGUMENTS"
22
if [ $? -ne 0 ] ; then
23
  echo "Failed for $ARGUMENTS" 
24
  exit 1 
25
fi
26
echo
27
}
28
##
15 29
ARGUMENTS="\"4*x^2\" 2"
16
bla "Testing: $ARGUMENTS"
30
ok_if_ok "Testing: $ARGUMENTS"
17 31

  
18 32
##
19 33
ARGUMENTS="\"4-2*x+4*x^2\" 0 2"
20
bla "Testing: $ARGUMENTS"
34
ok_if_ok "Testing: $ARGUMENTS"
21 35

  
22 36
##
23 37
ARGUMENTS="\"4-2*x+4*x^2\" 0 -1 2"
24
bla "Testing: $ARGUMENTS"
38
WHY=" (because -1 in exponents list)"
39
ok_if_fail "Testing: $ARGUMENTS"
25 40

  
26 41
##
27 42
echo "Tests terminated without error."
pobysoC-4.0/src/Makefile (revision 137)
14 14

  
15 15
ALL_TARGETS = test-pobyso-01 test-pobyso-02 \
16 16
              test-pobyso-constant-expression \
17
              test-pobyso-is-int \
17 18
              test-pobyso-new-monomial \
18 19
              test-pobyso-subpoly
19 20

  
pobysoC-4.0/src/test-pobyso-constant-expression.c (revision 137)
85 85
  func_exp = pobyso_parse_string("1/2");
86 86
  if (pobyso_is_constant_expression(func_exp))
87 87
  {
88
    pobyso_set_verbosity_off();
88 89
    pobyso_autoprint(func_exp);
89 90
    fprintf(stdout, "OK for \"1/2\".\n");
90 91
    sollya_lib_clear_obj(func_exp);
......
127 128
  if (! pobyso_is_constant_expression(func_exp))
128 129
  {
129 130
    pobyso_autoprint(func_exp);
130
    fprintf(stdout, "Non constant expression \"cos(x)\": OK\n");
131
    fprintf(stdout, "OK for non constant expression \"cos(x)\".\n");
131 132
    sollya_lib_clear_obj(func_exp);
132 133
  }
133 134
  else
......
139 140
  verbosity = pobyso_set_verbosity_off();
140 141
  func_exp = pobyso_parse_string("cos(pi)");
141 142
  pobyso_set_verbosity_to(verbosity);
143
  if (pobyso_is_constant_expression(func_exp))
142 144
  {
145
    pobyso_set_verbosity_off();
143 146
    pobyso_autoprint(func_exp);
144 147
    fprintf(stdout, "OK for \"cos(pi)\".\n");
145 148
    sollya_lib_clear_obj(func_exp);
146 149
  }
150
  else
151
  {
152
    fprintf(stdout, "Error for \"cos(pi)\".\n");
153
    return 1;
154
  }
147 155
  /*  */
148 156

  
149 157
  sollya_lib_close();
pobysoC-4.0/src/pobyso.h (revision 137)
2 2
 * Integration of Sollya to C programs
3 3
 * @author S.T.
4 4
 * @date 2011-10-11
5
 * Note: pobyso stands for POwered BY SOllya.
6
 *                         --      -- --
5
 * @note pobyso stands for POwered BY SOllya.
6
 * @todo                        --      -- --
7 7
 */
8 8
/******************************************************************************/
9 9
 
......
55 55
 * A very thin wrapper around the lib_sollya_autoprint() function.
56 56
 */
57 57
void
58
pobyso_autoprint(sollya_obj_t soObj);
58
pobyso_autoprint(sollya_obj_t objSo);
59 59

  
60 60
/**
61 61
 * Print object(s) to stdout: the va_list companion function.
......
64 64
 */
65 65

  
66 66
void
67
pobyso_autoprint_v(sollya_obj_t soObj, va_list va);
67
pobyso_autoprint_v(va_list va);
68 68

  
69 69
/**
70 70
 * Get the current verbosity level.
......
80 80
int
81 81
pobyso_is_constant_expression(sollya_obj_t obj_to_text);
82 82

  
83
int
84
pobyso_is_free_var_posze_int_power(sollya_obj_t objToTestSo);
85

  
83 86
/**
87
 * Check if an expression is a monomial (made of a constant expression
88
 * and the free variable to an integer positive or null power.
89
 * @param exprSo: a Sollya functional expression object;
90
 * @return 1 if exprSo is a monomial (as defined above), 0 otherwise.
91
 */
92
int pobyso_is_monomial(pobyso_func_exp_t exprSo);
93

  
94
/**
95
 * Check if an expression is an integer.
96
 */
97
int
98
pobyso_is_int(pobyso_func_exp_t exprSo);
99

  
100
/**
84 101
 * Create a Sollya monomial from a Sollya constant,
85 102
 * the coefficient, and an integer, the exponent.
86 103
 * @param coefficient must be a non NULL constant expression;
pobysoC-4.0/src/test-pobyso-is-int.sh (revision 137)
1
#! /bin/sh
2
#
3
#
4
TEST_BIN=./test-pobyso-is-int
5

  
6
echo
7
## Functions
8
ok_if_fail() {
9
  echo "$@"
10
  eval "$TEST_BIN $ARGUMENTS"
11
if [ $? -eq 0 ] ; then 
12
  echo "Succeeded (but should not have) for $ARGUMENTS"
13
  exit 1
14
fi
15
echo "Correctly failing$WHY."
16
echo
17
}
18
##
19
ok_if_ok() {
20
  echo "$@"
21
  eval "$TEST_BIN $ARGUMENTS"
22
if [ $? -ne 0 ] ; then
23
  echo "Failed for $ARGUMENTS" 
24
  exit 1 
25
fi
26
echo
27
}
28
##
29
ARGUMENTS="\"2\""
30
ok_if_ok "Testing: $ARGUMENTS"
31

  
32
ARGUMENTS="\"(2^63)-2\""
33
ok_if_ok "Testing: $ARGUMENTS"
34

  
35
ARGUMENTS="\"-2\""
36
ok_if_ok "Testing: $ARGUMENTS"
37

  
38
ARGUMENTS="\"-(2^63)+1\""
39
ok_if_ok "Testing: $ARGUMENTS"
40

  
41
ARGUMENTS="\"cos(pi)\""
42
#WHY=" ($ARGUMENTS is not an integer)"
43
ok_if_ok "Testing: $ARGUMENTS"
44

  
45
ARGUMENTS="\"sin(pi)\""
46
#WHY=" ($ARGUMENTS is not an integer)"
47
ok_if_ok "Testing: $ARGUMENTS"
48

  
49
##
50
ARGUMENTS="\"1.1\""
51
WHY=" ($ARGUMENTS is not an integer)"
52
ok_if_fail "Testing: $ARGUMENTS"
53

  
54
ARGUMENTS="\"-1.1\""
55
WHY=" ($ARGUMENTS is not an integer)"
56
ok_if_fail "Testing: $ARGUMENTS"
57

  
58
ARGUMENTS="\"pi\""
59
WHY=" (pi is not an integer)"
60
ok_if_fail "Testing: $ARGUMENTS"
61

  
62
##
63
ARGUMENTS="\"4-2*x+4*x^2\""
64
WHY=" (expression not int)"
65
ok_if_fail "Testing: $ARGUMENTS"
66

  
67
##
68
ARGUMENTS="\"yx+\""
69
WHY=" ($ARGUMENTS not expression)"
70
ok_if_fail "Testing: $ARGUMENTS"
71

  
72
##
73
ARGUMENTS="\"1/0\""
74
WHY=" ($ARGUMENTS not a number)"
75
ok_if_fail "Testing: $ARGUMENTS"
76

  
77
##
78
ARGUMENTS="\"exp(1000000)\""
79
WHY=" ($ARGUMENTS is too large)"
80
ok_if_fail "Testing: $ARGUMENTS"
81

  
82
ARGUMENTS="\"-exp(1000000)\""
83
WHY=" ($ARGUMENTS is too small)"
84
ok_if_fail "Testing: $ARGUMENTS"
85

  
86
ARGUMENTS="\"+infty\""
87
WHY=" ($ARGUMENTS is infinity)"
88
ok_if_fail "Testing: $ARGUMENTS"
89

  
90
ARGUMENTS="\"infty\""
91
WHY=" ($ARGUMENTS is infinity)"
92
ok_if_fail "Testing: $ARGUMENTS"
93

  
94
ARGUMENTS="\"-infty\""
95
WHY=" ($ARGUMENTS is infinity)"
96
ok_if_fail "Testing: $ARGUMENTS"
97

  
98
##
99
echo "Tests terminated without error."
100
echo
0 101

  

Formats disponibles : Unified diff