Hooked on LINQ

Hooked on LINQ - Developers' Wiki
for .NET Language Integrated Query

Companion book for this site
LINQ to Objects Using C# 4.0:
Using and Extending LINQ to Objects and Parallel LINQ (PLINQ)
Quick Search

Advanced Search »

Min Operator Unit Tests

Modified: 2007/01/10 04:18 by t_magennis - Categorized as: LINQ to Objects, Samples
Unit tests written by Troy Magennis. They run against the MAY 2006 CTP uning NUnit.


using System;
using System.Collections.Generic;
using System.Text;
using System.Query;
using NUnit.Framework;
 
namespace LINQUnitTests
{
    [TestFixture]
    public class MinTests
    {
        #region <int> Min
 
        /* Extract from Sequence.cs May 2006 CTP
        public static int Min(this IEnumerable<int> source) {
            if (source == null) throw Error.ArgumentNull("source");
            int value = 0;
            bool hasValue = false;
            foreach (int x in source) {
                if (hasValue) {
                    if (x < value) value = x;
                }
                else {
                    value = x;
                    hasValue = true;
                }
            }
            if (hasValue) return value;
            throw Error.NoElements();
        }
        */
        [Test]
        public void MinIntTest()
        {
            int[] values = new int[] { 5,4,3,2,1,-1,-2,-3,6,7,8,9,0 };
            int result = values.Min();
            Assert.AreEqual(-3, result, "<int> Min not returning the correct result.");
        }
        
        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void MinIntNullSourceTest()
        {
            int[] values = null;
            values.Min();
        } 
 
        [Test]
        [ExpectedException("System.InvalidOperationException")]
        public void MinIntEmptySourceTest()
        {
            var values = Sequence.Empty<int>();
            var result = values.Min();
        } 
 
        /* Extract from Sequence.cs May 2006 CTP
        public static int Min<T>(this IEnumerable<T> source, Func<T, int> selector) {
            return Sequence.Min(Sequence.Select(source, selector));
        }
        */        
        [Test]
        public void MinIntWithSelectorTest()
        {
            int[] values = new int[] { 5,4,3,2,1,-1,-2,-3,6,7,8,9,0 };
            int result = values.Min(i => i + 1);
            Assert.AreEqual(-2, result, "<int> Min extension method with Selector not returning correct Min.");
        }
 
        #endregion <int> Min
 
        #region <int?> Min
 
        /* Extract from Sequence.cs May 2006 CTP
        public static int? Min(this IEnumerable<int?> source) {
            if (source == null) throw Error.ArgumentNull("source");
            int? value = null;
            foreach (int? x in source) {
                if (value == null || x < value) value = x;
            }
            return value;
        }
        */
        [Test]
        public void MinNullableIntTest()
        {
            int?[] values = new int?[] { 5,null,4,null,3,2,1,-1,-2,-3,6,7,8,9,0,null };
            int? result = values.Min();
            Assert.AreEqual(-3, result, "<int?> Min not returning the correct result.");
        }
        
        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void MinNullableIntNullSourceTest()
        {
            int?[] values = null;
            values.Min();
        } 
 
        [Test]
        public void MinNullableIntEmptySourceTest()
        {
            int?[] values = Sequence.Empty<int?>().ToArray<int?>();
            int? result = values.Min();
            Assert.IsNull(result, "<int?> Min return null as their result when no elements exist");
        } 
 
        [Test]
        public void MinNullableIntOnlyNullSourceTest()
        {
            int?[] values = new int?[] {null,null,null};
            int? result = values.Min();
            Assert.IsNull(result, "<int?> Min return null as their result when only null elements exist");
        } 
 
        /* Extract from Sequence.cs May 2006 CTP
        public static int? Min<T>(this IEnumerable<T> source, Func<T, int?> selector) {
            return Sequence.Min(Sequence.Select(source, selector));
        }
        */        
        [Test]
        public void MinNullableIntWithSelectorTest()
        {
            int?[] values = new int?[] { 5,null,4,null,3,2,1,-1,-2,-3,6,7,8,9,0,null };
            int? result = values.Min(i => i + 1);
            Assert.AreEqual(-2, result, "<int?> Min extension method with Selector not returning correct Min.");
        }
 
        #endregion <int?> Min
       
        #region <long> Min
 
