How to check the correct ordering of a sequence of values?

I have many Action objects with the long Timestamp property. I want to do something like this:

 Assert.IsTrue(a1.Timestamp < a2.Timestamp < a3.Timestamp < ... < an.Timestamp); 

Unfortunately, this syntax is illegal. Is there a built-in method or extension \ LINQ \ any way to accomplish this?

Note that it targets the unit test class, so go crazy. I don't care about performance, readability, etc.

+4
source share
4 answers
 private static bool isValid(params Action[] actions) { for (int i = 1; i < actions.Length; i++) if (actions[i-1].TimeStamp >= actions[i].TimeStamp) return false; return true; } Assert.IsTrue(isValid(a1,a2,...,an)); 
+6
source

What about:

 Action[] actions = { a1, a2, a3, ... an }; Assert.IsTrue (actions.Skip(1) .Zip(action, (next, prev) => prev.Timestamp < next.Timestamp) .All(b => b)); 
+4
source
 public bool InOrder(params long[] data) { bool output = true; for (int i = 0; i <= data.Count-1;i++) { output &= data[i] < data[i + 1]; } return output; } 

I used a for loop, as this guarantees an iteration order that the foreach loop will not do.

+1
source

assuming actions is a list or array:

 actions.Skip(1).Where((x,index)=>x.Timespan > actions[i].Timespan).All(x=>x) 
+1
source

Source: https://habr.com/ru/post/1338731/


All Articles