From c5c1c39cf712fc3cd758a078467277bb2785cdf5 Mon Sep 17 00:00:00 2001 From: Ziyang Liu Date: Wed, 8 Nov 2023 19:21:35 +0400 Subject: [PATCH] Add budget tests for asData (#5608) --- plutus-tx-plugin/plutus-tx-plugin.cabal | 3 + .../9.2/onlyUseFirstField-budget.eval.golden | 2 + .../Budget/9.2/onlyUseFirstField.eval.golden | 1 + .../Budget/9.2/onlyUseFirstField.pir.golden | 24 ++++++ .../9.2/patternMatching-budget.eval.golden | 2 + .../Budget/9.2/patternMatching.eval.golden | 1 + .../Budget/9.2/patternMatching.pir.golden | 77 +++++++++++++++++ .../9.2/recordFields-budget.eval.golden | 2 + .../Budget/9.2/recordFields.eval.golden | 1 + .../AsData/Budget/9.2/recordFields.pir.golden | 64 ++++++++++++++ .../9.6/onlyUseFirstField-budget.eval.golden | 2 + .../Budget/9.6/onlyUseFirstField.eval.golden | 1 + .../Budget/9.6/onlyUseFirstField.pir.golden | 24 ++++++ .../9.6/patternMatching-budget.eval.golden | 2 + .../Budget/9.6/patternMatching.eval.golden | 1 + .../Budget/9.6/patternMatching.pir.golden | 53 ++++++++++++ .../9.6/recordFields-budget.eval.golden | 2 + .../Budget/9.6/recordFields.eval.golden | 1 + .../AsData/Budget/9.6/recordFields.pir.golden | 64 ++++++++++++++ plutus-tx-plugin/test/AsData/Budget/Spec.hs | 84 +++++++++++++++++++ plutus-tx-plugin/test/AsData/Budget/Types.hs | 27 ++++++ plutus-tx-plugin/test/Spec.hs | 2 + 22 files changed, 440 insertions(+) create mode 100644 plutus-tx-plugin/test/AsData/Budget/9.2/onlyUseFirstField-budget.eval.golden create mode 100644 plutus-tx-plugin/test/AsData/Budget/9.2/onlyUseFirstField.eval.golden create mode 100644 plutus-tx-plugin/test/AsData/Budget/9.2/onlyUseFirstField.pir.golden create mode 100644 plutus-tx-plugin/test/AsData/Budget/9.2/patternMatching-budget.eval.golden create mode 100644 plutus-tx-plugin/test/AsData/Budget/9.2/patternMatching.eval.golden create mode 100644 plutus-tx-plugin/test/AsData/Budget/9.2/patternMatching.pir.golden create mode 100644 plutus-tx-plugin/test/AsData/Budget/9.2/recordFields-budget.eval.golden create mode 100644 plutus-tx-plugin/test/AsData/Budget/9.2/recordFields.eval.golden create mode 100644 plutus-tx-plugin/test/AsData/Budget/9.2/recordFields.pir.golden create mode 100644 plutus-tx-plugin/test/AsData/Budget/9.6/onlyUseFirstField-budget.eval.golden create mode 100644 plutus-tx-plugin/test/AsData/Budget/9.6/onlyUseFirstField.eval.golden create mode 100644 plutus-tx-plugin/test/AsData/Budget/9.6/onlyUseFirstField.pir.golden create mode 100644 plutus-tx-plugin/test/AsData/Budget/9.6/patternMatching-budget.eval.golden create mode 100644 plutus-tx-plugin/test/AsData/Budget/9.6/patternMatching.eval.golden create mode 100644 plutus-tx-plugin/test/AsData/Budget/9.6/patternMatching.pir.golden create mode 100644 plutus-tx-plugin/test/AsData/Budget/9.6/recordFields-budget.eval.golden create mode 100644 plutus-tx-plugin/test/AsData/Budget/9.6/recordFields.eval.golden create mode 100644 plutus-tx-plugin/test/AsData/Budget/9.6/recordFields.pir.golden create mode 100644 plutus-tx-plugin/test/AsData/Budget/Spec.hs create mode 100644 plutus-tx-plugin/test/AsData/Budget/Types.hs diff --git a/plutus-tx-plugin/plutus-tx-plugin.cabal b/plutus-tx-plugin/plutus-tx-plugin.cabal index edf7cc77545..fcaff1d57bd 100644 --- a/plutus-tx-plugin/plutus-tx-plugin.cabal +++ b/plutus-tx-plugin/plutus-tx-plugin.cabal @@ -125,6 +125,8 @@ test-suite plutus-tx-tests hs-source-dirs: test main-is: Spec.hs other-modules: + AsData.Budget.Spec + AsData.Budget.Types Budget.Spec IntegerLiterals.NoStrict.NegativeLiterals.Spec IntegerLiterals.NoStrict.NoNegativeLiterals.Spec @@ -160,6 +162,7 @@ test-suite plutus-tx-tests , base >=4.9 && <5 , containers , deepseq + , filepath , flat ^>=0.6 , hedgehog , lens diff --git a/plutus-tx-plugin/test/AsData/Budget/9.2/onlyUseFirstField-budget.eval.golden b/plutus-tx-plugin/test/AsData/Budget/9.2/onlyUseFirstField-budget.eval.golden new file mode 100644 index 00000000000..cf1cdab0f28 --- /dev/null +++ b/plutus-tx-plugin/test/AsData/Budget/9.2/onlyUseFirstField-budget.eval.golden @@ -0,0 +1,2 @@ +({cpu: 2729622 +| mem: 8250}) \ No newline at end of file diff --git a/plutus-tx-plugin/test/AsData/Budget/9.2/onlyUseFirstField.eval.golden b/plutus-tx-plugin/test/AsData/Budget/9.2/onlyUseFirstField.eval.golden new file mode 100644 index 00000000000..3df7275df82 --- /dev/null +++ b/plutus-tx-plugin/test/AsData/Budget/9.2/onlyUseFirstField.eval.golden @@ -0,0 +1 @@ +(con integer 10) \ No newline at end of file diff --git a/plutus-tx-plugin/test/AsData/Budget/9.2/onlyUseFirstField.pir.golden b/plutus-tx-plugin/test/AsData/Budget/9.2/onlyUseFirstField.pir.golden new file mode 100644 index 00000000000..283db50a2b6 --- /dev/null +++ b/plutus-tx-plugin/test/AsData/Budget/9.2/onlyUseFirstField.pir.golden @@ -0,0 +1,24 @@ +let + data Bool | Bool_match where + True : Bool + False : Bool +in +\(d : data) -> + let + !tup : pair integer (list data) = unConstrData d + in + ifThenElse + {all dead. integer} + (equalsInteger 0 (fstPair {integer} {list data} tup)) + (/\dead -> + let + !l : list data = sndPair {integer} {list data} tup + !l : list data = tailList {data} l + !l : list data = tailList {data} l + !ds : integer = unIData (headList {data} l) + !ds : integer = unIData (headList {data} l) + !ds : integer = unIData (headList {data} (tailList {data} l)) + in + unIData (headList {data} l)) + (/\dead -> error {integer}) + {all dead. dead} \ No newline at end of file diff --git a/plutus-tx-plugin/test/AsData/Budget/9.2/patternMatching-budget.eval.golden b/plutus-tx-plugin/test/AsData/Budget/9.2/patternMatching-budget.eval.golden new file mode 100644 index 00000000000..6b39feccf87 --- /dev/null +++ b/plutus-tx-plugin/test/AsData/Budget/9.2/patternMatching-budget.eval.golden @@ -0,0 +1,2 @@ +({cpu: 4752053 +| mem: 14356}) \ No newline at end of file diff --git a/plutus-tx-plugin/test/AsData/Budget/9.2/patternMatching.eval.golden b/plutus-tx-plugin/test/AsData/Budget/9.2/patternMatching.eval.golden new file mode 100644 index 00000000000..b58276100aa --- /dev/null +++ b/plutus-tx-plugin/test/AsData/Budget/9.2/patternMatching.eval.golden @@ -0,0 +1 @@ +(con integer 100) \ No newline at end of file diff --git a/plutus-tx-plugin/test/AsData/Budget/9.2/patternMatching.pir.golden b/plutus-tx-plugin/test/AsData/Budget/9.2/patternMatching.pir.golden new file mode 100644 index 00000000000..a6a56e810a3 --- /dev/null +++ b/plutus-tx-plugin/test/AsData/Budget/9.2/patternMatching.pir.golden @@ -0,0 +1,77 @@ +let + data (Tuple4 :: * -> * -> * -> * -> *) a b c d | Tuple4_match where + Tuple4 : a -> b -> c -> d -> Tuple4 a b c d + data Bool | Bool_match where + True : Bool + False : Bool + !addInteger : integer -> integer -> integer + = \(x : integer) (y : integer) -> addInteger x y +in +\(d : data) -> + let + !ds : Tuple4 integer integer integer integer + = (let + r = Tuple4 integer integer integer integer + in + \(scrut : data) + (cont : integer -> integer -> integer -> integer -> r) + (fail : unit -> r) -> + let + !tup : pair integer (list data) = unConstrData scrut + in + ifThenElse + {all dead. r} + (equalsInteger 0 (fstPair {integer} {list data} tup)) + (/\dead -> + let + !l : list data = sndPair {integer} {list data} tup + !l : list data = tailList {data} l + !l : list data = tailList {data} l + in + cont + (unIData (headList {data} l)) + (unIData (headList {data} l)) + (unIData (headList {data} l)) + (unIData (headList {data} (tailList {data} l)))) + (/\dead -> fail ()) + {all dead. dead}) + d + (\(x : integer) (y : integer) (z : integer) (w : integer) -> + Tuple4 {integer} {integer} {integer} {integer} x y z w) + (\(void : unit) -> error {Tuple4 integer integer integer integer}) + in + addInteger + (addInteger + (addInteger + (Tuple4_match + {integer} + {integer} + {integer} + {integer} + ds + {integer} + (\(x : integer) (y : integer) (z : integer) (w : integer) -> x)) + (Tuple4_match + {integer} + {integer} + {integer} + {integer} + ds + {integer} + (\(x : integer) (y : integer) (z : integer) (w : integer) -> y))) + (Tuple4_match + {integer} + {integer} + {integer} + {integer} + ds + {integer} + (\(x : integer) (y : integer) (z : integer) (w : integer) -> z))) + (Tuple4_match + {integer} + {integer} + {integer} + {integer} + ds + {integer} + (\(x : integer) (y : integer) (z : integer) (w : integer) -> w)) \ No newline at end of file diff --git a/plutus-tx-plugin/test/AsData/Budget/9.2/recordFields-budget.eval.golden b/plutus-tx-plugin/test/AsData/Budget/9.2/recordFields-budget.eval.golden new file mode 100644 index 00000000000..ac0b6d31727 --- /dev/null +++ b/plutus-tx-plugin/test/AsData/Budget/9.2/recordFields-budget.eval.golden @@ -0,0 +1,2 @@ +({cpu: 12986619 +| mem: 39006}) \ No newline at end of file diff --git a/plutus-tx-plugin/test/AsData/Budget/9.2/recordFields.eval.golden b/plutus-tx-plugin/test/AsData/Budget/9.2/recordFields.eval.golden new file mode 100644 index 00000000000..b58276100aa --- /dev/null +++ b/plutus-tx-plugin/test/AsData/Budget/9.2/recordFields.eval.golden @@ -0,0 +1 @@ +(con integer 100) \ No newline at end of file diff --git a/plutus-tx-plugin/test/AsData/Budget/9.2/recordFields.pir.golden b/plutus-tx-plugin/test/AsData/Budget/9.2/recordFields.pir.golden new file mode 100644 index 00000000000..0de53c98b3f --- /dev/null +++ b/plutus-tx-plugin/test/AsData/Budget/9.2/recordFields.pir.golden @@ -0,0 +1,64 @@ +let + !addInteger : integer -> integer -> integer + = \(x : integer) (y : integer) -> addInteger x y + !`$mInts` : + all r. + data -> + (integer -> integer -> integer -> integer -> r) -> + (unit -> r) -> + r + = /\r -> + \(scrut : data) + (cont : integer -> integer -> integer -> integer -> r) + (fail : unit -> r) -> + let + !tup : pair integer (list data) = unConstrData scrut + in + ifThenElse + {all dead. r} + (equalsInteger 0 (fstPair {integer} {list data} tup)) + (/\dead -> + let + !l : list data = sndPair {integer} {list data} tup + !l : list data = tailList {data} l + !l : list data = tailList {data} l + in + cont + (unIData (headList {data} l)) + (unIData (headList {data} l)) + (unIData (headList {data} l)) + (unIData (headList {data} (tailList {data} l)))) + (/\dead -> fail ()) + {all dead. dead} + data Bool | Bool_match where + True : Bool + False : Bool +in +\(d : data) -> + let + !x : integer + = `$mInts` + {integer} + d + (\(ds : integer) (ds : integer) (ds : integer) (ds : integer) -> ds) + (\(void : unit) -> error {integer}) + !y : integer + = `$mInts` + {integer} + d + (\(ds : integer) (ds : integer) (ds : integer) (ds : integer) -> ds) + (\(void : unit) -> error {integer}) + !z : integer + = `$mInts` + {integer} + d + (\(ds : integer) (ds : integer) (ds : integer) (ds : integer) -> ds) + (\(void : unit) -> error {integer}) + !w : integer + = `$mInts` + {integer} + d + (\(ds : integer) (ds : integer) (ds : integer) (ds : integer) -> ds) + (\(void : unit) -> error {integer}) + in + addInteger (addInteger (addInteger x y) z) w \ No newline at end of file diff --git a/plutus-tx-plugin/test/AsData/Budget/9.6/onlyUseFirstField-budget.eval.golden b/plutus-tx-plugin/test/AsData/Budget/9.6/onlyUseFirstField-budget.eval.golden new file mode 100644 index 00000000000..cf1cdab0f28 --- /dev/null +++ b/plutus-tx-plugin/test/AsData/Budget/9.6/onlyUseFirstField-budget.eval.golden @@ -0,0 +1,2 @@ +({cpu: 2729622 +| mem: 8250}) \ No newline at end of file diff --git a/plutus-tx-plugin/test/AsData/Budget/9.6/onlyUseFirstField.eval.golden b/plutus-tx-plugin/test/AsData/Budget/9.6/onlyUseFirstField.eval.golden new file mode 100644 index 00000000000..3df7275df82 --- /dev/null +++ b/plutus-tx-plugin/test/AsData/Budget/9.6/onlyUseFirstField.eval.golden @@ -0,0 +1 @@ +(con integer 10) \ No newline at end of file diff --git a/plutus-tx-plugin/test/AsData/Budget/9.6/onlyUseFirstField.pir.golden b/plutus-tx-plugin/test/AsData/Budget/9.6/onlyUseFirstField.pir.golden new file mode 100644 index 00000000000..283db50a2b6 --- /dev/null +++ b/plutus-tx-plugin/test/AsData/Budget/9.6/onlyUseFirstField.pir.golden @@ -0,0 +1,24 @@ +let + data Bool | Bool_match where + True : Bool + False : Bool +in +\(d : data) -> + let + !tup : pair integer (list data) = unConstrData d + in + ifThenElse + {all dead. integer} + (equalsInteger 0 (fstPair {integer} {list data} tup)) + (/\dead -> + let + !l : list data = sndPair {integer} {list data} tup + !l : list data = tailList {data} l + !l : list data = tailList {data} l + !ds : integer = unIData (headList {data} l) + !ds : integer = unIData (headList {data} l) + !ds : integer = unIData (headList {data} (tailList {data} l)) + in + unIData (headList {data} l)) + (/\dead -> error {integer}) + {all dead. dead} \ No newline at end of file diff --git a/plutus-tx-plugin/test/AsData/Budget/9.6/patternMatching-budget.eval.golden b/plutus-tx-plugin/test/AsData/Budget/9.6/patternMatching-budget.eval.golden new file mode 100644 index 00000000000..f0b01461226 --- /dev/null +++ b/plutus-tx-plugin/test/AsData/Budget/9.6/patternMatching-budget.eval.golden @@ -0,0 +1,2 @@ +({cpu: 3786053 +| mem: 10156}) \ No newline at end of file diff --git a/plutus-tx-plugin/test/AsData/Budget/9.6/patternMatching.eval.golden b/plutus-tx-plugin/test/AsData/Budget/9.6/patternMatching.eval.golden new file mode 100644 index 00000000000..b58276100aa --- /dev/null +++ b/plutus-tx-plugin/test/AsData/Budget/9.6/patternMatching.eval.golden @@ -0,0 +1 @@ +(con integer 100) \ No newline at end of file diff --git a/plutus-tx-plugin/test/AsData/Budget/9.6/patternMatching.pir.golden b/plutus-tx-plugin/test/AsData/Budget/9.6/patternMatching.pir.golden new file mode 100644 index 00000000000..c9436d2bfa1 --- /dev/null +++ b/plutus-tx-plugin/test/AsData/Budget/9.6/patternMatching.pir.golden @@ -0,0 +1,53 @@ +let + data Unit | Unit_match where + Unit : Unit + data (Tuple4 :: * -> * -> * -> * -> *) a b c d | Tuple4_match where + Tuple4 : a -> b -> c -> d -> Tuple4 a b c d + data Bool | Bool_match where + True : Bool + False : Bool + !addInteger : integer -> integer -> integer + = \(x : integer) (y : integer) -> addInteger x y +in +\(d : data) -> + Tuple4_match + {integer} + {integer} + {integer} + {integer} + ((let + r = Tuple4 integer integer integer integer + in + \(scrut : data) + (cont : integer -> integer -> integer -> integer -> r) + (fail : unit -> r) -> + let + !tup : pair integer (list data) = unConstrData scrut + in + ifThenElse + {all dead. r} + (equalsInteger 0 (fstPair {integer} {list data} tup)) + (/\dead -> + let + !l : list data = sndPair {integer} {list data} tup + !l : list data = tailList {data} l + !l : list data = tailList {data} l + in + cont + (unIData (headList {data} l)) + (unIData (headList {data} l)) + (unIData (headList {data} l)) + (unIData (headList {data} (tailList {data} l)))) + (/\dead -> fail ()) + {all dead. dead}) + d + (\(x : integer) (y : integer) (z : integer) (w : integer) -> + Tuple4 {integer} {integer} {integer} {integer} x y z w) + (\(void : unit) -> + Unit_match + (error {Unit}) + {Tuple4 integer integer integer integer} + (error {Tuple4 integer integer integer integer}))) + {integer} + (\(ipv : integer) (ipv : integer) (ipv : integer) (ipv : integer) -> + addInteger (addInteger (addInteger ipv ipv) ipv) ipv) \ No newline at end of file diff --git a/plutus-tx-plugin/test/AsData/Budget/9.6/recordFields-budget.eval.golden b/plutus-tx-plugin/test/AsData/Budget/9.6/recordFields-budget.eval.golden new file mode 100644 index 00000000000..ac0b6d31727 --- /dev/null +++ b/plutus-tx-plugin/test/AsData/Budget/9.6/recordFields-budget.eval.golden @@ -0,0 +1,2 @@ +({cpu: 12986619 +| mem: 39006}) \ No newline at end of file diff --git a/plutus-tx-plugin/test/AsData/Budget/9.6/recordFields.eval.golden b/plutus-tx-plugin/test/AsData/Budget/9.6/recordFields.eval.golden new file mode 100644 index 00000000000..b58276100aa --- /dev/null +++ b/plutus-tx-plugin/test/AsData/Budget/9.6/recordFields.eval.golden @@ -0,0 +1 @@ +(con integer 100) \ No newline at end of file diff --git a/plutus-tx-plugin/test/AsData/Budget/9.6/recordFields.pir.golden b/plutus-tx-plugin/test/AsData/Budget/9.6/recordFields.pir.golden new file mode 100644 index 00000000000..0de53c98b3f --- /dev/null +++ b/plutus-tx-plugin/test/AsData/Budget/9.6/recordFields.pir.golden @@ -0,0 +1,64 @@ +let + !addInteger : integer -> integer -> integer + = \(x : integer) (y : integer) -> addInteger x y + !`$mInts` : + all r. + data -> + (integer -> integer -> integer -> integer -> r) -> + (unit -> r) -> + r + = /\r -> + \(scrut : data) + (cont : integer -> integer -> integer -> integer -> r) + (fail : unit -> r) -> + let + !tup : pair integer (list data) = unConstrData scrut + in + ifThenElse + {all dead. r} + (equalsInteger 0 (fstPair {integer} {list data} tup)) + (/\dead -> + let + !l : list data = sndPair {integer} {list data} tup + !l : list data = tailList {data} l + !l : list data = tailList {data} l + in + cont + (unIData (headList {data} l)) + (unIData (headList {data} l)) + (unIData (headList {data} l)) + (unIData (headList {data} (tailList {data} l)))) + (/\dead -> fail ()) + {all dead. dead} + data Bool | Bool_match where + True : Bool + False : Bool +in +\(d : data) -> + let + !x : integer + = `$mInts` + {integer} + d + (\(ds : integer) (ds : integer) (ds : integer) (ds : integer) -> ds) + (\(void : unit) -> error {integer}) + !y : integer + = `$mInts` + {integer} + d + (\(ds : integer) (ds : integer) (ds : integer) (ds : integer) -> ds) + (\(void : unit) -> error {integer}) + !z : integer + = `$mInts` + {integer} + d + (\(ds : integer) (ds : integer) (ds : integer) (ds : integer) -> ds) + (\(void : unit) -> error {integer}) + !w : integer + = `$mInts` + {integer} + d + (\(ds : integer) (ds : integer) (ds : integer) (ds : integer) -> ds) + (\(void : unit) -> error {integer}) + in + addInteger (addInteger (addInteger x y) z) w \ No newline at end of file diff --git a/plutus-tx-plugin/test/AsData/Budget/Spec.hs b/plutus-tx-plugin/test/AsData/Budget/Spec.hs new file mode 100644 index 00000000000..0d62fccbc0a --- /dev/null +++ b/plutus-tx-plugin/test/AsData/Budget/Spec.hs @@ -0,0 +1,84 @@ +{-# LANGUAGE BangPatterns #-} +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE MultiParamTypeClasses #-} +{-# LANGUAGE NegativeLiterals #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE PatternSynonyms #-} +{-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TemplateHaskell #-} +{-# LANGUAGE TypeApplications #-} +{-# LANGUAGE ViewPatterns #-} +{-# OPTIONS_GHC -fplugin PlutusTx.Plugin #-} +{-# OPTIONS_GHC -fplugin-opt PlutusTx.Plugin:context-level=0 #-} +{-# OPTIONS_GHC -fplugin-opt PlutusTx.Plugin:defer-errors #-} + +module AsData.Budget.Spec where + +import System.FilePath + +import Test.Tasty.Extras + +import PlutusTx.Builtins qualified as PlutusTx +import PlutusTx.Code +import PlutusTx.IsData qualified as PlutusTx +import PlutusTx.Lift (liftCodeDef) +import PlutusTx.Test (goldenBudget, goldenEvalCekCatch, goldenPirReadable) +import PlutusTx.TH (compile) + +import AsData.Budget.Types + +tests :: TestNested +tests = + testNestedGhc + ("AsData" "Budget") + [ goldenPirReadable "onlyUseFirstField" onlyUseFirstField + , goldenEvalCekCatch "onlyUseFirstField" $ [onlyUseFirstField `unsafeApplyCode` inp] + , goldenBudget "onlyUseFirstField-budget" (onlyUseFirstField `unsafeApplyCode` inp) + , goldenPirReadable "patternMatching" patternMatching + , goldenEvalCekCatch "patternMatching" $ [patternMatching `unsafeApplyCode` inp] + , goldenBudget "patternMatching-budget" (patternMatching `unsafeApplyCode` inp) + , goldenPirReadable "recordFields" recordFields + , goldenEvalCekCatch "recordFields" $ [recordFields `unsafeApplyCode` inp] + , goldenBudget "recordFields-budget" (recordFields `unsafeApplyCode` inp) + ] + +-- A function that only accesses the first field of `Ints`. +-- TODO: the compiled code currently accesses all fields. +onlyUseFirstField :: CompiledCode (PlutusTx.BuiltinData -> Integer) +onlyUseFirstField = + $$( compile + [|| + \d -> let ints = PlutusTx.unsafeFromBuiltinData d in int1 ints + ||] + ) + +patternMatching :: CompiledCode (PlutusTx.BuiltinData -> Integer) +patternMatching = + $$( compile + [|| + \d -> + let (Ints x y z w) = PlutusTx.unsafeFromBuiltinData d + in x `PlutusTx.addInteger` y `PlutusTx.addInteger` z `PlutusTx.addInteger` w + ||] + ) + +-- TODO: this does the same thing as `patternMatching`, but is much more expensive, +-- since there's no sharing between the code that accesses different fields. +recordFields :: CompiledCode (PlutusTx.BuiltinData -> Integer) +recordFields = + $$( compile + [|| + \d -> + let ints = PlutusTx.unsafeFromBuiltinData d + x = int1 ints + y = int2 ints + z = int3 ints + w = int4 ints + in x `PlutusTx.addInteger` y `PlutusTx.addInteger` z `PlutusTx.addInteger` w + ||] + ) + +inp :: CompiledCode PlutusTx.BuiltinData +inp = liftCodeDef (PlutusTx.toBuiltinData (Ints 10 20 30 40)) diff --git a/plutus-tx-plugin/test/AsData/Budget/Types.hs b/plutus-tx-plugin/test/AsData/Budget/Types.hs new file mode 100644 index 00000000000..d2c4248cca3 --- /dev/null +++ b/plutus-tx-plugin/test/AsData/Budget/Types.hs @@ -0,0 +1,27 @@ +{-# LANGUAGE BangPatterns #-} +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE PatternSynonyms #-} +{-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TemplateHaskell #-} +{-# LANGUAGE TypeApplications #-} +{-# LANGUAGE ViewPatterns #-} + +module AsData.Budget.Types where + +import PlutusTx.AsData qualified as AsData +import PlutusTx.IsData qualified as PlutusTx +import PlutusTx.Prelude qualified as PlutusTx + +AsData.asData + [d| + data Ints = Ints + { int1 :: Integer + , int2 :: Integer + , int3 :: Integer + , int4 :: Integer + } + deriving newtype (PlutusTx.Eq, PlutusTx.FromData, PlutusTx.UnsafeFromData, PlutusTx.ToData) + |] diff --git a/plutus-tx-plugin/test/Spec.hs b/plutus-tx-plugin/test/Spec.hs index 4ed026e612e..9bc0fc60924 100644 --- a/plutus-tx-plugin/test/Spec.hs +++ b/plutus-tx-plugin/test/Spec.hs @@ -1,6 +1,7 @@ -- editorconfig-checker-disable-file module Main (main) where +import AsData.Budget.Spec qualified as AsData.Budget import Budget.Spec qualified as Budget import IntegerLiterals.NoStrict.NegativeLiterals.Spec qualified as IntegerLiterals.NoStrict.NegativeLiterals import IntegerLiterals.NoStrict.NoNegativeLiterals.Spec qualified as IntegerLiterals.NoStrict.NoNegativeLiterals @@ -34,6 +35,7 @@ tests = , TH.tests , Lib.tests , Budget.tests + , AsData.Budget.tests , Optimization.tests , Strictness.tests ]