        /* Extract from Sequence.cs May 2006 CTP
        public static long Min(this IEnumerable<long> source) {
            if (source == null) throw Error.ArgumentNull("source");
            long value = 0;
            bool hasValue = false;
            foreach (long x in source) {
                if (hasValue) {
                    if (x < value) value = x;
                }
                else {
                    value = x;
                    hasValue = true;
                }
            }
            if (hasValue) return value;
            throw Error.NoElements();
        }
        */
        [Test]
        public void MinLongTest()
        {
            long[] values = new long[] { 5,4,3,2,1,-1,-2,-3,6,7,8,9,0 };
            long result = values.Min();
            Assert.AreEqual(-3, result, "<long> Min not returning the correct result.");
        }
        
        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void MinLongNullSourceTest()
        {
            long[] values = null;
            values.Min();
        } 
 
        [Test]
        [ExpectedException("System.InvalidOperationException")]
        public void MinLongEmptySourceTest()
        {
            var values = Sequence.Empty<long>();
            var result = values.Min();
        } 
 
        /* Extract from Sequence.cs May 2006 CTP
        public static long Min<T>(this IEnumerable<T> source, Func<T, long> selector) {
            return Sequence.Min(Sequence.Select(source, selector));
        }
        */        
        [Test]
        public void MinLongWithSelectorTest()
        {
            long[] values = new long[] { 5,4,3,2,1,-1,-2,-3,6,7,8,9,0 };
            long result = values.Min(i => i + 1);
            Assert.AreEqual(-2, result, "<long> Min extension method with Selector not returning correct Min.");
        }
 
        #endregion <long> Min
 
        #region <long?> Min
 
        /* Extract from Sequence.cs May 2006 CTP
        public static long? Min(this IEnumerable<long?> source) {
            if (source == null) throw Error.ArgumentNull("source");
            long? value = null;
            foreach (long? x in source) {
                if (value == null || x < value) value = x;
            }
            return value;
        }
        */
        [Test]
        public void MinNullableLongTest()
        {
            long?[] values = new long?[] { 5,null,4,null,3,2,1,-1,-2,-3,6,7,8,9,0,null };
            long? result = values.Min();
            Assert.AreEqual(-3, result, "<long?> Min not returning the correct result.");
        }
        
        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void MinNullableLongNullSourceTest()
        {
            long?[] values = null;
            values.Min();
        } 
 
        [Test]
        public void MinNullableLongEmptySourceTest()
        {
            long?[] values = Sequence.Empty<long?>().ToArray<long?>();
            long? result = values.Min();
            Assert.IsNull(result, "<long?> Min return null as their result when no elements exist");
        } 
 
        [Test]
        public void MinNullableLongOnlyNullSourceTest()
        {
            long?[] values = new long?[] {null,null,null};
            long? result = values.Min();
            Assert.IsNull(result, "<long?> Min return null as their result when only null elements exist");
        } 
 
        /* Extract from Sequence.cs May 2006 CTP
        public static long? Min<T>(this IEnumerable<T> source, Func<T, long?> selector) {
            return Sequence.Min(Sequence.Select(source, selector));
        }
        */        
        [Test]
        public void MinNullableLongWithSelectorTest()
        {
            long?[] values = new long?[] { 5,null,4,null,3,2,1,-1,-2,-3,6,7,8,9,0,null };
            long? result = values.Min(i => i + 1);
            Assert.AreEqual(-2, result, "<long?> Min extension method with Selector not returning correct Min.");
        }
 
        #endregion <long?> Min
    
        #region <double> Min
 
        /* Extract from Sequence.cs May 2006 CTP
        public static double Min(this IEnumerable<double> source) {
            if (source == null) throw Error.ArgumentNull("source");
            double value = 0;
            bool hasValue = false;
            foreach (double x in source) {
                if (hasValue) {
                    if (x < value) value = x;
                }
                else {
                    value = x;
                    hasValue = true;
                }
            }
            if (hasValue) return value;
            throw Error.NoElements();
        }
        */
        [Test]
        public void MinDoubleTest()
        {
            double[] values = new double[] { 5.5D,4.4D,3.3D,2.2D,1.1D,-1D,-2D,-3.0D,6D,7D,8D,9D,0D };
            double result = values.Min();
            Assert.AreEqual(-3, result, "<double> Min not returning the correct result.");
        }
        
        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void MinDoubleNullSourceTest()
        {
            double[] values = null;
            values.Min();
        } 
 
