Search Results for

    Show / Hide Table of Contents

    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

    1. Is.Even is equivalent to Is.MultipleOf(2).
    2. Is.Odd is the negation - a number that is not a multiple of 2.
    3. Works with all integer types (int, long, short, etc.).
    4. Zero is considered even (Is.MultipleOf(n) passes for any n when actual is 0).

    See Also

    • Range Constraint - Test if value is within a range
    • GreaterThan Constraint - Numeric comparisons
    • Edit this page
    In this article
    Back to top Generated by DocFX | Copyright (c) 2018- The NUnit Project - Licensed under CC BY-NC-SA 4.0