Inheritance: IScheminType, IScheminNumeric
Ejemplo n.º 1
0
        public void TestDivide()
        {
            var prim = PrimitiveFactory.Get("/");
            ScheminDecimal test_decimal = new ScheminDecimal(3.0m);
            ScheminInteger test_integer = new ScheminInteger(3);
            ScheminInteger test_divisor_int = new ScheminInteger(2);
            ScheminDecimal test_divisor_decimal = new ScheminDecimal(2);

            ScheminList decimal_args = new ScheminList(test_decimal);
            decimal_args.Append(test_divisor_decimal);

            ScheminList int_args = new ScheminList(test_integer);
            int_args.Append(test_divisor_int);

            ScheminList mixed_args = new ScheminList(test_integer);
            mixed_args.Append(test_divisor_decimal);

            ScheminDecimal decimal_result = (ScheminDecimal) prim.Execute(null, null, decimal_args);
            ScheminInteger int_result = (ScheminInteger) prim.Execute(null, null, int_args);
            ScheminDecimal mixed_result = (ScheminDecimal) prim.Execute(null, null, mixed_args);

            ScheminInteger expected = new ScheminInteger(1);

            Assert.AreEqual(1.5m, decimal_result.DecimalValue());
            Assert.AreEqual(expected.IntegerValue(), int_result.IntegerValue());
            Assert.AreEqual(1.5m, mixed_result.DecimalValue());
        }
Ejemplo n.º 2
0
        public void TestAdd()
        {
            var prim = PrimitiveFactory.Get("+");
            ScheminDecimal test_decimal = new ScheminDecimal(1.5m);
            ScheminInteger test_integer = new ScheminInteger(2);

            ScheminPair decimal_args = new ScheminPair(test_decimal);
            decimal_args = decimal_args.Append(test_decimal);

            ScheminPair int_args = new ScheminPair(test_integer);
            int_args = int_args.Append(test_integer);

            ScheminPair mixed_args = new ScheminPair(test_integer);
            mixed_args = mixed_args.Append(test_decimal);

            ScheminDecimal decimal_result = (ScheminDecimal) prim.Execute(null, null, decimal_args);
            ScheminInteger int_result = (ScheminInteger) prim.Execute(null, null, int_args);
            ScheminDecimal mixed_result = (ScheminDecimal) prim.Execute(null, null, mixed_args);

            ScheminInteger expected = new ScheminInteger(4);

            Assert.AreEqual(3.0m, decimal_result.DecimalValue());
            Assert.AreEqual(expected.IntegerValue(), int_result.IntegerValue());
            Assert.AreEqual(3.5m, mixed_result.DecimalValue());
        }
Ejemplo n.º 3
0
        public void TestStringLength()
        {
            var prim = PrimitiveFactory.Get("string-length");
            ScheminString test = new ScheminString("test");
            ScheminInteger result = (ScheminInteger) prim.Execute(null, null, new ScheminPair(test));
            ScheminInteger expected = new ScheminInteger(4);

            Assert.AreEqual(result.IntegerValue(), expected.IntegerValue());
        }
Ejemplo n.º 4
0
        public override IScheminType Execute(Environment env, Evaluator eval, ScheminPair args)
        {
            bool dec = false;
            var first = (IScheminNumeric) args.Car;
            IScheminNumeric result = null;

            foreach (IScheminType type in args)
            {
                if ((type as ScheminDecimal) != null)
                {
                    dec = true;
                    result = new ScheminDecimal(first.DecimalValue());
                }
            }

            if (!dec)
            {
                result = new ScheminInteger(first.IntegerValue());
            }

            if (args.Length < 2)
            {
                if (dec)
                {
                    return new ScheminDecimal(first.DecimalValue() * -1);
                }
                else
                {
                    return new ScheminInteger(first.IntegerValue() * -1);
                }
            }

            foreach (IScheminType type in args.ListCdr())
            {
                if ((type as IScheminNumeric) != null)
                {
                    var temp = (IScheminNumeric) type;
                    if (dec)
                    {
                        result = new ScheminDecimal(result.DecimalValue() - temp.DecimalValue());
                    }
                    else
                    {
                        result = new ScheminInteger(result.IntegerValue() - temp.IntegerValue());
                    }
                }
            }

            return (IScheminType) result;
        }
