Skip to content

Commit

Permalink
workaround for boost prior to 1.80
Browse files Browse the repository at this point in the history
  • Loading branch information
wistaria committed Mar 11, 2023
1 parent 987e6cf commit 3aec06d
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 58 deletions.
3 changes: 1 addition & 2 deletions fibonacci/cxx/06_general/test_fibonacci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
#include "fibonacci.hpp"

TEST_CASE("fibonacci", "test") {
const std::vector<std::tuple<int, int>> cases{
{0, 0}, {1, 1}, {2, 1}, {3, 2}, {10, 55}};
const std::vector<std::tuple<int, int>> cases{{0, 0}, {1, 1}, {2, 1}, {3, 2}, {10, 55}};
for (auto&& nv : cases) {
const auto n = std::get<0>(nv);
const auto v = std::get<1>(nv);
Expand Down
3 changes: 1 addition & 2 deletions fibonacci/cxx/07_negative/test_fibonacci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
#include "fibonacci.hpp"

TEST_CASE("fibonacci", "test") {
const std::vector<std::tuple<int, int>> cases{
{-2, -1}, {-1, 1}, {0, 0}, {1, 1}, {2, 1}, {3, 2}, {10, 55}};
const std::vector<std::tuple<int, int>> cases{{-2, -1}, {-1, 1}, {0, 0}, {1, 1}, {2, 1}, {3, 2}, {10, 55}};
for (auto &&nv : cases) {
const auto n = std::get<0>(nv);
const auto v = std::get<1>(nv);
Expand Down
14 changes: 10 additions & 4 deletions fibonacci/cxx/08_linear/fibonacci.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@ inline int fibonacci(int n) {
int v0 = 0;
int v1 = 1;
if (n < 0) {
for (int i = -1; i >= n; --i)
std::tie(v0, v1) = std::make_tuple(v1 - v0, v0);
for (int i = -1; i >= n; --i) {
auto v0_old = v0;
v0 = v1 - v0_old;
v1 = v0_old;
}
return v0;
}
if (n == 0)
return v0;
if (n == 1)
return v1;
for (int i = 2; i <= n; ++i)
std::tie(v1, v0) = std::make_tuple(v0 + v1, v1);
for (int i = 2; i <= n; ++i) {
auto v1_old = v1;
v1 = v0 + v1_old;
v0 = v1_old;
}
return v1;
}
4 changes: 1 addition & 3 deletions fibonacci/cxx/08_linear/test_fibonacci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
#include "fibonacci.hpp"

TEST_CASE("fibonacci", "test") {
const std::vector<std::tuple<int, int>> cases{
{-2, -1}, {-1, 1}, {0, 0}, {1, 1},
{2, 1}, {3, 2}, {10, 55}, {40, 102334155}};
const std::vector<std::tuple<int, int>> cases{{-2, -1}, {-1, 1}, {0, 0}, {1, 1}, {2, 1}, {3, 2}, {10, 55}, {40, 102334155}};
for (auto &&nv : cases) {
const auto n = std::get<0>(nv);
const auto v = std::get<1>(nv);
Expand Down
15 changes: 10 additions & 5 deletions fibonacci/cxx/09_multiprec/fibonacci.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#include <tuple>
#include <boost/multiprecision/cpp_int.hpp>

namespace mp = boost::multiprecision;
Expand All @@ -9,15 +8,21 @@ inline mp::cpp_int fibonacci(int n) {
mp::cpp_int v0 = 0;
mp::cpp_int v1 = 1;
if (n < 0) {
for (int i = -1; i >= n; --i)
std::tie(v0, v1) = std::make_tuple(v1 - v0, v0);
for (int i = -1; i >= n; --i) {
auto v0_old = v0;
v0 = v1 - v0_old;
v1 = v0_old;
}
return v0;
}
if (n == 0)
return v0;
if (n == 1)
return v1;
for (int i = 2; i <= n; ++i)
std::tie(v1, v0) = std::make_tuple(v0 + v1, v1);
for (int i = 2; i <= n; ++i) {
auto v1_old = v1;
v1 = v0 + v1_old;
v0 = v1_old;
}
return v1;
}
13 changes: 1 addition & 12 deletions fibonacci/cxx/09_multiprec/test_fibonacci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,7 @@
#include "fibonacci.hpp"

TEST_CASE("fibonacci", "test") {
const std::vector<std::tuple<int, mp::cpp_int>> cases{
{-2, -1},
{-1, 1},
{0, 0},
{1, 1},
{2, 1},
{3, 2},
{10, 55},
{40, 102334155},
{100, mp::cpp_int("354224848179261915075")},
{250,
mp::cpp_int("7896325826131730509282738943634332893686268675876375")}};
const std::vector<std::tuple<int, mp::cpp_int>> cases{{-2, -1}, {-1, 1}, {0, 0}, {1, 1}, {2, 1}, {3, 2}, {10, 55}, {40, 102334155}, {100, mp::cpp_int("354224848179261915075")}};
for (auto &&nv : cases) {
const auto n = std::get<0>(nv);
const auto v = std::get<1>(nv);
Expand Down
15 changes: 10 additions & 5 deletions fibonacci/cxx/10_lucas/recursion.hpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
#pragma once

#include <tuple>
#include <boost/multiprecision/cpp_int.hpp>

namespace mp = boost::multiprecision;

inline mp::cpp_int recursion(mp::cpp_int v0, mp::cpp_int v1, int n) {
if (n < 0) {
for (int i = -1; i >= n; --i)
std::tie(v0, v1) = std::make_tuple(v1 - v0, v0);
for (int i = -1; i >= n; --i) {
auto v0_old = v0;
v0 = v1 - v0_old;
v1 = v0_old;
}
return v0;
}
if (n == 0)
return v0;
if (n == 1)
return v1;
for (int i = 2; i <= n; ++i)
std::tie(v1, v0) = std::make_tuple(v0 + v1, v1);
for (int i = 2; i <= n; ++i) {
auto v1_old = v1;
v1 = v0 + v1_old;
v0 = v1_old;
}
return v1;
}
13 changes: 1 addition & 12 deletions fibonacci/cxx/10_lucas/test_fibonacci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,7 @@
#include "fibonacci.hpp"

TEST_CASE("fibonacci", "test") {
const std::vector<std::tuple<int, mp::cpp_int>> cases{
{-2, -1},
{-1, 1},
{0, 0},
{1, 1},
{2, 1},
{3, 2},
{10, 55},
{40, 102334155},
{100, mp::cpp_int("354224848179261915075")},
{250,
mp::cpp_int("7896325826131730509282738943634332893686268675876375")}};
const std::vector<std::tuple<int, mp::cpp_int>> cases{{-2, -1}, {-1, 1}, {0, 0}, {1, 1}, {2, 1}, {3, 2}, {10, 55}, {40, 102334155}, {100, mp::cpp_int("354224848179261915075")}};
for (auto &&nv : cases) {
const auto n = std::get<0>(nv);
const auto v = std::get<1>(nv);
Expand Down
17 changes: 4 additions & 13 deletions fibonacci/cxx/10_lucas/test_lucas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,10 @@
#include "lucas.hpp"

TEST_CASE("lucas", "test") {
const std::vector<std::tuple<int, mp::cpp_int>> cases{
{-2, 3},
{-1, -1},
{0, 2},
{1, 1},
{2, 3},
{3, 4},
{10, 123},
{40, 228826127},
{100, mp::cpp_int("792070839848372253127")},
{250,
mp::cpp_int("17656721319717734662791328845675730903632844218828123")}};
for (auto &&[n, v] : cases) {
const std::vector<std::tuple<int, mp::cpp_int>> cases{{-2, 3}, {-1, -1}, {0, 2}, {1, 1}, {2, 3}, {3, 4}, {10, 123}, {40, 228826127}, {100, mp::cpp_int("792070839848372253127")}};
for (auto &&nv : cases) {
const auto n = std::get<0>(nv);
const auto v = std::get<1>(nv);
REQUIRE(lucas(n) == v);
if (n > 0)
REQUIRE(lucas(n) > 0);
Expand Down
Binary file modified tdd-tutorial.key
Binary file not shown.

0 comments on commit 3aec06d

Please sign in to comment.