Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
| Download
GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
Project: cocalc-sagemath-dev-slelievre
Views: 418346/* Test expression evaluation (print nothing and exit 0 if successful).12Copyright 2000-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 either:89* the GNU Lesser General Public License as published by the Free10Software Foundation; either version 3 of the License, or (at your11option) any later version.1213or1415* the GNU General Public License as published by the Free Software16Foundation; either version 2 of the License, or (at your option) any17later version.1819or both in parallel, as here.2021The GNU MP Library is distributed in the hope that it will be useful, but22WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY23or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License24for more details.2526You should have received copies of the GNU General Public License and the27GNU Lesser General Public License along with the GNU MP Library. If not,28see https://www.gnu.org/licenses/. */2930#include <stdio.h>31#include <stdlib.h>3233#include "gmp.h"34#include "tests.h"35#include "expr-impl.h"363738int option_trace = 0;394041struct data_t {42int base;43const char *expr;44const char *want;45};4647#define numberof(x) (sizeof (x) / sizeof ((x)[0]))484950/* These data_xxx[] arrays are tables to be tested with one or more of the51mp?_t types. z=mpz_t, q=mpz_t, f=mpf_t. */5253struct data_t data_zqf[] = {5455/* various deliberately wrong expressions */56{ 0, "", NULL },57{ 0, "1+", NULL },58{ 0, "+2", NULL },59{ 0, "1,2", NULL },60{ 0, "foo(1,2)", NULL },61{ 0, "1+foo", NULL },62{ 10, "0fff", NULL },63{ 0, "!", NULL },64{ 0, "10!", NULL },65{ 0, "-10!", NULL },66{ 0, "gcd((4,6))", NULL },67{ 0, "()", NULL },68{ 0, "fac(2**1000)", NULL },69{ 0, "$", NULL },70{ 0, "$-", NULL },7172/* some basics */73{ 10, "123", "123" },74{ 10, "-123", "-123" },75{ 10, "1+2", "3" },76{ 10, "1+2+3", "6" },77{ 10, "1+2*3", "7" },78{ 10, "3*2+1", "7" },79{ 10, "$a", "55" },80{ 10, "b", "99" },81{ 16, "b", "11" },82{ 10, "4**3 * 2 + 1", "129" },83{ 10, "1<2", "1" },84{ 10, "1>2", "0" },8586{ 10, "(123)", "123" },8788{ 10, "sgn(-123)", "-1" },89{ 10, "5-7", "-2" },9091{ 0, "cmp(0,0)", "0" },92{ 0, "cmp(1,0)", "1" },93{ 0, "cmp(0,1)", "-1" },94{ 0, "cmp(-1,0)", "-1" },95{ 0, "cmp(0,-1)", "1" },9697{ 10, "0 ? 123 : 456", "456" },98{ 10, "1 ? 4+5 : 6+7", "9" },99100{ 10, "(123)", "123" },101{ 10, "(2+3)", "5" },102{ 10, "(4+5)*(5+6)", "99" },103104{ 0, "1 << 16", "65536" },105{ 0, "256 >> 4", "16" },106{ 0, "-256 >> 4", "-16" },107108{ 0, "!1", "0" },109{ 0, "!9", "0" },110{ 0, "!0", "1" },111112{ 0, "2**2**2", "16" },113{ 0, "-2**2**2", "-16" },114115{ 0, "0x100", "256" },116{ 10, "0x100", NULL },117{ 10, "0x 100", NULL },118119{ 0, " max ( 1, 2, 3, 4, 5, 6, 7, 8)", "8" },120{ 0, " max ( 1, 9, 2, 3, 4, 5, 6, 7, 8)", "9" },121{ 0, " min ( 1, 9, 2, 3, 4, 5, 6, 7, 8)", "1" },122123{ 10, "abs(123)", "123" },124{ 10, "abs(-123)", "123" },125{ 10, "abs(0)", "0" },126127/* filling data stack */128{ 0, "1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+1))))))))))))))", "16" },129130/* filling control stack */131{ 0, "----------------------------------------------------1", "1" },132};133134135const struct data_t data_z[] = {136{ 0, "divisible_p(333,3)", "1" },137{ 0, "congruent_p(7,1,3)", "1" },138139{ 0, "cmpabs(0,0)", "0" },140{ 0, "cmpabs(1,0)", "1" },141{ 0, "cmpabs(0,1)", "-1" },142{ 0, "cmpabs(-1,0)", "1" },143{ 0, "cmpabs(0,-1)", "-1" },144145{ 0, "odd_p(1)", "1" },146{ 0, "odd_p(0)", "0" },147{ 0, "odd_p(-1)", "1" },148149{ 0, "even_p(1)", "0" },150{ 0, "even_p(0)", "1" },151{ 0, "even_p(-1)", "0" },152153{ 0, "fac(0)", "1" },154{ 0, "fac(1)", "1" },155{ 0, "fac(2)", "2" },156{ 0, "fac(3)", "6" },157{ 0, "fac(10)", "3628800" },158159{ 10, "root(81,4)", "3" },160161{ 10, "gcd(4,6)", "2" },162{ 10, "gcd(4,6,9)", "1" },163164{ 10, "powm(3,2,9)", "0" },165{ 10, "powm(3,2,8)", "1" },166167/* filling data stack */168{ 0, "1 ? 1 : 1 || 1 && 1 | 1 ^ 1 & 1 == 1 >= 1 << 1 - 1 * 1 ** 1", "1" },169170/* filling control stack */171{ 0, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~1", "1" },172173{ 0, "fib(10)", "55" },174175{ 0, "setbit(0,5)", "32" },176{ 0, "clrbit(32,5)", "0" },177{ 0, "tstbit(32,5)", "1" },178{ 0, "tstbit(32,4)", "0" },179{ 0, "scan0(7,0)", "3" },180{ 0, "scan1(7,0)", "0" },181};182183const struct data_t data_zq[] = {184/* expecting failure */185{ 0, "1.2", NULL },186};187188const struct data_t data_q[] = {189{ 10, "(1/2 + 1/3 + 1/4 + 1/5 + 1/6)*20", "29" },190{ 0, "num(5/9)", "5" },191{ 0, "den(5/9)", "9" },192};193194const struct data_t data_zf[] = {195{ 10, "sqrt ( 49 )", "7" },196{ 10, "sqrt ( 49 ) + 1", "8" },197{ 10, "sqrt((49))", "7" },198{ 10, "sqrt((((((((49))))))))", "7" },199};200201const struct data_t data_f[] = {202{ 0, "1@10", "10000000000" },203{ 0, "1.5@10", "15000000000" },204{ 0, "1000@-1", "100" },205{ 0, "10.00@-1", "1" },206207{ 0, "1e10", "10000000000" },208{ 0, "1.5e10", "15000000000" },209{ 0, "1000e-1", "100" },210{ 0, "10.00e-1", "1" },211212{ 16, "1@9", "68719476736" },213214{ 16, "1@10", "18446744073709551616" },215{ -16, "1@10", "1099511627776" },216217{ 0, "ceil(0)", "0" },218{ 0, "ceil(0.25)", "1" },219{ 0, "ceil(0.5)", "1" },220{ 0, "ceil(1.5)", "2" },221{ 0, "ceil(-0.5)", "0" },222{ 0, "ceil(-1.5)", "-1" },223224/* only simple cases because mpf_eq currently only works on whole limbs */225{ 0, "eq(0xFFFFFFFFFFFFFFFF1111111111111111,0xFFFFFFFFFFFFFFFF2222222222222222,64)", "1" },226{ 0, "eq(0xFFFFFFFFFFFFFFFF1111111111111111,0xFFFFFFFFFFFFFFFF2222222222222222,128)", "0" },227228{ 0, "floor(0)", "0" },229{ 0, "floor(0.25)", "0" },230{ 0, "floor(0.5)", "0" },231{ 0, "floor(1.5)", "1" },232{ 0, "floor(-0.5)", "-1" },233{ 0, "floor(-1.5)", "-2" },234235{ 0, "integer_p(1)", "1" },236{ 0, "integer_p(0.5)", "0" },237238{ 0, "trunc(0)", "0" },239{ 0, "trunc(0.25)", "0" },240{ 0, "trunc(0.5)", "0" },241{ 0, "trunc(1.5)", "1" },242{ 0, "trunc(-0.5)", "0" },243{ 0, "trunc(-1.5)", "-1" },244};245246struct datalist_t {247const struct data_t *data;248int num;249};250251#define DATALIST(data) { data, numberof (data) }252253struct datalist_t list_z[] = {254DATALIST (data_z),255DATALIST (data_zq),256DATALIST (data_zf),257DATALIST (data_zqf),258};259260struct datalist_t list_q[] = {261DATALIST (data_q),262DATALIST (data_zq),263DATALIST (data_zqf),264};265266struct datalist_t list_f[] = {267DATALIST (data_zf),268DATALIST (data_zqf),269DATALIST (data_f),270};271272273void274check_z (void)275{276const struct data_t *data;277mpz_t a, b, got, want;278int l, i, ret;279280mpz_init (got);281mpz_init (want);282mpz_init_set_ui (a, 55);283mpz_init_set_ui (b, 99);284285for (l = 0; l < numberof (list_z); l++)286{287data = list_z[l].data;288289for (i = 0; i < list_z[l].num; i++)290{291if (option_trace)292printf ("mpz_expr \"%s\"\n", data[i].expr);293294ret = mpz_expr (got, data[i].base, data[i].expr, a, b, NULL);295296if (data[i].want == NULL)297{298/* expect to fail */299if (ret == MPEXPR_RESULT_OK)300{301printf ("mpz_expr wrong return value, got %d, expected failure\n", ret);302goto error;303}304}305else306{307if (mpz_set_str (want, data[i].want, 0) != 0)308{309printf ("Cannot parse wanted value string\n");310goto error;311}312if (ret != MPEXPR_RESULT_OK)313{314printf ("mpz_expr failed unexpectedly\n");315printf (" return value %d\n", ret);316goto error;317}318if (mpz_cmp (got, want) != 0)319{320printf ("mpz_expr wrong result\n");321printf (" got "); mpz_out_str (stdout, 10, got);322printf ("\n");323printf (" want "); mpz_out_str (stdout, 10, want);324printf ("\n");325goto error;326}327}328}329}330mpz_clear (a);331mpz_clear (b);332mpz_clear (got);333mpz_clear (want);334return;335336error:337printf (" base %d\n", data[i].base);338printf (" expr \"%s\"\n", data[i].expr);339if (data[i].want != NULL)340printf (" want \"%s\"\n", data[i].want);341abort ();342}343344void345check_q (void)346{347const struct data_t *data;348mpq_t a, b, got, want;349int l, i, ret;350351mpq_init (got);352mpq_init (want);353mpq_init (a);354mpq_init (b);355356mpq_set_ui (a, 55, 1);357mpq_set_ui (b, 99, 1);358359for (l = 0; l < numberof (list_q); l++)360{361data = list_q[l].data;362363for (i = 0; i < list_q[l].num; i++)364{365if (option_trace)366printf ("mpq_expr \"%s\"\n", data[i].expr);367368ret = mpq_expr (got, data[i].base, data[i].expr, a, b, NULL);369370if (data[i].want == NULL)371{372/* expect to fail */373if (ret == MPEXPR_RESULT_OK)374{375printf ("mpq_expr wrong return value, got %d, expected failure\n", ret);376goto error;377}378}379else380{381if (mpz_set_str (mpq_numref(want), data[i].want, 0) != 0)382{383printf ("Cannot parse wanted value string\n");384goto error;385}386mpz_set_ui (mpq_denref(want), 1);387388if (ret != MPEXPR_RESULT_OK)389{390printf ("mpq_expr failed unexpectedly\n");391printf (" return value %d\n", ret);392goto error;393}394if (mpq_cmp (got, want) != 0)395{396printf ("mpq_expr wrong result\n");397printf (" got "); mpq_out_str (stdout, 10, got);398printf ("\n");399printf (" want "); mpq_out_str (stdout, 10, want);400printf ("\n");401goto error;402}403}404}405}406mpq_clear (a);407mpq_clear (b);408mpq_clear (got);409mpq_clear (want);410return;411412error:413printf (" base %d\n", data[i].base);414printf (" expr \"%s\"\n", data[i].expr);415if (data[i].want != NULL)416printf (" want \"%s\"\n", data[i].want);417abort ();418}419420void421check_f (void)422{423const struct data_t *data;424mpf_t a, b, got, want;425int l, i, ret;426427mpf_set_default_prec (200L);428429mpf_init (got);430mpf_init (want);431mpf_init_set_ui (a, 55);432mpf_init_set_ui (b, 99);433434for (l = 0; l < numberof (list_f); l++)435{436data = list_f[l].data;437438for (i = 0; i < list_f[l].num; i++)439{440if (option_trace)441printf ("mpf_expr \"%s\"\n", data[i].expr);442443ret = mpf_expr (got, data[i].base, data[i].expr, a, b, NULL);444445if (data[i].want == NULL)446{447/* expect to fail */448if (ret == MPEXPR_RESULT_OK)449{450printf ("mpf_expr wrong return value, got %d, expected failure\n", ret);451goto error;452}453}454else455{456if (mpf_set_str (want, data[i].want, 0) != 0)457{458printf ("Cannot parse wanted value string\n");459goto error;460}461462if (ret != MPEXPR_RESULT_OK)463{464printf ("mpf_expr failed unexpectedly\n");465printf (" return value %d\n", ret);466goto error;467}468if (mpf_cmp (got, want) != 0)469{470printf ("mpf_expr wrong result\n");471printf (" got "); mpf_out_str (stdout, 10, 20, got);472printf ("\n");473printf (" want "); mpf_out_str (stdout, 10, 20, want);474printf ("\n");475goto error;476}477}478}479}480mpf_clear (a);481mpf_clear (b);482mpf_clear (got);483mpf_clear (want);484return;485486error:487printf (" base %d\n", data[i].base);488printf (" expr \"%s\"\n", data[i].expr);489if (data[i].want != NULL)490printf (" want \"%s\"\n", data[i].want);491abort ();492}493494495int496main (int argc, char *argv[])497{498tests_start ();499500if (argc >= 2)501option_trace = 1;502503check_z ();504check_q ();505check_f ();506507tests_end ();508exit (0);509}510511512