-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #169 from Code-Hex/add/attachmentWasDisconnectedWi…
…thError added NetworkDeviceAttachmentWasDisconnected api
- Loading branch information
Showing
10 changed files
with
345 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package sliceutil | ||
|
||
// FindValueByIndex returns the value of the index in s, | ||
// or zero value if not present. | ||
func FindValueByIndex[S ~[]E, E any](s S, idx int) (v E) { | ||
if idx < 0 || idx >= len(s) { | ||
return v // return zero value of type E | ||
} | ||
return s[idx] | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package sliceutil_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/Code-Hex/vz/v3/internal/sliceutil" | ||
) | ||
|
||
func TestFindValueByIndex(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
slice []int | ||
index int | ||
expected int | ||
}{ | ||
{ | ||
name: "Index within range", | ||
slice: []int{1, 2, 3, 4, 5}, | ||
index: 2, | ||
expected: 3, | ||
}, | ||
{ | ||
name: "Index out of range", | ||
slice: []int{1, 2, 3, 4, 5}, | ||
index: 10, | ||
expected: 0, // default value of int | ||
}, | ||
{ | ||
name: "Negative index", | ||
slice: []int{1, 2, 3, 4, 5}, | ||
index: -1, | ||
expected: 0, // default value of int | ||
}, | ||
{ | ||
name: "Empty slice", | ||
slice: []int{}, | ||
index: 0, | ||
expected: 0, // default value of int | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := sliceutil.FindValueByIndex(tt.slice, tt.index) | ||
if result != tt.expected { | ||
t.Errorf("FindValueByIndex(%v, %d) = %v; want %v", tt.slice, tt.index, result, tt.expected) | ||
} | ||
}) | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.