tailscale/util/set/slice_test.go
Andrew Dunham f352f8a0e6 util/set: move Slice type from corp to oss
This is an exact copy of the files misc/set/set{,_test}.go from
tailscale/corp@a5415daa9c, plus the
license headers.

For use in #7877

Signed-off-by: Andrew Dunham <andrew@du.nham.ca>
Change-Id: I712d09c6d1a180c6633abe3acf8feb59b27e2866
2023-04-14 19:25:39 -04:00

57 lines
1.3 KiB
Go

// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
package set
import (
"testing"
qt "github.com/frankban/quicktest"
)
func TestSliceSet(t *testing.T) {
c := qt.New(t)
var ss Slice[int]
c.Check(len(ss.slice), qt.Equals, 0)
ss.Add(1)
c.Check(len(ss.slice), qt.Equals, 1)
c.Check(len(ss.set), qt.Equals, 0)
c.Check(ss.Contains(1), qt.Equals, true)
c.Check(ss.Contains(2), qt.Equals, false)
ss.Add(1)
c.Check(len(ss.slice), qt.Equals, 1)
c.Check(len(ss.set), qt.Equals, 0)
ss.Add(2)
ss.Add(3)
ss.Add(4)
ss.Add(5)
ss.Add(6)
ss.Add(7)
ss.Add(8)
c.Check(len(ss.slice), qt.Equals, 8)
c.Check(len(ss.set), qt.Equals, 0)
ss.Add(9)
c.Check(len(ss.slice), qt.Equals, 9)
c.Check(len(ss.set), qt.Equals, 9)
ss.Remove(4)
c.Check(len(ss.slice), qt.Equals, 8)
c.Check(len(ss.set), qt.Equals, 8)
c.Assert(ss.Contains(4), qt.IsFalse)
// Ensure that the order of insertion is maintained
c.Assert(ss.Slice().AsSlice(), qt.DeepEquals, []int{1, 2, 3, 5, 6, 7, 8, 9})
ss.Add(4)
c.Check(len(ss.slice), qt.Equals, 9)
c.Check(len(ss.set), qt.Equals, 9)
c.Assert(ss.Contains(4), qt.IsTrue)
c.Assert(ss.Slice().AsSlice(), qt.DeepEquals, []int{1, 2, 3, 5, 6, 7, 8, 9, 4})
ss.Add(1, 234, 556)
c.Assert(ss.Slice().AsSlice(), qt.DeepEquals, []int{1, 2, 3, 5, 6, 7, 8, 9, 4, 234, 556})
}