public void RTreeStreamSerializeIndexAddTests()
        {
            // build test-data.
            var       testDataList    = new List <KeyValuePair <BoxF2D, string> >();
            const int count           = 1000;
            var       randomGenerator = new RandomGenerator(66707770); // make this deterministic

            for (int idx = 0; idx < count; idx++)
            {
                double x1 = randomGenerator.Generate(1.0);
                double x2 = randomGenerator.Generate(1.0);
                double y1 = randomGenerator.Generate(1.0);
                double y2 = randomGenerator.Generate(1.0);

                var box = new BoxF2D(new PointF2D(x1, y1), new PointF2D(x2, y2));

                testDataList.Add(new KeyValuePair <BoxF2D, string>(
                                     box, idx.ToString(System.Globalization.CultureInfo.InvariantCulture)));
            }

            // create the index and reference index.
            var index     = new RTreeMemoryIndex <string>();
            var reference = new ReferenceImplementation <string>();

            // add all the data.
            for (int idx = 0; idx < count; idx++)
            {
                var keyValuePair = testDataList[idx];
                index.Add(keyValuePair.Key, keyValuePair.Value);
                reference.Add(keyValuePair.Key, keyValuePair.Value);

                //Assert.AreEqual(reference.Count(), index.Count());
            }

            //Assert.AreEqual(count, index.Count());

            var stream     = new MemoryStream();
            var serializer = new DataTestClassSerializer();

            serializer.Serialize(stream, index);

            ISpatialIndexReadonly <string> deserialized =
                serializer.Deserialize(stream, true);

            // query all.
            var totalBox        = new BoxF2D(0, 0, 1, 1);
            var resultIndex     = new HashSet <string>(deserialized.Get(totalBox));
            var resultReference = new HashSet <string>(reference.Get(totalBox));

            foreach (var data in resultIndex)
            {
                Assert.IsTrue(resultReference.Contains(data));
            }
            foreach (var data in resultReference)
            {
                Assert.IsTrue(resultIndex.Contains(data));
            }

            // generate random boxes and compare results.
            for (int idx = 0; idx < 20; idx++)
            {
                double x1 = randomGenerator.Generate(1.0);
                double x2 = randomGenerator.Generate(1.0);
                double y1 = randomGenerator.Generate(1.0);
                double y2 = randomGenerator.Generate(1.0);

                var box = new BoxF2D(new PointF2D(x1, y1), new PointF2D(x2, y2));

                resultIndex     = new HashSet <string>(deserialized.Get(box));
                resultReference = new HashSet <string>(reference.Get(box));

                foreach (var data in resultIndex)
                {
                    Assert.IsTrue(resultReference.Contains(data));
                }
                foreach (var data in resultReference)
                {
                    Assert.IsTrue(resultIndex.Contains(data));
                }
            }
        }
Exemple #2
0
        public void RTreeMemoryIndexAddTests()
        {
            // build test-data.
            var       testDataList    = new List <KeyValuePair <BoxF2D, DataTestClass> >();
            const int count           = 10000;
            var       randomGenerator = new RandomGenerator(66707770); // make this deterministic

            for (int idx = 0; idx < count; idx++)
            {
                double x1 = randomGenerator.Generate(1.0);
                double x2 = randomGenerator.Generate(1.0);
                double y1 = randomGenerator.Generate(1.0);
                double y2 = randomGenerator.Generate(1.0);

                var box      = new BoxF2D(new PointF2D(x1, y1), new PointF2D(x2, y2));
                var testData = new DataTestClass();
                testData.Data = idx.ToString(System.Globalization.CultureInfo.InvariantCulture);

                testDataList.Add(new KeyValuePair <BoxF2D, DataTestClass>(
                                     box, testData));
            }

            // create the index and reference index.
            var index     = new RTreeMemoryIndex <DataTestClass>();
            var reference = new ReferenceImplementation <DataTestClass>();

            // add all the data.
            for (int idx = 0; idx < count; idx++)
            {
                var keyValuePair = testDataList[idx];
                index.Add(keyValuePair.Key, keyValuePair.Value);
                reference.Add(keyValuePair.Key, keyValuePair.Value);

                //Assert.AreEqual(reference.Count(), index.Count());
            }

            //Assert.AreEqual(count, index.Count());

            // generate random boxes and compare results.
            for (int idx = 0; idx < 200; idx++)
            {
                double x1 = randomGenerator.Generate(1.0);
                double x2 = randomGenerator.Generate(1.0);
                double y1 = randomGenerator.Generate(1.0);
                double y2 = randomGenerator.Generate(1.0);

                var box = new BoxF2D(new PointF2D(x1, y1), new PointF2D(x2, y2));

                var resultIndex     = new HashSet <DataTestClass>(index.Get(box));
                var resultReference = new HashSet <DataTestClass>(reference.Get(box));

                foreach (var data in resultIndex)
                {
                    Assert.IsTrue(resultReference.Contains(data));
                }
                foreach (var data in resultReference)
                {
                    Assert.IsTrue(resultIndex.Contains(data));
                }
            }
        }