Compare commits
No commits in common. "7f1752ba3fac4484e204a092dd663bf82bf4dec6" and "737336731a79df68fd946bf95cf7bd6e80cc8063" have entirely different histories.
7f1752ba3f
...
737336731a
6
Makefile
6
Makefile
@ -34,12 +34,6 @@ 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:
|
||||
|
@ -1,5 +1,5 @@
|
||||
CXX := g++
|
||||
CFLAGS := -std=c++11 -Wall -Werror -Wextra -O3
|
||||
CFLAGS := -std=c++11 -m64 -Wall -Werror -Wextra -O3
|
||||
|
||||
.PHONY: all
|
||||
all: bin/format_bytes
|
||||
@ -34,11 +34,6 @@ 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 BUFFER[STRING_BUFFER_SIZE];
|
||||
for(int c; i < STRING_BUFFER_SIZE && (c = fgetc(stdin)) != EOF; ++i)
|
||||
char c, BUFFER[STRING_BUFFER_SIZE];
|
||||
for(; 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 -Wall -Werror -Wextra -O3
|
||||
CFLAGS := -std=c++11 -m64 -Wall -Werror -Wextra -O3
|
||||
|
||||
.PHONY: all
|
||||
all: bin/format_time bin/format_time_ns
|
||||
@ -38,11 +38,6 @@ 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 BUFFER[STRING_BUFFER_SIZE];
|
||||
for(int c; i < STRING_BUFFER_SIZE && (c = fgetc(stdin)) != EOF; ++i)
|
||||
char c, BUFFER[STRING_BUFFER_SIZE];
|
||||
for(; 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");
|
||||
|
@ -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 BUFFER[STR_BUFFER_SIZE];
|
||||
for(int c; i < STR_BUFFER_SIZE && (c = fgetc(stdin)) != EOF; ++i)
|
||||
char c, BUFFER[STR_BUFFER_SIZE];
|
||||
for(; 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 -Wall -Werror -Wextra -O3
|
||||
CFLAGS := -std=c++11 -m64 -Wall -Werror -Wextra -O3
|
||||
|
||||
.PHONY: all
|
||||
all: bin/gcd
|
||||
@ -11,10 +11,6 @@ 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
|
||||
@ -23,10 +19,6 @@ install: bin/gcd
|
||||
uninstall: /usr/bin/gcd
|
||||
@rm -v $^
|
||||
|
||||
.PHONY: test
|
||||
test: bin/gcd_test
|
||||
@./$^
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
@rm -rfv bin
|
||||
@ -34,11 +26,6 @@ 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,7 +4,15 @@
|
||||
#include <cstring>
|
||||
#include <limits.h>
|
||||
#include <stdint.h>
|
||||
#include "gcd.hpp"
|
||||
|
||||
/**
|
||||
* @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); }
|
||||
|
||||
/**
|
||||
* @brief Convert a given string to a 64 bit integer
|
||||
@ -46,7 +54,8 @@ 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 || sstrtoll(argv[2], b) == EXIT_FAILURE) return EXIT_FAILURE;
|
||||
if(sstrtoll(argv[1], a) == EXIT_FAILURE) return EXIT_FAILURE;
|
||||
if(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);
|
||||
|
@ -1,10 +0,0 @@
|
||||
#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); }
|
@ -1,37 +0,0 @@
|
||||
#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;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user