public void ComparisonTest()
        {
            RestrictedNumber rn = new RestrictedNumber(5, 12);
            RestrictedNumber rnNull = null;

            rn.Set(7);

            (rn < 7).Should().Be(false);
            (rn > 7).Should().Be(false);
            (rn <= 7).Should().Be(true);
            (rn >= 7).Should().Be(true);
            (rn == 7).Should().Be(true);
            (rn != 7).Should().Be(false);

            (7 < rn).Should().Be(false);
            (7 > rn).Should().Be(false);
            (7 <= rn).Should().Be(true);
            (7 >= rn).Should().Be(true);
            (7 == rn).Should().Be(true);
            (7 != rn).Should().Be(false);

            (null == rnNull).Should().Be(true);
            (7 == rnNull).Should().Be(false);
            (rnNull == null).Should().Be(true);
            (rnNull == 7).Should().Be(false);
            (null != rnNull).Should().Be(false);
            (7 != rnNull).Should().Be(true);
            (rnNull != null).Should().Be(false);
            (rnNull != 7).Should().Be(true);

            (rn == rnNull).Should().Be(false);
            (rn != rnNull).Should().Be(true);
            (rnNull == rn).Should().Be(false);
            (rnNull != rn).Should().Be(true);
        }
示例#2
0
 static T get_restricted_value(RestrictedNumber <T> n)
 {
     // another yoink from Jon Skeet
     return(n.value.CompareTo(n.min) < 0 ? n.min
             : n.value.CompareTo(n.max) > 0 ? n.max
                 : n.value);
 }
示例#3
0
    public void ad_hoc_paces()
    {
        // declare with min, max, and value
        var i = new RestrictedNumber <int>(1, 10, 15);

        Debug.Assert(i == 10d);
        Debug.Assert(i.UnrestrictedValue == 15d);
        // declare implicitly
        // my implementation initially sets min and max equal to value
        RestrictedNumber <double> d = 15d;

        d.Min = 1;
        d.Max = 10;

        Debug.Assert(i == 10d);                   // compare with other, "true" doubles
        Debug.Assert(i.UnrestrictedValue == 15d); // still holds the original value
        RestrictedNumber <decimal> m = new RestrictedNumber <decimal>(55.5m, 55.5m, 55.499m);

        Debug.Assert(m == 55.5m);
        Debug.Assert(m > m.UnrestrictedValue);  // test out some other operators
        Debug.Assert(m >= m.UnrestrictedValue); // we didn't have to define these
        Debug.Assert(m + 10 == 65.5m);          // you even get unary operators
        RestrictedNumber <decimal> other = 50m;

        Debug.Assert(m > other);           // compare two of these objects
        Debug.Assert(other <= m);          // ...again without having to define the operators
        Debug.Assert(m - 5.5m == other);   // unary works with other Ts
        Debug.Assert(m + other == 105.5m); // ...and with other RestrictedNumbers
        Debug.Assert(55.5m - m == 0);
        Debug.Assert(m - m == 0);
        // passing to method that expects the primitive type
        Func <float, float>      square_float     = f => f * f;
        RestrictedNumber <float> restricted_float = 3;

        Debug.Assert(square_float(restricted_float) == 9f);
        // this sort of implementation is not without pitfalls
        // there are other IEquatable<T> & IComaparable<T> types out there...
        var restricted_string = new RestrictedNumber <string>("Abigail", "Xander", "Yolanda");

        Debug.Assert(restricted_string == "Xander");     // this works
        //Debug.Assert(restricted_string >= "Thomas"); // many operators not supported here
        var pitfall = new RestrictedNumber <int>(1, 100, 200);

        Debug.Assert(pitfall == 100);
        pitfall = 200;
        // Debug.Assert(pitfall == 100);
        // FAIL -- using the implicit operator is effectively
        // a factory method that returns a NEW RestrictedNumber
        // with min and max initially equal to value (in my implementation)
        Debug.Assert(pitfall == 200);
        pitfall = 10;
        Debug.Assert(pitfall.Min == 10 && pitfall.Max == 10);
        pitfall++;
        Debug.Assert(pitfall == 11);                          // d'oh!
        Debug.Assert(pitfall.Min == 11 && pitfall.Max == 11); // "it goes up to eleven"
        // if you need to change the input value for an existing
        // RestrictedNumber, you could expose a SetValue method
        // and make value not readonly
    }
        public void MinimumCantBeHigherThanMaximumTest()
        {
            Action action = (() => {
                var rn = new RestrictedNumber(6, 5);
            });

            action.ShouldThrow<ArgumentException>();
        }
        public void HashcodeShouldNotThrowException()
        {
            RestrictedNumber rn = new RestrictedNumber(0, int.MaxValue, int.MaxValue);

            Action action = () => rn.GetHashCode();

            action.ShouldNotThrow();
        }
        public void AsPercentageTest()
        {
            RestrictedNumber rn = new RestrictedNumber(0, 1000, 500);

            rn.AsPercent().Should().Be(50);
            rn.Set(499).AsPercent().Should().Be(49);
            rn.Set(501).AsPercent().Should().Be(50);
        }
示例#7
0
 public int CompareTo(RestrictedNumber <T> other)
 {
     return(restricted_value.CompareTo(other));
 }
示例#8
0
 public bool Equals(RestrictedNumber <T> other)
 {
     return(restricted_value.Equals(other));
 }
        public void OperatorTest()
        {
            RestrictedNumber rn = 5;

            rn.Current.Should().Be(5);
            rn.Minimum.Should().Be(int.MinValue);
            rn.Maximum.Should().Be(int.MaxValue);
            rn.Remainder.Should().Be(0);

            (rn * 5).Should().Be(25);
            (rn / 5).Should().Be(1);
            (rn + 5).Should().Be(10);
            (rn - 5).Should().Be(0);

            (5 * rn).Should().Be(25);
            (5 / rn).Should().Be(1);
            (5 + rn).Should().Be(10);
            (5 - rn).Should().Be(0);

            var rn2 = new RestrictedNumber(5, 12, 10);

            var rn3 = rn2 + rn2;
            rn3.Current.Should().Be(20);
            rn3.Minimum.Should().Be(5);
            rn3.Maximum.Should().Be(24);
            rn3.Remainder.Should().Be(0);

            rn3 = rn2 - rn2;
            rn3.Current.Should().Be(5);
            rn3.Minimum.Should().Be(5);
            rn3.Maximum.Should().Be(5);
            rn3.Remainder.Should().Be(5);
        }
        public void RemainderTest()
        {
            RestrictedNumber rn = new RestrictedNumber(5, 12);

            rn.Add(3);

            rn.Current.Should().Be(rn.Maximum);
            rn.Current.Should().Be(12);
            rn.Remainder.Should().Be(3);

            rn.Sub(5);

            rn.Current.Should().Be(7);
            rn.Remainder.Should().Be(0);

            rn.Sub(6);

            rn.Current.Should().Be(5);
            rn.Current.Should().Be(rn.Minimum);
            rn.Remainder.Should().Be(4);
        }