2024-10-01 21:00:27 +02:00

71 lines
2.2 KiB
Haskell

-- | Generate test cases for Nim
module TestGen.TestGoldilocks where
--------------------------------------------------------------------------------
import System.IO
import Goldilocks
import TestGen.Shared
--------------------------------------------------------------------------------
centered :: Integer -> Integer -> [Integer]
centered center width = [center-width .. center+width]
mkTestFieldElems :: Integer -> [F]
mkTestFieldElems width = map fromInteger $ concat
[ centered (0 ) width
, centered (2^16) width
, centered (2^31) width
, centered (2^32) width
, centered (2^33) width
, centered (2^48) width
, centered (2^63) width
]
testFieldElems :: [F]
testFieldElems = mkTestFieldElems 7
testFieldPairs :: [(F,F)]
testFieldPairs = [ (x,y) | x<-list, y<-list ] where
list = mkTestFieldElems 3
--------------------------------------------------------------------------------
unary :: String -> (F -> F) -> [F] -> String
unary varname f xs = unlines (header : stuff ++ footer) where
header = "const " ++ varname ++ "* : array[" ++ show (length xs) ++ ", tuple[x:uint64, y:uint64]] = "
footer = [" ]",""]
stuff = nimShowPairs [ (x, f x) | x<-xs ]
binary :: String -> (F -> F -> F) -> [(F,F)] -> String
binary varname f xys = unlines (header : stuff ++ footer) where
header = "const " ++ varname ++ "* : array[" ++ show (length xys) ++ ", tuple[x:uint64, y:uint64, z:uint64]] = "
footer = [" ]",""]
stuff = nimShowTriples [ (x, y, f x y) | (x,y)<-xys ]
--------------------------------------------------------------------------------
printTests :: IO ()
printTests = hPrintTests stdout
hPrintTests :: Handle -> IO ()
hPrintTests h = hPutStrLn h $ unlines
[ unary "testcases_neg" negate testFieldElems
, binary "testcases_add" (+) testFieldPairs
, binary "testcases_sub" (-) testFieldPairs
, binary "testcases_mul" (*) testFieldPairs
]
writeTests :: IO ()
writeTests = withFile "fieldTestCases.nim" WriteMode $ \h -> do
hPutStrLn h "# generated by TestGen/TestGoldilocks.hs\n"
hPutStrLn h "import goldilocks_hash/types\n"
hPrintTests h
--------------------------------------------------------------------------------