forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorting_test.go
134 lines (113 loc) · 4.13 KB
/
sorting_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package interfaces_test
import (
"sort"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/interfaces"
"github.com/snapcore/snapd/interfaces/ifacetest"
)
type SortingSuite struct{}
var _ = Suite(&SortingSuite{})
func newConnRef(plugSnap, plug, slotSnap, slot string) *interfaces.ConnRef {
return &interfaces.ConnRef{PlugRef: interfaces.PlugRef{Snap: plugSnap, Name: plug}, SlotRef: interfaces.SlotRef{Snap: slotSnap, Name: slot}}
}
func (s *SortingSuite) TestByInterfaceName(c *C) {
list := []interfaces.Interface{
&ifacetest.TestInterface{InterfaceName: "iface-2"},
&ifacetest.TestInterface{InterfaceName: "iface-1"},
}
sort.Sort(interfaces.ByInterfaceName(list))
c.Assert(list, DeepEquals, []interfaces.Interface{
&ifacetest.TestInterface{InterfaceName: "iface-1"},
&ifacetest.TestInterface{InterfaceName: "iface-2"},
})
}
func (s *SortingSuite) TestByConnRef(c *C) {
list := []*interfaces.ConnRef{
newConnRef("name-1", "plug-3", "name-2", "slot-1"),
newConnRef("name-1", "plug-1", "name-2", "slot-3"),
newConnRef("name-1", "plug-2", "name-2", "slot-2"),
newConnRef("name-1", "plug-1", "name-2", "slot-4"),
newConnRef("name-1", "plug-1", "name-2", "slot-1"),
newConnRef("name-1", "plug-1", "name-2_instance", "slot-1"),
newConnRef("name-1_instance", "plug-1", "name-2", "slot-1"),
}
sort.Sort(interfaces.ByConnRef(list))
c.Assert(list, DeepEquals, []*interfaces.ConnRef{
newConnRef("name-1", "plug-1", "name-2", "slot-1"),
newConnRef("name-1", "plug-1", "name-2", "slot-3"),
newConnRef("name-1", "plug-1", "name-2", "slot-4"),
newConnRef("name-1", "plug-1", "name-2_instance", "slot-1"),
newConnRef("name-1", "plug-2", "name-2", "slot-2"),
newConnRef("name-1", "plug-3", "name-2", "slot-1"),
newConnRef("name-1_instance", "plug-1", "name-2", "slot-1"),
})
}
func newSlotRef(snap, name string) *interfaces.SlotRef {
return &interfaces.SlotRef{Snap: snap, Name: name}
}
type bySlotRef []*interfaces.SlotRef
func (b bySlotRef) Len() int { return len(b) }
func (b bySlotRef) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b bySlotRef) Less(i, j int) bool {
return b[i].SortsBefore(*b[j])
}
func (s *SortingSuite) TestSortSlotRef(c *C) {
list := []*interfaces.SlotRef{
newSlotRef("name-2", "slot-3"),
newSlotRef("name-2_instance", "slot-1"),
newSlotRef("name-2", "slot-2"),
newSlotRef("name-2", "slot-4"),
newSlotRef("name-2", "slot-1"),
}
sort.Sort(bySlotRef(list))
c.Assert(list, DeepEquals, []*interfaces.SlotRef{
newSlotRef("name-2", "slot-1"),
newSlotRef("name-2", "slot-2"),
newSlotRef("name-2", "slot-3"),
newSlotRef("name-2", "slot-4"),
newSlotRef("name-2_instance", "slot-1"),
})
}
type byPlugRef []*interfaces.PlugRef
func (b byPlugRef) Len() int { return len(b) }
func (b byPlugRef) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b byPlugRef) Less(i, j int) bool {
return b[i].SortsBefore(*b[j])
}
func newPlugRef(snap, name string) *interfaces.PlugRef {
return &interfaces.PlugRef{Snap: snap, Name: name}
}
func (s *SortingSuite) TestSortPlugRef(c *C) {
list := []*interfaces.PlugRef{
newPlugRef("name-2", "plug-3"),
newPlugRef("name-2_instance", "plug-1"),
newPlugRef("name-2", "plug-4"),
newPlugRef("name-2", "plug-2"),
newPlugRef("name-2", "plug-1"),
}
sort.Sort(byPlugRef(list))
c.Assert(list, DeepEquals, []*interfaces.PlugRef{
newPlugRef("name-2", "plug-1"),
newPlugRef("name-2", "plug-2"),
newPlugRef("name-2", "plug-3"),
newPlugRef("name-2", "plug-4"),
newPlugRef("name-2_instance", "plug-1"),
})
}