package bridge import ( "fmt" "testing" ) func TestBoundedUUIDSet_AddAndContains(t *testing.T) { s := NewBoundedUUIDSet(10) if s.Contains("a") { t.Error("empty set should not contain 'a'") } if !s.Add("a") { t.Error("first Add should return true (new)") } if s.Add("a") { t.Error("second Add should return false (duplicate)") } if !s.Contains("a") { t.Error("should contain 'a' after Add") } } func TestBoundedUUIDSet_Eviction(t *testing.T) { cap := 5 s := NewBoundedUUIDSet(cap) // 填满 for i := 0; i < cap; i++ { s.Add(fmt.Sprintf("id-%d", i)) } if s.Len() != cap { t.Errorf("expected len=%d, got %d", cap, s.Len()) } // 超出容量,最老的 id-0 应该被淘汰 s.Add("id-new") if s.Len() != cap { t.Errorf("len should stay at cap=%d after eviction, got %d", cap, s.Len()) } if s.Contains("id-0") { t.Error("id-0 should have been evicted") } if !s.Contains("id-new") { t.Error("id-new should be in set") } } func TestBoundedUUIDSet_FIFO_Eviction_Order(t *testing.T) { s := NewBoundedUUIDSet(3) s.Add("a") s.Add("b") s.Add("c") // 满了 s.Add("d") // 淘汰 a if s.Contains("a") { t.Error("a should be evicted first (FIFO)") } if !s.Contains("b") { t.Error("b should still be present") } if !s.Contains("c") { t.Error("c should still be present") } if !s.Contains("d") { t.Error("d should be present") } } func TestBoundedUUIDSet_MultipleEvictions(t *testing.T) { s := NewBoundedUUIDSet(3) for i := 0; i < 10; i++ { id := fmt.Sprintf("id-%d", i) s.Add(id) } // 只有最后 3 个应该存在:id-7, id-8, id-9 for i := 0; i < 7; i++ { if s.Contains(fmt.Sprintf("id-%d", i)) { t.Errorf("id-%d should have been evicted", i) } } for i := 7; i < 10; i++ { if !s.Contains(fmt.Sprintf("id-%d", i)) { t.Errorf("id-%d should be present", i) } } } func TestBoundedUUIDSet_DefaultCap(t *testing.T) { s := NewBoundedUUIDSet(0) // 0 → 默认 1000 if s.Cap() != 1000 { t.Errorf("default cap should be 1000, got %d", s.Cap()) } } func TestBoundedUUIDSet_ConcurrentAdd(t *testing.T) { s := NewBoundedUUIDSet(100) done := make(chan struct{}) for g := 0; g < 10; g++ { g := g go func() { for i := 0; i < 20; i++ { s.Add(fmt.Sprintf("g%d-id%d", g, i)) } done <- struct{}{} }() } for i := 0; i < 10; i++ { <-done } // 100 个唯一 ID 填满 cap=100,不应 panic 或数据竞争 if s.Len() != 100 { t.Errorf("expected len=100, got %d", s.Len()) } }