GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
/* Test expression evaluation (print nothing and exit 0 if successful).12Copyright 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.34This file is part of the GNU MP Library.56The GNU MP Library is free software; you can redistribute it and/or modify7it under the terms of the GNU Lesser General Public License as published by8the Free Software Foundation; either version 2.1 of the License, or (at your9option) any later version.1011The GNU MP Library is distributed in the hope that it will be useful, but12WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY13or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public14License for more details.1516You should have received a copy of the GNU Lesser General Public License17along with the GNU MP Library; see the file COPYING.LIB. If not, write to18the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,19MA 02110-1301, USA. */2021#include <stdio.h>22#include <stdlib.h>2324#include "gmp.h"25#include "tests.h"26#include "expr-impl.h"272829int option_trace = 0;303132struct data_t {33int base;34const char *expr;35const char *want;36};3738#define numberof(x) (sizeof (x) / sizeof ((x)[0]))394041/* These data_xxx[] arrays are tables to be tested with one or more of the42mp?_t types. z=mpz_t, q=mpz_t, f=mpf_t. */4344struct data_t data_zqf[] = {4546/* various deliberately wrong expressions */47{ 0, "", NULL },48{ 0, "1+", NULL },49{ 0, "+2", NULL },50{ 0, "1,2", NULL },51{ 0, "foo(1,2)", NULL },52{ 0, "1+foo", NULL },53{ 10, "0fff", NULL },54{ 0, "!", NULL },55{ 0, "10!", NULL },56{ 0, "-10!", NULL },57{ 0, "gcd((4,6))", NULL },58{ 0, "()", NULL },59{ 0, "fac(2**1000)", NULL },60{ 0, "$", NULL },61{ 0, "$-", NULL },6263/* some basics */64{ 10, "123", "123" },65{ 10, "-123", "-123" },66{ 10, "1+2", "3" },67{ 10, "1+2+3", "6" },68{ 10, "1+2*3", "7" },69{ 10, "3*2+1", "7" },70{ 10, "$a", "55" },71{ 10, "b", "99" },72{ 16, "b", "11" },73{ 10, "4**3 * 2 + 1", "129" },74{ 10, "1<2", "1" },75{ 10, "1>2", "0" },7677{ 10, "(123)", "123" },7879{ 10, "sgn(-123)", "-1" },80{ 10, "5-7", "-2" },8182{ 0, "cmp(0,0)", "0" },83{ 0, "cmp(1,0)", "1" },84{ 0, "cmp(0,1)", "-1" },85{ 0, "cmp(-1,0)", "-1" },86{ 0, "cmp(0,-1)", "1" },8788{ 10, "0 ? 123 : 456", "456" },89{ 10, "1 ? 4+5 : 6+7", "9" },9091{ 10, "(123)", "123" },92{ 10, "(2+3)", "5" },93{ 10, "(4+5)*(5+6)", "99" },9495{ 0, "1 << 16", "65536" },96{ 0, "256 >> 4", "16" },97{ 0, "-256 >> 4", "-16" },9899{ 0, "!1", "0" },100{ 0, "!9", "0" },101{ 0, "!0", "1" },102103{ 0, "2**2**2", "16" },104{ 0, "-2**2**2", "-16" },105106{ 0, "0x100", "256" },107{ 10, "0x100", NULL },108{ 10, "0x 100", NULL },109110{ 0, " max ( 1, 2, 3, 4, 5, 6, 7, 8)", "8" },111{ 0, " max ( 1, 9, 2, 3, 4, 5, 6, 7, 8)", "9" },112{ 0, " min ( 1, 9, 2, 3, 4, 5, 6, 7, 8)", "1" },113114{ 10, "abs(123)", "123" },115{ 10, "abs(-123)", "123" },116{ 10, "abs(0)", "0" },117118/* filling data stack */119{ 0, "1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+1))))))))))))))", "16" },120121/* filling control stack */122{ 0, "----------------------------------------------------1", "1" },123};124125126const struct data_t data_z[] = {127{ 0, "divisible_p(333,3)", "1" },128{ 0, "congruent_p(7,1,3)", "1" },129130{ 0, "cmpabs(0,0)", "0" },131{ 0, "cmpabs(1,0)", "1" },132{ 0, "cmpabs(0,1)", "-1" },133{ 0, "cmpabs(-1,0)", "1" },134{ 0, "cmpabs(0,-1)", "-1" },135136{ 0, "odd_p(1)", "1" },137{ 0, "odd_p(0)", "0" },138{ 0, "odd_p(-1)", "1" },139140{ 0, "even_p(1)", "0" },141{ 0, "even_p(0)", "1" },142{ 0, "even_p(-1)", "0" },143144{ 0, "fac(0)", "1" },145{ 0, "fac(1)", "1" },146{ 0, "fac(2)", "2" },147{ 0, "fac(3)", "6" },148{ 0, "fac(10)", "3628800" },149150{ 10, "root(81,4)", "3" },151152{ 10, "gcd(4,6)", "2" },153{ 10, "gcd(4,6,9)", "1" },154155{ 10, "powm(3,2,9)", "0" },156{ 10, "powm(3,2,8)", "1" },157158/* filling data stack */159{ 0, "1 ? 1 : 1 || 1 && 1 | 1 ^ 1 & 1 == 1 >= 1 << 1 - 1 * 1 ** 1", "1" },160161/* filling control stack */162{ 0, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~1", "1" },163164{ 0, "fib(10)", "55" },165166{ 0, "setbit(0,5)", "32" },167{ 0, "clrbit(32,5)", "0" },168{ 0, "tstbit(32,5)", "1" },169{ 0, "tstbit(32,4)", "0" },170{ 0, "scan0(7,0)", "3" },171{ 0, "scan1(7,0)", "0" },172};173174const struct data_t data_zq[] = {175/* expecting failure */176{ 0, "1.2", NULL },177};178179const struct data_t data_q[] = {180{ 10, "(1/2 + 1/3 + 1/4 + 1/5 + 1/6)*20", "29" },181{ 0, "num(5/9)", "5" },182{ 0, "den(5/9)", "9" },183};184185const struct data_t data_zf[] = {186{ 10, "sqrt ( 49 )", "7" },187{ 10, "sqrt ( 49 ) + 1", "8" },188{ 10, "sqrt((49))", "7" },189{ 10, "sqrt((((((((49))))))))", "7" },190};191192const struct data_t data_f[] = {193{ 0, "1@10", "10000000000" },194{ 0, "1.5@10", "15000000000" },195{ 0, "1000@-1", "100" },196{ 0, "10.00@-1", "1" },197198{ 0, "1e10", "10000000000" },199{ 0, "1.5e10", "15000000000" },200{ 0, "1000e-1", "100" },201{ 0, "10.00e-1", "1" },202203{ 16, "1@9", "68719476736" },204205{ 16, "1@10", "18446744073709551616" },206{ -16, "1@10", "1099511627776" },207208{ 0, "ceil(0)", "0" },209{ 0, "ceil(0.25)", "1" },210{ 0, "ceil(0.5)", "1" },211{ 0, "ceil(1.5)", "2" },212{ 0, "ceil(-0.5)", "0" },213{ 0, "ceil(-1.5)", "-1" },214215/* only simple cases because mpf_eq currently only works on whole limbs */216{ 0, "eq(0xFFFFFFFFFFFFFFFF1111111111111111,0xFFFFFFFFFFFFFFFF2222222222222222,64)", "1" },217{ 0, "eq(0xFFFFFFFFFFFFFFFF1111111111111111,0xFFFFFFFFFFFFFFFF2222222222222222,128)", "0" },218219{ 0, "floor(0)", "0" },220{ 0, "floor(0.25)", "0" },221{ 0, "floor(0.5)", "0" },222{ 0, "floor(1.5)", "1" },223{ 0, "floor(-0.5)", "-1" },224{ 0, "floor(-1.5)", "-2" },225226{ 0, "integer_p(1)", "1" },227{ 0, "integer_p(0.5)", "0" },228229{ 0, "trunc(0)", "0" },230{ 0, "trunc(0.25)", "0" },231{ 0, "trunc(0.5)", "0" },232{ 0, "trunc(1.5)", "1" },233{ 0, "trunc(-0.5)", "0" },234{ 0, "trunc(-1.5)", "-1" },235};236237struct datalist_t {238const struct data_t *data;239int num;240};241242#define DATALIST(data) { data, numberof (data) }243244struct datalist_t list_z[] = {245DATALIST (data_z),246DATALIST (data_zq),247DATALIST (data_zf),248DATALIST (data_zqf),249};250251struct datalist_t list_q[] = {252DATALIST (data_q),253DATALIST (data_zq),254DATALIST (data_zqf),255};256257struct datalist_t list_f[] = {258DATALIST (data_zf),259DATALIST (data_zqf),260DATALIST (data_f),261};262263264void265check_z (void)266{267const struct data_t *data;268mpz_t a, b, got, want;269int l, i, ret;270271mpz_init (got);272mpz_init (want);273mpz_init_set_ui (a, 55);274mpz_init_set_ui (b, 99);275276for (l = 0; l < numberof (list_z); l++)277{278data = list_z[l].data;279280for (i = 0; i < list_z[l].num; i++)281{282if (option_trace)283printf ("mpz_expr \"%s\"\n", data[i].expr);284285ret = mpz_expr (got, data[i].base, data[i].expr, a, b, NULL);286287if (data[i].want == NULL)288{289/* expect to fail */290if (ret == MPEXPR_RESULT_OK)291{292printf ("mpz_expr wrong return value, got %d, expected failure\n", ret);293goto error;294}295}296else297{298if (mpz_set_str (want, data[i].want, 0) != 0)299{300printf ("Cannot parse wanted value string\n");301goto error;302}303if (ret != MPEXPR_RESULT_OK)304{305printf ("mpz_expr failed unexpectedly\n");306printf (" return value %d\n", ret);307goto error;308}309if (mpz_cmp (got, want) != 0)310{311printf ("mpz_expr wrong result\n");312printf (" got "); mpz_out_str (stdout, 10, got);313printf ("\n");314printf (" want "); mpz_out_str (stdout, 10, want);315printf ("\n");316goto error;317}318}319}320}321mpz_clear (a);322mpz_clear (b);323mpz_clear (got);324mpz_clear (want);325return;326327error:328printf (" base %d\n", data[i].base);329printf (" expr \"%s\"\n", data[i].expr);330if (data[i].want != NULL)331printf (" want \"%s\"\n", data[i].want);332abort ();333}334335void336check_q (void)337{338const struct data_t *data;339mpq_t a, b, got, want;340int l, i, ret;341342mpq_init (got);343mpq_init (want);344mpq_init (a);345mpq_init (b);346347mpq_set_ui (a, 55, 1);348mpq_set_ui (b, 99, 1);349350for (l = 0; l < numberof (list_q); l++)351{352data = list_q[l].data;353354for (i = 0; i < list_q[l].num; i++)355{356if (option_trace)357printf ("mpq_expr \"%s\"\n", data[i].expr);358359ret = mpq_expr (got, data[i].base, data[i].expr, a, b, NULL);360361if (data[i].want == NULL)362{363/* expect to fail */364if (ret == MPEXPR_RESULT_OK)365{366printf ("mpq_expr wrong return value, got %d, expected failure\n", ret);367goto error;368}369}370else371{372if (mpz_set_str (mpq_numref(want), data[i].want, 0) != 0)373{374printf ("Cannot parse wanted value string\n");375goto error;376}377mpz_set_ui (mpq_denref(want), 1);378379if (ret != MPEXPR_RESULT_OK)380{381printf ("mpq_expr failed unexpectedly\n");382printf (" return value %d\n", ret);383goto error;384}385if (mpq_cmp (got, want) != 0)386{387printf ("mpq_expr wrong result\n");388printf (" got "); mpq_out_str (stdout, 10, got);389printf ("\n");390printf (" want "); mpq_out_str (stdout, 10, want);391printf ("\n");392goto error;393}394}395}396}397mpq_clear (a);398mpq_clear (b);399mpq_clear (got);400mpq_clear (want);401return;402403error:404printf (" base %d\n", data[i].base);405printf (" expr \"%s\"\n", data[i].expr);406if (data[i].want != NULL)407printf (" want \"%s\"\n", data[i].want);408abort ();409}410411void412check_f (void)413{414const struct data_t *data;415mpf_t a, b, got, want;416int l, i, ret;417418mpf_set_default_prec (200L);419420mpf_init (got);421mpf_init (want);422mpf_init_set_ui (a, 55);423mpf_init_set_ui (b, 99);424425for (l = 0; l < numberof (list_f); l++)426{427data = list_f[l].data;428429for (i = 0; i < list_f[l].num; i++)430{431if (option_trace)432printf ("mpf_expr \"%s\"\n", data[i].expr);433434ret = mpf_expr (got, data[i].base, data[i].expr, a, b, NULL);435436if (data[i].want == NULL)437{438/* expect to fail */439if (ret == MPEXPR_RESULT_OK)440{441printf ("mpf_expr wrong return value, got %d, expected failure\n", ret);442goto error;443}444}445else446{447if (mpf_set_str (want, data[i].want, 0) != 0)448{449printf ("Cannot parse wanted value string\n");450goto error;451}452453if (ret != MPEXPR_RESULT_OK)454{455printf ("mpf_expr failed unexpectedly\n");456printf (" return value %d\n", ret);457goto error;458}459if (mpf_cmp (got, want) != 0)460{461printf ("mpf_expr wrong result\n");462printf (" got "); mpf_out_str (stdout, 10, 20, got);463printf ("\n");464printf (" want "); mpf_out_str (stdout, 10, 20, want);465printf ("\n");466goto error;467}468}469}470}471mpf_clear (a);472mpf_clear (b);473mpf_clear (got);474mpf_clear (want);475return;476477error:478printf (" base %d\n", data[i].base);479printf (" expr \"%s\"\n", data[i].expr);480if (data[i].want != NULL)481printf (" want \"%s\"\n", data[i].want);482abort ();483}484485486int487main (int argc, char *argv[])488{489tests_start ();490491if (argc >= 2)492option_trace = 1;493494check_z ();495check_q ();496check_f ();497498tests_end ();499exit (0);500}501502503