-
Notifications
You must be signed in to change notification settings - Fork 0
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 #106 from Cray-HPE/sort-hardware
Moved hardware sort code to standalone functions
- Loading branch information
Showing
4 changed files
with
139 additions
and
18 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
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,55 @@ | ||
/* | ||
* | ||
* MIT License | ||
* | ||
* (C) Copyright 2023 Hewlett Packard Enterprise Development LP | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a | ||
* copy of this software and associated documentation files (the "Software"), | ||
* to deal in the Software without restriction, including without limitation | ||
* the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
* and/or sell copies of the Software, and to permit persons to whom the | ||
* Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included | ||
* in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR | ||
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | ||
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
* OTHER DEALINGS IN THE SOFTWARE. | ||
* | ||
*/ | ||
package inventory | ||
|
||
// CompareHardwareByTypeThenLocation returns true if hw1 should sort before h2 | ||
// otherwise it returns false | ||
func CompareHardwareByTypeThenLocation(hw1 *Hardware, hw2 *Hardware) bool { | ||
if hw1.Type == hw2.Type { | ||
return CompareLocationPath(hw1.LocationPath, hw2.LocationPath) | ||
} | ||
return hw1.Type < hw2.Type | ||
} | ||
|
||
// CompareLocationPath returns true if location1 should be before location2 when | ||
// sorted. This func only compares the location ordinals, and thus it assumes | ||
// that the locations are for the same type of hardware. | ||
func CompareLocationPath(location1 LocationPath, location2 LocationPath) bool { | ||
len2 := len(location2) | ||
for i, loc1 := range location1 { | ||
if i >= len2 { | ||
return false | ||
} | ||
|
||
loc2 := location2[i] | ||
if loc1.Ordinal == loc2.Ordinal { | ||
continue // go to the next location | ||
} | ||
return loc1.Ordinal < loc2.Ordinal | ||
} | ||
// This case means that the two locations contain the same list of ordinals | ||
return false | ||
} |
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,81 @@ | ||
/* | ||
* | ||
* MIT License | ||
* | ||
* (C) Copyright 2023 Hewlett Packard Enterprise Development LP | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a | ||
* copy of this software and associated documentation files (the "Software"), | ||
* to deal in the Software without restriction, including without limitation | ||
* the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
* and/or sell copies of the Software, and to permit persons to whom the | ||
* Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included | ||
* in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR | ||
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | ||
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
* OTHER DEALINGS IN THE SOFTWARE. | ||
* | ||
*/ | ||
package inventory | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/Cray-HPE/cani/pkg/hardwaretypes" | ||
) | ||
|
||
func buildLocation(ordinals ...int) LocationPath { | ||
loc := LocationPath{} | ||
for _, ordinal := range ordinals { | ||
loc = append(loc, LocationToken{Ordinal: ordinal}) | ||
} | ||
return loc | ||
} | ||
|
||
func TestCompareHardwareByTypeThenLocation(t *testing.T) { | ||
hw1 := Hardware{Type: hardwaretypes.Cabinet} | ||
|
||
hw2 := Hardware{Type: hardwaretypes.Node} | ||
|
||
v := CompareHardwareByTypeThenLocation(&hw1, &hw1) | ||
if v { | ||
t.Fatalf("Expected false when comparing same hardware") | ||
} | ||
|
||
v = CompareHardwareByTypeThenLocation(&hw1, &hw2) | ||
if !v { | ||
t.Fatalf("Expected true when comparing Cabinet to Node, hw1: %v, hw2: %v", hw1, hw2) | ||
} | ||
|
||
v = CompareHardwareByTypeThenLocation(&hw2, &hw1) | ||
if v { | ||
t.Fatalf("Expected false when comparing Node to Cabinet, hw1: %v, hw2: %v", hw2, hw1) | ||
} | ||
|
||
hw3 := Hardware{Type: hardwaretypes.Node} | ||
hw2.LocationPath = buildLocation(1, 2, 3) | ||
hw3.LocationPath = buildLocation(1, 2, 3) | ||
v = CompareHardwareByTypeThenLocation(&hw2, &hw3) | ||
if v { | ||
t.Fatalf("Expected false when comparing same hardware with locations, hw1: %v, hw2: %v", hw2, hw3) | ||
} | ||
|
||
hw2.LocationPath = buildLocation(1, 2, 3) | ||
hw3.LocationPath = buildLocation(1, 2, 4) | ||
v = CompareHardwareByTypeThenLocation(&hw2, &hw3) | ||
if !v { | ||
t.Fatalf("Expected true when comparing hardware with locations, hw1: %v, hw2: %v", hw2, hw3) | ||
} | ||
|
||
v = CompareHardwareByTypeThenLocation(&hw3, &hw2) | ||
if v { | ||
t.Fatalf("Expected false when comparing hardware with locations, hw1: %v, hw2: %v", hw3, hw2) | ||
} | ||
} |