]>
git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_frrscript.c
3 * Copyright (C) 2021 Donald Lee
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "lib/frrscript.h"
23 #include "lib/frrlua.h"
25 int main(int argc
, char **argv
)
27 frrscript_init("./lib");
28 struct frrscript
*fs
= frrscript_new("script1");
31 /* Positive testing */
33 long long a
= 100, b
= 200;
35 result
= frrscript_load(fs
, "foo", NULL
);
37 result
= frrscript_call(fs
, "foo", ("a", &a
), ("b", &b
));
44 result
= frrscript_load(fs
, "bar", NULL
);
46 result
= frrscript_call(fs
, "bar", ("a", &a
), ("b", &b
));
48 long long *cptr
= frrscript_get_result(fs
, "bar", "c", lua_tointegerp
);
50 /* a should not occur in the returned table in script */
54 XFREE(MTYPE_SCRIPT_RES
, cptr
);
58 result
= frrscript_load(fs
, "fact", NULL
);
60 result
= frrscript_call(fs
, "fact", ("n", &n
));
63 frrscript_get_result(fs
, "fact", "ans", lua_tointegerp
);
64 assert(*ansptr
== 120);
66 /* check consecutive call + get_result without re-loading */
68 result
= frrscript_call(fs
, "fact", ("n", &n
));
70 ansptr
= frrscript_get_result(fs
, "fact", "ans", lua_tointegerp
);
71 assert(*ansptr
== 24);
73 XFREE(MTYPE_SCRIPT_RES
, ansptr
);
75 /* Negative testing */
77 /* Function does not exist in script file*/
78 result
= frrscript_load(fs
, "does_not_exist", NULL
);
81 /* Function was not (successfully) loaded */
82 result
= frrscript_call(fs
, "does_not_exist", ("a", &a
), ("b", &b
));
85 /* Get result from a function that was not loaded */
87 frrscript_get_result(fs
, "does_not_exist", "c", lua_tointegerp
);
88 assert(llptr
== NULL
);
90 /* Function returns void */
91 result
= frrscript_call(fs
, "bad_return1");
94 /* Function returns number */
95 result
= frrscript_call(fs
, "bad_return2");
98 /* Get non-existent result from a function */
99 result
= frrscript_call(fs
, "bad_return3");
102 frrscript_get_result(fs
, "bad_return3", "c", lua_tointegerp
);
103 assert(cllptr
== NULL
);
105 /* Function throws exception */
106 result
= frrscript_call(fs
, "bad_return4");
109 frrscript_delete(fs
);