github.com/anonymouse64/snapd@v0.0.0-20210824153203-04c4c42d842d/interfaces/sorting_test.go (about)

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