Compare commits

...

3 Commits

Author SHA1 Message Date
saundersp
7f1752ba3f gcd, format_{bytes,time} : Added cross_test target 2025-03-30 13:49:07 +02:00
saundersp
4bea07e7b8 gcd : Added unit test and Makefile target 2025-03-30 13:48:18 +02:00
saundersp
a0c77d9eef gcd, format_{bytes,time} : Fixed arm compilation errors 2025-03-30 13:47:45 +02:00
10 changed files with 87 additions and 20 deletions

View File

@ -34,6 +34,12 @@ test:
@(cd format_bytes && exec make -s test)
@(cd format_time && exec make -s test)
.NOTPARALLEL:
.PHONY: cross_test
cross_test:
@(cd format_bytes && exec make -s cross_test)
@(cd format_time && exec make -s cross_test)
.NOTPARALLEL:
.PHONY: clean
clean:

View File

@ -1,5 +1,5 @@
CXX := g++
CFLAGS := -std=c++11 -m64 -Wall -Werror -Wextra -O3
CFLAGS := -std=c++11 -Wall -Werror -Wextra -O3
.PHONY: all
all: bin/format_bytes
@ -34,6 +34,11 @@ clean:
.PHONY: mrproper
mrproper: clean
.PHONY: cross_test
cross_test:
make -s mrproper && make -s CXX=x86_64-pc-linux-gnu-g++
make -s mrproper && make -s CXX=aarch64-unknown-linux-gnu-g++
.PHONY: check-cxx-works
check-cxx-works:
@${CXX} --version >/dev/null 2>&1 || (echo 'Please install a C++ compiler.' && exit 1)

View File

@ -49,8 +49,8 @@ int32_t main(const int32_t argc, const char* const* argv) noexcept {
return EXIT_FAILURE;
} else {
size_t i = 0;
char c, BUFFER[STRING_BUFFER_SIZE];
for(; i < STRING_BUFFER_SIZE && (c = fgetc(stdin)) != EOF; ++i)
char BUFFER[STRING_BUFFER_SIZE];
for(int c; i < STRING_BUFFER_SIZE && (c = fgetc(stdin)) != EOF; ++i)
BUFFER[i] = c;
if(i == STRING_BUFFER_SIZE){
fprintf(stderr, "Error while converting to integer : invalid stdin input (too large)\n");

View File

@ -1,5 +1,5 @@
CXX := g++
CFLAGS := -std=c++11 -m64 -Wall -Werror -Wextra -O3
CFLAGS := -std=c++11 -Wall -Werror -Wextra -O3
.PHONY: all
all: bin/format_time bin/format_time_ns
@ -38,6 +38,11 @@ clean:
.PHONY: mrproper
mrproper: clean
.PHONY: cross_test
cross_test:
make -s mrproper && make -s CXX=x86_64-pc-linux-gnu-g++
make -s mrproper && make -s CXX=aarch64-unknown-linux-gnu-g++
.PHONY: check-cxx-works
check-cxx-works:
@${CXX} --version >/dev/null 2>&1 || (echo 'Please install a C++ compiler.' && exit 1)

View File

@ -50,8 +50,8 @@ int32_t main(const int32_t argc, const char* const* argv) noexcept {
return EXIT_FAILURE;
} else {
size_t i = 0;
char c, BUFFER[STRING_BUFFER_SIZE];
for(; i < STRING_BUFFER_SIZE && (c = fgetc(stdin)) != EOF; ++i)
char BUFFER[STRING_BUFFER_SIZE];
for(int c; i < STRING_BUFFER_SIZE && (c = fgetc(stdin)) != EOF; ++i)
BUFFER[i] = c;
if(i == STRING_BUFFER_SIZE){
fprintf(stderr, "Error while converting to integer : invalid stdin input (too large)\n");

View File

@ -56,8 +56,8 @@ int32_t main(const int32_t argc, const char* const* argv) noexcept {
return EXIT_FAILURE;
} else {
size_t i = 0;
char c, BUFFER[STR_BUFFER_SIZE];
for(; i < STR_BUFFER_SIZE && (c = fgetc(stdin)) != EOF; ++i)
char BUFFER[STR_BUFFER_SIZE];
for(int c; i < STR_BUFFER_SIZE && (c = fgetc(stdin)) != EOF; ++i)
BUFFER[i] = c;
if(i == STR_BUFFER_SIZE){
fprintf(stderr, "Error while converting to integer : invalid stdin input (too large)\n");

View File

@ -1,5 +1,5 @@
CXX := g++
CFLAGS := -std=c++11 -m64 -Wall -Werror -Wextra -O3
CFLAGS := -std=c++11 -Wall -Werror -Wextra -O3
.PHONY: all
all: bin/gcd
@ -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
@ -26,6 +34,11 @@ clean:
.PHONY: mrproper
mrproper: clean
.PHONY: cross_test
cross_test:
make -s mrproper && make -s CXX=x86_64-pc-linux-gnu-g++
make -s mrproper && make -s CXX=aarch64-unknown-linux-gnu-g++
.PHONY: check-cxx-works
check-cxx-works:
@${CXX} --version >/dev/null 2>&1 || (echo 'Please install a C++ compiler.' && exit 1)

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;
}