From 4bea07e7b83aabb264c2cdda91de48dc22440531 Mon Sep 17 00:00:00 2001 From: saundersp Date: Sun, 30 Mar 2025 13:48:18 +0200 Subject: [PATCH] gcd : Added unit test and Makefile target --- gcd/Makefile | 8 ++++++++ gcd/src/gcd.cpp | 13 ++----------- gcd/src/gcd.hpp | 10 ++++++++++ gcd/src/gcd_test.cpp | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 gcd/src/gcd.hpp create mode 100644 gcd/src/gcd_test.cpp diff --git a/gcd/Makefile b/gcd/Makefile index 430fefe..fc481ec 100644 --- a/gcd/Makefile +++ b/gcd/Makefile @@ -11,6 +11,10 @@ bin/gcd: src/gcd.cpp | check-cxx-works bin @echo Compiling $< @${CXX} ${CFLAGS} $^ -o $@ +bin/gcd_test: src/gcd_test.cpp | check-cxx-works bin + @echo Compiling $^ + @${CXX} ${CFLAGS} $^ -o $@ + .PHONY: install install: bin/gcd @cp -v $^ /usr/bin @@ -19,6 +23,10 @@ install: bin/gcd uninstall: /usr/bin/gcd @rm -v $^ +.PHONY: test +test: bin/gcd_test + @./$^ + .PHONY: clean clean: @rm -rfv bin diff --git a/gcd/src/gcd.cpp b/gcd/src/gcd.cpp index ab9e715..64d859e 100644 --- a/gcd/src/gcd.cpp +++ b/gcd/src/gcd.cpp @@ -4,15 +4,7 @@ #include #include #include - -/** - * @brief Calculate the greatest common divisor (GCD) - * - * @param a First integer - * @param b Second integer - * @return greatest common divisor between a and b - */ -constexpr int64_t gcd(const int64_t& a, const int64_t& b) noexcept { return a == 0 ? b : gcd(b % a, a); } +#include "gcd.hpp" /** * @brief Convert a given string to a 64 bit integer @@ -54,8 +46,7 @@ int32_t main(const int32_t argc, const char* const* argv) noexcept { return EXIT_FAILURE; } int64_t a, b; - if(sstrtoll(argv[1], a) == EXIT_FAILURE) return EXIT_FAILURE; - if(sstrtoll(argv[2], b) == EXIT_FAILURE) return EXIT_FAILURE; + if(sstrtoll(argv[1], a) == EXIT_FAILURE || sstrtoll(argv[2], b) == EXIT_FAILURE) return EXIT_FAILURE; const int64_t hcf = gcd(std::abs(a), std::abs(b)); printf("Common factor = %ld\nResult = %ld/%ld\n", hcf, a / hcf, b / hcf); diff --git a/gcd/src/gcd.hpp b/gcd/src/gcd.hpp new file mode 100644 index 0000000..adddf92 --- /dev/null +++ b/gcd/src/gcd.hpp @@ -0,0 +1,10 @@ +#include + +/** + * @brief Calculate the greatest common divisor (GCD) + * + * @param a First integer + * @param b Second integer + * @return greatest common divisor between a and b + */ +constexpr uint64_t gcd(const uint64_t a, const uint64_t b) noexcept { return a == 0 ? b : gcd(b % a, a); } diff --git a/gcd/src/gcd_test.cpp b/gcd/src/gcd_test.cpp new file mode 100644 index 0000000..f6f9ab5 --- /dev/null +++ b/gcd/src/gcd_test.cpp @@ -0,0 +1,37 @@ +#include +#include +#include "gcd.hpp" + +/** + * @brief Test if a given result is equal of the expected one and log result + * + * @param name of the unit test + * @param expected result of the function call + * @param result of the function + */ +static void Assert(const char* const name, const uint64_t expected, const uint64_t result) noexcept { + if(expected != result){ + std::cerr << "For test named " << name << " Expected '" << expected << "' but got '" << result << "' instead\n"; + } +} + +/** + * @brief Test suite for the gcd output + */ +void gcd_test(void) noexcept { + Assert("gcd null LR", 0, gcd(0, 0)); + Assert("gcd null L", 196883, gcd(196883, 0)); + Assert("gcd null R", 196883, gcd(0, 196883)); + Assert("gcd self", 196883, gcd(196883, 196883)); + Assert("gcd Full HD", 120, gcd(1920, 1080)); + Assert("gcd monsterfel", 2773, gcd(196883, 194110)); + Assert("gcd monsterfel 2", 2773, gcd(194110, 196883)); + Assert("gcd monsterful", 4189, gcd(196883, 192694)); + Assert("gcd monsterful 2", 4189, gcd(192694, 196883)); +} + +int32_t main(void){ + setlocale(LC_NUMERIC, ""); // Allow proper number display + gcd_test(); + return EXIT_SUCCESS; +}