better testing method

This commit is contained in:
Yann Esposito (Yogsototh) 2013-07-18 10:47:46 +02:00
parent 9e59d4fc53
commit aa04fe2b3c
17 changed files with 59 additions and 24 deletions

40
test.sh
View file

@ -1,17 +1,29 @@
#!/usr/bin/env zsh
for fic in tests/*.in; do
input="${fic:r}"
arg="$(cat $input.in)"
print -n -- "$input: "
runghc y.hs "$arg" > $input.res
if diff $input.res $input.out > /dev/null; then
print -- "OK"
else
print -- "ERROR"
print -- " expected: '$(cat $input.out)'"
print -- " got: '$(cat $input.res)'"
print -- ""
fi
\rm -f $input.res
tmpfic=tests/tmp
for input in tests/*; do
sed 's/\\/\\\\/g' $input > $tmpfic
# set 3 as the file descriptor for the file $tmpfic
exec 3< $tmpfic
done=0
num=1
until ((done == 1)); do
read <&3 program
(($?!=0)) && {done=1;continue}
read <&3 expected
(($?!=0)) && {done=1;continue}
result="$( runghc y.hs "$program")"
printf "%18s (line %3d): " ${input:t} $num
if [[ $expected == $result ]]; then
print -- "OK"
else
print -- "ERROR"
print -- " program: '$program'"
print -- " expected: '$expected'"
print -- " got: '$result'"
print -- ""
fi
((num+=2))
done
done
\rm -f $tmpfic

View file

@ -1 +1,2 @@
#b001010
Number 10

View file

@ -1 +0,0 @@
#b001010

View file

@ -1 +1,2 @@
#\a
Character 'a'

View file

@ -1 +0,0 @@
#\a

View file

@ -1 +1,2 @@
3.14
Float 3.14

View file

@ -1 +0,0 @@
3.14

View file

@ -1 +1,2 @@
32
Number 32

View file

@ -1 +0,0 @@
32

6
tests/string Normal file
View file

@ -0,0 +1,6 @@
"This is a simple string"
String "This is a simple string"
"This is a string with a return ->\n<- Here"
String "This is a string with a return ->\n<- Here"
"More difficult string \n, \r, \t, \\, \x, \A"
String "More difficult string \n, \r, \t, \\, x, A"

View file

@ -1 +0,0 @@
"This is a string with a return ->\n<- Here"

View file

@ -1 +0,0 @@
String "This is a string with a return ->\n<- Here"

View file

@ -1 +0,0 @@
"More difficult string \n, \r, \t, \\, \x, \A"

View file

@ -1 +0,0 @@
String "More difficult string \n, \r, \t, \\, x, A"

View file

@ -1 +0,0 @@
"This is a simple string"

View file

@ -1 +0,0 @@
String "This is a simple string"

23
y.hs
View file

@ -1,5 +1,6 @@
module Main where
import Control.Monad (liftM)
import Data.List (foldl')
import Text.ParserCombinators.Parsec hiding (spaces)
import System.Environment (getArgs)
@ -36,6 +37,12 @@ parseExpr = parseString
<|> try parseFloat -- 3.1415
<|> parseNumber -- 3, #b011001, #o070, #d930, #xFF3
<|> parseAtom -- symbol-323
<|> parseQuoted
<|> do
char '('
x <- try parseList <|> parseDottedList
char ')'
return x
symbol :: Parser Char
symbol = oneOf "!#$%&|*+-/:<=>?@^_~"
@ -138,3 +145,19 @@ parseNumber = do
<|> parseBaseSpecifiedNumber
return (Number number)
-- Recursive Parsers
parseList :: Parser LispVal
parseList = liftM List $ sepBy parseExpr spaces
parseDottedList :: Parser LispVal
parseDottedList = do
head <- endBy parseExpr spaces
tail <- char '.' >> spaces >> parseExpr
return $ DottedList head tail
parseQuoted :: Parser LispVal
parseQuoted = do
char '\''
x <- parseExpr
return $ List [Atom "quote", x]