Skip to content
Katabench
Try free

Test Unicode Scalar Counting

Medium About 25 min Free, no account needed

Distinguish UTF-16 code units from Unicode scalars and reject unpaired surrogates at every position.

The brief

ScalarCounter.Count(string text) counts Unicode scalar values in a .NET UTF-16 string. A valid high-surrogate/low-surrogate pair counts as one. Every nonsurrogate code unit counts as one, including NUL and combining marks. This method does not count user-perceived characters or perform normalization.

An unpaired high or low surrogate anywhere throws ArgumentException. A null string throws ArgumentNullException. Empty input returns zero.

Use C# escapes such as "\uD83D\uDE00" for a valid pair and "\uD800x" for an invalid pair. Check pairs at boundaries and beside ordinary characters, as well as malformed sequences.

Write a Tests class. Every public, parameterless method is a test; a test passes if it does not throw. Use Assert.Equal, True, False, and Throws<TException> as needed. Your suite must pass on the correct subject and catch every hidden mutant. Construct fresh state in each test.

Starter code

C#
public class Tests
{
    public void ASCII_text()
    {
        Assert.Equal(3, new ScalarCounter().Count("cat"));
    }
}

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.
Error path testing
Prove exceptions, retries and rejected inputs behave as specified.

Related katas