<commit_before>#!/usr/bin/env runhaskell

module Main(main) where

import Control.Applicative
import Control.Concurrent
import Control.Exception
import Control.Monad

import qualified Data.Set as S
import Data.Char (isSpace)

import System.Directory
import System.Environment
import System.Exit
import System.FilePath
import System.IO
import System.Process

futharkFlags :: String
futharkFlags = "-sfe"

-- | Number of tests to run concurrently.
concurrency :: Int
concurrency = 8

clearLine :: IO ()
clearLine = putStr "\27[2K"

data TestResult = Success
                | Failure String

data Test = TypeFailure FilePath
          | Optimise FilePath
          | Run FilePath FilePath FilePath
          | Compile FilePath FilePath FilePath
            deriving (Eq, Ord)

testDescription :: Test -> String
testDescription (TypeFailure f) = "type failure of " ++ f
testDescription (Optimise f) = "optimisation of " ++ f
testDescription (Run f _ _) = "interpretation of " ++ f
testDescription (Compile f _ _) = "compilation of " ++ f

futharkNotFound :: String
futharkNotFound = "futhark binary not found"

failureTest :: FilePath -> IO TestResult
failureTest f = do
  (code, _, err) <- readProcessWithExitCode "futhark" [futharkFlags, f] ""
  case code of
    ExitSuccess -> return $ Failure "Expected failure\n"
    ExitFailure 127 -> return $ Failure futharkNotFound
    ExitFailure 1 -> return $ Failure err
    ExitFailure _ -> return Success

typeCheckTest :: FilePath -> IO TestResult
typeCheckTest f = do
  (code, _, err) <- readProcessWithExitCode "futhark" [futharkFlags, f] ""
  case code of
    ExitSuccess -> return Success
    ExitFailure 127 -> return $ Failure futharkNotFound
    ExitFailure _ -> return $ Failure err

executeTest :: FilePath -> FilePath -> FilePath -> IO TestResult
executeTest f inputf outputf = do
  input <- readFile inputf
  (code, output, err) <- readProcessWithExitCode "futhark" [futharkFlags, "-i", f] input
  expectedOutput <- readFile outputf
  case code of
    ExitSuccess
      | output `compareOutput` expectedOutput -> return Success
      | otherwise -> do
        writeFile expectedOutputf output
        return $ Failure $ outputf ++ " and " ++ expectedOutputf ++ " do not match."
    ExitFailure 127 -> return $ Failure futharkNotFound
    ExitFailure _   -> return $ Failure err
  where expectedOutputf = outputf `replaceExtension` "interpreterout"

compileTest :: FilePath -> FilePath -> FilePath -> IO TestResult
compileTest f inputf outputf = do
  input <- readFile inputf
  expectedOutput <- readFile outputf
  (futcode, l0prog, l0err) <- readProcessWithExitCode "futhark" [futharkFlags, "--compile-sequential", f] ""
  writeFile cOutputf l0prog
  case futcode of
    ExitFailure 127 -> return $ Failure futharkNotFound
    ExitFailure _   -> return $ Failure l0err
    ExitSuccess     -> do
      (gccCode, _, gccerr) <- readProcessWithExitCode "gcc" [cOutputf, "-o", binOutputf, "-lm"] ""
      case gccCode of
        ExitFailure _ -> return $ Failure gccerr
        ExitSuccess   -> do
          (progCode, output, progerr) <- readProcessWithExitCode binOutputf [] input
          writeFile expectedOutputf output
          case progCode of
            ExitFailure _ -> return $ Failure progerr
            ExitSuccess
              | output `compareOutput` expectedOutput -> return Success
              | otherwise ->
                  return $ Failure $ outputf ++ " and " ++ expectedOutputf ++ " do not match."
  where cOutputf = outputf `replaceExtension` "c"
        binOutputf = outputf `replaceExtension` "bin"
        expectedOutputf = outputf `replaceExtension` "compilerout"

compareOutput :: String -> String -> Bool
compareOutput x y = filter (not . isSpace) x == filter (not . isSpace) y

catching :: IO TestResult -> IO TestResult
catching m = m `catch` save
  where save :: SomeException -> IO TestResult
        save e = return $ Failure $ show e
<commit_msg>Add newly required flags to Runtests.<commit_after>#!/usr/bin/env runhaskell

module Main(main) where

