コード例 #1
0
        static void TestExpressionsII(bool bPreserveProduct)
        {
            Expression    eOne   = MemoryAllocationUnit.GetInstance().NewValueExpression(1);
            Expression    eTwo   = MemoryAllocationUnit.GetInstance().NewValueExpression(2);
            Expression    eThree = MemoryAllocationUnit.GetInstance().NewValueExpression(3);
            SumExpression eSum12 = MemoryAllocationUnit.GetInstance().NewSumExpression();

            eSum12.AddSubExpression(eOne);
            eSum12.AddSubExpression(eTwo);
            SumExpression eSum23 = MemoryAllocationUnit.GetInstance().NewSumExpression();

            eSum23.AddSubExpression(eTwo);
            eSum23.AddSubExpression(eThree);
            ProductExpression eProd23 = MemoryAllocationUnit.GetInstance().NewProductExpression();

            eProd23.AddSubExpression(eTwo);
            eProd23.AddSubExpression(eThree);

            ProductExpression eProd = MemoryAllocationUnit.GetInstance().NewProductExpression();

            eProd.AddSubExpression(eSum12);
            eProd.AddSubExpression(eSum23);
            Debug.WriteLine(eProd);

            SumExpression eSum = MemoryAllocationUnit.GetInstance().NewSumExpression();

            eSum.AddSubExpression(eProd23);
            eSum.AddSubExpression(eThree);

            List <Expression> lRoot = new List <Expression>();

            if (bPreserveProduct)
            {
                lRoot.Add(eProd);
                MemoryAllocationUnit.GetInstance().GarbageCollection(lRoot);
                int cCollectedObjects = MemoryAllocationUnit.GetInstance().CollectedCount;
                //should be 2 with the example above:
                //eSum and eProd23 should be removed because they do not appear under eProd
                int iResult = eProd.Evaluate();
                eSum.Evaluate(); //should generate an exception
            }
            else
            {
                lRoot.Add(eSum);
                MemoryAllocationUnit.GetInstance().GarbageCollection(lRoot);
                int cCollectedObjects = MemoryAllocationUnit.GetInstance().CollectedCount;
                //should be 4 with the example above:
                //eProd, eSum12, eSum23, eOne do not appear under eSum
                int iResult = eSum.Evaluate();
                eProd.Evaluate(); //should generate an exception
            }
        }
コード例 #2
0
        public SumExpression NewSumExpression()
        {
            int idx = FindFreeMemory();

            if (idx == -1)
            {
                throw new OutOfMemoryException();
            }
            SumExpression exp = new SumExpression();

            m_aMemory[idx] = exp;
            return(exp);
        }