Exemple #1
0
        public void TestModelSerialization()
        {
            //Arrange
            var model  = new LustreModelBase(@"Examples\pressureTank.lus", "TANK", new List <Fault>());
            var memory = stackalloc byte[model.StateVectorSize];

            // Use toArray for a deep copy
            var bools          = model.Runner.Oc5ModelState.Bools.ToArray();
            var ints           = model.Runner.Oc5ModelState.Ints.ToArray();
            var strings        = model.Runner.Oc5ModelState.Strings.ToArray();
            var floats         = model.Runner.Oc5ModelState.Floats.ToArray();
            var doubles        = model.Runner.Oc5ModelState.Doubles.ToArray();
            var mappings       = model.Runner.Oc5ModelState.Mappings.ToArray();
            var inputMappings  = model.Runner.Oc5ModelState.InputMappings.ToArray();
            var outputMappings = model.Runner.Oc5ModelState.OutputMappings.ToArray();
            var state          = model.Runner.Oc5ModelState.CurrentState;

            //Act
            var serializer   = LustreModelSerializer.CreateFastInPlaceSerializer(model);
            var deserializer = LustreModelSerializer.CreateFastInPlaceDeserializer(model);

            serializer(memory);
            deserializer(memory);

            //Assert
            Assert.IsTrue(model.Runner.Oc5ModelState.Bools.SequenceEqual(bools));
            Assert.IsTrue(model.Runner.Oc5ModelState.Ints.SequenceEqual(ints));
            Assert.IsTrue(model.Runner.Oc5ModelState.Strings.SequenceEqual(strings));
            Assert.IsTrue(model.Runner.Oc5ModelState.Floats.SequenceEqual(floats));
            Assert.IsTrue(model.Runner.Oc5ModelState.Doubles.SequenceEqual(doubles));
            Assert.IsTrue(model.Runner.Oc5ModelState.Mappings.SequenceEqual(mappings));
            Assert.IsTrue(model.Runner.Oc5ModelState.InputMappings.SequenceEqual(inputMappings));
            Assert.IsTrue(model.Runner.Oc5ModelState.OutputMappings.SequenceEqual(outputMappings));
            Assert.IsTrue(model.Runner.Oc5ModelState.CurrentState == state);
        }
Exemple #2
0
        public void CheckSerializationDelegate()
        {
            unsafe
            {
                Program.ocExaplesPath = Directory.GetCurrentDirectory() + "\\Examples\\";

                var             faults = new Fault[0];
                LustreModelBase model  = new LustreModelBase("pressureTank", faults);

                for (int i = 0; i < model.program.variables.Count; i++)
                {
                    if (model.program.variables[i].getType() == 0)
                    {
                        model.program.variables[i].setValue((i % 2 == 0));
                    }
                    else if (model.program.variables[i].getType() == 1)
                    {
                        model.program.variables[i].setValue(i);
                    }
                    else if (model.program.variables[i].getType() == 3)
                    {
                        model.program.variables[i].setValue(i);
                    }
                    else if (model.program.variables[i].getType() == 4)
                    {
                        model.program.variables[i].setValue(i);
                    }
                }

                byte *state = (byte *)System.Runtime.InteropServices.Marshal.AllocHGlobal(model.StateVectorSize);
                LustreModelSerializer.CreateFastInPlaceSerializer(model)(state);

                for (int i = 0; i < model.program.variables.Count; i++)
                {
                    if (model.program.variables[i].getType() == 0)
                    {
                        model.program.variables[i].setValue((i % 2 != 0));
                    }
                    else if (model.program.variables[i].getType() == 1)
                    {
                        model.program.variables[i].setValue(i + 3);
                    }
                    else if (model.program.variables[i].getType() == 3)
                    {
                        model.program.variables[i].setValue(i + 3);
                    }
                    else if (model.program.variables[i].getType() == 4)
                    {
                        model.program.variables[i].setValue(i + 3);
                    }
                }

                LustreModelSerializer.CreateFastInPlaceDeserializer(model)(state);

                for (int i = 0; i < model.program.variables.Count; i++)
                {
                    if (model.program.variables[i].getType() == 0)
                    {
                        Requires.That((bool)model.program.variables[i].getValue() == (i % 2 == 0), "1");
                    }
                    else if (model.program.variables[i].getType() == 1)
                    {
                        Requires.That((int)model.program.variables[i].getValue() == i, "2");
                    }
                    else if (model.program.variables[i].getType() == 3)
                    {
                        Requires.That((float)model.program.variables[i].getValue() == i, "3");
                    }
                    else if (model.program.variables[i].getType() == 4)
                    {
                        Requires.That((double)model.program.variables[i].getValue() == i, "4");
                    }
                }
            }
        }