Preliminary support for generating test vectors.

This commit is contained in:
2019-08-15 20:24:04 -07:00
parent f4a3cf69ad
commit 4ae957aac6
7 changed files with 34 additions and 26 deletions

View File

@@ -11,7 +11,8 @@ base :: File
base = File { base = File {
predicate = \ _ _ -> True, predicate = \ _ _ -> True,
outputName = "base", outputName = "base",
generator = declareBaseStructure generator = declareBaseStructure,
testGenerator = Nothing
} }
declareBaseStructure :: Word -> Gen () declareBaseStructure :: Word -> Gen ()

View File

@@ -11,7 +11,8 @@ binaryOps :: File
binaryOps = File { binaryOps = File {
predicate = \ _ _ -> True, predicate = \ _ _ -> True,
outputName = "binary", outputName = "binary",
generator = declareBinaryOperators generator = declareBinaryOperators,
testGenerator = Nothing
} }
declareBinaryOperators :: Word -> Gen () declareBinaryOperators :: Word -> Gen ()

View File

@@ -9,7 +9,8 @@ comparisons :: File
comparisons = File { comparisons = File {
predicate = \ _ _ -> True, predicate = \ _ _ -> True,
outputName = "compare", outputName = "compare",
generator = declareComparators generator = declareComparators,
testGenerator = Nothing
} }
declareComparators :: Word -> Gen () declareComparators :: Word -> Gen ()

View File

@@ -11,7 +11,8 @@ conversions :: File
conversions = File { conversions = File {
predicate = \ _ _ -> True, predicate = \ _ _ -> True,
outputName = "conversions", outputName = "conversions",
generator = declareConversions generator = declareConversions,
testGenerator = Nothing
} }
declareConversions :: Word -> Gen () declareConversions :: Word -> Gen ()

View File

@@ -11,7 +11,8 @@ cryptoNum :: File
cryptoNum = File { cryptoNum = File {
predicate = \ _ _ -> True, predicate = \ _ _ -> True,
outputName = "cryptonum", outputName = "cryptonum",
generator = declareCryptoNumInstance generator = declareCryptoNumInstance,
testGenerator = Nothing
} }
declareCryptoNumInstance :: Word -> Gen () declareCryptoNumInstance :: Word -> Gen ()

View File

@@ -2,7 +2,7 @@ module File(
File(..), File(..),
Task(..), Task(..),
addModuleTasks, addModuleTasks,
makeTask makeTasks
) )
where where
@@ -15,7 +15,8 @@ import System.FilePath(takeBaseName,takeDirectory,takeFileName,(</>))
data File = File { data File = File {
predicate :: Word -> [Word] -> Bool, predicate :: Word -> [Word] -> Bool,
outputName :: FilePath, outputName :: FilePath,
generator :: Word -> Gen () generator :: Word -> Gen (),
testGenerator :: Maybe (Word -> Gen ())
} }
data Task = Task { data Task = Task {
@@ -23,18 +24,18 @@ data Task = Task {
fileGenerator :: Gen () fileGenerator :: Gen ()
} }
makeTask :: FilePath -> makeTasks :: FilePath -> FilePath ->
Word -> [Word] -> Word -> [Word] ->
File -> File ->
Maybe Task [Task]
makeTask base size allSizes file makeTasks srcBase testBase size allSizes file
| predicate file size allSizes = | predicate file size allSizes =
Just Task { let base = Task (srcBase </> ("u" ++ show size) </> outputName file <> ".rs") (generator file size)
outputFile = base </> ("u" ++ show size) </> outputName file <> ".rs", in case testGenerator file of
fileGenerator = generator file size Nothing -> [base]
} Just x ->
| otherwise = [base, Task (testBase </> outputName file </> ("U" ++ show size ++ ".test")) (x size)]
Nothing | otherwise = []
addModuleTasks :: FilePath -> [Task] -> [Task] addModuleTasks :: FilePath -> [Task] -> [Task]
addModuleTasks base baseTasks = unsignedTask : (baseTasks ++ moduleTasks) addModuleTasks base baseTasks = unsignedTask : (baseTasks ++ moduleTasks)

View File

@@ -7,9 +7,8 @@ import Compare(comparisons)
import Conversions(conversions) import Conversions(conversions)
import CryptoNum(cryptoNum) import CryptoNum(cryptoNum)
import Control.Monad(forM_,unless) import Control.Monad(forM_,unless)
import Data.Maybe(mapMaybe)
import Data.Word(Word) import Data.Word(Word)
import File(File,Task(..),addModuleTasks,makeTask) import File(File,Task(..),addModuleTasks,makeTasks)
import Gen(runGen) import Gen(runGen)
import System.Directory(createDirectoryIfMissing) import System.Directory(createDirectoryIfMissing)
import System.Environment(getArgs) import System.Environment(getArgs)
@@ -38,21 +37,24 @@ signedFiles :: [File]
signedFiles = [ signedFiles = [
] ]
makeTasks :: FilePath -> [File] -> [Task] makeTasks' :: FilePath -> FilePath -> [File] -> [Task]
makeTasks basePath files = makeTasks' srcPath testPath files =
concatMap (\ sz -> mapMaybe (makeTask basePath sz bitsizes) files) bitsizes concatMap (\ sz -> concatMap (makeTasks srcPath testPath sz bitsizes) files) bitsizes
makeAllTasks :: FilePath -> [Task] makeAllTasks :: FilePath -> FilePath -> [Task]
makeAllTasks basePath = addModuleTasks basePath $ makeAllTasks srcPath testPath = addModuleTasks srcPath $
makeTasks (basePath </> "unsigned") unsignedFiles ++ makeTasks' (srcPath </> "unsigned") testPath unsignedFiles ++
makeTasks (basePath </> "signed") signedFiles makeTasks' (srcPath </> "signed") testPath signedFiles
main :: IO () main :: IO ()
main = main =
do args <- getArgs do args <- getArgs
unless (length args == 1) $ unless (length args == 1) $
die ("generation takes exactly one argument, the target directory") 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 total = length tasks
forM_ (zip [(1::Word)..] tasks) $ \ (i, task) -> forM_ (zip [(1::Word)..] tasks) $ \ (i, task) ->
do putStrLn ("[" ++ show i ++ "/" ++ show total ++ "] " ++ outputFile task) do putStrLn ("[" ++ show i ++ "/" ++ show total ++ "] " ++ outputFile task)