Skip to content

Commit

Permalink
test(utils): sort keys
Browse files Browse the repository at this point in the history
longhorn/longhorn-8430

Signed-off-by: Chin-Ya Huang <[email protected]>
  • Loading branch information
c3y1huang authored and derekbit committed Oct 24, 2024
1 parent b55f2e1 commit 9e7505c
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions utils/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"strings"

"golang.org/x/exp/constraints"
. "gopkg.in/check.v1"

"github.com/longhorn/go-common-libs/test"
Expand Down Expand Up @@ -243,3 +244,65 @@ func (s *TestSuite) TestConvertTypeToString(c *C) {
c.Assert(result, Equals, testCase.expected, Commentf(test.ErrResultFmt, testName))
}
}

func (s *TestSuite) TestSortKeys(c *C) {
// Test cases for base cases
baseTestCases := map[string]testCaseSortKeys[string, any]{
"SortKeys(...): nil map": {
inputMap: nil,
expectError: true,
},
"SortKeys(...): empty map": {
inputMap: map[string]any{},
expected: []string{},
},
}

// Test cases for string keys
stringTestCases := map[string]testCaseSortKeys[string, any]{
"SortKeys(...): string": {
inputMap: map[string]any{
"b": "",
"c": "",
"a": "",
},
expected: []string{"a", "b", "c"},
},
}

// Test cases for uint64 keys
uint64TestCases := map[string]testCaseSortKeys[uint64, any]{
"SortKeys(...): uint64": {
inputMap: map[uint64]any{
2: "",
1: "",
3: "",
},
expected: []uint64{1, 2, 3},
},
}

runTestSortKeys(c, baseTestCases)
runTestSortKeys(c, stringTestCases)
runTestSortKeys(c, uint64TestCases)
}

type testCaseSortKeys[K constraints.Ordered, V any] struct {
inputMap map[K]V
expected []K
expectError bool
}

func runTestSortKeys[K constraints.Ordered, V any](c *C, testCases map[string]testCaseSortKeys[K, V]) {
for testName, tc := range testCases {
c.Logf("Testing utils.%v", testName)
result, err := SortKeys(tc.inputMap)

if tc.expectError {
c.Assert(err, NotNil, Commentf("Expected error in %v", testName))
continue
}
c.Assert(err, IsNil, Commentf("Unexpected error in %v", testName))
c.Assert(result, DeepEquals, tc.expected, Commentf("Unexpected result in %v", testName))
}
}

0 comments on commit 9e7505c

Please sign in to comment.