Search⌘ K
AI Features

Solution: Make the Unit Test Pass

Explore how to create correct function implementations that pass unit tests in D. Understand how different coding approaches can satisfy test requirements and how to modify code safely with confidence in test reliability.

We'll cover the following...

Solution

For demonstration purposes, let’s write an obviously incorrect implementation that passes the first test by accident. The following function simply returns a copy of the input:

D
dstring toFront(dstring str, dchar letter) {
dstring result;
foreach (c; str) {
result ~= c;
}
return result;
}
unittest {
immutable str = "hello"d;
assert(toFront(str, 'h') == "hello");
assert(toFront(str, 'o') == "ohell");
assert(toFront(str, 'l') == "llheo");
}
void main() {
}

Here is a correct ...