Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

563885 views
1
/* Test expression evaluation (print nothing and exit 0 if successful).
2
3
Copyright 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4
5
This file is part of the GNU MP Library.
6
7
The GNU MP Library is free software; you can redistribute it and/or modify
8
it under the terms of the GNU Lesser General Public License as published by
9
the Free Software Foundation; either version 2.1 of the License, or (at your
10
option) any later version.
11
12
The GNU MP Library is distributed in the hope that it will be useful, but
13
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15
License for more details.
16
17
You should have received a copy of the GNU Lesser General Public License
18
along with the GNU MP Library; see the file COPYING.LIB. If not, write to
19
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
MA 02110-1301, USA. */
21
22
#include <stdio.h>
23
#include <stdlib.h>
24
25
#include "gmp.h"
26
#include "tests.h"
27
#include "expr-impl.h"
28
29
30
int option_trace = 0;
31
32
33
struct data_t {
34
int base;
35
const char *expr;
36
const char *want;
37
};
38
39
#define numberof(x) (sizeof (x) / sizeof ((x)[0]))
40
41
42
/* These data_xxx[] arrays are tables to be tested with one or more of the
43
mp?_t types. z=mpz_t, q=mpz_t, f=mpf_t. */
44
45
struct data_t data_zqf[] = {
46
47
/* various deliberately wrong expressions */
48
{ 0, "", NULL },
49
{ 0, "1+", NULL },
50
{ 0, "+2", NULL },
51
{ 0, "1,2", NULL },
52
{ 0, "foo(1,2)", NULL },
53
{ 0, "1+foo", NULL },
54
{ 10, "0fff", NULL },
55
{ 0, "!", NULL },
56
{ 0, "10!", NULL },
57
{ 0, "-10!", NULL },
58
{ 0, "gcd((4,6))", NULL },
59
{ 0, "()", NULL },
60
{ 0, "fac(2**1000)", NULL },
61
{ 0, "$", NULL },
62
{ 0, "$-", NULL },
63
64
/* some basics */
65
{ 10, "123", "123" },
66
{ 10, "-123", "-123" },
67
{ 10, "1+2", "3" },
68
{ 10, "1+2+3", "6" },
69
{ 10, "1+2*3", "7" },
70
{ 10, "3*2+1", "7" },
71
{ 10, "$a", "55" },
72
{ 10, "b", "99" },
73
{ 16, "b", "11" },
74
{ 10, "4**3 * 2 + 1", "129" },
75
{ 10, "1<2", "1" },
76
{ 10, "1>2", "0" },
77
78
{ 10, "(123)", "123" },
79
80
{ 10, "sgn(-123)", "-1" },
81
{ 10, "5-7", "-2" },
82
83
{ 0, "cmp(0,0)", "0" },
84
{ 0, "cmp(1,0)", "1" },
85
{ 0, "cmp(0,1)", "-1" },
86
{ 0, "cmp(-1,0)", "-1" },
87
{ 0, "cmp(0,-1)", "1" },
88
89
{ 10, "0 ? 123 : 456", "456" },
90
{ 10, "1 ? 4+5 : 6+7", "9" },
91
92
{ 10, "(123)", "123" },
93
{ 10, "(2+3)", "5" },
94
{ 10, "(4+5)*(5+6)", "99" },
95
96
{ 0, "1 << 16", "65536" },
97
{ 0, "256 >> 4", "16" },
98
{ 0, "-256 >> 4", "-16" },
99
100
{ 0, "!1", "0" },
101
{ 0, "!9", "0" },
102
{ 0, "!0", "1" },
103
104
{ 0, "2**2**2", "16" },
105
{ 0, "-2**2**2", "-16" },
106
107
{ 0, "0x100", "256" },
108
{ 10, "0x100", NULL },
109
{ 10, "0x 100", NULL },
110
111
{ 0, " max ( 1, 2, 3, 4, 5, 6, 7, 8)", "8" },
112
{ 0, " max ( 1, 9, 2, 3, 4, 5, 6, 7, 8)", "9" },
113
{ 0, " min ( 1, 9, 2, 3, 4, 5, 6, 7, 8)", "1" },
114
115
{ 10, "abs(123)", "123" },
116
{ 10, "abs(-123)", "123" },
117
{ 10, "abs(0)", "0" },
118
119
/* filling data stack */
120
{ 0, "1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+1))))))))))))))", "16" },
121
122
/* filling control stack */
123
{ 0, "----------------------------------------------------1", "1" },
124
};
125
126
127
const struct data_t data_z[] = {
128
{ 0, "divisible_p(333,3)", "1" },
129
{ 0, "congruent_p(7,1,3)", "1" },
130
131
{ 0, "cmpabs(0,0)", "0" },
132
{ 0, "cmpabs(1,0)", "1" },
133
{ 0, "cmpabs(0,1)", "-1" },
134
{ 0, "cmpabs(-1,0)", "1" },
135
{ 0, "cmpabs(0,-1)", "-1" },
136
137
{ 0, "odd_p(1)", "1" },
138
{ 0, "odd_p(0)", "0" },
139
{ 0, "odd_p(-1)", "1" },
140
141
{ 0, "even_p(1)", "0" },
142
{ 0, "even_p(0)", "1" },
143
{ 0, "even_p(-1)", "0" },
144
145
{ 0, "fac(0)", "1" },
146
{ 0, "fac(1)", "1" },
147
{ 0, "fac(2)", "2" },
148
{ 0, "fac(3)", "6" },
149
{ 0, "fac(10)", "3628800" },
150
151
{ 10, "root(81,4)", "3" },
152
153
{ 10, "gcd(4,6)", "2" },
154
{ 10, "gcd(4,6,9)", "1" },
155
156
{ 10, "powm(3,2,9)", "0" },
157
{ 10, "powm(3,2,8)", "1" },
158
159
/* filling data stack */
160
{ 0, "1 ? 1 : 1 || 1 && 1 | 1 ^ 1 & 1 == 1 >= 1 << 1 - 1 * 1 ** 1", "1" },
161
162
/* filling control stack */
163
{ 0, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~1", "1" },
164
165
{ 0, "fib(10)", "55" },
166
167
{ 0, "setbit(0,5)", "32" },
168
{ 0, "clrbit(32,5)", "0" },
169
{ 0, "tstbit(32,5)", "1" },
170
{ 0, "tstbit(32,4)", "0" },
171
{ 0, "scan0(7,0)", "3" },
172
{ 0, "scan1(7,0)", "0" },
173
};
174
175
const struct data_t data_zq[] = {
176
/* expecting failure */
177
{ 0, "1.2", NULL },
178
};
179
180
const struct data_t data_q[] = {
181
{ 10, "(1/2 + 1/3 + 1/4 + 1/5 + 1/6)*20", "29" },
182
{ 0, "num(5/9)", "5" },
183
{ 0, "den(5/9)", "9" },
184
};
185
186
const struct data_t data_zf[] = {
187
{ 10, "sqrt ( 49 )", "7" },
188
{ 10, "sqrt ( 49 ) + 1", "8" },
189
{ 10, "sqrt((49))", "7" },
190
{ 10, "sqrt((((((((49))))))))", "7" },
191
};
192
193
const struct data_t data_f[] = {
194
{ 0, "1@10", "10000000000" },
195
{ 0, "1.5@10", "15000000000" },
196
{ 0, "1000@-1", "100" },
197
{ 0, "10.00@-1", "1" },
198
199
{ 0, "1e10", "10000000000" },
200
{ 0, "1.5e10", "15000000000" },
201
{ 0, "1000e-1", "100" },
202
{ 0, "10.00e-1", "1" },
203
204
{ 16, "1@9", "68719476736" },
205
206
{ 16, "1@10", "18446744073709551616" },
207
{ -16, "1@10", "1099511627776" },
208
209
{ 0, "ceil(0)", "0" },
210
{ 0, "ceil(0.25)", "1" },
211
{ 0, "ceil(0.5)", "1" },
212
{ 0, "ceil(1.5)", "2" },
213
{ 0, "ceil(-0.5)", "0" },
214
{ 0, "ceil(-1.5)", "-1" },
215
216
/* only simple cases because mpf_eq currently only works on whole limbs */
217
{ 0, "eq(0xFFFFFFFFFFFFFFFF1111111111111111,0xFFFFFFFFFFFFFFFF2222222222222222,64)", "1" },
218
{ 0, "eq(0xFFFFFFFFFFFFFFFF1111111111111111,0xFFFFFFFFFFFFFFFF2222222222222222,128)", "0" },
219
220
{ 0, "floor(0)", "0" },
221
{ 0, "floor(0.25)", "0" },
222
{ 0, "floor(0.5)", "0" },
223
{ 0, "floor(1.5)", "1" },
224
{ 0, "floor(-0.5)", "-1" },
225
{ 0, "floor(-1.5)", "-2" },
226
227
{ 0, "integer_p(1)", "1" },
228
{ 0, "integer_p(0.5)", "0" },
229
230
{ 0, "trunc(0)", "0" },
231
{ 0, "trunc(0.25)", "0" },
232
{ 0, "trunc(0.5)", "0" },
233
{ 0, "trunc(1.5)", "1" },
234
{ 0, "trunc(-0.5)", "0" },
235
{ 0, "trunc(-1.5)", "-1" },
236
};
237
238
struct datalist_t {
239
const struct data_t *data;
240
int num;
241
};
242
243
#define DATALIST(data) { data, numberof (data) }
244
245
struct datalist_t list_z[] = {
246
DATALIST (data_z),
247
DATALIST (data_zq),
248
DATALIST (data_zf),
249
DATALIST (data_zqf),
250
};
251
252
struct datalist_t list_q[] = {
253
DATALIST (data_q),
254
DATALIST (data_zq),
255
DATALIST (data_zqf),
256
};
257
258
struct datalist_t list_f[] = {
259
DATALIST (data_zf),
260
DATALIST (data_zqf),
261
DATALIST (data_f),
262
};
263
264
265
void
266
check_z (void)
267
{
268
const struct data_t *data;
269
mpz_t a, b, got, want;
270
int l, i, ret;
271
272
mpz_init (got);
273
mpz_init (want);
274
mpz_init_set_ui (a, 55);
275
mpz_init_set_ui (b, 99);
276
277
for (l = 0; l < numberof (list_z); l++)
278
{
279
data = list_z[l].data;
280
281
for (i = 0; i < list_z[l].num; i++)
282
{
283
if (option_trace)
284
printf ("mpz_expr \"%s\"\n", data[i].expr);
285
286
ret = mpz_expr (got, data[i].base, data[i].expr, a, b, NULL);
287
288
if (data[i].want == NULL)
289
{
290
/* expect to fail */
291
if (ret == MPEXPR_RESULT_OK)
292
{
293
printf ("mpz_expr wrong return value, got %d, expected failure\n", ret);
294
goto error;
295
}
296
}
297
else
298
{
299
if (mpz_set_str (want, data[i].want, 0) != 0)
300
{
301
printf ("Cannot parse wanted value string\n");
302
goto error;
303
}
304
if (ret != MPEXPR_RESULT_OK)
305
{
306
printf ("mpz_expr failed unexpectedly\n");
307
printf (" return value %d\n", ret);
308
goto error;
309
}
310
if (mpz_cmp (got, want) != 0)
311
{
312
printf ("mpz_expr wrong result\n");
313
printf (" got "); mpz_out_str (stdout, 10, got);
314
printf ("\n");
315
printf (" want "); mpz_out_str (stdout, 10, want);
316
printf ("\n");
317
goto error;
318
}
319
}
320
}
321
}
322
mpz_clear (a);
323
mpz_clear (b);
324
mpz_clear (got);
325
mpz_clear (want);
326
return;
327
328
error:
329
printf (" base %d\n", data[i].base);
330
printf (" expr \"%s\"\n", data[i].expr);
331
if (data[i].want != NULL)
332
printf (" want \"%s\"\n", data[i].want);
333
abort ();
334
}
335
336
void
337
check_q (void)
338
{
339
const struct data_t *data;
340
mpq_t a, b, got, want;
341
int l, i, ret;
342
343
mpq_init (got);
344
mpq_init (want);
345
mpq_init (a);
346
mpq_init (b);
347
348
mpq_set_ui (a, 55, 1);
349
mpq_set_ui (b, 99, 1);
350
351
for (l = 0; l < numberof (list_q); l++)
352
{
353
data = list_q[l].data;
354
355
for (i = 0; i < list_q[l].num; i++)
356
{
357
if (option_trace)
358
printf ("mpq_expr \"%s\"\n", data[i].expr);
359
360
ret = mpq_expr (got, data[i].base, data[i].expr, a, b, NULL);
361
362
if (data[i].want == NULL)
363
{
364
/* expect to fail */
365
if (ret == MPEXPR_RESULT_OK)
366
{
367
printf ("mpq_expr wrong return value, got %d, expected failure\n", ret);
368
goto error;
369
}
370
}
371
else
372
{
373
if (mpz_set_str (mpq_numref(want), data[i].want, 0) != 0)
374
{
375
printf ("Cannot parse wanted value string\n");
376
goto error;
377
}
378
mpz_set_ui (mpq_denref(want), 1);
379
380
if (ret != MPEXPR_RESULT_OK)
381
{
382
printf ("mpq_expr failed unexpectedly\n");
383
printf (" return value %d\n", ret);
384
goto error;
385
}
386
if (mpq_cmp (got, want) != 0)
387
{
388
printf ("mpq_expr wrong result\n");
389
printf (" got "); mpq_out_str (stdout, 10, got);
390
printf ("\n");
391
printf (" want "); mpq_out_str (stdout, 10, want);
392
printf ("\n");
393
goto error;
394
}
395
}
396
}
397
}
398
mpq_clear (a);
399
mpq_clear (b);
400
mpq_clear (got);
401
mpq_clear (want);
402
return;
403
404
error:
405
printf (" base %d\n", data[i].base);
406
printf (" expr \"%s\"\n", data[i].expr);
407
if (data[i].want != NULL)
408
printf (" want \"%s\"\n", data[i].want);
409
abort ();
410
}
411
412
void
413
check_f (void)
414
{
415
const struct data_t *data;
416
mpf_t a, b, got, want;
417
int l, i, ret;
418
419
mpf_set_default_prec (200L);
420
421
mpf_init (got);
422
mpf_init (want);
423
mpf_init_set_ui (a, 55);
424
mpf_init_set_ui (b, 99);
425
426
for (l = 0; l < numberof (list_f); l++)
427
{
428
data = list_f[l].data;
429
430
for (i = 0; i < list_f[l].num; i++)
431
{
432
if (option_trace)
433
printf ("mpf_expr \"%s\"\n", data[i].expr);
434
435
ret = mpf_expr (got, data[i].base, data[i].expr, a, b, NULL);
436
437
if (data[i].want == NULL)
438
{
439
/* expect to fail */
440
if (ret == MPEXPR_RESULT_OK)
441
{
442
printf ("mpf_expr wrong return value, got %d, expected failure\n", ret);
443
goto error;
444
}
445
}
446
else
447
{
448
if (mpf_set_str (want, data[i].want, 0) != 0)
449
{
450
printf ("Cannot parse wanted value string\n");
451
goto error;
452
}
453
454
if (ret != MPEXPR_RESULT_OK)
455
{
456
printf ("mpf_expr failed unexpectedly\n");
457
printf (" return value %d\n", ret);
458
goto error;
459
}
460
if (mpf_cmp (got, want) != 0)
461
{
462
printf ("mpf_expr wrong result\n");
463
printf (" got "); mpf_out_str (stdout, 10, 20, got);
464
printf ("\n");
465
printf (" want "); mpf_out_str (stdout, 10, 20, want);
466
printf ("\n");
467
goto error;
468
}
469
}
470
}
471
}
472
mpf_clear (a);
473
mpf_clear (b);
474
mpf_clear (got);
475
mpf_clear (want);
476
return;
477
478
error:
479
printf (" base %d\n", data[i].base);
480
printf (" expr \"%s\"\n", data[i].expr);
481
if (data[i].want != NULL)
482
printf (" want \"%s\"\n", data[i].want);
483
abort ();
484
}
485
486
487
int
488
main (int argc, char *argv[])
489
{
490
tests_start ();
491
492
if (argc >= 2)
493
option_trace = 1;
494
495
check_z ();
496
check_q ();
497
check_f ();
498
499
tests_end ();
500
exit (0);
501
}
502
503