mirror of
https://github.com/github/codeql.git
synced 2026-04-25 08:45:14 +02:00
Rust: Add a library test for Operations.
This commit is contained in:
30
rust/ql/test/library-tests/operations/Operations.ql
Normal file
30
rust/ql/test/library-tests/operations/Operations.ql
Normal 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>
|
||||
57
rust/ql/test/library-tests/operations/test.rs
Normal file
57
rust/ql/test/library-tests/operations/test.rs
Normal 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(());
|
||||
}
|
||||
Reference in New Issue
Block a user