Skip to content

Instantly share code, notes, and snippets.

@mormegil-cz
Created January 30, 2013 15:26
Show Gist options
  • Save mormegil-cz/4673989 to your computer and use it in GitHub Desktop.
Save mormegil-cz/4673989 to your computer and use it in GitHub Desktop.
unit test of predicate logic :-)
[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