85 lines
1.6 KiB
Go
85 lines
1.6 KiB
Go
package dataext
|
|
|
|
import (
|
|
"sort"
|
|
"testing"
|
|
)
|
|
|
|
func TestSyncRingSet_AddAndContains(t *testing.T) {
|
|
s := NewSyncRingSet[int](3)
|
|
if !s.Add(1) {
|
|
t.Fatal("first Add(1) should be true")
|
|
}
|
|
if s.Add(1) {
|
|
t.Fatal("duplicate Add(1) should be false")
|
|
}
|
|
if !s.Contains(1) {
|
|
t.Fatal("expected Contains(1)")
|
|
}
|
|
}
|
|
|
|
func TestSyncRingSet_CapacityEvicts(t *testing.T) {
|
|
s := NewSyncRingSet[int](3)
|
|
s.Add(1)
|
|
s.Add(2)
|
|
s.Add(3)
|
|
s.Add(4) // should evict the oldest (1)
|
|
if s.Contains(1) {
|
|
t.Fatal("1 should have been evicted")
|
|
}
|
|
for _, v := range []int{2, 3, 4} {
|
|
if !s.Contains(v) {
|
|
t.Fatalf("expected %d", v)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSyncRingSet_Remove(t *testing.T) {
|
|
s := NewSyncRingSet[string](3)
|
|
s.Add("a")
|
|
s.Add("b")
|
|
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("a should be gone")
|
|
}
|
|
}
|
|
|
|
func TestSyncRingSet_AddAllRemoveAll(t *testing.T) {
|
|
s := NewSyncRingSet[int](10)
|
|
s.AddAll([]int{1, 2, 3, 2})
|
|
out := s.Get()
|
|
sort.Ints(out)
|
|
if len(out) != 3 {
|
|
t.Fatalf("got %v", out)
|
|
}
|
|
|
|
s.RemoveAll([]int{1, 99})
|
|
if s.Contains(1) {
|
|
t.Fatal("1 should be removed")
|
|
}
|
|
if !s.Contains(2) || !s.Contains(3) {
|
|
t.Fatal("2/3 should remain")
|
|
}
|
|
}
|
|
|
|
func TestSyncRingSet_AddIfNotContainsRemoveIfContains(t *testing.T) {
|
|
s := NewSyncRingSet[string](5)
|
|
if !s.AddIfNotContains("x") {
|
|
t.Fatal("first should succeed")
|
|
}
|
|
if s.AddIfNotContains("x") {
|
|
t.Fatal("second should fail")
|
|
}
|
|
if !s.RemoveIfContains("x") {
|
|
t.Fatal("remove existing failed")
|
|
}
|
|
if s.RemoveIfContains("x") {
|
|
t.Fatal("remove missing returned true")
|
|
}
|
|
}
|