Skip to content

Commit

Permalink
test: Add unit testing support
Browse files Browse the repository at this point in the history
  • Loading branch information
oboukli committed Jun 22, 2023
1 parent d504982 commit 85069eb
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(CMAKE_TOOLCHAIN_FILE "${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake"
CACHE STRING "Vcpkg toolchain file")

project(
ForFun
LANGUAGES CXX
Expand Down Expand Up @@ -63,3 +66,10 @@ add_executable(
"sonar"
"test/sonar.cpp"
)

find_package(Catch2 3 REQUIRED)
add_executable(
tests
test/fizzbuzz_test.cpp
)
target_link_libraries(tests PRIVATE Catch2::Catch2WithMain)
1 change: 1 addition & 0 deletions include/forfun/fizzbuzz.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

/// Problem source:
/// https://imranontech.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/
/// https://en.wikipedia.org/wiki/Fizz_buzz
///
/// Original problem text:
/// Write a program that prints the numbers from 1 to 100. But for multiples of
Expand Down
89 changes: 89 additions & 0 deletions test/fizzbuzz_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright (c) Omar Boukli-Hacene. All rights reserved.
// Distributed under an MIT-style license that can be
// found in the LICENSE file.

// SPDX-License-Identifier: MIT

#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_string.hpp>

#include "forfun/fizzbuzz.hpp"

TEST_CASE("fizzbuzz") {
SECTION("Numbers other than three and five are concatenated as-is") {
REQUIRE_THAT(
fizzbuzz(1),
Catch::Matchers::Equals("1", Catch::CaseSensitive::Yes));

REQUIRE_THAT(
fizzbuzz(2),
Catch::Matchers::Equals("12", Catch::CaseSensitive::Yes));
}

SECTION("Multiples of three are concatenated as Fizz") {
REQUIRE_THAT(
fizzbuzz(3),
Catch::Matchers::Equals("12Fizz", Catch::CaseSensitive::Yes));

REQUIRE_THAT(
fizzbuzz(6),
Catch::Matchers::Equals(
"12Fizz4BuzzFizz", Catch::CaseSensitive::Yes));

REQUIRE_THAT(
fizzbuzz(9),
Catch::Matchers::Equals(
"12Fizz4BuzzFizz78Fizz", Catch::CaseSensitive::Yes));
}

SECTION("Multiples of five are concatenated as Buzz") {
REQUIRE_THAT(
fizzbuzz(5),
Catch::Matchers::Equals("12Fizz4Buzz", Catch::CaseSensitive::Yes));

REQUIRE_THAT(
fizzbuzz(10),
Catch::Matchers::Equals(
"12Fizz4BuzzFizz78FizzBuzz", Catch::CaseSensitive::Yes));

REQUIRE_THAT(
fizzbuzz(20),
Catch::Matchers::Equals(
"12Fizz4BuzzFizz78FizzBuzz11Fizz1314FizzBuzz1617Fizz19Buzz",
Catch::CaseSensitive::Yes));
}

SECTION("Multiples of both three and five are concatenated as FizzBuzz") {
REQUIRE_THAT(
fizzbuzz(15),
Catch::Matchers::Equals(
"12Fizz4BuzzFizz78FizzBuzz11Fizz1314FizzBuzz",
Catch::CaseSensitive::Yes));

REQUIRE_THAT(
fizzbuzz(16),
Catch::Matchers::Equals(
"12Fizz4BuzzFizz78FizzBuzz11Fizz1314FizzBuzz16",
Catch::CaseSensitive::Yes));

REQUIRE_THAT(
fizzbuzz(30),
Catch::Matchers::Equals(
"12Fizz4BuzzFizz78FizzBuzz11Fizz1314FizzBuzz1617Fizz19BuzzFizz2"
"223FizzBuzz26Fizz2829FizzBuzz",
Catch::CaseSensitive::Yes));
}

SECTION("Prime number input is computed correctly") {
REQUIRE_THAT(
fizzbuzz(11),
Catch::Matchers::Equals(
"12Fizz4BuzzFizz78FizzBuzz11", Catch::CaseSensitive::Yes));

REQUIRE_THAT(
fizzbuzz(17),
Catch::Matchers::Equals(
"12Fizz4BuzzFizz78FizzBuzz11Fizz1314FizzBuzz1617",
Catch::CaseSensitive::Yes));
}
}
5 changes: 5 additions & 0 deletions vcpkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": [
"catch2"
]
}

0 comments on commit 85069eb

Please sign in to comment.