Compare commits
6 Commits
edaf3f0fe6
...
main
Author | SHA1 | Date | |
---|---|---|---|
|
939b373b22 | ||
|
7f1752ba3f | ||
|
4bea07e7b8 | ||
|
a0c77d9eef | ||
|
737336731a | ||
|
3d64238db6 |
20
Makefile
20
Makefile
@@ -1,33 +1,53 @@
|
||||
.PHONY: all
|
||||
all: gcd/bin/gcd format_bytes/bin/format_bytes format_time/bin/format_time format_time/bin/format_time_ns
|
||||
|
||||
.NOTPARALLEL:
|
||||
format_time/bin/format_time:
|
||||
@cd format_time && exec make -s
|
||||
|
||||
.NOTPARALLEL:
|
||||
format_time/bin/format_time_ns:
|
||||
@cd format_time && exec make -s
|
||||
|
||||
.NOTPARALLEL:
|
||||
format_bytes/bin/format_bytes:
|
||||
@cd format_bytes && exec make -s
|
||||
|
||||
.NOTPARALLEL:
|
||||
gcd/bin/gcd:
|
||||
@cd gcd && exec make -s
|
||||
|
||||
.NOTPARALLEL:
|
||||
.PHONY: install
|
||||
install: gcd/bin/gcd format_bytes/bin/format_bytes format_time/bin/format_time format_time/bin/format_time_ns
|
||||
@cp -v compress convertUTF8 extract $^ /usr/bin
|
||||
@cd /usr/bin && chmod -v u+x compress convertUTF8 extract gcd format_bytes format_time format_time_ns
|
||||
|
||||
.NOTPARALLEL:
|
||||
.PHONY: uninstall
|
||||
uninstall:
|
||||
@cd /usr/bin && rm -v compress convertUTF8 extract gcd format_bytes format_time format_time_ns
|
||||
|
||||
.NOTPARALLEL:
|
||||
.PHONY: test
|
||||
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:
|
||||
@(cd gcd && exec make -s clean)
|
||||
@(cd format_bytes && exec make -s clean)
|
||||
@(cd format_time && exec make -s clean)
|
||||
|
||||
.NOTPARALLEL:
|
||||
.PHONY: mrproper
|
||||
mrproper: clean
|
||||
@(cd gcd && exec make -s mrproper)
|
||||
|
@@ -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)
|
||||
|
@@ -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");
|
||||
|
@@ -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)
|
||||
|
@@ -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");
|
||||
|
@@ -9,8 +9,6 @@ static constexpr const std::array<const char* const, N_TIMES> time_formats = { "
|
||||
static constexpr const std::array<const uint64_t, N_TIMES> time_numbers = { 1, 1000, 1000000, u64(1e9), u64(6e10), u64(36e11), u64(864e11),
|
||||
u64(6048e11), u64(26298e11), u64(315576e11), u64(315576e13) };
|
||||
|
||||
#define STRING_BUFFER_SIZE 64
|
||||
|
||||
/**
|
||||
* @brief Convert a given number of nanoseconds to a human readable format
|
||||
*
|
||||
@@ -18,7 +16,7 @@ static constexpr const std::array<const uint64_t, N_TIMES> time_numbers = { 1, 1
|
||||
* @return Human readable formatted string
|
||||
*/
|
||||
std::string format_time_ns(uint64_t time) noexcept {
|
||||
char s[STRING_BUFFER_SIZE] = {0};
|
||||
char s[STR_BUFFER_SIZE] = {0};
|
||||
size_t j = 0;
|
||||
|
||||
if (time == 0){
|
||||
@@ -56,8 +54,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");
|
||||
|
15
gcd/Makefile
15
gcd/Makefile
@@ -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)
|
||||
|
@@ -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
10
gcd/src/gcd.hpp
Normal 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
37
gcd/src/gcd_test.cpp
Normal 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;
|
||||
}
|
Reference in New Issue
Block a user