[BACK]Return to t-expr.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / demos / expr

Annotation of OpenXM_contrib/gmp/demos/expr/t-expr.c, Revision 1.1.1.1

1.1       ohara       1: /* Test expression evaluation (print nothing and exit 0 if successful).
                      2:
                      3: Copyright 2000, 2001, 2002 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., 59 Temple Place - Suite 330, Boston,
                     20: MA 02111-1307, USA. */
                     21:
                     22: #include <stdio.h>
                     23: #include <stdlib.h>
                     24:
                     25: #include "gmp.h"
                     26: #include "gmp-impl.h"
                     27: #include "tests.h"
                     28: #include "expr-impl.h"
                     29:
                     30: #if HAVE_MPFR
                     31: extern mpfr_t __mpfr_const_pi;
                     32: extern int __mpfr_const_pi_prec;
                     33: extern mpfr_t __mpfr_const_log2;
                     34: extern int __mpfr_const_log2_prec;
                     35: #endif
                     36:
                     37:
                     38: int  option_trace = 0;
                     39:
                     40:
                     41: struct data_t {
                     42:   int         base;
                     43:   const char  *expr;
                     44:   const char  *want;
                     45: };
                     46:
                     47:
                     48: /* These data_xxx[] arrays are tables to be tested with one or more of the
                     49:    mp?_t types.  z=mpz_t, q=mpz_t, f=mpf_t, r=mpfr_t.  */
                     50:
                     51: struct data_t  data_zqfr[] = {
                     52:
                     53:   /* various deliberately wrong expressions */
                     54:   { 0, "", NULL },
                     55:   { 0, "1+", NULL },
                     56:   { 0, "+2", NULL },
                     57:   { 0, "1,2", NULL },
                     58:   { 0, "foo(1,2)", NULL },
                     59:   { 0, "1+foo", NULL },
                     60:   { 10, "0fff", NULL },
                     61:   { 0, "!", NULL },
                     62:   { 0, "10!", NULL },
                     63:   { 0, "-10!", NULL },
                     64:   { 0, "gcd((4,6))", NULL },
                     65:   { 0, "()", NULL },
                     66:   { 0, "fac(2**1000)", NULL },
                     67:   { 0, "$", NULL },
                     68:   { 0, "$-", NULL },
                     69:
                     70:   /* some basics */
                     71:   { 10, "123", "123" },
                     72:   { 10, "-123", "-123" },
                     73:   { 10, "1+2", "3" },
                     74:   { 10, "1+2+3", "6" },
                     75:   { 10, "1+2*3", "7" },
                     76:   { 10, "3*2+1", "7" },
                     77:   { 10, "$a", "55" },
                     78:   { 10, "b", "99" },
                     79:   { 16, "b", "11" },
                     80:   { 10, "4**3 * 2 + 1", "129" },
                     81:   { 10, "1<2", "1" },
                     82:   { 10, "1>2", "0" },
                     83:
                     84:   { 10, "(123)", "123" },
                     85:
                     86:   { 10, "sgn(-123)", "-1" },
                     87:   { 10, "5-7", "-2" },
                     88:
                     89:   { 0, "cmp(0,0)", "0" },
                     90:   { 0, "cmp(1,0)", "1" },
                     91:   { 0, "cmp(0,1)", "-1" },
                     92:   { 0, "cmp(-1,0)", "-1" },
                     93:   { 0, "cmp(0,-1)", "1" },
                     94:
                     95:   { 10, "0 ? 123 : 456", "456" },
                     96:   { 10, "1 ? 4+5 : 6+7", "9" },
                     97:
                     98:   { 10, "(123)", "123" },
                     99:   { 10, "(2+3)", "5" },
                    100:   { 10, "(4+5)*(5+6)", "99" },
                    101:
                    102:   { 0, "1 << 16", "65536" },
                    103:   { 0, "256 >> 4", "16" },
                    104:   { 0, "-256 >> 4", "-16" },
                    105:
                    106:   { 0, "!1", "0" },
                    107:   { 0, "!9", "0" },
                    108:   { 0, "!0", "1" },
                    109:
                    110:   { 0, "2**2**2", "16" },
                    111:   { 0, "-2**2**2", "-16" },
                    112:
                    113:   { 0, "0x100", "256" },
                    114:   { 10, "0x100", NULL },
                    115:   { 10, "0x 100", NULL },
                    116:
                    117:   { 0, " max ( 1, 2, 3, 4, 5, 6, 7, 8)", "8" },
                    118:   { 0, " max ( 1, 9, 2, 3, 4, 5, 6, 7, 8)", "9" },
                    119:   { 0, " min ( 1, 9, 2, 3, 4, 5, 6, 7, 8)", "1" },
                    120:
                    121:   { 10, "abs(123)",  "123" },
                    122:   { 10, "abs(-123)", "123" },
                    123:   { 10, "abs(0)",    "0" },
                    124:
                    125:   /* filling data stack */
                    126:   { 0, "1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+(1+1))))))))))))))", "16" },
                    127:
                    128:   /* filling control stack */
                    129:   { 0, "----------------------------------------------------1", "1" },
                    130: };
                    131:
                    132:
                    133: const struct data_t  data_z[] = {
                    134:   { 0, "cmpabs(0,0)", "0" },
                    135:   { 0, "cmpabs(1,0)", "1" },
                    136:   { 0, "cmpabs(0,1)", "-1" },
                    137:   { 0, "cmpabs(-1,0)", "1" },
                    138:   { 0, "cmpabs(0,-1)", "-1" },
                    139:
                    140:   { 0, "divisible_p(333,3)", "1" },
                    141:   { 0, "congruent_p(7,1,3)", "1" },
                    142:
                    143:   { 0, "odd_p(1)", "1" },
                    144:   { 0, "odd_p(0)", "0" },
                    145:   { 0, "odd_p(-1)", "1" },
                    146:
                    147:   { 0, "even_p(1)", "0" },
                    148:   { 0, "even_p(0)", "1" },
                    149:   { 0, "even_p(-1)", "0" },
                    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_zr[] = {
                    181:   { 0, "fac(0)",  "1" },
                    182:   { 0, "fac(1)",  "1" },
                    183:   { 0, "fac(2)",  "2" },
                    184:   { 0, "fac(3)",  "6" },
                    185:   { 0, "fac(10)", "3628800" },
                    186: };
                    187:
                    188: const 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: };
                    193:
                    194: const struct data_t  data_zfr[] = {
                    195:   { 10, "sqrt ( 49 )", "7" },
                    196:   { 10, "sqrt ( 49 ) + 1", "8" },
                    197:   { 10, "sqrt((49))", "7" },
                    198:   { 10, "sqrt((((((((49))))))))", "7" },
                    199: };
                    200:
                    201: const struct data_t  data_fr[] = {
                    202:   { 0, "1@10",    "10000000000" },
                    203:   { 0, "1.5@10",  "15000000000" },
                    204:   { 0, "1000@-1", "100" },
                    205:   { 0, "10.00@-1", "1" },
                    206:
                    207:   { 0, "1e10",     "10000000000" },
                    208:   { 0, "1.5e10",   "15000000000" },
                    209:   { 0, "1000e-1",  "100" },
                    210:   { 0, "10.00e-1", "1" },
                    211:
                    212:   { 16, "1@9",  "68719476736" },
                    213:
                    214:   { 0, "ceil(0)",           "0" },
                    215:   { 0, "ceil(0.25)",        "1" },
                    216:   { 0, "ceil(0.5)",         "1" },
                    217:   { 0, "ceil(1.5)",         "2" },
                    218:   { 0, "ceil(-0.5)",        "0" },
                    219:   { 0, "ceil(-1.5)",        "-1" },
                    220:
                    221:   /* only simple cases because mpf_eq currently only works on whole limbs */
                    222:   { 0, "eq(0xFFFFFFFFFFFFFFFF1111111111111111,0xFFFFFFFFFFFFFFFF2222222222222222,64)", "1" },
                    223:   { 0, "eq(0xFFFFFFFFFFFFFFFF1111111111111111,0xFFFFFFFFFFFFFFFF2222222222222222,128)", "0" },
                    224:
                    225:   { 0, "floor(0)",           "0" },
                    226:   { 0, "floor(0.25)",        "0" },
                    227:   { 0, "floor(0.5)",         "0" },
                    228:   { 0, "floor(1.5)",         "1" },
                    229:   { 0, "floor(-0.5)",        "-1" },
                    230:   { 0, "floor(-1.5)",        "-2" },
                    231:
                    232:   { 0, "trunc(0)",           "0" },
                    233:   { 0, "trunc(0.25)",        "0" },
                    234:   { 0, "trunc(0.5)",         "0" },
                    235:   { 0, "trunc(1.5)",         "1" },
                    236:   { 0, "trunc(-0.5)",        "0" },
                    237:   { 0, "trunc(-1.5)",        "-1" },
                    238:
                    239: };
                    240:
                    241: const struct data_t  data_f[] = {
                    242:   { 16,  "1@10", "18446744073709551616" },
                    243:   { -16, "1@10", "1099511627776" },
                    244:
                    245:   { 0, "integer_p(1)",   "1" },
                    246:   { 0, "integer_p(0.5)", "0" },
                    247: };
                    248:
                    249: const struct data_t  data_r[] = {
                    250:   { 16, "1@10", "1099511627776" },
                    251:
                    252:   { 0, "euler",         "" },
                    253:   { 0, "euler+1",       "" },
                    254:
                    255:   { 0, "loge2",         "" },
                    256:   { 0, "loge2+1",       "" },
                    257:
                    258:   { 0, "pi",            "" },
                    259:   { 0, "2*pi",          "" },
                    260:   { 0, "pi()",          NULL },
                    261:   { 0, "2 + pi() * 4",  NULL },
                    262:
                    263:   { 0, "acos(0)-pi/2",  "~0" },
                    264:   { 0, "acos(1)",       "0" },
                    265:
                    266:   { 0, "agm(0,0)",      "0" },
                    267:   { 0, "agm(1,1)",      "1" },
                    268:
                    269:   { 0, "asin(0)",       "0" },
                    270:   { 0, "asin(1)-pi/2",  "~0" },
                    271:
                    272:   { 0, "atan(0)",       "0" },
                    273:   { 0, "atan(1)-pi/4",  "~0" },
                    274:   { 0, "atan(-1)+pi/4", "~0" },
                    275:
                    276:   { 0, "cos(0)",        "1" },
                    277:   { 0, "cos(pi/2)",     "~0" },
                    278:   { 0, "cos(pi)",       "~-1" },
                    279:   { 0, "cos(3*pi/2)",   "~0" },
                    280:   { 0, "cos(2*pi)",     "~1" },
                    281:
                    282:   { 0, "cosh(0)",       "1" },
                    283:
                    284:   { 0, "dim(0,0)",      "0" },
                    285:   { 0, "dim(2,0)",      "2" },
                    286:   { 0, "dim(3,1)",      "2" },
                    287:   { 0, "dim(1,3)",      "0" },
                    288:   { 0, "dim(-2,0)",     "0" },
                    289:   { 0, "dim(-3,-1)",    "0" },
                    290:   { 0, "dim(-1,-3)",    "2" },
                    291:
                    292:   { 0, "eq(0xFF,0xF0,4)", "1" },
                    293:   { 0, "eq(0xFF,0xF0,5)", "0" },
                    294:
                    295:   { 0, "exp(0)",          "1" },
                    296:   { 0, "expm1(0)",        "0" },
                    297:
                    298:   { 0, "fma(2,3,4)",      "10" },
                    299:   { 0, "fma(-2,3,4)",     "-2" },
                    300:   { 0, "fma(2,-3,4)",     "-2" },
                    301:   { 0, "fma(2,-3,-4)",    "-10" },
                    302:
                    303:   { 0, "hypot(3,4)",      "5" },
                    304:
                    305:   { 0, "inf_p(1)",        "0" },
                    306:   { 0, "inf_p(-1)",       "0" },
                    307:   { 0, "inf_p(1/0)",      "1" },
                    308:   { 0, "inf_p(sqrt(-1))", "0" },
                    309:   { 0, "inf_p(1)",        "0" },
                    310:   { 0, "inf_p(nan)",      "0" },
                    311:   { 0, "inf_p(inf)",      "1" },
                    312:   { 0, "inf_p(-inf)",     "1" },
                    313:
                    314:   { 0, "inf > 0",         "1" },
                    315:   { 0, "inf == 0",        "0" },
                    316:   { 0, "inf < 0",         "0" },
                    317:   { 0, "-inf > 0",        "0" },
                    318:   { 0, "-inf == 0",       "0" },
                    319:   { 0, "-inf < 0",        "1" },
                    320:   { 0, "inf == inf",      "1" },
                    321:   { 0, "inf != inf",      "0" },
                    322:   { 0, "inf == -inf",     "0" },
                    323:   { 0, "inf != -inf",     "1" },
                    324:
                    325:   { 0, "nan_p(log(-1))",  "1" },
                    326:   { 0, "inf_p(log(0))",   "1" },
                    327:   { 0, "log(0) == -inf",  "1" },
                    328:   { 0, "log(1)",          "~0" },
                    329:
                    330:   { 0, "inf_p(log2(0))",  "1" },
                    331:   { 0, "log2(1)",         "0" },
                    332:   { 0, "log2(2)",         "1" },
                    333:   { 0, "log2(4)",         "2" },
                    334:   { 0, "log2(8)",         "3" },
                    335:
                    336:   { 0, "inf_p(log10(0))",  "1" },
                    337:   { 0, "log10(1)",         "0" },
                    338:   { 0, "log10(10)",        "1" },
                    339:   { 0, "log10(100)",       "2" },
                    340:   { 0, "log10(1000)",      "3" },
                    341:
                    342:   { 0, "log1p(0)",         "0" },
                    343:
                    344:   { 0, "nan_p(-1)",        "0" },
                    345:   { 0, "nan_p(1/0)",       "0" },
                    346:   { 0, "nan_p(sqrt(-1))",  "1" },
                    347:   { 0, "nan_p(1)",         "0" },
                    348:   { 0, "nan_p(nan)",       "1" },
                    349:   { 0, "nan_p(inf)",       "0" },
                    350:   { 0, "nan_p(-inf)",      "0" },
                    351:
                    352:   { 0, "number_p(-1)",       "1" },
                    353:   { 0, "number_p(1/0)",      "0" },
                    354:   { 0, "number_p(sqrt(-1))", "0" },
                    355:   { 0, "number_p(1)",        "1" },
                    356:   { 0, "number_p(nan)",      "0" },
                    357:   { 0, "number_p(inf)",      "0" },
                    358:   { 0, "number_p(-inf)",     "0" },
                    359:
                    360:   { 0, "round(0)",           "0" },
                    361:   { 0, "round(0.25)",        "0" },
                    362:   { 0, "round(0.5)",         "1" },
                    363:   { 0, "round(1.5)",         "2" },
                    364:   { 0, "round(-0.5)",        "-1" },
                    365:   { 0, "round(-1.5)",        "-2" },
                    366:
                    367:   { 0, "sin(0)",        "0" },
                    368:   { 0, "sin(pi/2)",     "~1" },
                    369:   { 0, "sin(pi)",       "~0" },
                    370:   { 0, "sin(3*pi/2)",   "~-1" },
                    371:   { 0, "sin(2*pi)",     "~0" },
                    372:
                    373:   { 0, "sinh(0)",       "0" },
                    374:
                    375:   { 0, "tan(0)",        "0" },
                    376:   { 0, "tan(pi/4)",     "~1" },
                    377:   { 0, "tan(3*pi/4)",   "~-1" },
                    378:   { 0, "tan(pi)",       "~0" },
                    379:   { 0, "tan(5*pi/4)",   "~1" },
                    380:   { 0, "tan(7*pi/4)",   "~-1" },
                    381:
                    382:   { 0, "tan(0)",        "0" },
                    383: };
                    384:
                    385: struct datalist_t {
                    386:   const struct data_t  *data;
                    387:   int                  num;
                    388: };
                    389:
                    390: #define DATALIST(data)  { data, numberof (data) }
                    391:
                    392: struct datalist_t  list_z[] = {
                    393:   DATALIST (data_z),
                    394:   DATALIST (data_zq),
                    395:   DATALIST (data_zfr),
                    396:   DATALIST (data_zqfr),
                    397:   DATALIST (data_zr),
                    398: };
                    399:
                    400: struct datalist_t  list_q[] = {
                    401:   DATALIST (data_q),
                    402:   DATALIST (data_zq),
                    403:   DATALIST (data_zqfr),
                    404: };
                    405:
                    406: struct datalist_t  list_f[] = {
                    407:   DATALIST (data_zfr),
                    408:   DATALIST (data_zqfr),
                    409:   DATALIST (data_fr),
                    410:   DATALIST (data_f),
                    411: };
                    412:
                    413: struct datalist_t  list_r[] = {
                    414:   DATALIST (data_zfr),
                    415:   DATALIST (data_zqfr),
                    416:   DATALIST (data_zr),
                    417:   DATALIST (data_fr),
                    418:   DATALIST (data_r),
                    419: };
                    420:
                    421:
                    422: void
                    423: check_z (void)
                    424: {
                    425:   const struct data_t  *data;
                    426:   mpz_t  a, b, got, want;
                    427:   int    l, i, ret;
                    428:
                    429:   mpz_init (got);
                    430:   mpz_init (want);
                    431:   mpz_init_set_ui (a, 55);
                    432:   mpz_init_set_ui (b, 99);
                    433:
                    434:   for (l = 0; l < numberof (list_z); l++)
                    435:     {
                    436:       data = list_z[l].data;
                    437:
                    438:       for (i = 0; i < list_z[l].num; i++)
                    439:         {
                    440:           if (option_trace)
                    441:             printf ("mpz_expr \"%s\"\n", data[i].expr);
                    442:
                    443:           ret = mpz_expr (got, data[i].base, data[i].expr, a, b, NULL);
                    444:
                    445:           if (data[i].want == NULL)
                    446:             {
                    447:               /* expect to fail */
                    448:               if (ret == MPEXPR_RESULT_OK)
                    449:                 {
                    450:                   printf ("mpz_expr wrong return value, got %d, expected failure\n", ret);
                    451:                   goto error;
                    452:                 }
                    453:             }
                    454:           else
                    455:             {
                    456:               if (mpz_set_str (want, data[i].want, 0) != 0)
                    457:                 {
                    458:                   printf ("Cannot parse wanted value string\n");
                    459:                   goto error;
                    460:                 }
                    461:               if (ret != MPEXPR_RESULT_OK)
                    462:                 {
                    463:                   printf ("mpz_expr failed unexpectedly\n");
                    464:                   printf ("   return value %d\n", ret);
                    465:                   goto error;
                    466:                 }
                    467:               if (mpz_cmp (got, want) != 0)
                    468:                 {
                    469:                   printf ("mpz_expr wrong result\n");
                    470:                   printf ("   got  "); mpz_out_str (stdout, 10, got);
                    471:                   printf ("\n");
                    472:                   printf ("   want "); mpz_out_str (stdout, 10, want);
                    473:                   printf ("\n");
                    474:                   goto error;
                    475:                 }
                    476:             }
                    477:         }
                    478:     }
                    479:   mpz_clear (a);
                    480:   mpz_clear (b);
                    481:   mpz_clear (got);
                    482:   mpz_clear (want);
                    483:   return;
                    484:
                    485:  error:
                    486:   printf ("   base %d\n", data[i].base);
                    487:   printf ("   expr \"%s\"\n", data[i].expr);
                    488:   if (data[i].want != NULL)
                    489:     printf ("   want \"%s\"\n", data[i].want);
                    490:   abort ();
                    491: }
                    492:
                    493: void
                    494: check_q (void)
                    495: {
                    496:   const struct data_t  *data;
                    497:   mpq_t  a, b, got, want;
                    498:   int    l, i, ret;
                    499:
                    500:   mpq_init (got);
                    501:   mpq_init (want);
                    502:   mpq_init (a);
                    503:   mpq_init (b);
                    504:
                    505:   mpq_set_ui (a, 55, 1);
                    506:   mpq_set_ui (b, 99, 1);
                    507:
                    508:   for (l = 0; l < numberof (list_q); l++)
                    509:     {
                    510:       data = list_q[l].data;
                    511:
                    512:       for (i = 0; i < list_q[l].num; i++)
                    513:         {
                    514:           if (option_trace)
                    515:             printf ("mpq_expr \"%s\"\n", data[i].expr);
                    516:
                    517:           ret = mpq_expr (got, data[i].base, data[i].expr, a, b, NULL);
                    518:
                    519:           if (data[i].want == NULL)
                    520:             {
                    521:               /* expect to fail */
                    522:               if (ret == MPEXPR_RESULT_OK)
                    523:                 {
                    524:                   printf ("mpq_expr wrong return value, got %d, expected failure\n", ret);
                    525:                   goto error;
                    526:                 }
                    527:             }
                    528:           else
                    529:             {
                    530:               if (mpz_set_str (mpq_numref(want), data[i].want, 0) != 0)
                    531:                 {
                    532:                   printf ("Cannot parse wanted value string\n");
                    533:                   goto error;
                    534:                 }
                    535:               mpz_set_ui (mpq_denref(want), 1);
                    536:
                    537:               if (ret != MPEXPR_RESULT_OK)
                    538:                 {
                    539:                   printf ("mpq_expr failed unexpectedly\n");
                    540:                   printf ("   return value %d\n", ret);
                    541:                   goto error;
                    542:                 }
                    543:               if (mpq_cmp (got, want) != 0)
                    544:                 {
                    545:                   printf ("mpq_expr wrong result\n");
                    546:                   printf ("   got  "); mpq_out_str (stdout, 10, got);
                    547:                   printf ("\n");
                    548:                   printf ("   want "); mpq_out_str (stdout, 10, want);
                    549:                   printf ("\n");
                    550:                   goto error;
                    551:                 }
                    552:             }
                    553:         }
                    554:     }
                    555:   mpq_clear (a);
                    556:   mpq_clear (b);
                    557:   mpq_clear (got);
                    558:   mpq_clear (want);
                    559:   return;
                    560:
                    561:  error:
                    562:   printf ("   base %d\n", data[i].base);
                    563:   printf ("   expr \"%s\"\n", data[i].expr);
                    564:   if (data[i].want != NULL)
                    565:     printf ("   want \"%s\"\n", data[i].want);
                    566:   abort ();
                    567: }
                    568:
                    569: void
                    570: check_f (void)
                    571: {
                    572:   const struct data_t  *data;
                    573:   mpf_t  a, b, got, want;
                    574:   int    l, i, ret;
                    575:
                    576:   mpf_set_default_prec (200L);
                    577:
                    578:   mpf_init (got);
                    579:   mpf_init (want);
                    580:   mpf_init_set_ui (a, 55);
                    581:   mpf_init_set_ui (b, 99);
                    582:
                    583:   for (l = 0; l < numberof (list_f); l++)
                    584:     {
                    585:       data = list_f[l].data;
                    586:
                    587:       for (i = 0; i < list_f[l].num; i++)
                    588:         {
                    589:           if (option_trace)
                    590:             printf ("mpf_expr \"%s\"\n", data[i].expr);
                    591:
                    592:           ret = mpf_expr (got, data[i].base, data[i].expr, a, b, NULL);
                    593:
                    594:           if (data[i].want == NULL)
                    595:             {
                    596:               /* expect to fail */
                    597:               if (ret == MPEXPR_RESULT_OK)
                    598:                 {
                    599:                   printf ("mpf_expr wrong return value, got %d, expected failure\n", ret);
                    600:                   goto error;
                    601:                 }
                    602:             }
                    603:           else
                    604:             {
                    605:               if (mpf_set_str (want, data[i].want, 0) != 0)
                    606:                 {
                    607:                   printf ("Cannot parse wanted value string\n");
                    608:                   goto error;
                    609:                 }
                    610:
                    611:               if (ret != MPEXPR_RESULT_OK)
                    612:                 {
                    613:                   printf ("mpf_expr failed unexpectedly\n");
                    614:                   printf ("   return value %d\n", ret);
                    615:                   goto error;
                    616:                 }
                    617:               if (mpf_cmp (got, want) != 0)
                    618:                 {
                    619:                   printf ("mpf_expr wrong result\n");
                    620:                   printf ("   got  "); mpf_out_str (stdout, 10, 20, got);
                    621:                   printf ("\n");
                    622:                   printf ("   want "); mpf_out_str (stdout, 10, 20, want);
                    623:                   printf ("\n");
                    624:                   goto error;
                    625:                 }
                    626:             }
                    627:         }
                    628:     }
                    629:   mpf_clear (a);
                    630:   mpf_clear (b);
                    631:   mpf_clear (got);
                    632:   mpf_clear (want);
                    633:   return;
                    634:
                    635:  error:
                    636:   printf ("   base %d\n", data[i].base);
                    637:   printf ("   expr \"%s\"\n", data[i].expr);
                    638:   if (data[i].want != NULL)
                    639:     printf ("   want \"%s\"\n", data[i].want);
                    640:   abort ();
                    641: }
                    642:
                    643: #if HAVE_MPFR
                    644: void
                    645: check_r (void)
                    646: {
                    647:   const struct data_t  *data;
                    648:   mpfr_t  a, b, got, want, diff, tolerance;
                    649:   int     l, i, ret;
                    650:
                    651:   mpfr_set_default_prec (300L);
                    652:
                    653:   mpfr_init (got);
                    654:   mpfr_init (want);
                    655:   mpfr_init (diff);
                    656:   mpfr_init (tolerance);
                    657:   mpfr_init (a);
                    658:   mpfr_init (b);
                    659:   mpfr_set_ui (a, 55L, GMP_RNDZ);
                    660:   mpfr_set_ui (b, 99L, GMP_RNDZ);
                    661:
                    662:   mpfr_set_ui (tolerance, 1L, GMP_RNDZ);
                    663:   mpfr_div_2exp (tolerance, tolerance, 190L, GMP_RNDZ);
                    664:
                    665:   for (l = 0; l < numberof (list_r); l++)
                    666:     {
                    667:       data = list_r[l].data;
                    668:
                    669:       for (i = 0; i < list_r[l].num; i++)
                    670:         {
                    671:           if (option_trace)
                    672:             printf ("mpfr_expr \"%s\"\n", data[i].expr);
                    673:
                    674:           ret = mpfr_expr (got, data[i].base, data[i].expr, a, b, NULL);
                    675:
                    676:           if (data[i].want == NULL)
                    677:             {
                    678:               /* expect to fail */
                    679:               if (ret == MPEXPR_RESULT_OK)
                    680:                 {
                    681:                   printf ("mpfr_expr wrong return value, got %d, expected failure\n", ret);
                    682:                   goto error;
                    683:                 }
                    684:             }
                    685:           else
                    686:             {
                    687:               if (ret != MPEXPR_RESULT_OK)
                    688:                 {
                    689:                   printf ("mpfr_expr failed unexpectedly\n");
                    690:                   printf ("   return value %d\n", ret);
                    691:                   goto error;
                    692:                 }
                    693:
                    694:               /* empty string means don't check the result value as such */
                    695:               if (data[i].want[0] != '\0')
                    696:                 {
                    697:                   const char  *want_str = data[i].want;
                    698:                   int         approx = (*want_str == '~');
                    699:                   want_str += approx;
                    700:                   if (mpfr_set_str (want, want_str, 10, GMP_RNDZ) != 0)
                    701:                     {
                    702:                       printf ("Cannot parse wanted value string\n");
                    703:                       printf ("    \"%s\"\n", want_str);
                    704:                       goto error;
                    705:                     }
                    706:                   if (approx)
                    707:                     {
                    708:                       mpfr_sub (diff, got, want, GMP_RNDZ);
                    709:                       mpfr_abs (diff, diff, GMP_RNDZ);
                    710:                       if (mpfr_cmp (diff, tolerance) >= 0)
                    711:                         {
                    712:                           printf ("mpfr_expr result outside tolerance\n");
                    713:                           printf ("   \"%s\"\n", data[i].expr);
                    714:                           printf ("   got  ");
                    715:                           mpfr_out_str (stdout, 10, 0, got, GMP_RNDZ);
                    716:                           printf ("\n");
                    717:                           printf ("   want ");
                    718:                           mpfr_out_str (stdout, 10, 0, want, GMP_RNDZ);
                    719:                           printf ("\n");
                    720:                           printf ("   diff ");
                    721:                           mpfr_out_str (stdout, 10, 0, diff, GMP_RNDZ);
                    722:                           printf ("\n");
                    723:                           goto error;
                    724:                         }
                    725:                     }
                    726:                   else
                    727:                     {
                    728:                       if (mpfr_cmp (got, want) != 0)
                    729:                         {
                    730:                           printf ("mpfr_expr wrong result\n");
                    731:                           printf ("   \"%s\"\n", data[i].expr);
                    732:                           printf ("   got  ");
                    733:                           mpfr_out_str (stdout, 10, 20, got, GMP_RNDZ);
                    734:                           printf ("\n");
                    735:                           printf ("   want ");
                    736:                           mpfr_out_str (stdout, 10, 20, want, GMP_RNDZ);
                    737:                           printf ("\n");
                    738:                           goto error;
                    739:                         }
                    740:                     }
                    741:                 }
                    742:             }
                    743:         }
                    744:     }
                    745:   mpfr_clear (a);
                    746:   mpfr_clear (b);
                    747:   mpfr_clear (got);
                    748:   mpfr_clear (want);
                    749:   mpfr_clear (diff);
                    750:   mpfr_clear (tolerance);
                    751:
                    752:   if (__mpfr_const_pi_prec != 0)
                    753:     {
                    754:       mpfr_clear (__mpfr_const_pi);
                    755:       __mpfr_const_pi_prec = 0;
                    756:     }
                    757:   if (__mpfr_const_log2_prec != 0)
                    758:     {
                    759:       mpfr_clear (__mpfr_const_log2);
                    760:       __mpfr_const_log2_prec = 0;
                    761:     }
                    762:
                    763:   return;
                    764:
                    765:  error:
                    766:   printf ("   base %d\n", data[i].base);
                    767:   printf ("   expr \"%s\"\n", data[i].expr);
                    768:   if (data[i].want != NULL)
                    769:     printf ("   want \"%s\"\n", data[i].want);
                    770:   abort ();
                    771: }
                    772: #endif
                    773:
                    774:
                    775: int
                    776: main (int argc, char *argv[])
                    777: {
                    778:   tests_start ();
                    779:
                    780:   if (argc >= 2)
                    781:     option_trace = 1;
                    782:
                    783:   check_z ();
                    784:   check_q ();
                    785:   check_f ();
                    786: #if HAVE_MPFR
                    787:   check_r ();
                    788: #endif
                    789:
                    790:   tests_end ();
                    791:   exit (0);
                    792: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>