Skip to content

Commit

Permalink
Add another test for TCO
Browse files Browse the repository at this point in the history
  • Loading branch information
changlinli committed Dec 22, 2023
1 parent 1026445 commit b6cfb1e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
30 changes: 30 additions & 0 deletions elm-test-rs-tests/src/TCOMiscompilation3.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module TCOMiscompilation3 exposing (tcoMiscompilation3Test)

-- From https://github.com/elm/compiler/issues/2017#issue-522563527

import Test exposing (Test, test)
import Expect

windDownCrashes : Int -> (Int -> Bool) -> Bool
windDownCrashes value continuation =
if value > 0 then
windDownCrashes
(value - 1)
(\newValue -> continuation newValue)

else
continuation 0

windDownDoesNotCrash : Int -> (Int -> Bool) -> Bool
windDownDoesNotCrash value continuation =
if value > 0 then
windDownDoesNotCrash
(value - 1)
continuation -- Works!
-- (\newValue -> continuation newValue) -- Crashes; max stack size exceeded

else
continuation 0

tcoMiscompilation3Test = test "Another example where TCO should not cause a stack overflow in CPSed code" <|
\_ -> windDownCrashes 1 (\_ -> True) |> Expect.equal (windDownDoesNotCrash 1 (\_ -> True))
2 changes: 2 additions & 0 deletions elm-test-rs-tests/tests/Tests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import TCOMiscompilation0 exposing (tcoMiscompilation0Test)
-- This causes hanging in vanilla Elm, uncomment once I have a way of failing a test after it runs for too long
-- import TCOMiscompilation1 exposing (tcoMiscompilation1Test)
import TCOMiscompilation2 exposing (tcoMiscompilation2Test)
import TCOMiscompilation3 exposing (tcoMiscompilation3Test)


suite : Test
Expand All @@ -17,4 +18,5 @@ suite = describe "TCO tests"
-- This causes hanging in vanilla Elm, uncomment once I have a way of failing a test after it runs for too long
-- , tcoMiscompilation1Test
, tcoMiscompilation2Test
, tcoMiscompilation3Test
]

0 comments on commit b6cfb1e

Please sign in to comment.