Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Basic PScript tests...
0003  *
0004  * @file atmiclt27.c
0005  */
0006 /* -----------------------------------------------------------------------------
0007  * Enduro/X Middleware Platform for Distributed Transaction Processing
0008  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0009  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0010  * This software is released under one of the following licenses:
0011  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0012  * See LICENSE file for full text.
0013  * -----------------------------------------------------------------------------
0014  * AGPL license:
0015  *
0016  * This program is free software; you can redistribute it and/or modify it under
0017  * the terms of the GNU Affero General Public License, version 3 as published
0018  * by the Free Software Foundation;
0019  *
0020  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0021  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0022  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0023  * for more details.
0024  *
0025  * You should have received a copy of the GNU Affero General Public License along 
0026  * with this program; if not, write to the Free Software Foundation, Inc.,
0027  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0028  *
0029  * -----------------------------------------------------------------------------
0030  * A commercial use license is available from Mavimax, Ltd
0031  * contact@mavimax.com
0032  * -----------------------------------------------------------------------------
0033  */
0034 #include <string.h>
0035 #include <stdio.h>
0036 #include <stdlib.h>
0037 #include <memory.h>
0038 
0039 #include <atmi.h>
0040 #include <ubf.h>
0041 #include <ndebug.h>
0042 #include <test.fd.h>
0043 #include <ndrstandard.h>
0044 #include "test027.h"
0045 #include <pscript.h>
0046 #include <stdarg.h>     /* va_list, va_start, va_arg, va_end */
0047 #include <cgreen/cgreen.h>
0048 
0049 /*---------------------------Externs------------------------------------*/
0050 /*---------------------------Macros-------------------------------------*/
0051 /*---------------------------Enums--------------------------------------*/
0052 /*---------------------------Typedefs-----------------------------------*/
0053 /*---------------------------Globals------------------------------------*/
0054 /*---------------------------Statics------------------------------------*/
0055 /*---------------------------Prototypes---------------------------------*/
0056 
0057 void printfunc(HPSCRIPTVM v,const PSChar *s,...)
0058 {
0059     va_list vl;
0060     va_start(vl, s);
0061     vfprintf(stdout, s, vl);
0062     va_end(vl);
0063 }
0064 
0065 
0066 void errorfunc(HPSCRIPTVM v,const PSChar *s,...)
0067 {
0068     va_list vl;
0069     va_start(vl, s);
0070     vfprintf(stderr, s, vl);
0071     va_end(vl);
0072 }
0073 
0074 //int main(int argc, char* argv[])
0075 Ensure(basic_script_func_call)
0076 {
0077     HPSCRIPTVM v;
0078     v = ps_open(1024); //creates a VM with initial stack size 1024
0079 
0080     const PSChar *err;
0081     ps_setprintfunc(v,printfunc,errorfunc);
0082 
0083     char *script = "function testHello(name){return name+\" Hello World\";}";
0084     const PSChar *s;
0085     //do some stuff with pscript here
0086     if (PS_FAILED(ps_compilebuffer(v, script, strlen(script), "hello.psc", PSTrue)))
0087     {
0088         printf("Failed to compile...\n");
0089 
0090         if(PS_SUCCEEDED(ps_getstring(v,-1,&err)))
0091         {
0092             printf(_SC("Error [%s]\n"),err);
0093             assert_true(0); //FAIL
0094             return;
0095         }
0096     }
0097     else
0098     {
0099        ps_pushroottable(v);
0100     }
0101 
0102     /* Load the function */
0103     if (PS_FAILED(ps_call(v,1,PSTrue, PSTrue)))
0104     {
0105         printf("Failed to load...\n");
0106         ps_getlasterror(v);
0107         if(PS_SUCCEEDED(ps_getstring(v,-1,&err)))
0108         {
0109             printf(_SC("Error [%s]\n"),err);
0110             assert_true(0); //FAIL
0111             return;
0112         }
0113     }
0114 
0115     /* Finally call the stuff .*/
0116     ps_pushroottable(v);
0117     ps_pushstring(v,"testHello",-1);
0118     ps_get(v,-2); /* get the function from the root table */
0119     /* what is this? */
0120     ps_pushroottable(v);
0121     ps_pushstring(v, "Jimbo", -1);
0122     
0123     //Use this no params given to script
0124     //ps_push(v,-4);
0125 
0126     if (PS_FAILED(ps_call(v,2,PSTrue, PSTrue)))
0127     {
0128         printf("Failed to call...\n");
0129         ps_getlasterror(v);
0130         if(PS_SUCCEEDED(ps_getstring(v,-1,&err)))
0131         {
0132               printf(_SC("Error [%s]\n"),err);
0133               assert_true(0); //FAIL
0134               return;
0135         }
0136     }
0137     
0138     ps_getstring(v,-1,&s);
0139     printf("Got result: [%s]\n", s);
0140     assert_string_equal(s, "Jimbo Hello World");
0141     
0142     ps_pop(v,3); /* pops the roottable and the function*/
0143 
0144     ps_close(v);
0145 }
0146 
0147 
0148 TestSuite *pscrit_test_all(void)
0149 {
0150     TestSuite *suite = create_test_suite();
0151     add_test(suite, basic_script_func_call);
0152 
0153     return suite;
0154 }
0155 
0156 /*
0157  * Main test case entry
0158  */
0159 int main(int argc, char** argv)
0160 {
0161 
0162     int ret;
0163     TestSuite *suite = create_test_suite();
0164 
0165     add_suite(suite, pscrit_test_all());
0166 
0167 
0168     if (argc > 1) {
0169         ret=run_single_test(suite,argv[1],create_text_reporter());
0170     }
0171     else
0172     {
0173         ret=run_test_suite(suite, create_text_reporter());
0174     }
0175 
0176     destroy_test_suite(suite);
0177 
0178     return ret;
0179 }
0180 
0181 /* vim: set ts=4 sw=4 et smartindent: */