RTree icon indicating copy to clipboard operation
RTree copied to clipboard

FIX removing and entry could accidentally clear the whole tree

Open hagish opened this issue 9 years ago • 1 comments

This is a snippet from the unit test that triggered the bug.

private class Something
{
    public int Id;
    public Envelope Envelope;

    public override int GetHashCode()
    {
        return Id.GetHashCode();
    }

    public override bool Equals(System.Object obj)
    {
        // If parameter is null return false.
        if (obj == null)
        {
            return false;
        }

        if (obj is Something)
        {
            return Id == ((Something)obj).Id;
        }
        else
        {
            return false;
        }
    }

    public bool Equals(Something p)
    {
        // If parameter is null return false:
        if ((object)p == null)
        {
            return false;
        }

        // Return true if the fields match:
        return Id == p.Id;
    }

    public override string ToString()
    {
        return string.Format("[Id={0} Env={1}]", Id, Envelope);
    }
}

[TestMethod]
public void RTreeMass2()
{
    RTree<Something> r = new RTree<Something>();

    List<Something> l = new List<Something>();

    int count = 100;

    for (int i = 0; i < count; ++i)
    {
        int x = 5 - (i % 10);
        int y = 5 - ((i*i) % 10);
        int radius = 1 + i % 5;

        var o = new Something(){
            Id = i,
            Envelope = new Envelope(x-radius, y-radius, x+radius, y+radius),
        };

        l.Add(o);
    }

    // add all
    foreach(var it in l){
        r.Insert(it, it.Envelope);
    }

    // query all
    foreach(var it in l)
    {
        // search
        Assert.AreEqual(true, r.Search(it.Envelope).Any(i => i.Data == it));

        Assert.AreEqual(count, r.Search(new Envelope(-1000, -1000, 1000, 1000)).Count());

        // remove
        r.Remove(it, it.Envelope);
        count--;

        Assert.AreEqual(count, r.Search(new Envelope(-1000, -1000, 1000, 1000)).Count());                
    }

    Assert.AreEqual(count, r.Search(new Envelope(-1000, -1000, 1000, 1000)).Count());                
}

hagish avatar Jul 18 '15 17:07 hagish

Thank you very much for the contribuition! You saved me a lot of time.

Its a pity that your pull has not been accepted yet.

deight avatar Nov 17 '17 11:11 deight