Skip to content
Katabench
Try free

Test the Triangle Classifier

Medium About 20 min Free, no account needed

You're given a correct triangle classifier. Write the tests that cover every shape and the invalid cases.

The brief

This is the classic exercise interviewers use to see whether you can really test. The classifier is already written and correct. Your job is to write the tests that prove it.

Here is the code under test:

C#
public class Triangles
{
    // "Equilateral", "Isosceles", "Scalene", or "Invalid" when the sides
    // cannot form a triangle (a non-positive side, or the triangle inequality failing).
    public string Classify(int a, int b, int c)
    {
        if (a <= 0 || b <= 0 || c <= 0) return "Invalid";
        if (a + b <= c || a + c <= b || b + c <= a) return "Invalid";
        if (a == b && b == c) return "Equilateral";
        if (a == b || b == c || a == c) return "Isosceles";
        return "Scalene";
    }
}

Write a Tests class. Each public, parameterless method is one test: it passes if it runs without throwing and fails when an assertion throws. A tiny assertion library is in scope (no using needed):

C#
Assert.True(condition);          Assert.False(condition);
Assert.Equal(expected, actual);  Assert.NotEqual(a, b);
Assert.Null(x);                  Assert.NotNull(x);
Assert.Throws<SomeException>(() => ...);

How you're graded

Your suite is run twice over:

  1. Against the correct Triangles above, every one of your tests must pass.
  2. Against a set of planted-bug "mutants", broken classifiers you never see. A mutant is caught when at least one of your tests fails against it.

You pass when your suite is green on the correct code and catches at least 5 of the 6 mutants. The mutants drop the validity check, confuse equilateral with isosceles, compare the wrong pair of sides, or mishandle the degenerate case. Cover all four outcomes, both placements of an equal pair, and both reasons a triangle is invalid. Stuck? Reveal the model suite to see what thorough looks like.

Starter code

C#
// Write tests for the `Triangles` class. Each public, parameterless method is one test; it passes
// if it doesn't throw. Make your suite pass on the correct code AND catch the hidden buggy versions.
// Cover all four outcomes: equilateral, isosceles, scalene, and invalid.
public class Tests
{
    public void Three_equal_sides_are_equilateral()
    {
        Assert.Equal("Equilateral", new Triangles().Classify(3, 3, 3));
    }

    // TODO: add tests for isosceles (try each pair of equal sides), scalene, and invalid inputs.
}

How it is graded

You write the tests. A suite passes only when it stays green on correct code and turns enough faulty implementations red.

C# unit testing exercises →

What it practices

Equivalence partitioning
Cover each class of input and every branch with one deliberate case.
Boundary value testing
Pin behavior exactly at, just below and just above every boundary.
Mutation testing
Write tests strong enough to catch planted bugs, not just to pass.

Related katas