Rust: Add a library test for Operations.

This commit is contained in:
Geoffrey White
2025-04-09 20:55:34 +01:00
parent 93f8cea884
commit f64e86fe2e
3 changed files with 87 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
import rust
import utils.test.InlineExpectationsTest
string describe(Expr op) {
op instanceof PrefixExpr and result = "PrefixExpr"
or
op instanceof BinaryExpr and result = "BinaryExpr"
or
op instanceof AssignmentOperation and result = "AssignmentOperation"
or
op instanceof LogicalOperation and result = "LogicalOperation"
or
op instanceof RefExpr and result = "RefExpr"
}
module OperationsTest implements TestSig {
string getARelevantTag() { result = describe(_) }
predicate hasActualResult(Location location, string element, string tag, string value) {
exists(Expr op |
location = op.getLocation() and
location.getFile().getBaseName() != "" and
element = op.toString() and
tag = describe(op) and
value = ""
)
}
}
import MakeTest<OperationsTest>

View File

@@ -0,0 +1,57 @@
// --- tests ---
fn test_operations(
y: i32, a: bool, b: bool,
ptr: &i32, res: Result<i32, ()>) -> Result<(), ()>
{
let mut x: i32;
// simple assignment
x = y; // $ AssignmentOperation BinaryExpr
// comparison operations
x == y; // $ BinaryExpr
x != y; // $ BinaryExpr
x < y; // $ BinaryExpr
x <= y; // $ BinaryExpr
x > y; // $ BinaryExpr
x >= y; // $ BinaryExpr
// arithmetic operations
x + y; // $ BinaryExpr
x - y; // $ BinaryExpr
x * y; // $ BinaryExpr
x / y; // $ BinaryExpr
x % y; // $ BinaryExpr
x += y; // $ AssignmentOperation BinaryExpr
x -= y; // $ AssignmentOperation BinaryExpr
x *= y; // $ AssignmentOperation BinaryExpr
x /= y; // $ AssignmentOperation BinaryExpr
x %= y; // $ AssignmentOperation BinaryExpr
-x; // $ PrefixExpr
// logical operations
a && b; // $ BinaryExpr LogicalOperation
a || b; // $ BinaryExpr LogicalOperation
!a; // $ PrefixExpr LogicalOperation
// bitwise operations
x & y; // $ BinaryExpr
x | y; // $ BinaryExpr
x ^ y; // $ BinaryExpr
x << y; // $ BinaryExpr
x >> y; // $ BinaryExpr
x &= y; // $ AssignmentOperation BinaryExpr
x |= y; // $ AssignmentOperation BinaryExpr
x ^= y; // $ AssignmentOperation BinaryExpr
x <<= y; // $ AssignmentOperation BinaryExpr
x >>= y; // $ AssignmentOperation BinaryExpr
// miscellaneous expressions that might be operations
*ptr; // $ PrefixExpr
&x; // $ RefExpr
res?;
return Ok(());
}