public void ResizeTest()
        {
            var stringArrayRef = new string[1000];
            var stringArray    = new MemoryArray <string>(1000);

            var randomGenerator = new System.Random(66707770); // make this deterministic

            for (int idx = 0; idx < 1000; idx++)
            {
                if (randomGenerator.Next(4) >= 2)
                { // add data.
                    stringArrayRef[idx] = idx.ToString();
                    stringArray[idx]    = idx.ToString();
                }
                else
                {
                    stringArrayRef[idx] = null;
                    stringArray[idx]    = null;
                }
            }

            Array.Resize <string>(ref stringArrayRef, 335);
            stringArray.Resize(335);

            Assert.AreEqual(stringArrayRef.Length, stringArray.Length);
            for (int idx = 0; idx < stringArrayRef.Length; idx++)
            {
                Assert.AreEqual(stringArrayRef[idx], stringArray[idx]);
            }

            stringArrayRef = new string[1000];
            stringArray    = new MemoryArray <string>(1000);

            for (int idx = 0; idx < 1000; idx++)
            {
                if (randomGenerator.Next(4) >= 2)
                { // add data.
                    stringArrayRef[idx] = idx.ToString();
                    stringArray[idx]    = idx.ToString();
                }
                else
                {
                    stringArrayRef[idx] = null;
                    stringArray[idx]    = null;
                }
            }

            Array.Resize <string>(ref stringArrayRef, 1235);
            stringArray.Resize(1235);

            Assert.AreEqual(stringArrayRef.Length, stringArray.Length);
            for (int idx = 0; idx < stringArrayRef.Length; idx++)
            {
                Assert.AreEqual(stringArrayRef[idx], stringArray[idx]);
            }
        }
Пример #2
0
        public void TestEnsureMinimumSize()
        {
            const long InitialLength = 16;
            Guid       initVal       = Guid.NewGuid();
            Guid       fillVal       = Guid.NewGuid();

            var array = new MemoryArray <Guid>(0);

            // resizing should work if it starts at 0.
            array.EnsureMinimumSize(InitialLength, initVal);
            Assert.LessOrEqual(InitialLength, array.Length, "EnsureMinimumSize should be able to resize up from 0 to at least a big enough size.");
            Assert.AreEqual(initVal, array[0], "EnsureMinimumSize with a 'fill' parameter should fill the new slots with the given value.");
            Assert.AreEqual(initVal, array[array.Length - 1], "EnsureMinimumSize with a 'fill' parameter should fill the new slots with the given value.");

            // now that we've gone up from 0, set the size directly.
            array.Resize(InitialLength);

            // resizing smaller should do nothing.
            array.EnsureMinimumSize(array.Length - 5, fillVal);
            array.EnsureMinimumSize(array.Length - 5);
            Assert.AreEqual(InitialLength, array.Length, "EnsureMinimumSize should leave the array alone when the array is bigger than needed.");

            // resizing equal should do nothing.
            array.EnsureMinimumSize(InitialLength, fillVal);
            array.EnsureMinimumSize(InitialLength);
            Assert.AreEqual(InitialLength, array.Length, "EnsureMinimumSize should leave the array alone when the array is exactly as big as needed.");

            // first resize goes straight up to 1024.
            array.EnsureMinimumSize(InitialLength + 1, fillVal);
            Assert.AreEqual(1024, array.Length, "EnsureMinimumSize should have a floor of 1024 elements when resizing.");
            Assert.AreEqual(initVal, array[0], "EnsureMinimumSize should not change any data that's already in the array when resizing.");
            Assert.AreEqual(initVal, array[InitialLength - 1], "EnsureMinimumSize should not change any data that's already in the array when resizing.");
            Assert.AreEqual(fillVal, array[InitialLength], "EnsureMinimumSize with a 'fill' parameter should fill the new slots with the given value.");
            Assert.AreEqual(fillVal, array[array.Length - 1], "EnsureMinimumSize with a 'fill' parameter should fill the new slots with the given value.");

            // resizes above the current size should double whatever it was.
            array.Resize(1050);
            array.EnsureMinimumSize(1051);
            Assert.AreEqual(2100, array.Length, "EnsureMinimumSize should double current size, even if it wasn't a power of two before.");
            Assert.AreEqual(Guid.Empty, array[array.Length - 1], "EnsureMinimumSize without a 'fill' parameter should fill with default.");
        }
Пример #3
0
        /// <summary>
        /// Tests a huge array, reading and writing elements.
        /// </summary>
        public static void TestHugeArray()
        {
            var array = new MemoryArray <long>(1024 * 1024);

            for (var i = 0L; i < (long)int.MaxValue * 2; i++)
            {
                if (i >= array.Length)
                {
                    array.Resize(array.Length + (1024 * 1024));
                }
                array[i] = i * 4 + i / 3;

                var j = array[i];

                Assert.Equal(j, i * 4 + i / 3);
            }
        }
