From a9abb6948c2aec00ff633d46a229378511a79d7c Mon Sep 17 00:00:00 2001 From: Shereen Haj Date: Mon, 11 Nov 2024 14:48:37 +0200 Subject: [PATCH] PPC: unit: test sortTopology for expected output The node topology contains several slices that later on are sorted and used to determine whether two nodes are equal. Add a unit test to verify this sorting is done as expected. Signed-off-by: Shereen Haj --- .../profilecreator/profilecreator.go | 6 +- .../profilecreatorfuncs_test.go | 166 ++++++++++++++++++ 2 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 pkg/performanceprofile/profilecreator/profilecreatorfuncs_test.go diff --git a/pkg/performanceprofile/profilecreator/profilecreator.go b/pkg/performanceprofile/profilecreator/profilecreator.go index 27bf80c3f..332adfbae 100644 --- a/pkg/performanceprofile/profilecreator/profilecreator.go +++ b/pkg/performanceprofile/profilecreator/profilecreator.go @@ -270,6 +270,11 @@ func (ghwHandler GHWHandler) SortedTopology() (*topology.Info, error) { if err != nil { return nil, fmt.Errorf("can't obtain topology info from GHW snapshot: %v", err) } + sortTopology(topologyInfo) + return topologyInfo, nil +} + +func sortTopology(topologyInfo *topology.Info) { sort.Slice(topologyInfo.Nodes, func(x, y int) bool { return topologyInfo.Nodes[x].ID < topologyInfo.Nodes[y].ID }) @@ -283,7 +288,6 @@ func (ghwHandler GHWHandler) SortedTopology() (*topology.Info, error) { return node.Cores[i].LogicalProcessors[0] < node.Cores[j].LogicalProcessors[0] }) } - return topologyInfo, nil } // topologyHTDisabled returns topologyinfo in case Hyperthreading needs to be disabled. diff --git a/pkg/performanceprofile/profilecreator/profilecreatorfuncs_test.go b/pkg/performanceprofile/profilecreator/profilecreatorfuncs_test.go new file mode 100644 index 000000000..5d6f59b3d --- /dev/null +++ b/pkg/performanceprofile/profilecreator/profilecreatorfuncs_test.go @@ -0,0 +1,166 @@ +package profilecreator + +import ( + "reflect" + "testing" + + "github.com/jaypipes/ghw/pkg/cpu" + "github.com/jaypipes/ghw/pkg/topology" +) + +func TestSortTopology(t *testing.T) { + testcases := []struct { + name string + topoInfo topology.Info + expected topology.Info + }{ + { + name: "unsorted", + topoInfo: topology.Info{ + Nodes: []*topology.Node{ + { + ID: 2, + Cores: []*cpu.ProcessorCore{ + { + ID: 0, + LogicalProcessors: []int{2, 0, 1}, + }, + { + ID: 3, + LogicalProcessors: []int{11, 9, 10}, + }, + { + ID: 2, + LogicalProcessors: []int{8, 7, 6}, + }, + { + ID: 1, + LogicalProcessors: []int{3, 5, 4}, + }, + }, + }, + { + ID: 1, + Cores: []*cpu.ProcessorCore{ + + { + ID: 2, + LogicalProcessors: []int{8, 7, 6}, + }, + { + ID: 3, + LogicalProcessors: []int{11, 9, 10}, + }, + { + ID: 0, + LogicalProcessors: []int{2, 0, 1}, + }, + { + ID: 1, + LogicalProcessors: []int{3, 5, 4}, + }, + }, + }, + { + ID: 0, + Cores: []*cpu.ProcessorCore{ + { + ID: 1, + LogicalProcessors: []int{3, 5, 4}, + }, + { + ID: 0, + LogicalProcessors: []int{2, 0, 1}, + }, + { + ID: 3, + LogicalProcessors: []int{11, 9, 10}, + }, + { + ID: 2, + LogicalProcessors: []int{8, 7, 6}, + }, + }, + }, + }, + }, + expected: topology.Info{ + Nodes: []*topology.Node{ + { + ID: 0, + Cores: []*cpu.ProcessorCore{ + { + ID: 0, + LogicalProcessors: []int{0, 1, 2}, + }, + { + ID: 1, + LogicalProcessors: []int{3, 4, 5}, + }, + { + ID: 2, + LogicalProcessors: []int{6, 7, 8}, + }, + { + ID: 3, + LogicalProcessors: []int{9, 10, 11}, + }, + }, + }, + { + ID: 1, + Cores: []*cpu.ProcessorCore{ + { + ID: 0, + LogicalProcessors: []int{0, 1, 2}, + }, + { + ID: 1, + LogicalProcessors: []int{3, 4, 5}, + }, + { + ID: 2, + LogicalProcessors: []int{6, 7, 8}, + }, + { + ID: 3, + LogicalProcessors: []int{9, 10, 11}, + }, + }, + }, + { + ID: 2, + Cores: []*cpu.ProcessorCore{ + { + ID: 0, + LogicalProcessors: []int{0, 1, 2}, + }, + { + ID: 1, + LogicalProcessors: []int{3, 4, 5}, + }, + { + ID: 2, + LogicalProcessors: []int{6, 7, 8}, + }, + { + ID: 3, + LogicalProcessors: []int{9, 10, 11}, + }, + }, + }, + }, + }, + }, + } + + for _, tc := range testcases { + t.Run(tc.name, func(t *testing.T) { + sortTopology(&tc.topoInfo) + if !reflect.DeepEqual(tc.topoInfo, tc.expected) { + t.Errorf("expected %+v, got %+v", tc.expected, tc.topoInfo) + } + + }) + } +}