Exemplo n.º 1
0
        public void ExactlyDivideTest()
        {
            FakeVariableLinker fvl = new FakeVariableLinker();
            DoubleVar          d1  = new DoubleVar(FakeVariableLinker.DoubleA);
            LongVar            i1  = new LongVar(FakeVariableLinker.IntA);
            LongConst          a   = new LongConst(300);
            LongConst          c   = new LongConst(603);
            DoubleConst        b   = new DoubleConst(20.7);

            ArithmeticExpression ae = new ArithmeticExpression(a, i1, Operator.ExactlyDivide);

            Assert.IsTrue(ae.GetResult(fvl).ToString() == "5");
            ae = new ArithmeticExpression(c, a, Operator.ExactlyDivide);
            Assert.IsTrue(ae.GetResult(fvl).ToString() == "2");
            ae = new ArithmeticExpression(c, a, Operator.Divide);
            Assert.IsTrue(ae.GetResult(fvl).ToString() == "2.01");
            ae = new ArithmeticExpression(c, b, Operator.ExactlyDivide);
            Assert.IsTrue(ae.GetResult(fvl).ToString() == "29");
            ae = new ArithmeticExpression(c, a, Operator.Remainder);
            Assert.IsTrue(ae.GetResult(fvl).ToString() == "3");
            ae = new ArithmeticExpression(c, b, Operator.Remainder);
            Assert.IsTrue(ae.GetResult(fvl).ToString() == (603 % 20.7).ToString());
            TestContext.WriteLine(ae.GetResult(fvl).ToString()); // Scan
            ae = new ArithmeticExpression(c, new DoubleConst(0), Operator.Divide);
            Assert.ThrowsException <DivideByZeroException>(() => ae.GetResult(fvl));
            ae = new ArithmeticExpression(c, new DoubleConst(15), Operator.Divide);
            Assert.IsTrue(ae.GetResult(fvl).ToString() == "40.2");

            //TestContext.WriteLine(ae.GetResult(fvl).ToString());
        }
Exemplo n.º 2
0
 /// <summary>
 /// Generate code or evaluate the type for double const
 /// </summary>
 void IConstVisitor.Visit(DoubleConst This)
 {
     if (checkType)
     {
         type = TypeEntity.Double;
         return;
     }
     IL.Emit(OpCodes.Ldc_R8, This.Value);
 }
Exemplo n.º 3
0
        public void ConstCalculate()
        {
            LongConst            a   = new LongConst(3);
            LongConst            b   = new LongConst(5);
            DoubleConst          c   = new DoubleConst(7.5);
            ArithmeticExpression ae  = new ArithmeticExpression(a, b);
            ArithmeticExpression ae2 = new ArithmeticExpression(ae, c, Operator.Multiply);

            Assert.IsTrue(ae2.GetResult(null).ToString() == "60");
        }
Exemplo n.º 4
0
        public void JsonSave()
        {
            DoubleVar            d1  = new DoubleVar(FakeVariableLinker.DoubleA);
            LongVar              i1  = new LongVar(FakeVariableLinker.IntA);
            LongConst            a   = new LongConst(300);
            DoubleConst          b   = new DoubleConst(20.7);
            ArithmeticExpression ae  = new ArithmeticExpression(a, i1);
            ArithmeticExpression ae2 = new ArithmeticExpression(ae, d1, Operator.Multiply);
            ArithmeticExpression ae3 = new ArithmeticExpression(ae2, b, Operator.Minus);

            TestContext.WriteLine(JsonConvert.SerializeObject(ae3));
        }
Exemplo n.º 5
0
        public void VariableAndConst()
        {
            FakeVariableLinker   fvl = new FakeVariableLinker();
            DoubleVar            d1  = new DoubleVar(FakeVariableLinker.DoubleA);
            LongVar              i1  = new LongVar(FakeVariableLinker.IntA);
            LongConst            a   = new LongConst(300);
            DoubleConst          b   = new DoubleConst(20.7);
            ArithmeticExpression ae  = new ArithmeticExpression(a, i1);
            ArithmeticExpression ae2 = new ArithmeticExpression(ae, d1, Operator.Multiply);
            ArithmeticExpression ae3 = new ArithmeticExpression(ae2, b, Operator.Minus);

            Assert.IsTrue(ae3.GetResult(fvl).ToString() == "897.3");
        }
