MultipleOf Constraint
MultipleOfConstraint tests that a numeric value is a multiple of a specified number. It also provides convenient
shortcuts for testing even and odd numbers.
Usage
Is.MultipleOf(int factor)
Is.Even
Is.Odd
Examples
[Test]
public void MultipleOfConstraint_Examples()
{
// Test for multiples
Assert.That(12, Is.MultipleOf(3)); // 12 is a multiple of 3
Assert.That(12, Is.MultipleOf(4)); // 12 is a multiple of 4
Assert.That(12, Is.Not.MultipleOf(5)); // 12 is not a multiple of 5
// Even numbers
Assert.That(0, Is.Even);
Assert.That(2, Is.Even);
Assert.That(100, Is.Even);
Assert.That(-4, Is.Even);
Assert.That(3, Is.Not.Even);
// Odd numbers
Assert.That(1, Is.Odd);
Assert.That(3, Is.Odd);
Assert.That(99, Is.Odd);
Assert.That(-7, Is.Odd);
Assert.That(4, Is.Not.Odd);
}
Notes
Is.Evenis equivalent toIs.MultipleOf(2).Is.Oddis the negation - a number that is not a multiple of 2.- Works with all integer types (
int,long,short, etc.). - Zero is considered even (
Is.MultipleOf(n)passes for anynwhen actual is 0).
See Also
- Range Constraint - Test if value is within a range
- GreaterThan Constraint - Numeric comparisons