Ejemplo n.º 1
0
        /// <summary>
        /// Returns a value for the logical AND operator node.
        /// </summary>
        /// <param name="context">Context to evaluate expressions against.</param>
        /// <param name="evalContext">Current expression evaluation context.</param>
        /// <returns>Node's value.</returns>
        protected override object Get(object context, EvaluationContext evalContext)
        {
            object l = GetLeftValue(context, evalContext);
            object r = GetRightValue(context, evalContext);

            if (NumberUtils.IsInteger(l) && NumberUtils.IsInteger(r))
            {
                return(NumberUtils.BitwiseXor(l, r));
            }
            else if (NumberUtils.IsInteger(l))
            {
                r = Convert.ToInt32(r);
                return(NumberUtils.BitwiseXor(l, r));
            }
            else if (NumberUtils.IsInteger(r))
            {
                l = Convert.ToInt32(l);
                return(NumberUtils.BitwiseXor(l, r));
            }
            else if (l is Enum && l.GetType() == r.GetType())
            {
                Type enumType     = l.GetType();
                Type integralType = Enum.GetUnderlyingType(enumType);
                l = Convert.ChangeType(l, integralType);
                r = Convert.ChangeType(r, integralType);
                object result = NumberUtils.BitwiseXor(l, r);
                return(Enum.ToObject(enumType, result));
            }
            return(Convert.ToBoolean(l) ^ Convert.ToBoolean(r));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns a value for the logical AND operator node.
        /// </summary>
        /// <param name="context">Context to evaluate expressions against.</param>
        /// <param name="evalContext">Current expression evaluation context.</param>
        /// <returns>Node's value.</returns>
        protected override object Get(object context, EvaluationContext evalContext)
        {
            var lhs = GetLeftValue(context, evalContext);
            var rhs = GetRightValue(context, evalContext);

            if (NumberUtils.IsInteger(lhs) && NumberUtils.IsInteger(rhs))
            {
                return(NumberUtils.BitwiseXor(lhs, rhs));
            }
            if (lhs is Enum && lhs.GetType() == rhs.GetType())
            {
                var enumType     = lhs.GetType();
                var integralType = Enum.GetUnderlyingType(enumType);
                lhs = Convert.ChangeType(lhs, integralType);
                rhs = Convert.ChangeType(rhs, integralType);
                var result = NumberUtils.BitwiseXor(lhs, rhs);

                return(Enum.ToObject(enumType, result));
            }
            return(Convert.ToBoolean(lhs) ^ Convert.ToBoolean(rhs));
        }
Ejemplo n.º 3
0
 public void BitwiseXor()
 {
     Assert.AreEqual( ((Byte)2) ^ ((Byte)3), NumberUtils.BitwiseXor((Byte)2, (Byte)3));
     Assert.AreEqual(((SByte)2) ^ ((SByte)3), NumberUtils.BitwiseXor((SByte)2, (SByte)3));
     Assert.AreEqual(((Int16)2) ^ ((Int16)3), NumberUtils.BitwiseXor((Int16)2, (Int16)3));
     Assert.AreEqual(((UInt16)2) ^ ((UInt16)3), NumberUtils.BitwiseXor((UInt16)2, (UInt16)3));
     Assert.AreEqual(((Int32)2) ^ ((Int32)3), NumberUtils.BitwiseXor((Int32)2, (Int32)3));
     Assert.AreEqual(((UInt32)2) ^ ((UInt32)3), NumberUtils.BitwiseXor((UInt32)2, (UInt32)3));
     Assert.AreEqual(((Int64)2) ^ ((Int64)3), NumberUtils.BitwiseXor((Int64)2, (Int64)3));
     Assert.AreEqual(((UInt64)2) ^ ((UInt64)3), NumberUtils.BitwiseXor((UInt64)2, (UInt64)3));
     Assert.AreEqual(((UInt64)2) ^ ((Byte)3), NumberUtils.BitwiseXor((UInt64)2, (Byte)3));
     Assert.AreEqual(false, NumberUtils.BitwiseXor(false, false));
     Assert.AreEqual(false, NumberUtils.BitwiseXor(true, true));
     Assert.AreEqual(true, NumberUtils.BitwiseXor(false, true));
     Assert.AreEqual(true, NumberUtils.BitwiseXor(true, false));
     try
     {
         NumberUtils.BitwiseAnd((double)2.0, 3);
         Assert.Fail();
     }
     catch(ArgumentException)
     {}
 }