Пример #1
0
        public static Rule GetRule()
        {
            Rule firstRule = new NumericRule();

            firstRule.ChainRule(new DigitsRule())
            .ChainRule(new MatchRule())
            .ChainRule(new GameRule());

            return(firstRule);
        }
        /// <summary>
        /// Implementation of <see cref="IRuleUserControl"/>. This method is called by the parent form
        /// for invoking the addition of a new rule.
        /// </summary>
        /// <param name="ruleKind"><see cref="RuleKind"/></param>
        /// <returns>New <see cref="Rule"/></returns>
        public Rule InvokeSubmission(RuleKind ruleKind)
        {
            bool parsingSuccessful = double.TryParse(textBoxValue.Text, out double value);

            if (!parsingSuccessful)
            {
                MessageBox.Show("Bitte eine gültige Zahl eingeben.");
                return(null);
            }
            if (passedRule != null)
            {
                passedRule.Name         = textBoxDescription.Text;
                passedRule.NumericValue = value;
                return(passedRule);
            }
            TableUtility tableUtility = new TableUtility();
            Rule         newRule      = new NumericRule(value, ruleKind, textBoxDescription.Text);

            tableUtility.CreateTable(newRule);
            tableUtility.InsertTableRow(newRule);
            return(newRule);
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="passedRule"><see cref="NumericRule"/> if a rule has to be edited</param>
 public UserControlNumericOneValue(NumericRule passedRule = null)
 {
     this.passedRule = passedRule;
     InitializeComponent();
     InitializePassedRule();
 }
Пример #4
0
        public static Why Check(double value, NumericRule rule, Action <string> onError = null)
        {
            string reason = "";

            //handle -0
            double negativeZero = -1.0 * 0;

            if (BitConverter.DoubleToInt64Bits(0) == BitConverter.DoubleToInt64Bits(negativeZero))
            {
                value = 0;
            }

            //if ok, return true
            switch (rule)
            {
            case NumericRule.IsZero:
                if (value == 0.0)
                {
                    return(true);
                }
                reason = string.Format("expecting zero, got: {0}", value);
                break;

            case NumericRule.IsNotZero:
                if (value != 0.0)      //nb -0 == 0 in c#
                {
                    return(true);
                }
                reason = string.Format("value can not be zero");
                break;

            case NumericRule.IsGreaterThanOrEqualZero:
                if (value >= 0.0)
                {
                    return(true);
                }
                reason = string.Format("expecting >= zero, got: {0}", value);
                break;

            case NumericRule.IsGreaterThanZero:
                if (value > 0.0)
                {
                    return(true);
                }
                reason = string.Format("expecting > zero, got: {0}", value);
                break;

            case NumericRule.IsLessThanOrEqualZero:
                if (value <= 0.0)
                {
                    return(true);
                }
                reason = string.Format("expecting <= zero, got: {0}", value);
                break;

            case NumericRule.IsLesThanZero:
                if (value < 0.0)
                {
                    return(true);
                }
                reason = string.Format("expecting < zero, got: {0}", value);
                break;

            case NumericRule.IsOdd:
                if (isWholeNumber(value))
                {
                    if (isOdd((int)value))
                    {
                        return(true);
                    }
                    reason = string.Format("expecting an odd number, got: {0}", value);
                }
                else
                {
                    reason = string.Format("expecting odd number, but did not get a whole number {0}", value);
                }
                break;

            case NumericRule.IsEven:
                if (isWholeNumber(value))
                {
                    if (isEven((int)value))
                    {
                        return(true);
                    }
                    reason = string.Format("expecting an even number, got: {0}", value);
                }
                else
                {
                    reason = string.Format("expecting even number, but did not get a whole number {0}", value);
                }
                break;

            case NumericRule.IsUnsignedByte:
                return(CheckRangeInclusive(value, byte.MinValue, byte.MaxValue, onError));

            case NumericRule.IsSignedByte:
                return(CheckRangeInclusive(value, sbyte.MinValue, sbyte.MaxValue, onError));

            case NumericRule.IsUnsignedInt32:
                return(CheckRangeInclusive(value, uint.MinValue, uint.MaxValue, onError));

            case NumericRule.IsSignedInt32:
                return(CheckRangeInclusive(value, int.MinValue, int.MaxValue, onError));

            case NumericRule.IsPow2:
                if (isWholeNumber(value))
                {
                    if (IsPowerOfTwo((ulong)value))
                    {
                        return(true);
                    }
                    reason = string.Format("expecting a power of 2, got: {0}", value);
                }
                else
                {
                    reason = string.Format("expecting a power of 2, but did not get a whole number {0}", value);
                }
                break;

            default:
                reason = string.Format("INVALID CHECK ({0})", rule);
                break;
            }


            //here because the test failed
            doAction(onError, reason);
            return(Why.FalseBecause(reason));
        }