From c403598d7722f46534e96f94fd3e3ddb8028dedc Mon Sep 17 00:00:00 2001 From: Andor Penzes Date: Fri, 26 Jul 2019 04:25:35 +0200 Subject: [PATCH] Compiler test: check against user defined output filenames. --- .gitignore | 3 +- .travis.yml | 1 - .../pnode.grin.opts | 2 + grin/test-data/sum-simple/sum_simple_exp.grin | 37 +++++++++++++++++++ .../sum-simple/sum_simple_exp.grin.expected | 24 ++++++++++++ .../sum-simple/sum_simple_exp.grin.opts | 6 +++ grin/test/Test/Hspec/Compiler.hs | 35 ++++++++++++------ 7 files changed, 95 insertions(+), 13 deletions(-) create mode 100644 grin/test-data/sum-simple/sum_simple_exp.grin create mode 100644 grin/test-data/sum-simple/sum_simple_exp.grin.expected create mode 100644 grin/test-data/sum-simple/sum_simple_exp.grin.opts diff --git a/.gitignore b/.gitignore index b60caff2..5c9255bb 100644 --- a/.gitignore +++ b/.gitignore @@ -26,4 +26,5 @@ output/ .grin-output/ .vscode/ *.out - +*.out.ll +*.out.s diff --git a/.travis.yml b/.travis.yml index c68ed240..48a403e0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -39,7 +39,6 @@ script: - stack build --coverage - stack exec grin-test --coverage - stack exec grin-end-to-end-test --coverage - - stack exec grin -- grin/grin/sum_simple.grin after_script: - travis_retry curl -L https://github.com/rubik/stack-hpc-coveralls/releases/download/v0.0.4.0/shc-linux-x64-8.0.1.tar.bz2 | tar -xj diff --git a/grin/test-data/dead-parameter-elimination/pnode.grin.opts b/grin/test-data/dead-parameter-elimination/pnode.grin.opts index 3bdcd17a..daeb4bf8 100644 --- a/grin/test-data/dead-parameter-elimination/pnode.grin.opts +++ b/grin/test-data/dead-parameter-elimination/pnode.grin.opts @@ -1,3 +1,5 @@ +outputExtension: "out" +grinOptions: - "--quiet" - "--dpe" - "--save-grin=$$$OUT$$$" diff --git a/grin/test-data/sum-simple/sum_simple_exp.grin b/grin/test-data/sum-simple/sum_simple_exp.grin new file mode 100644 index 00000000..0e3607b6 --- /dev/null +++ b/grin/test-data/sum-simple/sum_simple_exp.grin @@ -0,0 +1,37 @@ +grinMain = t1 <- store (CInt 1) + t2 <- store (CInt 10000) + t3 <- store (Fupto t1 t2) + t4 <- store (Fsum t3) + (CInt r') <- eval t4 + _prim_int_print r' + +upto m n = (CInt m') <- eval m + (CInt n') <- eval n + b' <- _prim_int_gt m' n' + if b' then + pure (CNil) + else + m1' <- _prim_int_add m' 1 + m1 <- store (CInt m1') + p <- store (Fupto m1 n) + pure (CCons m p) + +sum l = l2 <- eval l + case l2 of + (CNil) -> pure (CInt 0) + (CCons x xs) -> (CInt x') <- eval x + (CInt s') <- sum xs + ax' <- _prim_int_add x' s' + pure (CInt ax') + +eval q = v <- fetch q + case v of + (CInt x'1) -> pure (CInt x'1) + (CNil) -> pure (CNil) + (CCons y ys) -> pure (CCons y ys) + (Fupto a b) -> w <- upto a b + update q w + pure w + (Fsum c) -> z <- sum c + update q z + pure z diff --git a/grin/test-data/sum-simple/sum_simple_exp.grin.expected b/grin/test-data/sum-simple/sum_simple_exp.grin.expected new file mode 100644 index 00000000..649ece4b --- /dev/null +++ b/grin/test-data/sum-simple/sum_simple_exp.grin.expected @@ -0,0 +1,24 @@ + .text + .file "" + .globl grinMain # -- Begin function grinMain + .p2align 4, 0x90 + .type grinMain,@function +grinMain: # @grinMain + .cfi_startproc +# %bb.0: # %grinMain.entry + movl $50005000, %edi # imm = 0x2FB0408 + jmp _prim_int_print # TAILCALL +.Lfunc_end0: + .size grinMain, .Lfunc_end0-grinMain + .cfi_endproc + # -- End function + .type _heap_ptr_,@object # @_heap_ptr_ + .bss + .globl _heap_ptr_ + .p2align 3 +_heap_ptr_: + .quad 0 # 0x0 + .size _heap_ptr_, 8 + + + .section ".note.GNU-stack","",@progbits diff --git a/grin/test-data/sum-simple/sum_simple_exp.grin.opts b/grin/test-data/sum-simple/sum_simple_exp.grin.opts new file mode 100644 index 00000000..557e6fdb --- /dev/null +++ b/grin/test-data/sum-simple/sum_simple_exp.grin.opts @@ -0,0 +1,6 @@ +outputExtension: "out.s" +grinOptions: +- "--optimize" +- "--hpt" +- "--quiet" +- "--save-llvm=$$$OUT$$$" diff --git a/grin/test/Test/Hspec/Compiler.hs b/grin/test/Test/Hspec/Compiler.hs index b0603779..ab24b67f 100644 --- a/grin/test/Test/Hspec/Compiler.hs +++ b/grin/test/Test/Hspec/Compiler.hs @@ -1,4 +1,4 @@ -{-# LANGUAGE TypeFamilies, LambdaCase, TypeApplications #-} +{-# LANGUAGE TypeFamilies, LambdaCase, TypeApplications, DeriveGeneric #-} module Test.Hspec.Compiler where import Control.Arrow ((&&&)) @@ -22,6 +22,7 @@ import Data.Char (isDigit) import Data.List (isSuffixOf) import Data.String.Utils (replace) import System.Directory +import GHC.Generics data InputFile @@ -36,16 +37,17 @@ inputToFilePath = \case data CompilerTest = PipelineTest - { compilerInput :: InputFile -- name.grin name.binary - , compilerOptions :: [String] -- input.opts - , compilerExpected :: FilePath -- input.expected + { compilerInput :: InputFile -- name.grin name.binary + , compilerOptions :: [String] -- input.opts + , compilerExpected :: FilePath -- input.expected + , compilerOutputExt :: FilePath -- input.out -- defined in configuration } | EndToEndTest { compilerInput :: InputFile } -- TODO: Documentation -evaluatePipelineTest input options expected params actionWith progressCallback = do +evaluatePipelineTest input options expected ext params actionWith progressCallback = do result <- newIORef $ Result "" $ Failure Nothing $ Reason "End-to-end test did not set test as success." actionWith $ \() -> catch (do let args = case input of @@ -53,7 +55,7 @@ evaluatePipelineTest input options expected params actionWith progressCallback = Textual fp -> [fp] let outFile = inputToFilePath input <.> "out" mainWithArgs $ args ++ (map (replace "$$$OUT$$$" outFile) options) - content <- readFile outFile + content <- readFile (inputToFilePath input <.> ext) expected <- readFile expected if (content == expected) then writeIORef result $ Result "" Success @@ -156,8 +158,8 @@ bisect directory expected = do instance Example CompilerTest where type Arg CompilerTest = () evaluateExample compilerTest = case compilerTest of - PipelineTest i o e -> evaluatePipelineTest i o e - EndToEndTest i -> evaluateEndToEndTest i + PipelineTest i o e x -> evaluatePipelineTest i o e x + EndToEndTest i -> evaluateEndToEndTest i endToEnd :: FilePath -> Spec endToEnd dp = describe "End to end tests" $ do @@ -183,21 +185,32 @@ inputFile fp = case takeExtension fp of ".grin" -> Textual fp ".binary" -> Binary fp +data Options = Options + { outputExtension :: String + , grinOptions :: [String] + } deriving (Generic) + +instance FromJSON Options + createPipelineTest :: FilePath -> IO (Either String CompilerTest) createPipelineTest fp = do eopts <- Yaml.decodeFileWithWarnings (fp <.> "opts") case eopts of Left errs -> pure $ Left $ show errs - Right (warnings, opts) -> do - print ("YAML warnings:", warnings) + Right (warnings, Options ext opts) -> do pure $ Right $ PipelineTest { compilerInput = inputFile fp , compilerOptions = opts , compilerExpected = fp <.> "expected" + , compilerOutputExt = ext } treeToSpec :: DirTree (Maybe (Either String CompilerTest)) -> Spec treeToSpec = \case Failed name ex -> it name $ pendingWith $ show ex Dir name contents -> describe name $ mapM_ treeToSpec contents - File name test -> maybe (pure ()) (either (it name . pendingWith) (it name)) test + File name test -> maybe (pure ()) (either (it name . pendingWith) (itName name)) test + where + itName name test = case test of + PipelineTest{} -> it ("pipeline-test: " ++ name) test + EndToEndTest{} -> it ("end-to-end: " ++ name) test