gopkg.in/ubuntu-core/snappy.v0@v0.0.0-20210902073436-25a8614f10a6/strutil/set_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2019 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 strutil_test 21 22 import ( 23 . "gopkg.in/check.v1" 24 25 "github.com/snapcore/snapd/strutil" 26 ) 27 28 type orderedSetSuite struct { 29 set strutil.OrderedSet 30 } 31 32 var _ = Suite(&orderedSetSuite{}) 33 34 func (s *orderedSetSuite) SetUpTest(c *C) { 35 s.set = strutil.OrderedSet{} 36 } 37 38 func (s *orderedSetSuite) TestZeroValueItems(c *C) { 39 c.Assert(s.set.Items(), HasLen, 0) 40 } 41 42 func (s *orderedSetSuite) TestZeroValueContains(c *C) { 43 c.Check(s.set.Contains("foo"), Equals, false) 44 } 45 46 func (s *orderedSetSuite) TestZeroValueIndexOf(c *C) { 47 _, ok := s.set.IndexOf("foo") 48 c.Check(ok, Equals, false) 49 } 50 51 func (s *orderedSetSuite) TestZeroValuePut(c *C) { 52 items := []string{"foo", "bar", "froz"} 53 for idx, item := range items { 54 s.set.Put(item) 55 c.Check(s.set.Contains(item), Equals, true) 56 realIdx, ok := s.set.IndexOf(item) 57 c.Check(ok, Equals, true) 58 c.Check(idx, Equals, realIdx) 59 c.Check(s.set.Size(), Equals, idx+1) 60 c.Check(s.set.Items(), DeepEquals, items[:idx+1]) 61 } 62 } 63 64 func (s *orderedSetSuite) TestZeroValueSize(c *C) { 65 c.Assert(s.set.Size(), Equals, 0) 66 } 67 68 func (s *orderedSetSuite) TestDeduplication(c *C) { 69 s.set.Put("a") 70 s.set.Put("b") 71 s.set.Put("a") 72 s.set.Put("c") 73 74 c.Assert(s.set.Items(), DeepEquals, []string{"a", "b", "c"}) 75 c.Check(s.set.Size(), Equals, 3) 76 c.Check(s.set.Contains("a"), Equals, true) 77 c.Check(s.set.Contains("b"), Equals, true) 78 c.Check(s.set.Contains("c"), Equals, true) 79 }