        [Test]
        [ExpectedException("System.InvalidOperationException")]
        public void MinDoubleEmptySourceTest()
        {
            var values = Sequence.Empty<double>();
            var result = values.Min();
        } 
 
        /* Extract from Sequence.cs May 2006 CTP
        public static double Min<T>(this IEnumerable<T> source, Func<T, double> selector) {
            return Sequence.Min(Sequence.Select(source, selector));
        }
        */        
        [Test]
        public void MinDoubleWithSelectorTest()
        {
            double[] values = new double[] { 5.5D,4.4D,3.3D,2.2D,1.1D,-1D,-2D,-3.0D,6D,7D,8D,9D,0D };
            double result = values.Min(i => i + 1);
            Assert.AreEqual(-2, result, "<double> Min extension method with Selector not returning correct Min.");
        }
 
        #endregion <double> Min
 
        #region <double?> Min
 
        /* Extract from Sequence.cs May 2006 CTP
        public static double? Min(this IEnumerable<double?> source) {
            if (source == null) throw Error.ArgumentNull("source");
            double? value = null;
            foreach (double? x in source) {
                if (value == null || x < value) value = x;
            }
            return value;
        }
        */
        [Test]
        public void MinNullableDoubleTest()
        {
            double?[] values = new double?[] { 5D,null,4D,null,3D,2D,1D,-1D,-2D,-3D,6D,7D,8D,9D,0D,null };
            double? result = values.Min();
            Assert.AreEqual(-3, result, "<double?> Min not returning the correct result.");
        }
        
        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void MinNullableDoubleNullSourceTest()
        {
            double?[] values = null;
            values.Min();
        } 
 
        [Test]
        public void MinNullableDoubleEmptySourceTest()
        {
            double?[] values = Sequence.Empty<double?>().ToArray<double?>();
            double? result = values.Min();
            Assert.IsNull(result, "<double?> Min return null as their result when no elements exist");
        } 
 
        [Test]
        public void MinNullableDoubleOnlyNullSourceTest()
        {
            double?[] values = new double?[] {null,null,null};
            double? result = values.Min();
            Assert.IsNull(result, "<double?> Min return null as their result when only null elements exist");
        } 
 
        /* Extract from Sequence.cs May 2006 CTP
        public static double? Min<T>(this IEnumerable<T> source, Func<T, double?> selector) {
            return Sequence.Min(Sequence.Select(source, selector));
        }
        */        
        [Test]
        public void MinNullableDoubleWithSelectorTest()
        {
            double?[] values = new double?[] { 5D,null,4D,null,3D,2D,1D,-1D,-2D,-3D,6D,7D,8D,9D,0D,null };
            double? result = values.Min(i => i + 1);
            Assert.AreEqual(-2, result, "<double?> Min extension method with Selector not returning correct Min.");
        }
 
        #endregion <double?> Min 
 
        #region <decimal> Min
 
        /* Extract from Sequence.cs May 2006 CTP
        public static decimal Min(this IEnumerable<decimal> source) {
            if (source == null) throw Error.ArgumentNull("source");
            decimal value = 0;
            bool hasValue = false;
            foreach (decimal x in source) {
                if (hasValue) {
                    if (x < value) value = x;
                }
                else {
                    value = x;
                    hasValue = true;
                }
            }
            if (hasValue) return value;
            throw Error.NoElements();
        }
        */
        [Test]
        public void MinDecimalTest()
        {
            decimal[] values = new decimal[] { 5,4,3,2,1,-1,-2,-3,6,7,8,9,0 };
            decimal result = values.Min();
            Assert.AreEqual(-3, result, "<decimal> Min not returning the correct result.");
        }
        
        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void MinDecimalNullSourceTest()
        {
            decimal[] values = null;
            values.Min();
        } 
 
        [Test]
        [ExpectedException("System.InvalidOperationException")]
        public void MinDecimalEmptySourceTest()
        {
            var values = Sequence.Empty<decimal>();
            var result = values.Min();
        } 
 
        /* Extract from Sequence.cs May 2006 CTP
        public static decimal Min<T>(this IEnumerable<T> source, Func<T, decimal> selector) {
            return Sequence.Min(Sequence.Select(source, selector));
        }
        */        
        [Test]
        public void MinDecimalWithSelectorTest()
        {
            decimal[] values = new decimal[] { 5,4,3,2,1,-1,-2,-3,6,7,8,9,0 };
            decimal result = values.Min(i => i + 1);
            Assert.AreEqual(-2, result, "<decimal> Min extension method with Selector not returning correct Min.");
        }
 
