Fizz Buzz Sample C++ Code

Code:

string FizzBuzz::CallFizzBuzz(int number)
{
    if (number <= 0) return to_string(number);

    string result = "";
    if (number % 3 == 0)
    {
        result = "fizz";
    }
    if (number % 5 == 0)
    {
        result += "buzz";
    }
    if (result == "")
    {
        result = to_string(number);
    }

    return result;
}

string FizzBuzz::CallFizzBuzzExtended(int number)
{
    if (number <= 0) return to_string(number);

    string result = "";
    string number_str = to_string(number);

    if (number % 3 == 0 || number_str.find("3") != string::npos)
    {
        result = "fizz";
    }
    if (number % 5 == 0 || number_str.find("5") != string::npos)
    {
        result += "buzz";
    }
    if (result == "")
    {
        result = number_str;
    }

    return result;
}

Test:

TEST_METHOD(TestFizzBuzz)
{
    // arrange
    int input[8] = { 1, 2, 3, 4, 5, 6, 10, 15 };
    string output[8] = { "1", "2", "fizz", "4", "buzz", "fizz", "buzz", "fizzbuzz" };

    // act
    FizzBuzz test;
    for (int i = 0; i < 8; i++)
    {
        // assert
        Assert::AreEqual(output[i], test.CallFizzBuzz(input[i]));
    }
}