Esempio n. 1
0
        public void MergeInplace_TwoPrimitives_Throws()
        {
            var a = CreateNode("a", StatePrimitive.I16);
            var b = CreateNode("b", StatePrimitive.I32);

            Assert.Catch(() => SolvingFunctions.MergeInplace(a, b));
        }
Esempio n. 2
0
        public void MergeInplace_WhereSecondaryIsReferenced_ReturnsOrigin()
        {
            var a      = CreateNode("a", new ConstrainsState(StatePrimitive.I16, StatePrimitive.Real));
            var refToA = CreateNode("b", new StateRefTo(a));

            SolvingFunctions.MergeInplace(a, refToA);
            Assert.AreEqual(new ConstrainsState(StatePrimitive.I16, StatePrimitive.Real), a.State);
            Assert.AreEqual(new StateRefTo(a), refToA.State);
        }
Esempio n. 3
0
        public void MergeInplace_PrimitiveAndConstrains_ReturnsPrimitive()
        {
            var a = CreateNode("a", new ConstrainsState(StatePrimitive.I16, StatePrimitive.Real));
            var b = CreateNode("b", StatePrimitive.I64);

            SolvingFunctions.MergeInplace(b, a);
            Assert.AreEqual(StatePrimitive.I64, a.State);
            Assert.AreEqual(StatePrimitive.I64, b.State);
        }
Esempio n. 4
0
        public void MergeInplace_ConstrainsAndPrimitive_ReturnsPrimitive()
        {
            var a = CreateNode("a", new ConstrainsState(StatePrimitive.U16, StatePrimitive.Real));
            var b = CreateNode("b", StatePrimitive.U32);

            SolvingFunctions.MergeInplace(a, b);
            Assert.AreEqual(StatePrimitive.U32, a.State);
            Assert.AreEqual(StatePrimitive.U32, b.State);
        }
Esempio n. 5
0
        public void MergeInplace_TwoReferencedPrimitives_Throws()
        {
            var a    = CreateNode("a", StatePrimitive.I16);
            var b    = CreateNode("b", StatePrimitive.I32);
            var refA = CreateNode("a", new StateRefTo(a));
            var refB = CreateNode("b", new StateRefTo(b));

            Assert.Catch(() => SolvingFunctions.MergeInplace(refA, refB));
        }
Esempio n. 6
0
        public void MergeInplace_TwoConstrains_ReturnsMerged()
        {
            var a = CreateNode("a", new ConstrainsState(StatePrimitive.I16, StatePrimitive.Real));
            var b = CreateNode("b", new ConstrainsState(StatePrimitive.I24, StatePrimitive.Real));

            SolvingFunctions.MergeInplace(a, b);
            Assert.AreEqual(new ConstrainsState(StatePrimitive.I24, StatePrimitive.Real), a.State);
            Assert.AreEqual(new StateRefTo(a), b.State);
        }
Esempio n. 7
0
        private static bool TryMergeStructFields(StateStruct ancStruct, StateStruct descStruct)
        {
            foreach (var ancField in ancStruct.Fields)
            {
                TicNode descFieldNode = descStruct.GetFieldOrNull(ancField.Key);
                if (descFieldNode == null)
                {
                    return(false);
                }
                //  i m not sure why - but it is very important to set descFieldNode as main merge node...
                SolvingFunctions.MergeInplace(descFieldNode, ancField.Value);
            }

            return(true);
        }
Esempio n. 8
0
        public bool Apply(ICompositeState ancestor, ICompositeState descendant, TicNode ancestorNode, TicNode descendantNode)
        {
            if (ancestor.GetType() != descendant.GetType())
            {
                return(false);
            }
            if (ancestor is StateArray ancArray)
            {
                var descArray = (StateArray)descendant;
                if (descArray.ElementNode != ancArray.ElementNode)
                {
                    descArray.ElementNode.AddAncestor(ancArray.ElementNode);
                }
                descendantNode.RemoveAncestor(ancestorNode);
            }
            else if (ancestor is StateFun ancFun)
            {
                var descFun = (StateFun)descendant;

                if (descFun.ArgsCount != ancFun.ArgsCount)
                {
                    return(false);
                }
                descFun.RetNode.AddAncestor(ancFun.RetNode);
                for (int i = 0; i < descFun.ArgsCount; i++)
                {
                    ancFun.ArgNodes[i].AddAncestor(descFun.ArgNodes[i]);
                }
                descendantNode.RemoveAncestor(ancestorNode);
            }
            else if (ancestor is StateStruct ancStruct)
            {
                var descStruct = (StateStruct)descendant;
                // desc node has to have all ancestors fields that has exast same type as desc type
                // (implicit field convertion is not allowed)
                foreach (var ancField in ancStruct.Fields)
                {
                    var descField = descStruct.GetFieldOrNull(ancField.Key);
                    if (descField == null)
                    {
                        descendantNode.State = descStruct.With(ancField.Key, ancField.Value);
                    }
                    else
                    {
                        SolvingFunctions.MergeInplace(ancField.Value, descField);
                        if (ancField.Value.State is StateRefTo)
                        {
                            ancestorNode.State = ancestor.GetNonReferenced();
                        }
                        if (descField.State is StateRefTo)
                        {
                            descendantNode.State = descendant.GetNonReferenced();
                        }
                    }
                }
                // descendantNode.RemoveAncestor(ancestorNode);
            }
            else
            {
                throw new NotSupportedException($"Composite type {ancestor.GetType().Name} is not supported");
            }

            return(true);
        }