Skip to content
Katabench
Try free

Test the Run-Length Encoder

Medium About 20 min Free, no account needed

You're given a correct run-length encoder. Write the tests that pin run boundaries, counts, and the empty case.

The brief

Run-length encoding collapses repeated characters into a character plus a count. The encoder here is already written and correct. Your job is to write the tests that prove it.

Here is the code under test:

C#
public class RunLength
{
    // "aaabbc" -> "a3b2c1". The empty string encodes to "".
    public string Encode(string input)
    {
        if (input.Length == 0) return "";
        var sb = new StringBuilder();
        char current = input[0];
        int count = 1;
        for (int i = 1; i < input.Length; i++)
        {
            if (input[i] == current) count++;
            else { sb.Append(current).Append(count); current = input[i]; count = 1; }
        }
        sb.Append(current).Append(count);
        return sb.ToString();
    }
}

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 RunLength above, every one of your tests must pass.
  2. Against a set of planted-bug "mutants", broken encoders 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 miscount runs, drop the count of 1, forget the final run, never reset between runs, or mishandle the empty string. One long run catches almost none of them. Use several runs, distinct characters, a repeated-in-two-places character, and the empty string. Stuck? Reveal the model suite to see what thorough looks like.

Starter code

C#
// Write tests for the `RunLength` 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.
// Watch the run boundaries, the count, the empty string, and a character that recurs in two runs.
public class Tests
{
    public void A_single_long_run_is_counted()
    {
        Assert.Equal("a4", new RunLength().Encode("aaaa"));
    }

    // TODO: add multiple runs, distinct characters, a separated repeat, and the empty string.
}

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 →

Related guides

What it practices

Equivalence partitioning
Cover each class of input and every branch with one deliberate case.
Mutation testing
Write tests strong enough to catch planted bugs, not just to pass.

Related katas