// Insert, Len, IsEmpty, Hash, Clear, AppendTo. func TestMoreBasics(t *testing.T) { set := new(intsets.Sparse) set.Insert(456) set.Insert(123) set.Insert(789) if set.Len() != 3 { t.Errorf("%s.Len: got %d, want 3", set, set.Len()) } if set.IsEmpty() { t.Errorf("%s.IsEmpty: got true", set) } if !set.Has(123) { t.Errorf("%s.Has(123): got false", set) } if set.Has(1234) { t.Errorf("%s.Has(1234): got true", set) } got := set.AppendTo([]int{-1}) if want := []int{-1, 123, 456, 789}; fmt.Sprint(got) != fmt.Sprint(want) { t.Errorf("%s.AppendTo: got %v, want %v", set, got, want) } set.Clear() if set.Len() != 0 { t.Errorf("Clear: got %d, want 0", set.Len()) } if !set.IsEmpty() { t.Errorf("IsEmpty: got false") } if set.Has(123) { t.Errorf("%s.Has: got false", set) } }
// Intersects reports whether this points-to set and the // argument points-to set contain common members. func (x PointsToSet) Intersects(y PointsToSet) bool { if x.pts == nil || y.pts == nil { return false } // This takes Θ(|x|+|y|) time. var z intsets.Sparse z.Intersection(&x.pts.Sparse, &y.pts.Sparse) return !z.IsEmpty() }
func TestIntersects(t *testing.T) { prng := rand.New(rand.NewSource(0)) for i := uint(0); i < 12; i++ { X, Y := randomPset(prng, 1<<i), randomPset(prng, 1<<i) x, y := &X.bits, &Y.bits // test the slow way var z intsets.Sparse z.Copy(x) z.IntersectionWith(y) if got, want := x.Intersects(y), !z.IsEmpty(); got != want { t.Errorf("Intersects: got %v, want %v", got, want) } // make it false a := x.AppendTo(nil) for _, v := range a { y.Remove(v) } if got, want := x.Intersects(y), false; got != want { t.Errorf("Intersects: got %v, want %v", got, want) } // make it true if x.IsEmpty() { continue } i := prng.Intn(len(a)) y.Insert(a[i]) if got, want := x.Intersects(y), true; got != want { t.Errorf("Intersects: got %v, want %v", got, want) } } }
func TestSubsetOf(t *testing.T) { prng := rand.New(rand.NewSource(0)) for i := uint(0); i < 12; i++ { X, Y := randomPset(prng, 1<<i), randomPset(prng, 1<<i) x, y := &X.bits, &Y.bits // test the slow way var z intsets.Sparse z.Copy(x) z.DifferenceWith(y) if got, want := x.SubsetOf(y), z.IsEmpty(); got != want { t.Errorf("SubsetOf: got %v, want %v", got, want) } // make it true y.UnionWith(x) if got, want := x.SubsetOf(y), true; got != want { t.Errorf("SubsetOf: got %v, want %v", got, want) } // make it false if x.IsEmpty() { continue } a := x.AppendTo(nil) i := prng.Intn(len(a)) y.Remove(a[i]) if got, want := x.SubsetOf(y), false; got != want { t.Errorf("SubsetOf: got %v, want %v", got, want) } } }