File size: 2,182 Bytes
6380833 | 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 | package set
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestSet(t *testing.T) {
t.Run("Union", func(t *testing.T) {
res := Union(New(1, 2), New(2, 3))
assert.ElementsMatch(t, res.AsSlice(), []int{1, 2, 3})
})
t.Run("Union (empty)", func(t *testing.T) {
res := Union(New(1, 2))
assert.ElementsMatch(t, res.AsSlice(), []int{1, 2})
})
t.Run("Subtract", func(t *testing.T) {
res := Subtract(New(1, 2, 3), New(2, 3))
assert.ElementsMatch(t, res.AsSlice(), []int{1})
})
}
func TestSet_IsEmpty(t *testing.T) {
t.Run("new set is empty", func(t *testing.T) {
// Create a new empty set
s := New[string]()
// Check that it's empty
assert.True(t, s.IsEmpty(), "A newly created set with no items should be empty")
})
t.Run("set with items is not empty", func(t *testing.T) {
// Create a set with items
s := New("item1", "item2", "item3")
// Check that it's not empty
assert.False(t, s.IsEmpty(), "A set with items should not be empty")
})
t.Run("set becomes empty after removing all items", func(t *testing.T) {
// Create a set with items
s := New("item1", "item2")
// Remove the items
s.Remove("item1", "item2")
// Check that it's now empty
assert.True(t, s.IsEmpty(), "A set should be empty after removing all items")
})
t.Run("empty set becomes non-empty after adding an item", func(t *testing.T) {
// Create an empty set
s := New[string]()
// Check that it starts empty
assert.True(t, s.IsEmpty(), "A newly created set with no items should be empty")
// Add an item
s.Add("item1")
// Check that it's no longer empty
assert.False(t, s.IsEmpty(), "A set should not be empty after adding an item")
})
t.Run("concurrency safety test", func(t *testing.T) {
// This test doesn't really verify concurrency safety directly,
// but it serves as a smoke test for the locking mechanism
s := New[int]()
// Add and remove in succession to exercise the locks
for i := 0; i < 100; i++ {
s.Add(i)
assert.False(t, s.IsEmpty(), "Set should not be empty after adding an item")
s.Remove(i)
assert.True(t, s.IsEmpty(), "Set should be empty after removing all items")
}
})
}
|