Пример #1
0
        /// <summary>
        /// Gets the maximum value in <paramref name="_this"/>.
        /// </summary>
        /// <typeparam name="T">The type of the objects to compare.</typeparam>
        /// <param name="_this">The <see cref="IEnumerable{T}"/> that is used.</param>
        /// <param name="comparer">The <see cref="IComparer"/> used for comparison.</param>
        /// <returns>The maximum value in <paramref name="_this"/>.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="_this"/> is null.</exception>
        /// <exception cref="ArgumentException">Thrown if <paramref name="_this"/> is empty.</exception>
        /// <exception cref="ArgumentException">Thrown if <paramref name="_this"/> contains null values only.</exception>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="comparer"/> is null.</exception>
        /// <example>
        /// <code>
        /// var min = myEnumeration.Max(new MyComparer());
        /// </code>
        /// </example>
        public static T Maximum <T>(this IEnumerable <T> _this, IComparer comparer)
        {
            Throw.If.Object.IsNull(_this, nameof(_this));
            Throw.If.Object.IsNull(comparer, nameof(comparer));

            return(_this.Maximum(DynamicComparer.FromComparer <T>(comparer)));
        }
Пример #2
0
        void FromIComparable()
        {
            IComparer comp   = null;
            Int32     result = 0;

            Test.IfNot.Action.ThrowsException(() => comp = DynamicComparer.FromIComparable <ExDummyIComparable>(), out Exception ex);

            Test.IfNot.Action.ThrowsException(() => result = comp.Compare(null, null), out ArithmeticException ex2);
            Test.If.Value.IsEqual(result, 0);

            Test.IfNot.Action.ThrowsException(() => result = comp.Compare(null, (ExDummyIComparableT)0), out ex2);
            Test.If.Value.IsEqual(result, 0);

            Test.IfNot.Action.ThrowsException(() => result = comp.Compare((ExDummyIComparableT)0, null), out ex2);
            Test.If.Value.IsEqual(result, 0);

            Test.IfNot.Action.ThrowsException(() => result = comp.Compare((ExDummyIComparableT)0, (ExDummyIComparableT)1), out ex2);
            Test.If.Value.IsEqual(result, 0);

            Test.IfNot.Action.ThrowsException(() => result = comp.Compare(null, (ExDummyIComparable)0), out ex2);
            Test.If.Value.IsEqual(result, -1);

            Test.IfNot.Action.ThrowsException(() => result = comp.Compare((ExDummyIComparable)0, null), out ex2);
            Test.If.Value.IsEqual(result, 1);

            Test.If.Action.ThrowsException(() => result = comp.Compare((ExDummyIComparable)0, (ExDummyIComparable)1), out ex2);
            Test.If.Value.IsEqual(ex2.Message, "'1'");
        }
Пример #3
0
        /// <summary>
        /// Gets the minimum value in <paramref name="_this"/>.
        /// </summary>
        /// <typeparam name="T">The type of the objects to compare.</typeparam>
        /// <param name="_this">The <see cref="IEnumerable{T}"/> that is used.</param>
        /// <param name="comparer">The <see cref="IComparer"/> used for comparison.</param>
        /// <returns>The minimum value in <paramref name="_this"/>.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="_this"/> is null.</exception>
        /// <exception cref="ArgumentException">Thrown if <paramref name="_this"/> is empty.</exception>
        /// <exception cref="ArgumentException">Thrown if <paramref name="_this"/> contains null values only.</exception>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="comparer"/> is null.</exception>
        /// <example>
        /// <code>
        /// var min = myEnumeration.Min(new MyComparer());
        /// </code>
        /// </example>
        public static T Minimum <T>(this IEnumerable <T> _this, IComparer comparer)
        {
            Throw.If.Object.IsNull(_this, nameof(_this));
            Throw.If.Value.IsFalse(_this.Count() > 0, nameof(_this), "The enumeration is empty.");
            Throw.If.Value.IsFalse(_this.Any((element) => element != null), nameof(_this), "The enumeration only contains null values.");
            Throw.If.Object.IsNull(comparer, nameof(comparer));

            return(_this.Minimum(DynamicComparer.FromComparer <T>(comparer)));
        }
Пример #4
0
        void FromDelegate()
        {
            IComparer comp = null;

            Test.If.Action.ThrowsException(() => comp = DynamicComparer.FromDelegate(null), out ArgumentNullException ex1);
            Test.If.Value.IsEqual(ex1.ParamName, "compare");

            Test.IfNot.Action.ThrowsException(() => comp = DynamicComparer.FromDelegate((x, y) => 21), out Exception ex2);
        }
        void ComparisonInvokes()
        {
            IComparer comp   = null;
            Int32     result = 0;

            comp = DynamicComparer.FromDelegate((x, y) => throw new NotImplementedException());
            Test.If.Action.ThrowsException(() => result = comp.Compare(0, 1), out NotImplementedException ex1);

            comp = DynamicComparer.FromDelegate((x, y) => 42);
            Test.IfNot.Action.ThrowsException(() => result = comp.Compare(0, 1), out Exception ex2);
            Test.If.Value.IsEqual(result, 42);
        }
Пример #6
0
        void FromComparer()
        {
            IComparer <Dummy> comp   = null;
            Int32             result = 0;

            Test.If.Action.ThrowsException(() => comp = DynamicComparer.FromComparer <Dummy>(null), out ArgumentNullException ex1);
            Test.If.Value.IsEqual(ex1.ParamName, "comparer");

            Test.IfNot.Action.ThrowsException(() => comp = DynamicComparer.FromComparer <Dummy>(DynamicComparer.FromDelegate((x, y) => 42)), out Exception ex2);

            result = comp.Compare(0, 1);
            Test.If.Value.IsEqual(result, 42);
        }
Пример #7
0
        void MaxIComparerT_ThrowsException()
        {
            IEnumerable <Dummy> notEmpty = new Dummy[] { null, -1, -1, null, 0, 1, 1 };
            IEnumerable <Dummy> empty    = Enumerable.Empty <Dummy>();
            IEnumerable <Dummy> nulls    = new Dummy[] { null, null };

            Test.If.Action.ThrowsException(() => IEnumerableTExtensions.Maximum(null, null as IComparer <Dummy>), out ArgumentNullException ex1);
            Test.If.Value.IsEqual(ex1.ParamName, "_this");

            Test.If.Action.ThrowsException(() => IEnumerableTExtensions.Maximum(null, DynamicComparer.FromDelegate <Dummy>((x, y) => 0)), out ex1);
            Test.If.Value.IsEqual(ex1.ParamName, "_this");

            Test.If.Action.ThrowsException(() => notEmpty.Maximum(null as IComparer <Dummy>), out ex1);
            Test.If.Value.IsEqual(ex1.ParamName, "comparer");

            Test.If.Action.ThrowsException(() => empty.Maximum(DynamicComparer.FromDelegate <Dummy>((x, y) => 0)), out ArgumentException ex2);
            Test.If.Value.IsEqual(ex2.ParamName, "_this");
            Test.If.String.StartsWith(ex2.Message, "The enumeration is empty.");

            Test.If.Action.ThrowsException(() => nulls.Maximum(DynamicComparer.FromDelegate <Dummy>((x, y) => 0)), out ex2);
            Test.If.Value.IsEqual(ex2.ParamName, "_this");
            Test.If.String.StartsWith(ex2.Message, "The enumeration only contains null values.");
        }