@@ -28,7 +28,8 @@ package dnshosts
28
28
import (
29
29
"fmt"
30
30
"net"
31
- "sort"
31
+ "slices"
32
+ "strings"
32
33
"testing"
33
34
34
35
"github.com/containers/gvisor-tap-vsock/pkg/types"
@@ -96,14 +97,6 @@ func Test_zoneHost(t *testing.T) {
96
97
97
98
func TestExtractZones (t * testing.T ) {
98
99
equalZones := func (za , zb []types.Zone ) bool {
99
- find := func (list []types.Zone , name string ) (types.Zone , bool ) {
100
- for _ , z := range list {
101
- if z .Name == name {
102
- return z , true
103
- }
104
- }
105
- return types.Zone {}, false
106
- }
107
100
equal := func (a , b types.Zone ) bool {
108
101
if a .Name != b .Name {
109
102
return false
@@ -125,10 +118,11 @@ func TestExtractZones(t *testing.T) {
125
118
}
126
119
127
120
for _ , a := range za {
128
- b , ok := find (zb , a .Name )
129
- if ! ok {
121
+ ib := slices . IndexFunc (zb , func ( z types. Zone ) bool { return z . Name == a .Name } )
122
+ if ib < 0 {
130
123
return false
131
124
}
125
+ b := zb [ib ]
132
126
if ! equal (a , b ) {
133
127
return false
134
128
}
@@ -182,52 +176,13 @@ func TestExtractZones(t *testing.T) {
182
176
t .Run (fmt .Sprint (i ), func (t * testing.T ) {
183
177
gotZones := ExtractZones (hosts )
184
178
for _ , zone := range gotZones {
185
- sort . Sort ( recordSorter ( zone .Records ) )
179
+ slices . SortFunc ( zone .Records , func ( a , b types. Record ) int { return strings . Compare ( a . Name , b . Name ) } )
186
180
}
187
- sort . Sort ( zoneSorter ( gotZones ) )
181
+ slices . SortFunc ( gotZones , func ( a , b types. Zone ) int { return strings . Compare ( a . Name , b . Name ) } )
188
182
189
183
if ! equalZones (gotZones , tt .wantZones ) {
190
184
t .Errorf ("extractZones() = %+v, want %+v" , gotZones , tt .wantZones )
191
185
}
192
186
})
193
187
}
194
188
}
195
-
196
- var (
197
- _ sort.Interface = recordSorter (nil )
198
- _ sort.Interface = zoneSorter (nil )
199
- )
200
-
201
- type recordSorter []types.Record
202
-
203
- // Len implements sort.Interface
204
- func (r recordSorter ) Len () int {
205
- return len (r )
206
- }
207
-
208
- // Less implements sort.Interface
209
- func (r recordSorter ) Less (i , j int ) bool {
210
- return r [i ].Name < r [j ].Name
211
- }
212
-
213
- // Swap implements sort.Interface
214
- func (r recordSorter ) Swap (i , j int ) {
215
- r [i ], r [j ] = r [j ], r [i ]
216
- }
217
-
218
- type zoneSorter []types.Zone
219
-
220
- // Len implements sort.Interface
221
- func (z zoneSorter ) Len () int {
222
- return len (z )
223
- }
224
-
225
- // Less implements sort.Interface
226
- func (z zoneSorter ) Less (i , j int ) bool {
227
- return z [i ].Name < z [j ].Name
228
- }
229
-
230
- // Swap implements sort.Interface
231
- func (z zoneSorter ) Swap (i , j int ) {
232
- z [i ], z [j ] = z [j ], z [i ]
233
- }
0 commit comments