From eb9295cfcdf89ade8b65f5fc9afc695ce30b7f68 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 1 Sep 2021 19:37:00 +0200 Subject: [PATCH 01/15] abacus: remove meson build system --- meson.build | 13 ------------ src/bignum/meson.build | 16 -------------- src/meson.build | 16 -------------- src/parse/meson.build | 48 ------------------------------------------ tests/meson.build | 1 - tests/unit/meson.build | 26 ----------------------- 6 files changed, 120 deletions(-) delete mode 100644 meson.build delete mode 100644 src/bignum/meson.build delete mode 100644 src/meson.build delete mode 100644 src/parse/meson.build delete mode 100644 tests/meson.build delete mode 100644 tests/unit/meson.build diff --git a/meson.build b/meson.build deleted file mode 100644 index 4910f6a..0000000 --- a/meson.build +++ /dev/null @@ -1,13 +0,0 @@ -project( - 'abacus', - 'cpp', - version: '0.0.0', - license: 'MIT', - default_options: [ - 'warning_level=3', - 'cpp_std=c++17', - ], -) - -subdir('src') -subdir('tests') diff --git a/src/bignum/meson.build b/src/bignum/meson.build deleted file mode 100644 index af1e4e5..0000000 --- a/src/bignum/meson.build +++ /dev/null @@ -1,16 +0,0 @@ -bignum_sources = files( - 'bignum.cc', - 'bignum.hh', -) - -bignum_inc = include_directories('.') - -bignum_lib = static_library( - 'bignum', - sources: bignum_sources, -) - -bignum = declare_dependency( - link_with: bignum_lib, - include_directories: bignum_inc, -) diff --git a/src/meson.build b/src/meson.build deleted file mode 100644 index a6c5cb2..0000000 --- a/src/meson.build +++ /dev/null @@ -1,16 +0,0 @@ -abacus_sources = files( - 'abacus.cc', -) - -subdir('bignum') -subdir('parse') - -abacus = executable( - 'abacus', - sources: abacus_sources, - dependencies: [ - bignum, - parse, - ], - install: true, -) diff --git a/src/parse/meson.build b/src/parse/meson.build deleted file mode 100644 index 1a3bdcb..0000000 --- a/src/parse/meson.build +++ /dev/null @@ -1,48 +0,0 @@ -flex_binary = find_program('flex', required: true) -lexer_sources = custom_target( - 'lexer_sources', - input: 'scanner.ll', - output: 'scanner.cc', - command: [ - flex_binary, - '-o', - '@OUTPUT@', - '@INPUT@', - ], -) - -bison_binary = find_program('bison', required: true) -parser_sources = custom_target( - 'parser_sources', - input: 'parser.yy', - output: [ - 'parser.cc', - 'parser.hh', - ], - command: [ - bison_binary, - '@INPUT@', - '--output=@OUTPUT0@', - '--defines=@OUTPUT1@', - '--graph', - # FIXME: html output in bison 3.7.90 - ], -) - -parse_inc = include_directories('.') - -parse_lib = static_library( - 'parser', - 'parser-driver.cc', - 'parser-driver.hh', - lexer_sources, - parser_sources, - dependencies: [ - bignum, - ], -) - -parse = declare_dependency( - link_with: parse_lib, - include_directories: parse_inc, -) diff --git a/tests/meson.build b/tests/meson.build deleted file mode 100644 index 082b746..0000000 --- a/tests/meson.build +++ /dev/null @@ -1 +0,0 @@ -subdir('unit') diff --git a/tests/unit/meson.build b/tests/unit/meson.build deleted file mode 100644 index db8eddd..0000000 --- a/tests/unit/meson.build +++ /dev/null @@ -1,26 +0,0 @@ -gtest = dependency( - 'gtest', - main: true, - required: false, -) - -if gtest.found() - unit_test_sources = files( - 'bignum.cc', - ) - - unit_tests = executable( - 'unit_tests', - sources: unit_test_sources, - dependencies: [ - bignum, - gtest, - ], - ) - - test( - 'unit tests', - unit_tests, - protocol: 'gtest', - ) -endif From 171ef10a6d8f0626e1e3e289b868f2a1dfd23c9b Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 1 Sep 2021 19:42:58 +0200 Subject: [PATCH 02/15] abacus: fix include directories This cleans up one of my major pain points with the Meson way of doing things. The Meson solution to have nice includes would have been to create an `include/` directory per library I create... --- CMakeLists.txt | 3 +++ src/abacus.cc | 2 +- src/bignum/CMakeLists.txt | 4 ---- src/parse/CMakeLists.txt | 2 +- src/parse/parser-driver.hh | 2 +- src/parse/parser.yy | 2 +- tests/unit/bignum.cc | 2 +- 7 files changed, 8 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4ef9a7b..d343e5a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,9 @@ target_compile_options(common_options INTERFACE -Wall -Wextra ) +target_include_directories(common_options INTERFACE + src +) add_subdirectory(src) add_subdirectory(tests) diff --git a/src/abacus.cc b/src/abacus.cc index 3e735a0..45d2394 100644 --- a/src/abacus.cc +++ b/src/abacus.cc @@ -1,4 +1,4 @@ -#include "parser-driver.hh" // FIXME: I would like `parse/parser-driver.hh` path instead... +#include "parse/parser-driver.hh" int main() { abacus::parse::ParserDriver driver{}; diff --git a/src/bignum/CMakeLists.txt b/src/bignum/CMakeLists.txt index b2434b7..6b64eb6 100644 --- a/src/bignum/CMakeLists.txt +++ b/src/bignum/CMakeLists.txt @@ -3,7 +3,3 @@ add_library(bignum STATIC bignum.hh ) target_link_libraries(bignum PRIVATE common_options) - -target_include_directories(bignum PUBLIC - $ -) diff --git a/src/parse/CMakeLists.txt b/src/parse/CMakeLists.txt index 3c87a92..22a706c 100644 --- a/src/parse/CMakeLists.txt +++ b/src/parse/CMakeLists.txt @@ -18,6 +18,6 @@ target_link_libraries(parse PRIVATE ) target_include_directories(parse PUBLIC - $ $ + $ ) diff --git a/src/parse/parser-driver.hh b/src/parse/parser-driver.hh index d5ba7ea..ad88efd 100644 --- a/src/parse/parser-driver.hh +++ b/src/parse/parser-driver.hh @@ -4,7 +4,7 @@ #include "parser.hh" -#include "bignum.hh" // FIXME: I would like `bignum/bignum.hh` path instead... +#include "bignum/bignum.hh" namespace abacus::parse { diff --git a/src/parse/parser.yy b/src/parse/parser.yy index d8fbc87..07e3e3a 100644 --- a/src/parse/parser.yy +++ b/src/parse/parser.yy @@ -36,7 +36,7 @@ namespace abacus::parse { class ParserDriver; } // namespace abacus::parse -#include "bignum.hh" // FIXME: I would like `bignum/bignum.hh` path instead... +#include "bignum/bignum.hh" } %code provides { diff --git a/tests/unit/bignum.cc b/tests/unit/bignum.cc index f584778..b7306f4 100644 --- a/tests/unit/bignum.cc +++ b/tests/unit/bignum.cc @@ -2,7 +2,7 @@ #include -#include "bignum.hh" +#include "bignum/bignum.hh" using namespace abacus::bignum; From 5c7c2af2896f9895962bd0cff4df99fd05125609 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Fri, 8 Oct 2021 15:47:26 +0200 Subject: [PATCH 03/15] nix: use 'inputsFrom' --- flake.nix | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/flake.nix b/flake.nix index cdf39b2..c5c0dd4 100644 --- a/flake.nix +++ b/flake.nix @@ -59,10 +59,9 @@ defaultPackage = self.packages.${system}.abacus; devShell = pkgs.mkShell { - inherit (self.defaultPackage.${system}) - checkInputs - nativeBuildInputs - ; + inputsFrom = with self.packages.${system}; [ + abacus + ]; inherit (self.checks.${system}.pre-commit) shellHook; }; From da8b08e4e3d7c189c11b1db6abf10926ba8373f7 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 12 Oct 2021 15:02:33 +0200 Subject: [PATCH 04/15] abacus: bignum: use IIFE I think it looks cleaner to use Immediately Invoked Function Expressions in such cases. --- src/bignum/bignum.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/bignum/bignum.cc b/src/bignum/bignum.cc index 2121cdc..6b6987e 100644 --- a/src/bignum/bignum.cc +++ b/src/bignum/bignum.cc @@ -82,12 +82,12 @@ digits_type do_addition(digits_type const& lhs, digits_type const& rhs) { ++it2; } - auto it = it1; - auto end = end1; - if (it1 == end1) { - it = it2; - end = end2; - } + auto [it, end] = [=]() { + if (it1 != end1) { + return std::make_pair(it1, end1); + } + return std::make_pair(it2, end2); + }(); while (it != end) { int addition = *it + carry; From 4ea7a38b614fdef27423a340c85459ce151fdf70 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 12 Oct 2021 15:05:38 +0200 Subject: [PATCH 05/15] abacus: use C++20 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d343e5a..7d72bf3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ enable_testing() add_library(common_options INTERFACE) target_compile_features(common_options INTERFACE - cxx_std_17 + cxx_std_20 ) target_compile_options(common_options INTERFACE -Wall From 8cb26ee31611287cc32d8cf042bac47198e5c5f0 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 12 Oct 2021 15:05:21 +0200 Subject: [PATCH 06/15] abacus: bignum: use span Once again, the code looks cleaner that way. --- src/bignum/bignum.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/bignum/bignum.cc b/src/bignum/bignum.cc index 6b6987e..cadc170 100644 --- a/src/bignum/bignum.cc +++ b/src/bignum/bignum.cc @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -82,18 +83,17 @@ digits_type do_addition(digits_type const& lhs, digits_type const& rhs) { ++it2; } - auto [it, end] = [=]() { + auto leftover = [=]() { if (it1 != end1) { - return std::make_pair(it1, end1); + return std::span(it1, end1); } - return std::make_pair(it2, end2); + return std::span(it2, end2); }(); - while (it != end) { - int addition = *it + carry; + for (auto value : leftover) { + int addition = value + carry; carry = addition / BASE; res.push_back(addition % BASE); - ++it; } if (carry != 0) { From 3e5133da1ae0fa0eac384a81a23e02877113b5e0 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 12 Oct 2021 15:37:18 +0200 Subject: [PATCH 07/15] ci: add package check phase This takes care of launching the test suite. --- .drone.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.drone.yml b/.drone.yml index e52d282..7037529 100644 --- a/.drone.yml +++ b/.drone.yml @@ -8,6 +8,10 @@ steps: commands: - nix flake check +- name: package check + commands: + - nix build + - name: notifiy commands: - nix run github:ambroisie/matrix-notifier From a382b299b0addf3873778aa5430cbd0ffc35b14c Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Thu, 17 Feb 2022 10:03:21 +0100 Subject: [PATCH 08/15] abacus: bignum: fix comparison of negative numbers --- src/bignum/bignum.cc | 6 +++++- tests/unit/bignum.cc | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/bignum/bignum.cc b/src/bignum/bignum.cc index cadc170..c6b2c6c 100644 --- a/src/bignum/bignum.cc +++ b/src/bignum/bignum.cc @@ -363,7 +363,11 @@ bool BigNum::less_than(BigNum const& rhs) const { return sign_ < rhs.sign_; } - return do_less_than(digits_, rhs.digits_); + if (is_positive()) { + return do_less_than(digits_, rhs.digits_); + } else { + return do_less_than(rhs.digits_, digits_); + } } void BigNum::canonicalize() { diff --git a/tests/unit/bignum.cc b/tests/unit/bignum.cc index b7306f4..1126a17 100644 --- a/tests/unit/bignum.cc +++ b/tests/unit/bignum.cc @@ -68,6 +68,34 @@ TEST(BigNum, comparisons_digits) { EXPECT_GT(ten, nine); } +TEST(BigNum, comparisons_negative) { + auto const zero = BigNum(0); + auto const minus_one = BigNum(-1); + auto const minus_two = BigNum(-2); + + EXPECT_LT(minus_one, zero); + EXPECT_LE(minus_one, zero); + EXPECT_LT(minus_two, minus_one); + EXPECT_LE(minus_two, minus_one); + EXPECT_LE(minus_one, minus_one); + EXPECT_GE(minus_two, minus_two); + + EXPECT_GT(zero, minus_one); + EXPECT_GE(zero, minus_one); + EXPECT_GT(minus_one, minus_two); + EXPECT_GE(minus_one, minus_two); + EXPECT_GE(minus_one, minus_one); + EXPECT_GE(minus_two, minus_two); +} + +TEST(BigNum, comparisons_digits_negative) { + auto const minus_nine = BigNum(-9); + auto const minus_ten = BigNum(-10); + + EXPECT_LT(minus_ten, minus_nine); + EXPECT_GT(minus_nine, minus_ten); +} + TEST(BigNum, unary) { auto const zero = BigNum(0); auto const one = BigNum(1); From c9460c09d5e0938d6e09330460d785f35ac7dbb8 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Thu, 17 Feb 2022 10:13:11 +0100 Subject: [PATCH 09/15] abacus: bignum: simplify is_canonicalized --- src/bignum/bignum.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/bignum/bignum.cc b/src/bignum/bignum.cc index c6b2c6c..3fce460 100644 --- a/src/bignum/bignum.cc +++ b/src/bignum/bignum.cc @@ -385,15 +385,15 @@ bool BigNum::is_canonicalized() const { return sign_ == 0; } - auto const leading_zeros = std::find_if(digits_.rbegin(), digits_.rend(), - [](auto v) { return v != 0; }); - if (leading_zeros != digits_.rbegin()) { + // `back` is valid since there is at least one element + auto const has_leading_zero = digits_.back() == 0; + if (has_leading_zero) { return false; } - auto const overflow = std::find_if(digits_.begin(), digits_.end(), - [](auto v) { return v >= BASE; }); - if (overflow != digits_.end()) { + auto const has_overflow = std::any_of(digits_.begin(), digits_.end(), + [](auto v) { return v >= BASE; }); + if (has_overflow) { return false; } From d5507dce2d7399efc8db7818b0728e1357de5d7b Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Thu, 17 Feb 2022 10:27:11 +0100 Subject: [PATCH 10/15] nix: use overlay --- flake.nix | 64 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/flake.nix b/flake.nix index c5c0dd4..e5531d2 100644 --- a/flake.nix +++ b/flake.nix @@ -29,9 +29,40 @@ }; outputs = { self, futils, nixpkgs, pre-commit-hooks }: - futils.lib.eachDefaultSystem (system: + { + overlay = final: prev: { + abacus = final.stdenv.mkDerivation { + pname = "abacus"; + version = "0.0.0"; + + src = self; + + nativeBuildInputs = with final; [ + bison + cmake + flex + ninja + pkg-config + ]; + + checkInputs = with final; [ + gtest + ]; + + doCheck = true; + + meta = with final.lib; { + description = "A simple calculator using big numbers"; + homepage = "https://gitea.belanyi.fr/ambroisie/abacus"; + license = licenses.mit; + maintainers = [ ambroisie ]; + platforms = platforms.unix; + }; + }; + }; + } // futils.lib.eachDefaultSystem (system: let - pkgs = import nixpkgs { inherit system; }; + pkgs = import nixpkgs { inherit system; overlays = [ self.overlay ]; }; in { checks = { @@ -67,34 +98,7 @@ }; packages = futils.lib.flattenTree { - abacus = pkgs.stdenv.mkDerivation { - pname = "abacus"; - version = "0.0.0"; - - src = self; - - nativeBuildInputs = with pkgs; [ - bison - cmake - flex - ninja - pkg-config - ]; - - checkInputs = with pkgs; [ - gtest - ]; - - doCheck = true; - - meta = with pkgs.lib; { - description = "A simple calculator using big numbers"; - homepage = "https://gitea.belanyi.fr/ambroisie/abacus"; - license = licenses.mit; - maintainers = [ ambroisie ]; - platforms = platforms.unix; - }; - }; + inherit (pkgs) abacus; }; }); } From 33de39b62a424b1c2e48e1c78f3ac636ad7be86f Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 1 Apr 2023 16:46:32 +0100 Subject: [PATCH 11/15] ci: add Woodpecker CI workflow --- .woodpecker/check.yml | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 .woodpecker/check.yml diff --git a/.woodpecker/check.yml b/.woodpecker/check.yml new file mode 100644 index 0000000..921c363 --- /dev/null +++ b/.woodpecker/check.yml @@ -0,0 +1,31 @@ +labels: + type: exec + +pipeline: +- name: flake check + image: bash + commands: + - nix flake check + +- name: package check + image: bash + commands: + - nix build + +- name: notify + image: bash + secrets: + - source: matrix_roomid + target: room + - source: matrix_username + target: user + - source: matrix_password + target: pass + - source: matrix_homeserver + target: address + commands: + - nix run github:ambroisie/matrix-notifier + when: + status: + - failure + - success From 8b3288c1ce190951926f015c730f7290dfdf3270 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 1 Apr 2023 17:13:24 +0100 Subject: [PATCH 12/15] ci: remove Drone CI --- .drone.yml | 31 ------------------------------- 1 file changed, 31 deletions(-) delete mode 100644 .drone.yml diff --git a/.drone.yml b/.drone.yml deleted file mode 100644 index 7037529..0000000 --- a/.drone.yml +++ /dev/null @@ -1,31 +0,0 @@ ---- -kind: pipeline -type: exec -name: abacus checks - -steps: -- name: flake check - commands: - - nix flake check - -- name: package check - commands: - - nix build - -- name: notifiy - commands: - - nix run github:ambroisie/matrix-notifier - environment: - ADDRESS: - from_secret: matrix_homeserver - ROOM: - from_secret: matrix_roomid - USER: - from_secret: matrix_username - PASS: - from_secret: matrix_password - when: - status: - - failure - - success -... From 2e29dea2b6c946466ce3b2c9f56593acf7156bb2 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 6 Dec 2023 11:54:21 +0000 Subject: [PATCH 13/15] abacus: bignum: use base to compute complement Instead of hard-coding 9 for base-10 computations. --- src/bignum/bignum.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bignum/bignum.cc b/src/bignum/bignum.cc index 3fce460..f38159f 100644 --- a/src/bignum/bignum.cc +++ b/src/bignum/bignum.cc @@ -107,7 +107,7 @@ digits_type do_substraction(digits_type const& lhs, digits_type const& rhs) { assert(!do_less_than(lhs, rhs)); digits_type complement; - auto const take_complement = [](auto num) { return 9 - num; }; + auto const take_complement = [](auto num) { return BASE - 1 - num; }; std::transform(lhs.begin(), lhs.end(), std::back_inserter(complement), take_complement); From 34df20cdb7e702360d6c3793f19ad83096d1f4ae Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 6 Dec 2023 11:58:17 +0000 Subject: [PATCH 14/15] nix: fix deprecated flake attributes --- flake.nix | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/flake.nix b/flake.nix index e5531d2..a697e6b 100644 --- a/flake.nix +++ b/flake.nix @@ -30,7 +30,7 @@ outputs = { self, futils, nixpkgs, pre-commit-hooks }: { - overlay = final: prev: { + overlays.default = final: prev: { abacus = final.stdenv.mkDerivation { pname = "abacus"; version = "0.0.0"; @@ -62,7 +62,7 @@ }; } // futils.lib.eachDefaultSystem (system: let - pkgs = import nixpkgs { inherit system; overlays = [ self.overlay ]; }; + pkgs = import nixpkgs { inherit system; overlays = [ self.overlays.default ]; }; in { checks = { @@ -87,9 +87,7 @@ }; }; - defaultPackage = self.packages.${system}.abacus; - - devShell = pkgs.mkShell { + devShells.default = pkgs.mkShell { inputsFrom = with self.packages.${system}; [ abacus ]; @@ -99,6 +97,7 @@ packages = futils.lib.flattenTree { inherit (pkgs) abacus; + default = pkgs.abacus; }; }); } From c72032f0ce417db0047582bbab004ba36adc2c60 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 6 Dec 2023 11:59:16 +0000 Subject: [PATCH 15/15] ci: fix deprecated syntax --- .woodpecker/check.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.woodpecker/check.yml b/.woodpecker/check.yml index 921c363..0901624 100644 --- a/.woodpecker/check.yml +++ b/.woodpecker/check.yml @@ -1,7 +1,7 @@ labels: type: exec -pipeline: +steps: - name: flake check image: bash commands: