I am doing some unit tests and I want to know if there's any way to test if a list is ordered by a property of the objects it contains.
Right now I am doing it this way but I don't like it, I want a better way. Can somebody help me please?
// (fill the list) List<StudyFeedItem> studyFeeds = Feeds.GetStudyFeeds(2120, DateTime.Today.AddDays(-200), 20); StudyFeedItem previous = studyFeeds.First(); foreach (StudyFeedItem item in studyFeeds) { if (item != previous) { Assert.IsTrue(previous.Date > item.Date); } previous = item; }
The simplest way to check this is run a loop for first element and check if we could find any smaller element than it after that element, if yes, the list is not sorted. if ( not flag) : print ( "Yes, List is sorted." )
You can go through the example given: # using naive method to # check sorted list flag = 0 i = 1 while i < len(test_list): if(test_list[i] < test_list[i - 1]): flag = 1 i += 1 # printing result if (not flag) : print ("Yes, List is sorted.") else : print ("No, List is not sorted.")
4.1.reverseOrder() to check if a list is sorted in reverse order. In addition, we can use natural(). nullFirst() and natural(). nullLast() to check if null appears to the first or the last of the sorted list.
GetItems(true) returns an ordered list of items, then test that. But don't test that items. OrderBy(x => x, new YourComparer()) does indeed sort the list. However, do unit test that YourComparer does indeed compare correctly.
If you are using MSTest, you may want to take a look at CollectionAssert.AreEqual.
Enumerable.SequenceEqual may be another useful API to use in an assertion.
In both cases you should prepare a list that holds the expected list in the expected order, and then compare that list to the result.
Here's an example:
var studyFeeds = Feeds.GetStudyFeeds(2120, DateTime.Today.AddDays(-200), 20); var expectedList = studyFeeds.OrderByDescending(x => x.Date); Assert.IsTrue(expectedList.SequenceEqual(studyFeeds));
A .NET 4.0 way would be to use the Enumerable.Zip
method to zip the list with itself offset by one, which pairs each item with the subsequent item in the list. You can then check that the condition holds true for each pair, e.g.
var ordered = studyFeeds.Zip(studyFeeds.Skip(1), (a, b) => new { a, b }) .All(p => p.a.Date < p.b.Date);
If you're on an earlier version of the framework you can write your own Zip method without too much trouble, something like the following (argument validation and disposal of the enumerators if applicable is left to the reader):
public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>( this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> selector) { var e1 = first.GetEnumerator(); var e2 = second.GetEnumerator(); while (e1.MoveNext() & e2.MoveNext()) // one & is important yield return selector(e1.Current, e2.Current); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With