Ejemplo n.º 5
0
 public override IScheminType Execute(Environment env, Evaluator eval, ScheminPair args)
 {
     ScheminString str = (ScheminString) args.Car;
     ScheminInteger len = new ScheminInteger(str.Value.Length);
     return len;
 }
Ejemplo n.º 6
0
        public void TestSubtract()
        {
            var prim = PrimitiveFactory.Get("-");
            ScheminDecimal test_decimal = new ScheminDecimal(8.5m);
            ScheminInteger test_integer = new ScheminInteger(20);
            ScheminDecimal test_decimal_2 = new ScheminDecimal(4.1m);
            ScheminInteger test_integer_2 = new ScheminInteger(6);

            ScheminList decimal_args = new ScheminList(test_decimal);
            decimal_args.Append(test_decimal_2);

            ScheminList int_args = new ScheminList(test_integer);
            int_args.Append(test_integer_2);

            ScheminList mixed_args = new ScheminList(test_integer);
            mixed_args.Append(test_decimal);

            ScheminDecimal decimal_result = (ScheminDecimal) prim.Execute(null, null, decimal_args);
            ScheminInteger int_result = (ScheminInteger) prim.Execute(null, null, int_args);
            ScheminDecimal mixed_result = (ScheminDecimal) prim.Execute(null, null, mixed_args);

            ScheminInteger expected = new ScheminInteger(14);

            Assert.AreEqual(4.4m, decimal_result.DecimalValue());
            Assert.AreEqual(expected.IntegerValue(), int_result.IntegerValue());
            Assert.AreEqual(11.5m, mixed_result.DecimalValue());
        }
Ejemplo n.º 7
0
        public void TestMultiply()
        {
            var prim = PrimitiveFactory.Get("*");
            ScheminDecimal test_decimal = new ScheminDecimal(1.5m);
            ScheminInteger test_integer = new ScheminInteger(2);
            ScheminInteger test_mult_int = new ScheminInteger(6);
            ScheminDecimal test_mult_decimal = new ScheminDecimal(0.8m);
            ScheminInteger test_large = new ScheminInteger(BigInteger.Parse("100000000000000000000000000000000"));
            ScheminInteger test_large_2nd = new ScheminInteger(BigInteger.Parse("400000000000000000000"));

            ScheminList decimal_args = new ScheminList(test_decimal);
            decimal_args.Append(test_mult_decimal);

            ScheminList int_args = new ScheminList(test_integer);
            int_args.Append(test_mult_int);

            ScheminList mixed_args = new ScheminList(test_integer);
            mixed_args.Append(test_mult_decimal);

            ScheminList large_args = new ScheminList(test_large);
            large_args.Append(test_large_2nd);

            ScheminDecimal decimal_result = (ScheminDecimal) prim.Execute(null, null, decimal_args);
            ScheminInteger int_result = (ScheminInteger) prim.Execute(null, null, int_args);
            ScheminDecimal mixed_result = (ScheminDecimal) prim.Execute(null, null, mixed_args);
            ScheminInteger large_result = (ScheminInteger) prim.Execute(null, null, large_args);

            ScheminInteger expected = new ScheminInteger(12);
            ScheminInteger exp_large = new ScheminInteger(BigInteger.Parse("40000000000000000000000000000000000000000000000000000"));

            Assert.AreEqual(1.2m, decimal_result.DecimalValue());
            Assert.AreEqual(expected.IntegerValue(), int_result.IntegerValue());
            Assert.AreEqual(1.6m, mixed_result.DecimalValue());
            Assert.AreEqual(exp_large.IntegerValue(), large_result.IntegerValue());
        }