83 lines
1.6 KiB
Go
83 lines
1.6 KiB
Go
package dataext
|
|
|
|
import (
|
|
"sort"
|
|
"testing"
|
|
)
|
|
|
|
func TestSyncSet_Add(t *testing.T) {
|
|
s := NewSyncSet[string]()
|
|
if !s.Add("a") {
|
|
t.Fatal("first add should be true")
|
|
}
|
|
if s.Add("a") {
|
|
t.Fatal("duplicate add should be false")
|
|
}
|
|
if !s.Contains("a") {
|
|
t.Fatal("Contains a should be true")
|
|
}
|
|
}
|
|
|
|
func TestSyncSet_AddAll(t *testing.T) {
|
|
s := NewSyncSet[int]()
|
|
s.AddAll([]int{1, 2, 3, 2})
|
|
if !s.Contains(1) || !s.Contains(2) || !s.Contains(3) {
|
|
t.Fatal("missing items")
|
|
}
|
|
if len(s.Get()) != 3 {
|
|
t.Fatalf("got len %d", len(s.Get()))
|
|
}
|
|
}
|
|
|
|
func TestSyncSet_Remove(t *testing.T) {
|
|
s := NewSyncSet[string]()
|
|
s.Add("a")
|
|
if !s.Remove("a") {
|
|
t.Fatal("remove existing failed")
|
|
}
|
|
if s.Remove("a") {
|
|
t.Fatal("remove missing returned true")
|
|
}
|
|
if s.Contains("a") {
|
|
t.Fatal("still contains after remove")
|
|
}
|
|
}
|
|
|
|
func TestSyncSet_RemoveAll(t *testing.T) {
|
|
s := NewSyncSet[int]()
|
|
s.AddAll([]int{1, 2, 3})
|
|
s.RemoveAll([]int{1, 2, 99})
|
|
if s.Contains(1) || s.Contains(2) {
|
|
t.Fatal("should be removed")
|
|
}
|
|
if !s.Contains(3) {
|
|
t.Fatal("3 should remain")
|
|
}
|
|
}
|
|
|
|
func TestSyncSet_Get(t *testing.T) {
|
|
s := NewSyncSet[int]()
|
|
s.AddAll([]int{3, 1, 2})
|
|
out := s.Get()
|
|
sort.Ints(out)
|
|
if len(out) != 3 || out[0] != 1 || out[2] != 3 {
|
|
t.Fatalf("out=%v", out)
|
|
}
|
|
}
|
|
|
|
func TestSyncSet_AddIfNotContainsRemoveIfContains(t *testing.T) {
|
|
s := NewSyncSet[string]()
|
|
if !s.AddIfNotContains("x") {
|
|
t.Fatal("first AddIfNotContains failed")
|
|
}
|
|
if s.AddIfNotContains("x") {
|
|
t.Fatal("second AddIfNotContains succeeded")
|
|
}
|
|
if !s.RemoveIfContains("x") {
|
|
t.Fatal("RemoveIfContains failed")
|
|
}
|
|
if s.RemoveIfContains("x") {
|
|
t.Fatal("RemoveIfContains on missing succeeded")
|
|
}
|
|
}
|