diff options
| -rw-r--r-- | pkg/subsystem/service.go | 13 | ||||
| -rw-r--r-- | pkg/subsystem/service_test.go | 19 |
2 files changed, 30 insertions, 2 deletions
diff --git a/pkg/subsystem/service.go b/pkg/subsystem/service.go index d8c144cc4..24f86dd54 100644 --- a/pkg/subsystem/service.go +++ b/pkg/subsystem/service.go @@ -9,7 +9,8 @@ import ( type Service struct { *Extractor - perName map[string]*Subsystem + perName map[string]*Subsystem + perParent map[*Subsystem][]*Subsystem } func MustMakeService(list []*Subsystem) *Service { @@ -26,6 +27,7 @@ func MustMakeService(list []*Subsystem) *Service { func MakeService(list []*Subsystem) (*Service, error) { extractor := MakeExtractor(list) perName := map[string]*Subsystem{} + perParent := map[*Subsystem][]*Subsystem{} for _, item := range list { if item.Name == "" { return nil, fmt.Errorf("input contains a subsystem without a name") @@ -34,11 +36,14 @@ func MakeService(list []*Subsystem) (*Service, error) { return nil, fmt.Errorf("collision on %#v name", item.Name) } perName[item.Name] = item + for _, p := range item.Parents { + perParent[p] = append(perParent[p], item) + } } - return &Service{ Extractor: extractor, perName: perName, + perParent: perParent, }, nil } @@ -53,3 +58,7 @@ func (s *Service) List() []*Subsystem { } return ret } + +func (s *Service) Children(parent *Subsystem) []*Subsystem { + return append([]*Subsystem{}, s.perParent[parent]...) +} diff --git a/pkg/subsystem/service_test.go b/pkg/subsystem/service_test.go new file mode 100644 index 000000000..2a95b9558 --- /dev/null +++ b/pkg/subsystem/service_test.go @@ -0,0 +1,19 @@ +// Copyright 2023 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +package subsystem + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestServiceChildren(t *testing.T) { + unrelated := &Subsystem{Name: "unrelated"} + parent := &Subsystem{Name: "parent"} + childA := &Subsystem{Name: "childA", Parents: []*Subsystem{parent}} + childB := &Subsystem{Name: "childB", Parents: []*Subsystem{parent}} + service := MustMakeService([]*Subsystem{unrelated, parent, childA, childB}) + assert.ElementsMatch(t, service.Children(parent), []*Subsystem{childA, childB}) +} |
