mirror of
https://github.com/zeek/zeek.git
synced 2025-10-05 16:18:19 +00:00
Add more language tests
Added tests for the conditional operator, operator precedence, modules ("module" and "export" keywords, and the "::" operator), and for the "copy" keyword. Also improved tests of max/min values of int, count, and double constants.
This commit is contained in:
parent
90281a2423
commit
621a90d248
14 changed files with 312 additions and 9 deletions
66
testing/btest/language/conditional-expression.bro
Normal file
66
testing/btest/language/conditional-expression.bro
Normal file
|
@ -0,0 +1,66 @@
|
|||
# @TEST-EXEC: bro %INPUT >out
|
||||
# @TEST-EXEC: btest-diff out
|
||||
|
||||
function test_case(msg: string, expect: bool)
|
||||
{
|
||||
print fmt("%s (%s)", msg, expect ? "PASS" : "FAIL");
|
||||
}
|
||||
|
||||
global ct: count;
|
||||
|
||||
function f1(): bool
|
||||
{
|
||||
ct += 1;
|
||||
return T;
|
||||
}
|
||||
|
||||
function f2(): bool
|
||||
{
|
||||
ct += 4;
|
||||
return F;
|
||||
}
|
||||
|
||||
|
||||
event bro_init()
|
||||
{
|
||||
local a: count;
|
||||
local b: count;
|
||||
local res: count;
|
||||
local res2: bool;
|
||||
|
||||
# Test that the correct operand is evaluated
|
||||
|
||||
a = b = 0;
|
||||
res = T ? ++a : ++b;
|
||||
test_case( "true condition", a == 1 && b == 0 && res == 1);
|
||||
|
||||
a = b = 0;
|
||||
res = F ? ++a : ++b;
|
||||
test_case( "false condition", a == 0 && b == 1 && res == 1);
|
||||
|
||||
# Test again using function calls as operands
|
||||
|
||||
ct = 0;
|
||||
res2 = ct == 0 ? f1() : f2();
|
||||
test_case( "true condition", ct == 1 && res2 == T);
|
||||
|
||||
ct = 0;
|
||||
res2 = ct != 0 ? f1() : f2();
|
||||
test_case( "false condition", ct == 4 && res2 == F);
|
||||
|
||||
# Test that the conditional operator is right-associative
|
||||
|
||||
ct = 0;
|
||||
T ? f1() : T ? f1() : f2();
|
||||
test_case( "associativity", ct == 1 );
|
||||
|
||||
ct = 0;
|
||||
T ? f1() : (T ? f1() : f2());
|
||||
test_case( "associativity", ct == 1 );
|
||||
|
||||
ct = 0;
|
||||
(T ? f1() : T) ? f1() : f2();
|
||||
test_case( "associativity", ct == 2 );
|
||||
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue