From 4ae957aac6e161844f621515b3eb85f73156c5f7 Mon Sep 17 00:00:00 2001 From: Adam Wick Date: Thu, 15 Aug 2019 20:24:04 -0700 Subject: [PATCH] Preliminary support for generating test vectors. --- generation/src/Base.hs | 3 ++- generation/src/BinaryOps.hs | 3 ++- generation/src/Compare.hs | 3 ++- generation/src/Conversions.hs | 3 ++- generation/src/CryptoNum.hs | 3 ++- generation/src/File.hs | 23 ++++++++++++----------- generation/src/Main.hs | 22 ++++++++++++---------- 7 files changed, 34 insertions(+), 26 deletions(-) diff --git a/generation/src/Base.hs b/generation/src/Base.hs index 7cbcfd2..5d757a1 100644 --- a/generation/src/Base.hs +++ b/generation/src/Base.hs @@ -11,7 +11,8 @@ base :: File base = File { predicate = \ _ _ -> True, outputName = "base", - generator = declareBaseStructure + generator = declareBaseStructure, + testGenerator = Nothing } declareBaseStructure :: Word -> Gen () diff --git a/generation/src/BinaryOps.hs b/generation/src/BinaryOps.hs index 7732b6d..aee5286 100644 --- a/generation/src/BinaryOps.hs +++ b/generation/src/BinaryOps.hs @@ -11,7 +11,8 @@ binaryOps :: File binaryOps = File { predicate = \ _ _ -> True, outputName = "binary", - generator = declareBinaryOperators + generator = declareBinaryOperators, + testGenerator = Nothing } declareBinaryOperators :: Word -> Gen () diff --git a/generation/src/Compare.hs b/generation/src/Compare.hs index 79d9d9b..4b57a4f 100644 --- a/generation/src/Compare.hs +++ b/generation/src/Compare.hs @@ -9,7 +9,8 @@ comparisons :: File comparisons = File { predicate = \ _ _ -> True, outputName = "compare", - generator = declareComparators + generator = declareComparators, + testGenerator = Nothing } declareComparators :: Word -> Gen () diff --git a/generation/src/Conversions.hs b/generation/src/Conversions.hs index 72f3fde..e61b248 100644 --- a/generation/src/Conversions.hs +++ b/generation/src/Conversions.hs @@ -11,7 +11,8 @@ conversions :: File conversions = File { predicate = \ _ _ -> True, outputName = "conversions", - generator = declareConversions + generator = declareConversions, + testGenerator = Nothing } declareConversions :: Word -> Gen () diff --git a/generation/src/CryptoNum.hs b/generation/src/CryptoNum.hs index bee2492..6c94237 100644 --- a/generation/src/CryptoNum.hs +++ b/generation/src/CryptoNum.hs @@ -11,7 +11,8 @@ cryptoNum :: File cryptoNum = File { predicate = \ _ _ -> True, outputName = "cryptonum", - generator = declareCryptoNumInstance + generator = declareCryptoNumInstance, + testGenerator = Nothing } declareCryptoNumInstance :: Word -> Gen () diff --git a/generation/src/File.hs b/generation/src/File.hs index 1702e38..1b771b6 100644 --- a/generation/src/File.hs +++ b/generation/src/File.hs @@ -2,7 +2,7 @@ module File( File(..), Task(..), addModuleTasks, - makeTask + makeTasks ) where @@ -15,7 +15,8 @@ import System.FilePath(takeBaseName,takeDirectory,takeFileName,()) data File = File { predicate :: Word -> [Word] -> Bool, outputName :: FilePath, - generator :: Word -> Gen () + generator :: Word -> Gen (), + testGenerator :: Maybe (Word -> Gen ()) } data Task = Task { @@ -23,18 +24,18 @@ data Task = Task { fileGenerator :: Gen () } -makeTask :: FilePath -> +makeTasks :: FilePath -> FilePath -> Word -> [Word] -> File -> - Maybe Task -makeTask base size allSizes file + [Task] +makeTasks srcBase testBase size allSizes file | predicate file size allSizes = - Just Task { - outputFile = base ("u" ++ show size) outputName file <> ".rs", - fileGenerator = generator file size - } - | otherwise = - Nothing + let base = Task (srcBase ("u" ++ show size) outputName file <> ".rs") (generator file size) + in case testGenerator file of + Nothing -> [base] + Just x -> + [base, Task (testBase outputName file ("U" ++ show size ++ ".test")) (x size)] + | otherwise = [] addModuleTasks :: FilePath -> [Task] -> [Task] addModuleTasks base baseTasks = unsignedTask : (baseTasks ++ moduleTasks) diff --git a/generation/src/Main.hs b/generation/src/Main.hs index ca6346e..f518c46 100644 --- a/generation/src/Main.hs +++ b/generation/src/Main.hs @@ -7,9 +7,8 @@ import Compare(comparisons) import Conversions(conversions) import CryptoNum(cryptoNum) import Control.Monad(forM_,unless) -import Data.Maybe(mapMaybe) import Data.Word(Word) -import File(File,Task(..),addModuleTasks,makeTask) +import File(File,Task(..),addModuleTasks,makeTasks) import Gen(runGen) import System.Directory(createDirectoryIfMissing) import System.Environment(getArgs) @@ -38,21 +37,24 @@ signedFiles :: [File] signedFiles = [ ] -makeTasks :: FilePath -> [File] -> [Task] -makeTasks basePath files = - concatMap (\ sz -> mapMaybe (makeTask basePath sz bitsizes) files) bitsizes +makeTasks' :: FilePath -> FilePath -> [File] -> [Task] +makeTasks' srcPath testPath files = + concatMap (\ sz -> concatMap (makeTasks srcPath testPath sz bitsizes) files) bitsizes -makeAllTasks :: FilePath -> [Task] -makeAllTasks basePath = addModuleTasks basePath $ - makeTasks (basePath "unsigned") unsignedFiles ++ - makeTasks (basePath "signed") signedFiles +makeAllTasks :: FilePath -> FilePath -> [Task] +makeAllTasks srcPath testPath = addModuleTasks srcPath $ + makeTasks' (srcPath "unsigned") testPath unsignedFiles ++ + makeTasks' (srcPath "signed") testPath signedFiles main :: IO () main = do args <- getArgs unless (length args == 1) $ die ("generation takes exactly one argument, the target directory") - let tasks = makeAllTasks (head args) + let topLevel = head args + srcPath = topLevel "src" + testPath = topLevel "testdata" + tasks = makeAllTasks srcPath testPath total = length tasks forM_ (zip [(1::Word)..] tasks) $ \ (i, task) -> do putStrLn ("[" ++ show i ++ "/" ++ show total ++ "] " ++ outputFile task)