]>
Commit | Line | Data |
---|---|---|
1e0e4d23 DL |
1 | /* |
2 | * frrscript unit tests | |
3 | * Copyright (C) 2021 Donald Lee | |
4 | * | |
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) | |
8 | * any later version. | |
9 | * | |
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 | |
13 | * more details. | |
14 | * | |
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 | |
18 | */ | |
19 | ||
20 | #include <zebra.h> | |
21 | ||
22 | #include "lib/frrscript.h" | |
5090d724 | 23 | #include "lib/frrlua.h" |
1e0e4d23 DL |
24 | |
25 | int main(int argc, char **argv) | |
26 | { | |
27 | frrscript_init("./lib"); | |
4535b611 DL |
28 | struct frrscript *fs = frrscript_new("script1"); |
29 | int result; | |
1e0e4d23 | 30 | |
8a04c1e7 DL |
31 | /* Positive testing */ |
32 | ||
1e0e4d23 | 33 | long long a = 100, b = 200; |
1e0e4d23 | 34 | |
4535b611 DL |
35 | result = frrscript_load(fs, "foo", NULL); |
36 | assert(result == 0); | |
37 | result = frrscript_call(fs, "foo", ("a", &a), ("b", &b)); | |
1e0e4d23 | 38 | assert(result == 0); |
8a04c1e7 | 39 | assert(a == 101); |
26693b3a | 40 | assert(b == 201); |
8a04c1e7 DL |
41 | |
42 | a = 100, b = 200; | |
43 | ||
44 | result = frrscript_load(fs, "bar", NULL); | |
45 | assert(result == 0); | |
46 | result = frrscript_call(fs, "bar", ("a", &a), ("b", &b)); | |
47 | assert(result == 0); | |
48 | long long *cptr = frrscript_get_result(fs, "bar", "c", lua_tointegerp); | |
49 | ||
50 | /* a should not occur in the returned table in script */ | |
51 | assert(a == 100); | |
26693b3a | 52 | assert(b == 201); |
8a04c1e7 | 53 | assert(*cptr == 303); |
78f1ac25 | 54 | XFREE(MTYPE_SCRIPT_RES, cptr); |
1e0e4d23 | 55 | |
7948c5d2 DL |
56 | long long n = 5; |
57 | ||
5090d724 DL |
58 | result = frrscript_load(fs, "fact", NULL); |
59 | assert(result == 0); | |
7948c5d2 DL |
60 | result = frrscript_call(fs, "fact", ("n", &n)); |
61 | assert(result == 0); | |
5090d724 DL |
62 | long long *ansptr = |
63 | frrscript_get_result(fs, "fact", "ans", lua_tointegerp); | |
64 | assert(*ansptr == 120); | |
78f1ac25 | 65 | XFREE(MTYPE_SCRIPT_RES, ansptr); |
7948c5d2 | 66 | |
8a04c1e7 | 67 | /* Negative testing */ |
7948c5d2 DL |
68 | |
69 | /* Function does not exist in script file*/ | |
4535b611 DL |
70 | result = frrscript_load(fs, "does_not_exist", NULL); |
71 | assert(result == 1); | |
72 | ||
7948c5d2 | 73 | /* Function was not (successfully) loaded */ |
4535b611 DL |
74 | result = frrscript_call(fs, "does_not_exist", ("a", &a), ("b", &b)); |
75 | assert(result == 1); | |
76 | ||
8a04c1e7 DL |
77 | /* Get result from a function that was not loaded */ |
78 | long long *llptr = | |
79 | frrscript_get_result(fs, "does_not_exist", "c", lua_tointegerp); | |
80 | assert(llptr == NULL); | |
81 | ||
7948c5d2 DL |
82 | /* Function returns void */ |
83 | result = frrscript_call(fs, "bad_return1"); | |
84 | assert(result == 1); | |
4535b611 | 85 | |
7948c5d2 DL |
86 | /* Function returns number */ |
87 | result = frrscript_call(fs, "bad_return2"); | |
88 | assert(result == 1); | |
89 | ||
8a04c1e7 | 90 | /* Get non-existent result from a function */ |
7948c5d2 DL |
91 | result = frrscript_call(fs, "bad_return3"); |
92 | assert(result == 1); | |
8a04c1e7 DL |
93 | long long *cllptr = |
94 | frrscript_get_result(fs, "bad_return3", "c", lua_tointegerp); | |
95 | assert(cllptr == NULL); | |
96 | ||
97 | /* Function throws exception */ | |
98 | result = frrscript_call(fs, "bad_return4"); | |
99 | assert(result == 1); | |
4535b611 | 100 | |
64d457d7 | 101 | frrscript_delete(fs); |
ad6e9b85 | 102 | |
1e0e4d23 DL |
103 | return 0; |
104 | } |