Created
January 30, 2013 15:26
-
-
Save mormegil-cz/4673989 to your computer and use it in GitHub Desktop.
unit test of predicate logic :-)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestFixture] | |
public class PredicateLogicTests | |
{ | |
[Test] | |
public void TestNegationOfUniversalQuantifier() | |
{ | |
var empty = new int[0]; | |
var allOdd = new[] { 1, 3, 5 }; | |
var allEven = new[] { 2, 4, 6 }; | |
var allNumbers = new[] { 1, 2, 3 }; | |
Func<int, bool> isEven = x => (x % 2) == 0; | |
Assert.IsFalse(AllTest(allOdd, isEven)); | |
Assert.IsFalse(AnyTest(allOdd, isEven)); | |
Assert.IsTrue(AllTest(allEven, isEven)); | |
Assert.IsTrue(AnyTest(allEven, isEven)); | |
Assert.IsFalse(AllTest(allNumbers, isEven)); | |
Assert.IsFalse(AnyTest(allNumbers, isEven)); | |
Assert.IsTrue(AllTest(empty, isEven)); | |
Assert.IsTrue(AnyTest(empty, isEven)); | |
} | |
private static bool AllTest(IEnumerable<int> data, Func<int, bool> predicate) | |
{ | |
return data.All(predicate); | |
} | |
private static bool AnyTest(IEnumerable<int> data, Func<int, bool> predicate) | |
{ | |
return !data.Any(x => !predicate(x)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment