Skip to content

Instantly share code, notes, and snippets.

@maekawatoshiki
Last active July 4, 2022 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maekawatoshiki/bc5cd710aee4b29e47b4f3c4384bcb39 to your computer and use it in GitHub Desktop.
Save maekawatoshiki/bc5cd710aee4b29e47b4f3c4384bcb39 to your computer and use it in GitHub Desktop.
#!/bin/bash
unique() {
echo $RANDOM | md5sum | head -c 10
}
LOG=/tmp/log$(unique)
do_assert() {
expected="$1"
input="$2"
UNIQ=$(unique)
# 9ccを実行し、アセンブリ(.s)を出力
./9cc "$input" > /tmp/tmp$UNIQ.s && \
cc -o /tmp/tmp$UNIQ /tmp/tmp$UNIQ.s && \
/tmp/tmp$UNIQ
actual="$?"
if [ "$actual" = "$expected" ]; then
echo "$input => $actual"
else
printf "\033[31m$input => $expected expected, but got $actual\033[0m\n" | tee $LOG -a
fi
}
assert() {
do_assert "$1" "$2" &
}
check() {
if [ ! -f $LOG ]; then
printf "\033[32mOK\033[0m\n"
return 0
else
FAILS=$(cat $LOG | wc -l)
printf "\033[31m$FAILS tests failed\033[0m\n"
return 1
fi
}
assert 0 '0;'
assert 42 '42;'
assert 21 '5+20-4;'
assert 41 ' 12 + 34 - 5 ;'
assert 148 ' 100 + 56 - 8 ;'
assert 47 '5+6*7;'
assert 15 '5*(9-6);'
assert 4 '(3+5)/2;'
assert 10 '-10+20;'
assert 10 '+(+10);'
assert 10 '-(-10);'
assert 10 '+ +10;'
assert 10 '- -10;'
assert 10 '(-(-15 + 25) + 50) / 4;'
assert 0 '0==1;'
assert 1 '42==42;'
assert 1 '0!=1;'
assert 0 '42!=42;'
assert 1 '0<1;'
assert 0 '1<1;'
assert 0 '2<1;'
assert 1 '0<=1;'
assert 1 '1<=1;'
assert 0 '2<=1;'
assert 1 '1>0;'
assert 0 '1>1;'
assert 0 '1>2;'
assert 1 '1>=0;'
assert 1 '1>=1;'
assert 0 '1>=2;'
assert 2 'a=2; a;'
assert 6 'x=4; y=2; x+y;'
assert 9 'p=q=3; r=2; p+q*r;'
wait
check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment