Esempio n. 1
0
        public static RangedInt operator -(RangedInt original, RangedInt subtraction)
        {
            var result = new RangedInt(minValue: original.MinValue, maxValue: original.MaxValue, onBoundaryExceeded: original.OnBoundaryExceeded);

            if (int.MaxValue - original.Value < subtraction.Value)
            {
                result.SetValue(original.MinValue);
                result.OnBoundaryExceeded?.Invoke(new BoundaryExceededArgs($"{original.Value} - {subtraction.Value} < int.MinValue"));
            }
            else
            {
                result.SetValue(original.Value - subtraction.Value);
            }

            return(result);
        }
Esempio n. 2
0
        // TODO: test adding negative numbers
        public static RangedInt operator +(RangedInt original, int addition)
        {
            var result = new RangedInt(minValue: original.MinValue, maxValue: original.MaxValue, onBoundaryExceeded: original.OnBoundaryExceeded);

            if (int.MaxValue - original.Value < addition)
            {
                result.SetValue(original.MaxValue);
                result.OnBoundaryExceeded?.Invoke(new BoundaryExceededArgs($"{original.Value} + {addition} > int.MaxValue"));
            }
            else
            {
                result.SetValue(original.Value + addition);
            }

            return(result);
        }