        #endregion <decimal> Min
 
        #region <decimal?> Min
 
        /* Extract from Sequence.cs May 2006 CTP
        public static decimal? Min(this IEnumerable<decimal?> source) {
            if (source == null) throw Error.ArgumentNull("source");
            decimal? value = null;
            foreach (decimal? x in source) {
                if (value == null || x < value) value = x;
            }
            return value;
        }
        */
        [Test]
        public void MinNullableDecimalTest()
        {
            decimal?[] values = new decimal?[] { 5,null,4,null,3,2,1,-1,-2,-3,6,7,8,9,0,null };
            decimal? result = values.Min();
            Assert.AreEqual(-3, result, "<decimal?> Min not returning the correct result.");
        }
        
        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void MinNullableDecimalNullSourceTest()
        {
            decimal?[] values = null;
            values.Min();
        } 
 
        [Test]
        public void MinNullableDecimalEmptySourceTest()
        {
            decimal?[] values = Sequence.Empty<decimal?>().ToArray<decimal?>();
            decimal? result = values.Min();
            Assert.IsNull(result, "<decimal?> Min return null as their result when no elements exist");
        } 
 
        [Test]
        public void MinNullableDecimalOnlyNullSourceTest()
        {
            decimal?[] values = new decimal?[] {null,null,null};
            decimal? result = values.Min();
            Assert.IsNull(result, "<decimal?> Min return null as their result when only null elements exist");
        } 
 
        /* Extract from Sequence.cs May 2006 CTP
        public static decimal? Min<T>(this IEnumerable<T> source, Func<T, decimal?> selector) {
            return Sequence.Min(Sequence.Select(source, selector));
        }
        */        
        [Test]
        public void MinNullableDecimalWithSelectorTest()
        {
            decimal?[] values = new decimal?[] { 5,null,4,null,3,2,1,-1,-2,-3,6,7,8,9,0,null };
            decimal? result = values.Min(i => i + 1);
            Assert.AreEqual(-2, result, "<decimal?> Min extension method with Selector not returning correct Min.");
        }
 
        #endregion <decimal?> Min
    
        #region <T> Min
        /* Extract from Sequence.cs May 2006 CTP
        public static T Min<T>(this IEnumerable<T> source) {
            if (source == null) throw Error.ArgumentNull("source");
            Comparer<T> comparer = Comparer<T>.Default;
            T value = default(T);
            bool hasValue = false;
            foreach (T x in source) {
                if (hasValue) {
                    if (comparer.Compare(x, value) < 0)
                        value = x;
                }
                else {
                    value = x;
                    hasValue = true;
                }
            }
            if (hasValue) return value;
            throw Error.NoElements();
        }
 
        public static S Min<T, S>(this IEnumerable<T> source, Func<T, S> selector) {
            return Sequence.Min(Sequence.Select(source, selector));
        }
        */ 
 
        [Test]
        public void MinGenericTest()
        {
            string[] values = new string[] { "AB", "AAA", "CDE", "AA", "F" };
            string result = values.Min<string>();
            Assert.AreEqual("AA", result, "<T> Min with default not returning the correct result.");
        }
        
        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void MinGenericNullSourceTest()
        {
            string[] values = null;
            values.Min<string>();
        } 
 
        [Test]
        [ExpectedException("System.InvalidOperationException")]
        public void MinGenericEmptySourceTest()
        {
            var values = Sequence.Empty<string>();
            var result = values.Min<string>();
        } 
 
        [Test]
        public void MinGenericWithSelectorTest()
        {
            string[] values = new string[] { "AB", "AAA", "CDE", "AA", "F" };
            string result = values.Min<string,string>(x => x.ToLower());
            Assert.AreEqual("aa", result, "<T> Min extension method with Selector not returning correct Min.");
        }
 
        #endregion <T> Min
 
    }
}



If you would like to comment on this page, click on the Discuss button located on the top-right of each page. Feel free to edit any mistakes or omissions you find. If you have an objection or find in-appropriate content then contact the administrator. This website is not affiliated with Microsoft®, all content and opinions are those of the specific author and some advice, solutions and article may contain unintentional errors - please use care. Powered by ScrewTurn Wiki version 2.0.33. Some of the icons created by FamFamFam.