forked from Mirror/frr
tests: Add more examples to get_result
Signed-off-by: Donald Lee <dlqs@gmx.com>
This commit is contained in:
parent
b664092990
commit
8a04c1e74e
|
@ -1,12 +1,28 @@
|
||||||
|
|
||||||
|
-- Positive testing
|
||||||
|
|
||||||
function foo(a, b)
|
function foo(a, b)
|
||||||
a = a + b
|
a = a + 1
|
||||||
|
b = b + 1
|
||||||
return {
|
return {
|
||||||
a = a,
|
a = a,
|
||||||
b = b,
|
b = b,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function bar(a, b)
|
||||||
|
a = a + 1
|
||||||
|
b = b + 1
|
||||||
|
c = 303
|
||||||
|
return {
|
||||||
|
b = b,
|
||||||
|
c = c,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
function fact(n)
|
function fact(n)
|
||||||
|
-- outer function must return a table
|
||||||
|
-- inner functions can be used to recurse or as helpers
|
||||||
function helper(m)
|
function helper(m)
|
||||||
if m == 0 then
|
if m == 0 then
|
||||||
return 1
|
return 1
|
||||||
|
@ -19,14 +35,20 @@ function fact(n)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Negative testing
|
||||||
|
|
||||||
function bad_return1()
|
function bad_return1()
|
||||||
end
|
end
|
||||||
|
|
||||||
function bad_return2()
|
function bad_return2()
|
||||||
return 1
|
return 123
|
||||||
end
|
end
|
||||||
|
|
||||||
function bad_return3()
|
function bad_return3()
|
||||||
|
return {}
|
||||||
|
end
|
||||||
|
|
||||||
|
function bad_return4()
|
||||||
error("Something bad!")
|
error("Something bad!")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -28,14 +28,30 @@ int main(int argc, char **argv)
|
||||||
struct frrscript *fs = frrscript_new("script1");
|
struct frrscript *fs = frrscript_new("script1");
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
|
/* Positive testing */
|
||||||
|
|
||||||
long long a = 100, b = 200;
|
long long a = 100, b = 200;
|
||||||
|
|
||||||
result = frrscript_load(fs, "foo", NULL);
|
result = frrscript_load(fs, "foo", NULL);
|
||||||
assert(result == 0);
|
assert(result == 0);
|
||||||
result = frrscript_call(fs, "foo", ("a", &a), ("b", &b));
|
result = frrscript_call(fs, "foo", ("a", &a), ("b", &b));
|
||||||
assert(result == 0);
|
assert(result == 0);
|
||||||
assert(a == 300);
|
assert(a == 101);
|
||||||
assert(b == 200);
|
assert(b == 202);
|
||||||
|
|
||||||
|
a = 100, b = 200;
|
||||||
|
|
||||||
|
result = frrscript_load(fs, "bar", NULL);
|
||||||
|
assert(result == 0);
|
||||||
|
result = frrscript_call(fs, "bar", ("a", &a), ("b", &b));
|
||||||
|
assert(result == 0);
|
||||||
|
long long *cptr = frrscript_get_result(fs, "bar", "c", lua_tointegerp);
|
||||||
|
|
||||||
|
/* a should not occur in the returned table in script */
|
||||||
|
assert(a == 100);
|
||||||
|
assert(b == 202);
|
||||||
|
assert(*cptr == 303);
|
||||||
|
XFREE(MTYPE_TMP, cptr);
|
||||||
|
|
||||||
long long n = 5;
|
long long n = 5;
|
||||||
|
|
||||||
|
@ -48,9 +64,7 @@ int main(int argc, char **argv)
|
||||||
assert(*ansptr == 120);
|
assert(*ansptr == 120);
|
||||||
XFREE(MTYPE_TMP, ansptr);
|
XFREE(MTYPE_TMP, ansptr);
|
||||||
|
|
||||||
/* Function does not exist in script file*/
|
/* Negative testing */
|
||||||
result = frrscript_load(fs, "does_not_exist", NULL);
|
|
||||||
assert(result == 1);
|
|
||||||
|
|
||||||
/* Function does not exist in script file*/
|
/* Function does not exist in script file*/
|
||||||
result = frrscript_load(fs, "does_not_exist", NULL);
|
result = frrscript_load(fs, "does_not_exist", NULL);
|
||||||
|
@ -60,6 +74,11 @@ int main(int argc, char **argv)
|
||||||
result = frrscript_call(fs, "does_not_exist", ("a", &a), ("b", &b));
|
result = frrscript_call(fs, "does_not_exist", ("a", &a), ("b", &b));
|
||||||
assert(result == 1);
|
assert(result == 1);
|
||||||
|
|
||||||
|
/* Get result from a function that was not loaded */
|
||||||
|
long long *llptr =
|
||||||
|
frrscript_get_result(fs, "does_not_exist", "c", lua_tointegerp);
|
||||||
|
assert(llptr == NULL);
|
||||||
|
|
||||||
/* Function returns void */
|
/* Function returns void */
|
||||||
result = frrscript_call(fs, "bad_return1");
|
result = frrscript_call(fs, "bad_return1");
|
||||||
assert(result == 1);
|
assert(result == 1);
|
||||||
|
@ -68,9 +87,16 @@ int main(int argc, char **argv)
|
||||||
result = frrscript_call(fs, "bad_return2");
|
result = frrscript_call(fs, "bad_return2");
|
||||||
assert(result == 1);
|
assert(result == 1);
|
||||||
|
|
||||||
/* Function throws exception */
|
/* Get non-existent result from a function */
|
||||||
result = frrscript_call(fs, "bad_return3");
|
result = frrscript_call(fs, "bad_return3");
|
||||||
assert(result == 1);
|
assert(result == 1);
|
||||||
|
long long *cllptr =
|
||||||
|
frrscript_get_result(fs, "bad_return3", "c", lua_tointegerp);
|
||||||
|
assert(cllptr == NULL);
|
||||||
|
|
||||||
|
/* Function throws exception */
|
||||||
|
result = frrscript_call(fs, "bad_return4");
|
||||||
|
assert(result == 1);
|
||||||
|
|
||||||
frrscript_unload(fs);
|
frrscript_unload(fs);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue