zeek/testing/btest/language/conditional-expression.zeek
Robin Sommer 789cb376fd GH-239: Rename bro to zeek, bro-config to zeek-config, and bro-path-dev to zeek-path-dev.
This also installs symlinks from "zeek" and "bro-config" to a wrapper
script that prints a deprecation warning.

The btests pass, but this is still WIP. broctl renaming is still
missing.

#239
2019-05-01 21:43:45 +00:00

66 lines
1.2 KiB
Text

# @TEST-EXEC: zeek -b %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 zeek_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 );
}