Пример #4
0
        /// <summary>
        /// Copies all core data stops, schedules, trips, and connections from the given transit db.
        /// </summary>
        public static void CopyFrom(this TransitDb db, TransitDb other)
        {
            if (db == null)
            {
                throw new ArgumentNullException("db");
            }
            if (other == null)
            {
                throw new ArgumentNullException("other");
            }
            if (other.ConnectionSorting == null)
            {
                throw new ArgumentException("A database can only be copied if connections are sorted.");
            }

            var stopIdsBlockSize = 1024 * 32;
            var stopIds          = new MemoryArray <uint>(stopIdsBlockSize);
            var tripIdsBlockSize = 1024 * 32;
            var tripIds          = new MemoryArray <uint>(tripIdsBlockSize);
            var scheduleIds      = new Dictionary <uint, uint>();
            var agencyIds        = new Dictionary <uint, uint>();

            // copy stops and keep id transformations.
            var stopsEnumerator = other.GetStopsEnumerator();

            while (stopsEnumerator.MoveNext())
            {
                var stopsMeta = other.StopAttributes.Get(stopsEnumerator.MetaId);
                var newMetaId = db.StopAttributes.Add(stopsMeta);
                var newStopId = db.AddStop(stopsEnumerator.Latitude, stopsEnumerator.Longitude, newMetaId);
                if (stopsEnumerator.Id >= stopIds.Length)
                {
                    stopIds.Resize(stopIds.Length + stopIdsBlockSize);
                }
                stopIds[stopsEnumerator.Id] = newStopId;
            }

            // copy trips, copy schedules that have not been copied yet, and keep trip id transformations.
            var tripsEnumerator    = other.GetTripsEnumerator();
            var scheduleEnumerator = other.GetSchedulesEnumerator();

            while (tripsEnumerator.MoveNext())
            {
                var tripsMeta = other.TripAttributes.Get(tripsEnumerator.MetaId);
                var newMetaId = db.TripAttributes.Add(tripsMeta);

                uint newAgencyMetaId = uint.MaxValue;
                if (!agencyIds.TryGetValue(tripsEnumerator.AgencyId, out newAgencyMetaId))
                {
                    var agencyMeta = other.AgencyAttributes.Get(tripsEnumerator.AgencyId);
                    newAgencyMetaId = db.AgencyAttributes.Add(agencyMeta);
                    agencyIds.Add(tripsEnumerator.AgencyId, newAgencyMetaId);
                }

                uint newScheduleId = uint.MaxValue;
                if (!scheduleIds.TryGetValue(tripsEnumerator.ScheduleId, out newScheduleId))
                {
                    if (scheduleEnumerator.MoveTo(tripsEnumerator.ScheduleId))
                    {
                        newScheduleId = scheduleEnumerator.CopyTo(db.SchedulesDb);
                        scheduleIds[tripsEnumerator.ScheduleId] = newScheduleId;
                    }
                }

                var newTripId = db.AddTrip(newScheduleId, newAgencyMetaId, newMetaId);
                if (tripsEnumerator.Id >= tripIds.Length)
                {
                    tripIds.Resize(tripIds.Length + tripIdsBlockSize);
                }
                tripIds[tripsEnumerator.Id] = newTripId;
            }

            // copy connections.
            var connectionEnumerator = other.GetConnectionsEnumerator(other.ConnectionSorting.Value);

            while (connectionEnumerator.MoveNext())
            {
                var newArrivalStop   = stopIds[connectionEnumerator.ArrivalStop];
                var newDepartureStop = stopIds[connectionEnumerator.DepartureStop];
                var newTripId        = tripIds[connectionEnumerator.TripId];

                db.AddConnection(newDepartureStop, newArrivalStop, newTripId, connectionEnumerator.DepartureTime,
                                 connectionEnumerator.ArrivalTime);
            }

            // copy shapes.
            if (other.ShapesDb != null)
            {
                var shapesEnumerator = other.ShapesDb.GetEnumerator();
                while (shapesEnumerator.MoveNext())
                {
                    var stop1 = stopIds[shapesEnumerator.Stop1];
                    var stop2 = stopIds[shapesEnumerator.Stop2];

                    db.ShapesDb.Add(stop1, stop2, shapesEnumerator.Shape);
                }
            }
        }
Пример #5
0
        public void ResizeTest()
        {
            var stringArrayRef = new string[1000];
            var stringArray = new MemoryArray<string>(1000);

            var randomGenerator = new System.Random(66707770); // make this deterministic 
            for (int idx = 0; idx < 1000; idx++)
            {
                if (randomGenerator.Next(4) >= 2)
                { // add data.
                    stringArrayRef[idx] = idx.ToString();
                    stringArray[idx] = idx.ToString();
                }
                else
                {
                    stringArrayRef[idx] = null;
                    stringArray[idx] = null;
                }
            }

            Array.Resize<string>(ref stringArrayRef, 335);
            stringArray.Resize(335);

            Assert.AreEqual(stringArrayRef.Length, stringArray.Length);
            for (int idx = 0; idx < stringArrayRef.Length; idx++)
            {
                Assert.AreEqual(stringArrayRef[idx], stringArray[idx]);
            }

            stringArrayRef = new string[1000];
            stringArray = new MemoryArray<string>(1000);

            for (int idx = 0; idx < 1000; idx++)
            {
                if (randomGenerator.Next(4) >= 2)
                { // add data.
                    stringArrayRef[idx] = idx.ToString();
                    stringArray[idx] = idx.ToString();
                }
                else
                {
                    stringArrayRef[idx] = null;
                    stringArray[idx] = null;
                }
            }

            Array.Resize<string>(ref stringArrayRef, 1235);
            stringArray.Resize(1235);

            Assert.AreEqual(stringArrayRef.Length, stringArray.Length);
            for (int idx = 0; idx < stringArrayRef.Length; idx++)
            {
                Assert.AreEqual(stringArrayRef[idx], stringArray[idx]);
            }
        }