Exemplo n.º 1
0
        public void TrainSegmentModelComparerClassCompareMethodReturnsObjectWithItem2EqualToNullIfFirstParameterHasNoTimingsAndSecondParameterHasNoTimings()
        {
            TrainSegmentModelComparer testObject = new TrainSegmentModelComparer(new List <LocationDisplayModel>());

            Tuple <int, TrainSegmentModel> testOutput = testObject.Compare(new TrainSegmentModel(null), new TrainSegmentModel(null));

            Assert.AreEqual(null, testOutput.Item2);
        }
Exemplo n.º 2
0
        public void TrainSegmentModelComparerClassCompareMethodReturnsObjectWithItem2EqualToNullIfBothParametersAreNull()
        {
            TrainSegmentModelComparer testObject = new TrainSegmentModelComparer(new List <LocationDisplayModel>());

            Tuple <int, TrainSegmentModel> testOutput = testObject.Compare(null, null);

            Assert.IsNull(testOutput.Item2);
        }
Exemplo n.º 3
0
        public void TrainSegmentModelComparerClassCompareMethodWithCommonLocationsCase8()
        {
            TrainSegmentModelComparer testObject = new TrainSegmentModelComparer(new List <LocationDisplayModel>
            {
                new LocationDisplayModel {
                    LocationKey = "A"
                },
                new LocationDisplayModel {
                    LocationKey = "B"
                },
                new LocationDisplayModel {
                    LocationKey = "C"
                },
                new LocationDisplayModel {
                    LocationKey = "D"
                },
            });
            TrainSegmentModel testParam0 = new TrainSegmentModel(
                new List <ILocationEntry>
            {
                new TrainLocationTimeModel {
                    LocationKey = "A", ActualTime = new TimeOfDay(10, 0)
                },
                new TrainLocationTimeModel {
                    LocationKey = "B", ActualTime = new TimeOfDay(10, 7)
                },
                new TrainLocationTimeModel {
                    LocationKey = "C", ActualTime = new TimeOfDay(10, 33)
                },
                new TrainLocationTimeModel {
                    LocationKey = "D", ActualTime = new TimeOfDay(10, 38, 15)
                },
            }
                );
            TrainSegmentModel testParam1 = new TrainSegmentModel(
                new List <ILocationEntry>
            {
                new TrainLocationTimeModel {
                    LocationKey = "A", ActualTime = new TimeOfDay(9, 0)
                },
                new TrainLocationTimeModel {
                    LocationKey = "B", ActualTime = new TimeOfDay(9, 45, 15)
                },
                new TrainLocationTimeModel {
                    LocationKey = "C", ActualTime = new TimeOfDay(10, 33)
                },
                new TrainLocationTimeModel {
                    LocationKey = "D", ActualTime = new TimeOfDay(10, 38, 15)
                },
            }
                );

            Tuple <int, TrainSegmentModel> testOutput = testObject.Compare(testParam0, testParam1);

            Assert.AreEqual(1, testOutput.Item1);
            Assert.IsNull(testOutput.Item2);
        }