Exemplo n.º 6
0
        public void CompareExpression()
        {
            FakeVariableLinker fvl = new FakeVariableLinker();

            LongConst   a = new LongConst(0304);
            NumberConst b = new DoubleConst(56.8);
            ObjectConst c = new DoubleConst(690.8);

            Assert.IsFalse((a < b).Value);
            Assert.IsTrue((a < c).Value);
            CompareExpression ce = new CompareExpression(new LongConst(30), new LongConst(30));

            Assert.IsTrue(ce.GetResult(fvl).Value);
            CompareExpression ce2 = new CompareExpression(new LongConst(30), new LongVar(FakeVariableLinker.IntA));

            Assert.IsFalse(ce2.GetResult(fvl).Value);
            ArithmeticExpression ae  = new ArithmeticExpression(new LongConst(30), new DoubleConst(30d));
            CompareExpression    ce3 = new CompareExpression(ae, new LongVar(FakeVariableLinker.IntA));

            Assert.IsTrue(ce3.GetResult(fvl).Value);
            CompareExpression ce4 = new CompareExpression(new LongConst(40), new DoubleConst(30), Operator.GreaterThan);

            Assert.IsTrue(ce4.GetResult(fvl).Value);
            CompareExpression ce5 = new CompareExpression(new BooleanConst(true), new DoubleConst(30), Operator.GreaterThan);

            Assert.ThrowsException <ArithmeticException>(() => ce5.GetResult(fvl));
            CompareExpression ce6 = new CompareExpression(new LongConst(20), new DoubleConst(30), Operator.LessThan);

            Assert.IsTrue(ce6.GetResult(fvl).Value);
            CompareExpression ce7 = new CompareExpression(new LongConst(20), new DoubleConst(20), Operator.LessThanOrEqualTo);

            Assert.IsTrue(ce7.GetResult(fvl).Value);
            CompareExpression ce8 = new CompareExpression(new DoubleVar(FakeVariableLinker.DoubleA), new DoubleConst(2.55), Operator.NotEqualTo);

            Assert.IsFalse(ce8.GetResult(fvl).Value);
        }
Exemplo n.º 7
0
        public void DoubleUnitTest()
        {
            Random     rnd = new Random((int)DateTime.Now.Ticks);
            DoubleUnit du  = new DoubleUnit();

            Assert.IsTrue(du.Compare(new DoubleConst(rnd.NextRandomDouble())));
            Assert.IsTrue(du.Compare(new DoubleConst(rnd.NextRandomDouble())));
            Assert.IsTrue(du.Compare(new DoubleConst(rnd.NextRandomDouble())));
            Assert.IsTrue(du.Compare(new DoubleConst(rnd.NextRandomDouble())));
            Assert.IsTrue(du.Compare(new DoubleConst(rnd.NextRandomDouble())));
            Assert.IsTrue(du.Compare(DoubleConst.MinValue));
            Assert.IsTrue(du.Compare(DoubleConst.MaxValue));
            Assert.IsFalse(du.Compare(new StringConst("dd")));
            Assert.IsFalse(du.Compare(new CharConst('\n')));
            Assert.IsTrue(du.Compare(du.Random()));
            Assert.IsTrue(du.Compare(du.Random()));
            Assert.IsTrue(du.Compare(du.Random()));
            Assert.IsTrue(du.Compare(du.Random()));
            TestContext.WriteLine(du.Random().ToString());
            TestContext.WriteLine(du.Random().ToString());
            TestContext.WriteLine(du.Random().ToString());
            du = new DoubleUnit(56.6875);
            Assert.IsTrue(du.Compare(new DoubleConst(56.6875)));
            Assert.IsFalse(du.Compare(new DoubleConst(56.6876)));
            Assert.IsFalse(du.Compare(new DoubleConst(56.6874)));
            Assert.IsTrue(du.Compare(du.Random()));
            Assert.IsTrue(du.Compare(du.Random()));
            Assert.IsTrue(du.Compare(du.Random()));
            Assert.IsTrue(du.Compare(du.Random()));
            TestContext.WriteLine(du.Random().ToString());
            TestContext.WriteLine(du.Random().ToString());
            du = new DoubleUnit(-15.988, 360.62559);
            Assert.IsFalse(du.Compare(new LongConst(-30)));
            Assert.IsTrue(!du.Compare(new DoubleConst(-20.70368)));
            Assert.IsTrue(du.Compare(new DoubleConst(-15.98800)));
            Assert.IsTrue(du.Compare(new LongConst(0)));
            Assert.IsTrue(du.Compare(new DoubleConst(205.288)));
            Assert.IsTrue(du.Compare(new DoubleConst(360.62559)));
            Assert.IsFalse(du.Compare(new DoubleConst(360.62560)));
            Assert.IsFalse(du.Compare(new DoubleConst(578.698)));
            Assert.IsTrue(du.Compare(du.Random()));
            Assert.IsTrue(du.Compare(du.Random()));
            Assert.IsTrue(du.Compare(du.Random()));
            Assert.IsTrue(du.Compare(du.Random()));
            TestContext.WriteLine(du.Random().ToString());
            TestContext.WriteLine(du.Random().ToString());
            TestContext.WriteLine(du.Random().ToString());
            TestContext.WriteLine(du.Random().ToString());
            TestContext.WriteLine(du.Random().ToString());

            du = new DoubleUnit(6933.988, 5932.648819);
            Assert.ThrowsException <ArgumentException>(() => du.Random());

            du = new DoubleUnit(-965378515.5646, 8534832.683458, CompareMethod.NotMinMax);

            du = new DoubleUnit();
            for (int i = 0; i < 5000; i++)
            {
                DoubleConst d = du.Random() as DoubleConst;
                if (double.IsNaN((double)d.Value))
                {
                    TestContext.Write($"{i}=NaN");
                }
            }
        }