diff --git a/SacsCoreUnitTests/Standard/Base/Utils/ListUtil_Tester.cs b/SacsCoreUnitTests/Standard/Base/Utils/ListUtil_Tester.cs new file mode 100644 index 0000000..3f03934 --- /dev/null +++ b/SacsCoreUnitTests/Standard/Base/Utils/ListUtil_Tester.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Reflection; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Sarand.Net.Standard.Base.Utils; + +namespace Sarand.UnitTests.Core.Base.Utils +{ + [TestClass] + public class ListUtil_Tester + { + //------------------------------------------------------------------- + // Data members. + + //------------------------------------------------------------------- + // Is Empty. + //------------------------------------------------------------------- + [TestMethod] + public void ListUtil_IsEmpty_Test() + { + List values = new List(); + + Assert.AreEqual(true, ListUtil.IsEmpty(values)); + Assert.AreEqual(true, values.IsEmpty()); + + for (int i = 0; i < 10; i++) + values.Add(i.ToString()); + + Assert.AreEqual(false, ListUtil.IsEmpty(values)); + Assert.AreEqual(false, values.IsEmpty()); + + values.Clear(); + + Assert.AreEqual(true, ListUtil.IsEmpty(values)); + Assert.AreEqual(true, values.IsEmpty()); + } + + //------------------------------------------------------------------- + // Randomise. + //------------------------------------------------------------------- + [TestMethod] + public void ListUtil_Randomise_Test() + { + // Assume that randomising a list of 100 values highly unlikely to result in same sequence! + int itemsInList = 100; + int testsToRun = 1000; + + for (int testIdx = 0; testIdx < testsToRun; testIdx++) + { + // Initialise list with sequence. + List values = new List(); + for (int i = 0; i < itemsInList; i++) + values.Add(i.ToString()); + + for (int i = 0; i < itemsInList; i++) + Assert.AreEqual(i.ToString(), values[i]); + + values = values.Randomise(); + //Debug.WriteLine($"{SysUtil.Fn} Values: {values.Concat(",")}"); + + bool originalSequence = true; + for (int i = 0; i < itemsInList; i++) + { + if (values[i] != i.ToString()) + { + originalSequence = false; + break; + } + } + Assert.AreEqual(false, originalSequence); + } + } + + //------------------------------------------------------------------- + } +} diff --git a/SacsStandardBase/Base/Utils/ListUtil.cs b/SacsStandardBase/Base/Utils/ListUtil.cs index dcff176..63f530d 100644 --- a/SacsStandardBase/Base/Utils/ListUtil.cs +++ b/SacsStandardBase/Base/Utils/ListUtil.cs @@ -24,7 +24,7 @@ namespace Sarand.Net.Standard.Base.Utils /// Is the list empty public static Boolean IsEmpty(this IEnumerable items) { - return items.Count() == 0; + return !items.Any(); } //-------------------------------------------------------------------