/// <summary>
        /// Loads, saves and checks if it's equal
        /// </summary>
        /// <param name="stateName">State name</param>
        public static void LoadSaveAndAssert(string stateName)
        {
            MemoryStream streamExpected = new MemoryStream();
            MemoryStream streamCurrent  = new MemoryStream();

            using (Stream streamTemp = OpenStateFromResources(stateName)) {
                streamTemp.CopyTo(streamExpected);
            }

            streamExpected.Position = 0;
            streamCurrent.Position  = 0;

            SimulationBase sim = SimulationBase.LoadFromStream(streamExpected);

            SimulationBase.SaveToStream(streamCurrent, sim);

            streamExpected.Position = 0;
            streamCurrent.Position  = 0;

            SHA1Managed hash = new SHA1Managed();

            byte[] hashExpected = hash.ComputeHash(streamExpected);
            byte[] hashCurrent  = hash.ComputeHash(streamCurrent);

            AssertArraysEqual(hashExpected, hashCurrent);
        }
        /// <summary>
        /// Checks if current simulation state is the same as expected state
        /// </summary>
        /// <param name="simCurrent">Simulation</param>
        /// <param name="simType">Simulation type</param>
        /// <param name="stateName">State name</param>
        public static void AssertSimulationWithState(SimulationBase simCurrent, SimulationType simType, string stateName)
        {
            SimulationBase simExpected = SimulationBase.LoadFromStream(OpenStateFromResources(stateName), RandomSeed);

            Assert.AreEqual(simType, simExpected.CurrentType);
            Assert.AreEqual(simExpected.CurrentType, simCurrent.CurrentType);

            Assert.IsTrue(simExpected.IsReady);
            Assert.AreEqual(simExpected.IsReady, simCurrent.IsReady);

            Assert.AreEqual(simExpected.CurrentStep, simCurrent.CurrentStep);

            Assert.AreEqual(simExpected.GetType(), simCurrent.GetType());

            switch (simType)
            {
            case SimulationType.CellBased: {
                CellBasedSim simExpected2 = simExpected as CellBasedSim;
                Assert.IsNotNull(simExpected2);

                CellBasedSim simCurrent2 = simCurrent as CellBasedSim;
                Assert.IsNotNull(simCurrent2);

                Assert.IsTrue(simCurrent2.CheckIntegrity());
                break;
            }

            case SimulationType.CarFollowing: {
                CarFollowingSim simExpected2 = simExpected as CarFollowingSim;
                Assert.IsNotNull(simExpected2);

                CarFollowingSim simCurrent2 = simCurrent as CarFollowingSim;
                Assert.IsNotNull(simCurrent2);

                Assert.AreEqual(simExpected2.Current.CarsPerCell, simCurrent2.Current.CarsPerCell);

                Assert.IsTrue(simCurrent2.CheckIntegrity());
                break;
            }

            default:
                Assert.Fail("Unknown simulation type");
                break;
            }
        }
示例#3
0
        private void OnOpenButtonClick(object sender, EventArgs e)
        {
            using (OpenFileDialog dialog = new OpenFileDialog()) {
                const string ext = ".trs";

                dialog.CheckFileExists  = true;
                dialog.Filter           = "Simulation State File (*" + ext + ")|*" + ext;
                dialog.FilterIndex      = 0;
                dialog.RestoreDirectory = true;
                dialog.Title            = "Load state…";

                if (dialog.ShowDialog(this) != DialogResult.OK)
                {
                    return;
                }

                string filename = dialog.FileName;
                using (FileStream s = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)) {
                    BindSimulation(SimulationBase.LoadFromStream(s));
                    RefreshToolbar(false);
                }
            }
        }