Skip to content

Commit

Permalink
Add systematic test for if statements
Browse files Browse the repository at this point in the history
  • Loading branch information
rillig committed Nov 6, 2022
1 parent d9b9938 commit e7acc2c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 22 deletions.
43 changes: 32 additions & 11 deletions testdata/instrumenter/IfStmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,42 @@ package instrumenter

// https://go.dev/ref/spec#If_statements

// TODO: Add systematic tests.

// The condition from an if statement is always a boolean expression.
// And even if the condition is a simple variable, it is wrapped.
// This is different from arguments to function calls, where simple
// variables are not wrapped.
func ifStmt(a int, b string, c bool) bool {
if a > 0 && b == "positive" {
func ifStmt(i int, s string, cond bool) bool {

if i > 0 && s == "positive" {
return true
}
if len(b) > 5 {
return len(b) > 10

if len(s) > 5 {
return len(s) > 10
}
if c {

// The condition from an if statement is always a boolean expression.
// And even if the condition is a simple variable, it is wrapped.
// This is different from arguments to function calls, where simple
// variables are not wrapped.
if cond {
return true
}

// An if statement, like a switch statement, can have an initializer
// statement. Other than in a switch statement, the condition in an if
// statement is used exactly once, so there is no need to introduce a new
// variable. Therefore, no complicated rewriting is needed.

if i++; cond {
return i > 5
}

if i := i + 1; cond {
return i > 6
}

// Conditions in the initializer are instrumented as well.
// TODO: Instrument the initialization before the condition.
if cond := i > 7; cond {
return i > 8
}

return false
}
45 changes: 34 additions & 11 deletions testdata/instrumenter/IfStmt.gobco
Original file line number Diff line number Diff line change
@@ -1,21 +1,44 @@
package instrumenter

func ifStmt(a int, b string, c bool) bool {
if gobcoCover(0, gobcoCover(1, a > 0) && gobcoCover(2, b == "positive")) {
func ifStmt(i int, s string, cond bool) bool {

if gobcoCover(0, gobcoCover(1, i > 0) && gobcoCover(2, s == "positive")) {
return true
}
if gobcoCover(3, len(b) > 5) {
return gobcoCover(4, len(b) > 10)

if gobcoCover(3, len(s) > 5) {
return gobcoCover(4, len(s) > 10)
}
if gobcoCover(5, c) {

if gobcoCover(5, cond) {
return true
}

if i++; gobcoCover(6, cond) {
return gobcoCover(7, i > 5)
}

if i := i + 1; gobcoCover(8, cond) {
return gobcoCover(9, i > 6)
}

if cond := gobcoCover(11, i > 7); gobcoCover(10, cond) {
return gobcoCover(12, i > 8)
}

return false
}

// :12:5: "a > 0 && b == \"positive\""
// :12:5: "a > 0"
// :12:14: "b == \"positive\""
// :15:5: "len(b) > 5"
// :16:10: "len(b) > 10"
// :18:5: "c"
// :7:5: "i > 0 && s == \"positive\""
// :7:5: "i > 0"
// :7:14: "s == \"positive\""
// :11:5: "len(s) > 5"
// :12:10: "len(s) > 10"
// :19:5: "cond"
// :28:10: "cond"
// :29:10: "i > 5"
// :32:17: "cond"
// :33:10: "i > 6"
// :38:20: "cond"
// :38:13: "i > 7"
// :39:10: "i > 8"

0 comments on commit e7acc2c

Please sign in to comment.