From c0663ac13142c934a809ae971ad91a668b4471f9 Mon Sep 17 00:00:00 2001 From: Gregory Conrad Date: Sun, 10 Dec 2023 12:51:15 -0500 Subject: [PATCH] test: add fibonacci ic test --- packages/rearch/test/basic_test.dart | 30 ++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/packages/rearch/test/basic_test.dart b/packages/rearch/test/basic_test.dart index 8fe6931..67f51c5 100644 --- a/packages/rearch/test/basic_test.dart +++ b/packages/rearch/test/basic_test.dart @@ -370,4 +370,34 @@ void main() { expect(builds[g], equals(2)); expect(builds[h], equals(1)); }); + + test('fibonacci numbers', () { + Capsule Function(int) getFibonacciCapsuleAction(CapsuleHandle use) { + final fibCapsules = use.value(>{}); + return (n) { + return fibCapsules.putIfAbsent(n, () { + return (CapsuleHandle use) { + final getFibCapsule = use(getFibonacciCapsuleAction); + return switch (n) { + _ when n < 0 => throw ArgumentError.value(n), + 0 => BigInt.zero, + 1 => BigInt.one, + _ => use(getFibCapsule(n - 1)) + use(getFibCapsule(n - 2)), + }; + }; + }); + }; + } + + final container = useContainer(); + final getFibCapsule = container.read(getFibonacciCapsuleAction); + expect( + container.read(getFibCapsule(1000)).toString(), + equals( + '4346655768693745643568852767504062580256466051737178040248172908953655' + '5417949051890403879840079255169295922593080322634775209689623239873322' + '471161642996440906533187938298969649928516003704476137795166849228875', + ), + ); + }); }