-
Notifications
You must be signed in to change notification settings - Fork 483
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test case that demonstrates lack of short-curicuiting under no GHC op…
…timisations.
- Loading branch information
Showing
5 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
{-# LANGUAGE BlockArguments #-} | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE TemplateHaskell #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
|
||
module ShortCircuit.Spec (tests) where | ||
|
||
import ShortCircuit.WithGHCOptimisations qualified as WithOptimisations | ||
import ShortCircuit.WithoutGHCOptimisations qualified as WithoutOptimisations | ||
|
||
import Control.Lens ((&), (^.)) | ||
import PlutusCore.Default (DefaultFun, DefaultUni) | ||
import PlutusCore.Evaluation.Machine.ExBudgetingDefaults (defaultCekParameters) | ||
import PlutusTx.Code (CompiledCode, getPlc, unsafeApplyCode) | ||
import PlutusTx.Lift (liftCodeDef) | ||
import PlutusTx.TH (compile) | ||
import Test.Tasty (TestTree, testGroup) | ||
import Test.Tasty.HUnit (Assertion, assertEqual, assertFailure, testCase) | ||
import UntypedPlutusCore.Core.Type (Term (Constr), progTerm) | ||
import UntypedPlutusCore.Evaluation.Machine.Cek (counting, noEmitter) | ||
import UntypedPlutusCore.Evaluation.Machine.Cek.Internal (NTerm, runCekDeBruijn) | ||
|
||
tests :: TestTree | ||
tests = testGroup "ShortCircuit" [withGhcOptimisations, withoutGhcOptimisations] | ||
|
||
withGhcOptimisations :: TestTree | ||
withGhcOptimisations = | ||
testGroup | ||
"GHC Optimisations ON" | ||
[ testCase "GHC inlines the (&&) unfolding making it short-circuit" $ | ||
unsafeApplyCode $$(compile [||WithOptimisations.shortCircuitAnd||]) false' | ||
& assertResult termFalse | ||
, testCase "GHC inlines the (||) unfolding making it short-circuit" do | ||
unsafeApplyCode $$(compile [||WithOptimisations.shortCircuitOr||]) true' | ||
& assertResult termTrue | ||
] | ||
|
||
withoutGhcOptimisations :: TestTree | ||
withoutGhcOptimisations = | ||
testGroup | ||
"GHC Optimisations OFF" | ||
[ testCase "(&&) isn't inlined but it short-circuits anyway" do | ||
unsafeApplyCode $$(compile [||WithoutOptimisations.shortCircuitAnd||]) false' | ||
& assertResult termFalse | ||
, testCase "(||) isn't inlined but it short-circuits anyway" do | ||
unsafeApplyCode $$(compile [||WithoutOptimisations.shortCircuitOr||]) true' | ||
& assertResult termTrue | ||
] | ||
|
||
---------------------------------------------------------------------------------------------------- | ||
-- Helpers ----------------------------------------------------------------------------------------- | ||
|
||
assertResult :: NTerm DefaultUni DefaultFun () -> CompiledCode a -> Assertion | ||
assertResult expected code = | ||
case runCekDeBruijn defaultCekParameters counting noEmitter (getPlc code ^. progTerm) of | ||
(Left ex, _counting, _logs) -> assertFailure $ show ex | ||
(Right actual, _counting, _logs) -> assertEqual "Evaluation has succeeded" expected actual | ||
|
||
false' :: CompiledCode Bool | ||
false' = liftCodeDef False | ||
|
||
true' :: CompiledCode Bool | ||
true' = liftCodeDef True | ||
|
||
termFalse :: Term name uni fun () | ||
termFalse = Constr mempty 1 [] | ||
|
||
termTrue :: Term name uni fun () | ||
termTrue = Constr mempty 0 [] |
16 changes: 16 additions & 0 deletions
16
plutus-tx-plugin/test/ShortCircuit/WithGHCOptimisations.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{-# LANGUAGE NoImplicitPrelude #-} | ||
{-# OPTIONS_GHC -O1 #-} | ||
|
||
module ShortCircuit.WithGHCOptimisations | ||
( shortCircuitAnd | ||
, shortCircuitOr | ||
) where | ||
|
||
import PlutusTx.Prelude (error, (&&), (||)) | ||
import Prelude (Bool) | ||
|
||
shortCircuitAnd :: Bool -> Bool | ||
shortCircuitAnd x = x && error () | ||
|
||
shortCircuitOr :: Bool -> Bool | ||
shortCircuitOr x = x || error () |
19 changes: 19 additions & 0 deletions
19
plutus-tx-plugin/test/ShortCircuit/WithoutGHCOptimisations.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{-# LANGUAGE NoImplicitPrelude #-} | ||
{-# OPTIONS_GHC -O0 #-} | ||
{-# OPTIONS_GHC -fmax-simplifier-iterations=0 #-} | ||
{-# OPTIONS_GHC -fno-ignore-interface-pragmas #-} | ||
{-# OPTIONS_GHC -fno-omit-interface-pragmas #-} | ||
|
||
module ShortCircuit.WithoutGHCOptimisations | ||
( shortCircuitAnd | ||
, shortCircuitOr | ||
) where | ||
|
||
import PlutusTx.Prelude (error, (&&), (||)) | ||
import Prelude (Bool) | ||
|
||
shortCircuitAnd :: Bool -> Bool | ||
shortCircuitAnd x = x && error () | ||
|
||
shortCircuitOr :: Bool -> Bool | ||
shortCircuitOr x = x || error () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters