From da9a9b43aac11437eaf36b1de96c394ab048cfd3 Mon Sep 17 00:00:00 2001 From: Lukasz Czajka Date: Wed, 22 Mar 2023 18:01:39 +0100 Subject: [PATCH] add tests --- .../Core/Transformation/UnrollRecursion.hs | 2 +- test/Core/Transformation.hs | 4 +- test/Core/Transformation/Unrolling.hs | 49 +++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 test/Core/Transformation/Unrolling.hs diff --git a/src/Juvix/Compiler/Core/Transformation/UnrollRecursion.hs b/src/Juvix/Compiler/Core/Transformation/UnrollRecursion.hs index e9c7bd796d..8082f0dde3 100644 --- a/src/Juvix/Compiler/Core/Transformation/UnrollRecursion.hs +++ b/src/Juvix/Compiler/Core/Transformation/UnrollRecursion.hs @@ -43,7 +43,7 @@ unrollRecursion tab = modify (\mp -> foldr (mapSymbol freshSyms) mp syms) where unrollLimit :: Int - unrollLimit = 100 + unrollLimit = 140 mapSymbol :: HashMap (Symbol, Int) Symbol -> Symbol -> HashMap Symbol Symbol -> HashMap Symbol Symbol mapSymbol freshSyms sym = HashMap.insert sym (fromJust $ HashMap.lookup (sym, unrollLimit) freshSyms) diff --git a/test/Core/Transformation.hs b/test/Core/Transformation.hs index 092e236716..a339b3a595 100644 --- a/test/Core/Transformation.hs +++ b/test/Core/Transformation.hs @@ -5,6 +5,7 @@ import Core.Transformation.Identity qualified as Identity import Core.Transformation.Lifting qualified as Lifting import Core.Transformation.Pipeline qualified as Pipeline import Core.Transformation.TopEtaExpand qualified as TopEtaExpand +import Core.Transformation.Unrolling qualified as Unrolling allTests :: TestTree allTests = @@ -13,5 +14,6 @@ allTests = [ Identity.allTests, TopEtaExpand.allTests, Lifting.allTests, - Pipeline.allTests + Pipeline.allTests, + Unrolling.allTests ] diff --git a/test/Core/Transformation/Unrolling.hs b/test/Core/Transformation/Unrolling.hs new file mode 100644 index 0000000000..554394ff75 --- /dev/null +++ b/test/Core/Transformation/Unrolling.hs @@ -0,0 +1,49 @@ +module Core.Transformation.Unrolling (allTests) where + +import Base +import Core.Eval.Positive qualified as Eval +import Core.Transformation.Base +import Juvix.Compiler.Core.Data.IdentDependencyInfo +import Juvix.Compiler.Core.Transformation + +allTests :: TestTree +allTests = + testGroup + "Recursion unrolling" + ( map + liftTest + ( -- filter out tests which require recursion deeper than 140 + Eval.filterOutTests + [ "Recursion", + "Tail recursion", + "Tail recursion: Fibonacci numbers in linear time", + "Tail recursion through higher-order functions", + "Recursive functions: McCarthy's 91 function, subtraction by increments", + "Lists", + "Streams without memoization", + "Ackermann function (higher-order definition)", + "Merge sort", + "Big numbers" + ] + Eval.tests + ) + ) + +pipe :: [TransformationId] +pipe = [UnrollRecursion] + +liftTest :: Eval.PosTest -> TestTree +liftTest _testEval = + fromTest + Test + { _testTransformations = pipe, + _testAssertion = checkNoRecursion, + _testEval + } + +checkNoRecursion :: InfoTable -> Assertion +checkNoRecursion tab + | isCyclic (createIdentDependencyInfo tab) = + assertFailure "recursion detected" + | otherwise = + return ()