Exemplo n.º 4
0
        public void TrainSegmentModelComparerClassCompareMethodReturnsObjectWithItem1EqualTo1IfFirstParameterHasTimingsAndSecondParameterIsNull()
        {
            TrainSegmentModelComparer testObject = new TrainSegmentModelComparer(new List <LocationDisplayModel>());

            Tuple <int, TrainSegmentModel> testOutput = testObject.Compare(new TrainSegmentModel(new List <ILocationEntry> {
                new TrainLocationTimeModel {
                    LocationKey = "x"
                }
            }), null);

            Assert.AreEqual(1, testOutput.Item1);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Add a <see cref="TrainSegmentModel"/> to a sorted collection, maintaining the sort order.
        /// </summary>
        /// <param name="item">The <see cref="TrainSegmentModel"/> to add.</param>
        /// <param name="comparer">The <see cref="TrainSegmentModelComparer"/> to use to determine the sort order.</param>
        /// <remarks>
        /// This method assumes that the collection is already sorted in the order that the passed comparer would recognise.  If not, you may get inconsistent results.  The
        /// comparer does not need to be provided if the collection is empty.
        /// </remarks>
        /// <exception cref="ArgumentNullException">Thrown if the <see cref="TrainSegmentModelComparer"/> parameter is null and the collection is not empty.</exception>
        public void AddSorted(TrainSegmentModel item, TrainSegmentModelComparer comparer)
        {
            lock (InnerCollection)
            {
                // Degenerate case with no sorting required.
                if (InnerCollection.Count == 0)
                {
                    Add(item);
                    return;
                }

                if (comparer == null)
                {
                    throw new ArgumentNullException(nameof(comparer));
                }

                List <TrainSegmentModel> newAdditions = new List <TrainSegmentModel>();
                InnerCollection.Add(item);
                bool eventRaised = false;
                for (var i = InnerCollection.Count - 1; i > 0; i--)
                {
                    var comparison = comparer.Compare(InnerCollection[i], InnerCollection[i - 1]);
                    if (comparison.Item2 != null)
                    {
                        newAdditions.Add(comparison.Item2);
                    }
                    if (comparison.Item1 < 0)
                    {
                        TrainSegmentModel swap = InnerCollection[i];
                        InnerCollection[i]     = InnerCollection[i - 1];
                        InnerCollection[i - 1] = swap;
                    }
                    else
                    {
                        OnAdd(item, i);
                        eventRaised = true;
                        break;
                    }
                }

                if (!eventRaised)
                {
                    OnAdd(item, 0);
                }

                foreach (var splitSegment in newAdditions)
                {
                    AddSorted(splitSegment, comparer);
                }
            }
        }
Exemplo n.º 6
0
        public void TrainSegmentModelComparerClassCompareMethodWithCommonLocationsCase10()
        {
            TrainSegmentModelComparer testObject = new TrainSegmentModelComparer(new List <LocationDisplayModel>
            {
                new LocationDisplayModel {
                    LocationKey = "A"
                },
                new LocationDisplayModel {
                    LocationKey = "B"
                },
                new LocationDisplayModel {
                    LocationKey = "C"
                },
                new LocationDisplayModel {
                    LocationKey = "D"
                },
            });
            TrainSegmentModel testParam1 = new TrainSegmentModel(
                new List <ILocationEntry>
            {
                new TrainLocationTimeModel {
                    LocationKey = "A", ActualTime = new TimeOfDay(10, 0)
                },
                new TrainLocationTimeModel {
                    LocationKey = "B", ActualTime = new TimeOfDay(10, 7)
                },
                new TrainLocationTimeModel {
                    LocationKey = "C", ActualTime = new TimeOfDay(10, 35)
                },
                new TrainLocationTimeModel {
                    LocationKey = "D", ActualTime = new TimeOfDay(10, 38, 15)
                },
            }
                );
            TrainSegmentModel testParam0 = new TrainSegmentModel(
                new List <ILocationEntry>
            {
                new TrainLocationTimeModel {
                    LocationKey = "A", ActualTime = new TimeOfDay(10, 7)
                },
                new TrainLocationTimeModel {
                    LocationKey = "B", ActualTime = new TimeOfDay(10, 12)
                },
                new TrainLocationTimeModel {
                    LocationKey = "C", ActualTime = new TimeOfDay(10, 17, 1)
                },
                new TrainLocationTimeModel {
                    LocationKey = "D", ActualTime = new TimeOfDay(10, 22, 59)
                },
            }
                );

            Tuple <int, TrainSegmentModel> testOutput = testObject.Compare(testParam0, testParam1);

            Assert.AreEqual(1, testOutput.Item1);
            Assert.IsNotNull(testOutput.Item2);
            Assert.AreEqual(3, testParam1.Timings.Count);
            Assert.AreEqual(new TimeOfDay(10, 0), (testParam1.Timings[0] as TrainLocationTimeModel).ActualTime);
            Assert.AreEqual("A", testParam1.Timings[0].LocationKey);
            Assert.AreEqual(new TimeOfDay(10, 7), (testParam1.Timings[1] as TrainLocationTimeModel).ActualTime);
            Assert.AreEqual("B", testParam1.Timings[1].LocationKey);
            Assert.AreEqual(new TimeOfDay(10, 35), (testParam1.Timings[2] as TrainLocationTimeModel).ActualTime);
            Assert.AreEqual("C", testParam1.Timings[2].LocationKey);
            Assert.IsTrue(testParam1.ContinuesLater);
            Assert.AreEqual(3, testOutput.Item2.Timings.Count);
            Assert.AreEqual(new TimeOfDay(10, 7), (testOutput.Item2.Timings[0] as TrainLocationTimeModel).ActualTime);
            Assert.AreEqual("B", testOutput.Item2.Timings[0].LocationKey);
            Assert.AreEqual(new TimeOfDay(10, 35), (testOutput.Item2.Timings[1] as TrainLocationTimeModel).ActualTime);
            Assert.AreEqual("C", testOutput.Item2.Timings[1].LocationKey);
            Assert.AreEqual(new TimeOfDay(10, 38, 15), (testOutput.Item2.Timings[2] as TrainLocationTimeModel).ActualTime);
            Assert.AreEqual("D", testOutput.Item2.Timings[2].LocationKey);
            Assert.IsTrue(testOutput.Item2.ContinuationFromEarlier);
        }