home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c160 / 1.ddi / TEST / T4EXPR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-22  |  9.3 KB  |  435 lines

  1. /* t4expr.c
  2.  
  3.    (c)Copyright Sequiter Software Inc., 1987-1990. All rights reserved.
  4.  
  5.    Tests Code Base Expression Evaluation Routines.
  6. */
  7.  
  8. #include "p4misc.h"
  9. #include "d4all.h"
  10. #include "w4.h"
  11. #include "u4error.h"
  12.  
  13. #include <string.h> 
  14.  
  15. static FIELD testfields[] =
  16. {
  17.    /* Name  ,  Type,  Width,  Dec,  Offset */
  18.    {"N1",   'F',      5,    2,    0    },
  19.    {"N2",   'N',     10,    0,    0    },
  20.    {"C1",   'C',     10,    0,    0    },
  21.    {"C2",   'C',     10,    0,    0    },
  22.    {"C3",   'C',     10,    0,    0    },
  23.    {"C4",   'C',     15,    0,    0    },
  24.    {"C5",   'C',     15,    0,    0    },
  25.    {"D1",   'D',      8,    0,    0    },
  26.    {"D2",   'D',      8,    0,    0    },
  27.    {"L1",   'L',      1,    0,    0    },
  28.    {"L2",   'L',      1,    0,    0    }
  29. };
  30.  
  31. typedef struct test_expr_st
  32. {
  33.    char  expr[30] ;
  34.    char  result[30] ;
  35.    char  result_type ;
  36. }  TEST_EXPR ;
  37.  
  38.  
  39. static TEST_EXPR test_expr[] =
  40. {
  41.    {  "5>4.AND.6.7>4.3              ",
  42.       "1                            ",
  43.       'L'
  44.    },
  45.  
  46.    {  ".NOT. .F.                    ",
  47.       "1                            ",
  48.       'L'
  49.    },
  50.  
  51.    {  "\"NEW   \"  -  \"ME \"           ",
  52.       "NEWME    \0                   ",
  53.       'C'
  54.    },
  55.  
  56.    {  ".T. .AND. .F.                ",
  57.       "0                            ",
  58.       'L'
  59.    },
  60.  
  61.    {  "IIF(C1=\"SMUR\",\"TRUE\",\"FALSE\")",
  62.       "FALSE\0                       ",
  63.       'C'
  64.    },
  65.  
  66.    {  "((((7))))*(1+2)              ",   
  67.       "21                           ",
  68.       'N'
  69.    },
  70.  
  71.    {  "N1+N2                        ",
  72.       "12                           ",
  73.       'N'
  74.    },
  75.  
  76.    {  "N1-N2                        ", 
  77.       "8                            ",
  78.       'N'
  79.    },
  80.  
  81.    {  "N1*N2                        ",
  82.       "20                           ",
  83.       'N'
  84.    },
  85.  
  86.    {  "N1/N2                        ",
  87.       "5                            ",
  88.       'N'
  89.    },
  90.  
  91.    #ifndef NO_POW
  92.    #ifndef ZORTECH
  93.    /* Zortech fails because Zortech 'pow' does not round the result. */
  94.    {  "N1^N2                        ", 
  95.       "100                          ",
  96.       'N'
  97.    },
  98.  
  99.    {  "2^4                          ",  
  100.       "16                           ", 
  101.       'N'
  102.    },
  103.  
  104.    {  "3**5                         ",  
  105.       "243                          ",
  106.       'N'
  107.    },
  108.    #endif
  109.    #endif
  110.  
  111.    {  "C1+C2                        ",
  112.       "character1CHARACTER2\0        ",
  113.       'C'
  114.    },
  115.  
  116.    {  "C1-C2                        ", 
  117.       "character1CHARACTER2\0        ",
  118.       'C'
  119.    },
  120.  
  121.    {  "C1=C2                        ",
  122.       "0                            ",
  123.       'L'
  124.    },
  125.  
  126.    {  "C1=C3                        ", 
  127.       "0                            ",
  128.       'L'
  129.    },
  130.  
  131.    {  "C1>C2                        ",
  132.       "1                            ",
  133.       'L'
  134.    },
  135.  
  136.    {  "C1<C2                        ",
  137.       "0                            ",
  138.       'L'
  139.    },
  140.  
  141.    {  "C1<>C2                       ",
  142.       "1                            ",
  143.       'L'
  144.    },
  145.  
  146.    {  "C1#C2                        ", 
  147.       "1                            ",
  148.       'L'
  149.    },
  150.  
  151.    {  "C1<>C3                       ",
  152.       "1                            ",
  153.       'L'
  154.    },
  155.  
  156.    {  "C1$C2                        ",
  157.       "0                            ",
  158.       'L'
  159.    },
  160.  
  161.    {  "\"ST\"$\"MOST\"                  ",
  162.       "1                            ",
  163.       'L'
  164.    },
  165.  
  166.    {  "CTOD(\"02/01/88\")             ",
  167.       "19880201\0                    ",
  168.       'D'
  169.    },
  170.  
  171.    {  "STOD(C4)                     ",
  172.       "19880201\0                    ",
  173.       'D'
  174.    },
  175.  
  176.    {  "DTOC(D2)                     ",
  177.       "03/23/88\0                    ",
  178.       'C'
  179.    },
  180.  
  181.    {  "DTOS(D2)                     ",
  182.       "19880323\0                    ",
  183.       'C'
  184.    },
  185.  
  186.    {  "D1=STOD(C4)                  ",
  187.       "1                            ",
  188.       'L'
  189.    },
  190.  
  191.    {  "C5=DTOS(D2)                  ", 
  192.       "1                            ",
  193.       'L'
  194.    },
  195.  
  196.    {  "IIF(C1=\"SMUR\",\"TRUE\",\"FALSE\")",
  197.       "FALSE\0                       ",
  198.       'C'
  199.    },
  200.  
  201.    {  "RECCOUNT()                   ",
  202.       "1                            ",
  203.       'N'
  204.    },
  205.  
  206.    {  "RECNO()                      ",
  207.       "1                            ",
  208.       'N'
  209.    },
  210.  
  211.    #ifndef ZORTECH
  212.    {  "STR(22.2,5,1)                ",
  213.       " 22.2\0                       ",
  214.       'C'
  215.    },
  216.    #endif
  217.  
  218.    {  "STR(5.7,4,2)                 ",
  219.       "5.70\0                        ",
  220.       'C'
  221.    },
  222.  
  223.    #ifndef ZORTECH
  224.    {  "STR(21.5,6,0)                ",
  225.       "    22\0                      ",
  226.       'C'
  227.    },
  228.    #endif
  229.  
  230.    {  "VAL(\"2323\")                  ",
  231.       "2323                         ",
  232.       'N'
  233.    },
  234.  
  235.    {  "SUBSTR(C1,2,2)               ",
  236.       "ha\0                          ",
  237.       'C'
  238.    },
  239.  
  240.    {  "3*4-2                        ",  
  241.       "10                           ",
  242.       'N'
  243.    },
  244.  
  245.    {  "L1                           ",  
  246.       "0                            ",
  247.       'L'
  248.    },
  249.  
  250.    {  ".NOT. .T.                    ",  
  251.       "0                            ",
  252.       'L'
  253.    },
  254.  
  255.    {  ".NOT. .F.                    ",  
  256.       "1                            ", 
  257.       'L'
  258.    },
  259.  
  260.    {  ".T.                          ",  
  261.       "1                            ",
  262.       'L'
  263.    },
  264.  
  265.    {  ".T. .OR. .F.                 ",  
  266.       "1                            ",
  267.       'L'
  268.    },
  269.  
  270.    {  ".T. .AND. .F.                ",  
  271.       "0                            ",
  272.       'L'
  273.    },
  274.  
  275.    {  "\"A \" + \"B   \"                ",  
  276.       "A B   \0                      ",
  277.       'C'
  278.    },
  279.  
  280.    {  "\"NEW   \"  -  \"ME \"           ",
  281.       "NEWME    \0                   ",
  282.       'C'
  283.    },
  284.  
  285.    {  "\"    \" - \" ab\"               ",
  286.       " ab    \0                     ",
  287.       'C'
  288.    },
  289.  
  290.    {  "((((7))))*(1+2)              ",  
  291.       "21                           ",
  292.       'N'
  293.    },
  294.  
  295.    {  " IIF( .F. , 1, 2)            ",  
  296.       "2                            ",
  297.       'N'
  298.    },
  299.  
  300.    {  "   IIF ( .T., \"GOOD\", \"BAD\" )",
  301.       "GOOD\0                        ",
  302.       'C'
  303.    },
  304.  
  305.    {  "\0                            ",
  306.       "                             ",         
  307.       '\0'
  308.    },
  309.  
  310. };
  311.  
  312.  
  313. main()
  314. {
  315.    int    logical, i ;
  316.    double d ;
  317.    void   *ptr ;
  318.  
  319.    #ifdef NO_HUGE
  320.       #ifdef IS_386
  321.          d4init() ;
  322.       #else
  323.          d4initialize( 4, 4, 20, 1000, 20000L ) ;
  324.       #endif
  325.    #else
  326.       d4init() ;
  327.    #endif
  328.  
  329.    if ( d4create( "T4EXPR", 11, testfields, 0) < 0 )
  330.    {
  331.       u4error( 0, "t4expr:  d4create", (char *) 0 ) ;
  332.       w4exit(1) ;
  333.    }
  334.  
  335.    f4replace( f4ref("c1"), "character1" );
  336.    f4replace( f4ref("c2"), "CHARACTER2" );
  337.    f4replace( f4ref("c3"), "ChArAcTeR3" );
  338.    f4replace( f4ref("c4"), "19880201" );
  339.    f4replace( f4ref("c5"), "19880323" );
  340.  
  341.    d = 10.0 ;   f4replace( f4ref("n1"), &d ) ;
  342.    d = 2.0 ;    f4replace( f4ref("n2"), &d );
  343.  
  344.    f4replace( f4ref("d1"), "19880201" );
  345.    f4replace( f4ref("d2"), "19880323" );
  346.  
  347.    logical = 0 ;   f4replace( f4ref("l1"), &logical ) ;
  348.    logical = 1 ;   f4replace( f4ref("l2"), &logical );
  349.  
  350.    if ( d4append() < 0 ) 
  351.    {
  352.       u4error( 0, "t4expr:  d4write", (char *) 0 ) ;
  353.       w4exit(1) ;
  354.    }
  355.  
  356.    for ( i=0; test_expr[i].result_type != '\0'; i++ )
  357.    {
  358.       w4( w4row()+1,0,  "\nOn Expression: " ) ;
  359.       w4( w4row(),0,  test_expr[i].expr ) ;
  360.  
  361.       ptr =  e4eval( test_expr[i].expr ) ;
  362.       if ( ptr == (void *) 0 )
  363.       {
  364.          u4error( 0, "t4expr:  e4eval", (char *) 0 ) ;
  365.          w4exit(1) ;
  366.       }
  367.  
  368.       if ( e4type() != test_expr[i].result_type )
  369.       {
  370.          u4error( 0, "t4expr:  e4type", (char *) 0 ) ;
  371.          w4exit(1) ;
  372.       }
  373.  
  374.       switch (e4type())
  375.       {
  376.          case 'C' :
  377.         if ( strcmp( (char *) ptr, test_expr[i].result) != 0 )
  378.             {
  379.                u4error( 0, "t4expr  EXPR:", test_expr[i].expr, (char *) 0 ) ;
  380.                w4exit(1) ;
  381.             }
  382.             break ;
  383.  
  384.            case 'L' :
  385.             if ( (*(int *) ptr) && ! c4atoi( test_expr[i].result,30 )  ||
  386.                         ! (*(int *) ptr) && c4atoi( test_expr[i].result,30 ) )
  387.             {
  388.                u4error( 0, "t4expr  EXPR:", test_expr[i].expr, (char *) 0 ) ;
  389.                w4exit(1) ;
  390.             }
  391.               break;
  392.       
  393.            case 'N' :
  394.            case 'F' :
  395.         if ( (*(double *) ptr) != c4atod(test_expr[i].result,30))
  396.             {
  397.                u4error( 0, "t4expr  EXPR:", test_expr[i].expr, (char *) 0 ) ;
  398.                w4exit(1) ;
  399.             }
  400.               break;
  401.  
  402.            case 'D' :
  403.         if ( strncmp((char *) ptr, test_expr[i].result, 8 ) != 0 )
  404.             {
  405.                u4error( 0, "t4expr  EXPR:", test_expr[i].expr, (char *) 0 ) ;
  406.                w4exit(1) ;
  407.             }
  408.               break;
  409.       }
  410.    }
  411.  
  412.    d4close_all() ;
  413.  
  414.    #ifdef H4TEST
  415.    {
  416.       int  rc ;
  417.       d4init_undo() ;
  418.       rc = h4free_check(32000) ;
  419.       d4init() ;
  420.       if ( rc != 0 )
  421.       {
  422.          u4error( 0, "t4expr:  Memory items not freed", (char *) 0 );
  423.          w4exit(1) ;
  424.       }
  425.    }
  426.    #endif
  427.  
  428.    w4handle(1) ;
  429.    w4( w4row()+1,0, "t4expr:  SUCCESS" ) ;
  430.  
  431.    w4exit(0) ;
  432. }
  433.  
  434.  
  435.