Chapter 6 Exercise Set 2: Doctest Exercises

We introduced doctest in Chapter 5 Extention: Introducing doctest. Review that section if you are not yet familiar with it.

Create a file named test_string_functions.cpp with the following scaffolding and then modify it according to the given problem:

#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <string>
#include <doctest.h>
using namespace std;

// Your function goes here

TEST_CASE("...") {
    CHECK(...);
}
  1. Add the following test case for the function reverse_string(string) that takes a string, s, as an argument, and returns a new string which has the charaters from s in reverse order.

    TEST_CASE("reverse_string(s) returns s backwards") {
        CHECK(reverse_string("happy") == "yppah");
        CHECK(reverse_string("GHC!") == "!CHG");
        CHECK(reverse_string("The end.") == ".dne ehT");
    }
    
  2. Add the following test case for the function count_vowels(string) takes a string, s, as an argument, and returns the number of vowels in s.

    TEST_CASE("count_vowels counts lowercase vowels") {
        CHECK(count_vowels("") == 0);
        CHECK(count_vowels("xyz") == 0);
        CHECK(count_vowels("hello") == 2);
        CHECK(count_vowels("aeiou") == 5);
        CHECK(count_vowels("MISSISSIPPI") == 4);
    }
    
  3. Add the following test case for the function is_palindrom(string) that takes a string, s, as an argument, and returns true if it is a palindrome and false if it is not.

    TEST_CASE("is_palindrome detects palindromes") {
        CHECK(is_palindrome("") == true);
        CHECK(is_palindrome("a") == true);
        CHECK(is_palindrome("aba") == true);
        CHECK(is_palindrome("abba") == true);
        CHECK(is_palindrome("abc") == false);
    }
    
  4. Add the following test case for the function count_words(string) that takes a string, s, as an argument, and returns the number of “words” (substrings separated by whitespace) it contains.

    TEST_CASE("count_words counts words") {
        CHECK(count_words("") == 0);
        CHECK(count_words("Word!") == 1);
        CHECK(count_words("Thing1 and Thing2") == 3);
        CHECK(count_words("This is the song that never ends.") == 7);
    }
    
  5. Add the following test case for the function named shout(string) that takes a string, s, containing an imperative mood statement as an argument, and returns it with upper case letters and ending with an exclamation mark (!). Think about the test case and a few additional tests of your own depending on how you want your function to handle input not covered by the current test.

    TEST_CASE("shout turns an exclaimation into a demand") {
        CHECK(shout("Don't touch that.") == "DON'T TOUCH THAT!");
        CHECK(shout("Let's go.") == "LET'S GO!");
        CHECK(shout("Leave it there!") == "LEAVE IT THERE!");
    }
    
  6. Add the following test case for the function mock(string) that takes a string, s, as an argument, and returns the string in “SpongeBob mocking meme” format, with alternating upper and lower case letters beginning with a lower case.

    TEST_CASE("mock turns a string into a SpongeBob meme") {
        CHECK(mock("We are learning C++.") == "wE aRe lEaRnInG c++.");
        CHECK(
            mock("I'm not sure how to do this.") ==
            "i'M nOt SuRe hOw To Do ThIs."
        );
        CHECK(mock("Mississippi") == "mIsSiSsIpPi");
    }
    
  7. Generalize the count_vowels(string) function you wrote in a previous exercise into count_char(string, char) that takes a string s and a character, ch as arguments and returns the number of times ch occurs in s. a string, s, as an argument, and returns the number of vowels in s.

    TEST_CASE("count_char(s, ch) counts number of times ch occurs in s") {
        CHECK(count_char("abcd", 'c') == 1);
        CHECK(count_char("abcd", 'x') == 0);
        CHECK(count_char("Excellent!", 'e') == 3);
        CHECK(count_char("Abracadabra", 'a') == 5);
    }