Skip to content
Prev Previous commit
Next Next commit
Rust: Add a library test for Operations.
  • Loading branch information
geoffw0 committed May 2, 2025
commit f64e86fe2e661d88b892c483e7d8fe7015d39c9b
Empty file.
30 changes: 30 additions & 0 deletions rust/ql/test/library-tests/operations/Operations.ql
Original file line number Diff line number Diff line change
@@ -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>
57 changes: 57 additions & 0 deletions rust/ql/test/library-tests/operations/test.rs
Original file line number Diff line number Diff line change
@@ -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(());
}