From e2f3c264f333152a28434c4d5a4a2dd3534581c9 Mon Sep 17 00:00:00 2001 From: BrandonPacewic <92102436+BrandonPacewic@users.noreply.github.com> Date: Tue, 27 Jan 2026 00:35:19 -0800 Subject: [PATCH 1/2] test: added `y_combinator` test --- tests/cpl/y_combinator/test.cpp | 72 +++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 tests/cpl/y_combinator/test.cpp diff --git a/tests/cpl/y_combinator/test.cpp b/tests/cpl/y_combinator/test.cpp new file mode 100644 index 0000000..0fc35f6 --- /dev/null +++ b/tests/cpl/y_combinator/test.cpp @@ -0,0 +1,72 @@ +// Copyright (c) Brandon Pacewic +// SPDX-License-Identifier: MIT + +#include +#include + +#include "xutility.h" + +int main() { + using namespace cpl; + { + auto factorial = y_combinator([](auto self, int n) -> int { + return n <= 1 ? 1 : n * self(n - 1); + }); + + assert(factorial(0) == 1); + assert(factorial(1) == 1); + assert(factorial(5) == 120); + assert(factorial(10) == 3628800); + } + { + auto gcd = y_combinator([](auto self, int a, int b) -> int { + return b == 0 ? a : self(b, a % b); + }); + + assert(gcd(20, 30) == 10); + assert(gcd(48, 18) == 6); + assert(gcd(0, 5) == 5); + assert(gcd(17, 13) == 1); + } + { + auto fib = y_combinator([](auto self, int n) -> int { + return n <= 1 ? n : self(n - 1) + self(n - 2); + }); + + assert(fib(0) == 0); + assert(fib(1) == 1); + assert(fib(10) == 55); + assert(fib(15) == 610); + } + { + std::vector> adj = {{1, 2}, {3, 4}, {}, {}, {}}; + std::vector visited; + + auto dfs = y_combinator([&](auto self, int node) -> void { + visited.push_back(node); + for (int next : adj[node]) { + self(next); + } + }); + + dfs(0); + assert(visited.size() == 5); + assert(visited[0] == 0); + assert(visited[1] == 1); + assert(visited[2] == 3); + assert(visited[3] == 4); + assert(visited[4] == 2); + } + { + int call_count = 0; + auto counter = y_combinator([&](auto self, int n) -> int { + ++call_count; + return n <= 0 ? 0 : self(n - 1); + }); + + counter(5); + assert(call_count == 6); + } + + return 0; +} From ac8c30fecfbec70edaad9b4b4411b8be9c539d62 Mon Sep 17 00:00:00 2001 From: BrandonPacewic <92102436+BrandonPacewic@users.noreply.github.com> Date: Tue, 27 Jan 2026 00:35:58 -0800 Subject: [PATCH 2/2] fix: formatting --- tests/cpl/y_combinator/test.cpp | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/tests/cpl/y_combinator/test.cpp b/tests/cpl/y_combinator/test.cpp index 0fc35f6..5092e73 100644 --- a/tests/cpl/y_combinator/test.cpp +++ b/tests/cpl/y_combinator/test.cpp @@ -9,9 +9,7 @@ int main() { using namespace cpl; { - auto factorial = y_combinator([](auto self, int n) -> int { - return n <= 1 ? 1 : n * self(n - 1); - }); + auto factorial = y_combinator([](auto self, int n) -> int { return n <= 1 ? 1 : n * self(n - 1); }); assert(factorial(0) == 1); assert(factorial(1) == 1); @@ -19,9 +17,7 @@ int main() { assert(factorial(10) == 3628800); } { - auto gcd = y_combinator([](auto self, int a, int b) -> int { - return b == 0 ? a : self(b, a % b); - }); + auto gcd = y_combinator([](auto self, int a, int b) -> int { return b == 0 ? a : self(b, a % b); }); assert(gcd(20, 30) == 10); assert(gcd(48, 18) == 6); @@ -29,9 +25,7 @@ int main() { assert(gcd(17, 13) == 1); } { - auto fib = y_combinator([](auto self, int n) -> int { - return n <= 1 ? n : self(n - 1) + self(n - 2); - }); + auto fib = y_combinator([](auto self, int n) -> int { return n <= 1 ? n : self(n - 1) + self(n - 2); }); assert(fib(0) == 0); assert(fib(1) == 1); @@ -40,7 +34,7 @@ int main() { } { std::vector> adj = {{1, 2}, {3, 4}, {}, {}, {}}; - std::vector visited; + std::vector visited; auto dfs = y_combinator([&](auto self, int node) -> void { visited.push_back(node); @@ -58,8 +52,8 @@ int main() { assert(visited[4] == 2); } { - int call_count = 0; - auto counter = y_combinator([&](auto self, int n) -> int { + int call_count = 0; + auto counter = y_combinator([&](auto self, int n) -> int { ++call_count; return n <= 0 ? 0 : self(n - 1); });