gcd : Added unit test and Makefile target

This commit is contained in:
saundersp 2025-03-30 13:48:18 +02:00
parent a0c77d9eef
commit 4bea07e7b8
4 changed files with 57 additions and 11 deletions

View File

@ -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

View File

@ -4,15 +4,7 @@
#include <cstring>
#include <limits.h>
#include <stdint.h>
/**
* @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);

10
gcd/src/gcd.hpp Normal file
View File

@ -0,0 +1,10 @@
#include <stdint.h>
/**
* @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); }

37
gcd/src/gcd_test.cpp Normal file
View File

@ -0,0 +1,37 @@
#include <iostream>
#include <cassert>
#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;
}