import Control.Applicative
import Control.Concurrent
import Control.Exception
import Control.Monad

import qualified Data.Set as S
import Data.Char (isSpace)

import System.Directory
import System.Environment
import System.Exit
import System.FilePath
import System.IO
import System.Process

futharkFlags :: String
futharkFlags = "-sfe"

-- | Number of tests to run concurrently.
concurrency :: Int
concurrency = 8

clearLine :: IO ()
clearLine = putStr "\27[2K"

data TestResult = Success
                | Failure String

data Test = TypeFailure FilePath
          | Optimise FilePath
          | Run FilePath FilePath FilePath
          | Compile FilePath FilePath FilePath
            deriving (Eq, Ord)

testDescription :: Test -> String
testDescription (TypeFailure f) = "type failure of " ++ f
testDescription (Optimise f) = "optimisation of " ++ f
testDescription (Run f _ _) = "interpretation of " ++ f
testDescription (Compile f _ _) = "compilation of " ++ f

futharkNotFound :: String
futharkNotFound = "futhark binary not found"

failureTest :: FilePath -> IO TestResult
failureTest f = do
  (code, _, err) <- readProcessWithExitCode "futhark" [futharkFlags, f] ""
  case code of
    ExitSuccess -> return $ Failure "Expected failure\n"
    ExitFailure 127 -> return $ Failure futharkNotFound
    ExitFailure 1 -> return $ Failure err
    ExitFailure _ -> return Success

typeCheckTest :: FilePath -> IO TestResult
typeCheckTest f = do
  (code, _, err) <- readProcessWithExitCode "futhark" [futharkFlags, f] ""
  case code of
    ExitSuccess -> return Success
    ExitFailure 127 -> return $ Failure futharkNotFound
    ExitFailure _ -> return $ Failure err

executeTest :: FilePath -> FilePath -> FilePath -> IO TestResult
executeTest f inputf outputf = do
  input <- readFile inputf
  (code, output, err) <- readProcessWithExitCode "futhark" [futharkFlags, "-i", f] input
  expectedOutput <- readFile outputf
  case code of
    ExitSuccess
      | output `compareOutput` expectedOutput -> return Success
      | otherwise -> do
        writeFile expectedOutputf output
        return $ Failure $ outputf ++ " and " ++ expectedOutputf ++ " do not match."
    ExitFailure 127 -> return $ Failure futharkNotFound
    ExitFailure _   -> return $ Failure err
  where expectedOutputf = outputf `replaceExtension` "interpreterout"

compileTest :: FilePath -> FilePath -> FilePath -> IO TestResult
compileTest f inputf outputf = do
  input <- readFile inputf
  expectedOutput <- readFile outputf
  (futcode, l0prog, l0err) <- readProcessWithExitCode "futhark" [futharkFlags, "-fsa", "--compile-sequential", f] ""
  writeFile cOutputf l0prog
  case futcode of
    ExitFailure 127 -> return $ Failure futharkNotFound
    ExitFailure _   -> return $ Failure l0err
    ExitSuccess     -> do
      (gccCode, _, gccerr) <- readProcessWithExitCode "gcc" [cOutputf, "-o", binOutputf, "-lm"] ""
      case gccCode of
        ExitFailure _ -> return $ Failure gccerr
        ExitSuccess   -> do
          (progCode, output, progerr) <- readProcessWithExitCode binOutputf [] input
          writeFile expectedOutputf output
          case progCode of
            ExitFailure _ -> return $ Failure progerr
            ExitSuccess
              | output `compareOutput` expectedOutput -> return Success
              | otherwise ->
                  return $ Failure $ outputf ++ " and " ++ expectedOutputf ++ " do not match."
  where cOutputf = outputf `replaceExtension` "c"
        binOutputf = outputf `replaceExtension` "bin"
        expectedOutputf = outputf `replaceExtension` "compilerout"

compareOutput :: String -> String -> Bool
compareOutput x y = filter (not . isSpace) x == filter (not . isSpace) y

catching :: IO TestResult -> IO TestResult
catching m = m `catch` save
  where save :: SomeException -> IO TestResult
        save e = return $ Failure $ show e

{'fraction_non_alphanumeric': 0.08963337547408344, 'fraction_numerical': 0.005309734513274336, 'mean_word_length': 3.196816976127321, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7413431', 'n_tokens_mistral': 2313, 'n_tokens_neox': 2163, 'n_words': 750}