fix minor code unreachability error#4919
Merged
srenatus merged 2 commits intoopen-policy-agent:mainfrom Jul 21, 2022
Merged
Conversation
Contributor
Author
|
An example: package main
import (
"testing"
)
// LastIndex returns the index of the last instance of x in list, or
// -1 if x is not present.
func LastIndex(list []int, x int) int {
for i := len(list) - 1; i >= 0; i-- {
if list[i] == x {
return i
}
}
return -1
}
func TestLastIndex(t *testing.T) {
tests := []struct {
list []int
x int
want int
}{
{list: []int{1}, x: 1, want: 0},
{list: []int{1, 1}, x: 1, want: 3}, // change want:1 to want:3, should fail
{list: []int{2, 1}, x: 2, want: 0},
{list: []int{1, 2, 1, 1}, x: 2, want: 1},
{list: []int{1, 1, 1, 2, 2, 1}, x: 3, want: -1},
{list: []int{3, 1, 2, 2, 1, 1}, x: 3, want: 111}, // change want:0 to want:111, should fail
}
for i, tt := range tests {
t.Logf("index:%d", i)
if got := LastIndex(tt.list, tt.x); got != tt.want {
t.Errorf("LastIndex(%v, %v) = %v, want %v", tt.list, tt.x, got, tt.want)
}
}
}
func TestLastIndex2(t *testing.T) {
tests := []struct {
list []int
x int
want int
}{
{list: []int{1}, x: 1, want: 0},
{list: []int{1, 1}, x: 1, want: 3}, // change want:1 to want:3, should fail
{list: []int{2, 1}, x: 2, want: 0},
{list: []int{1, 2, 1, 1}, x: 2, want: 1},
{list: []int{1, 1, 1, 2, 2, 1}, x: 3, want: -1},
{list: []int{3, 1, 2, 2, 1, 1}, x: 3, want: 111}, // change want:0 to want:111, should fail
}
for i, tt := range tests {
t.Logf("index:%d", i)
if got := LastIndex(tt.list, tt.x); got != tt.want {
t.Fatalf("LastIndex(%v, %v) = %v, want %v", tt.list, tt.x, got, tt.want) // Fatalf is equivalent to Logf followed by FailNow, so the following code will be unreachable
continue // unreachable
}
}
}
/* output:
=== RUN TestLastIndex
prog.go:32: index:0
prog.go:32: index:1
prog.go:34: LastIndex([1 1], 1) = 1, want 3
prog.go:32: index:2
prog.go:32: index:3
prog.go:32: index:4
prog.go:32: index:5
prog.go:34: LastIndex([3 1 2 2 1 1], 3) = 0, want 111
--- FAIL: TestLastIndex (0.00s)
=== RUN TestLastIndex2
prog.go:53: index:0
prog.go:53: index:1
prog.go:55: LastIndex([1 1], 1) = 1, want 3
--- FAIL: TestLastIndex2 (0.00s)
FAIL
*/ |
Contributor
|
The test failure is because of an unrelated flakey test, but we do need to get DCO fixed, please have a look here |
Signed-off-by: Abirdcfly <fp544037857@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Signed-off-by: Abirdcfly fp544037857@gmail.com
t.Error* will report test failures but continue executing the test.
t.Fatal* will report test failures and stop the test immediately.
So some of the code is actually unreachable