#!/bin/sh #set -eux try() { expected="$1" input="$2" cargo run -q "$input" > tmp.s gcc -static -o tmp tmp.s tmp-test.o ./tmp actual="$?" if [ "$actual" == "$expected" ]; then echo "$input => $actual" else echo "$input expected, but got $actual" exit 1 fi } cat <0; }' try 0 'int main() { return 0>1; }' try 1 'int main() { return 1>0; }' try 60 'int main() { int sum=0; int i; for (i=10; i<15; i=i+1) sum = sum + i; return sum;}' try 89 'int main() { int i=1; int j=1; for (int k=0; k<10; k=k+1) { int m=i+j; i=j; j=m; } return i;}' try 8 'int main() { int *p = alloc1(3, 5); return *p + *(p + 1); }' try 9 'int main() { int *p = alloc2(2, 7); return *p + *(p - 1); }' try 2 'int main() { int **p = alloc_ptr_ptr(2); return **p; }' try 3 'int main() { int ary[2]; *ary=1; *(ary+1)=2; return *ary + *(ary+1);}' try 5 'int main() { int x; int *p = &x; x = 5; return *p;}' try 3 'int main() { int ary[2]; ary[0]=1; ary[1]=2; return ary[0] + ary[0+1];}' try 5 'int main() { int x; int *p = &x; x = 5; return p[0];}' try 1 'int main() { char x; return sizeof(x); }' try 4 'int main() { int x; return sizeof(x); }' try 8 'int main() { int *x; return sizeof x; }' try 16 'int main() { int x[4]; return sizeof x; }' try 5 'int main() { char x = 5; return x; }' try 42 'int main() { int x = 0; char *p = &x; p[0] = 42; return x; }' try 97 'int main() { char *p = "abc"; return p[0]; }' try 98 'int main() { char *p = "abc"; return p[1]; }' try 99 'int main() { char *p = "abc"; return p[2]; }' try 0 'int main() { char *p = "abc"; return p[3]; }' try 1 'int main() { int x = 1; { int x = 2; } return x; }' try 0 'int x; int main() { return x; }' try 5 'int x; int main() { x = 5; return x; }' try 20 'int x[5]; int main() { return sizeof(x); }' try 15 'int x[5]; int main() { x[0] = 5; x[4] = 10; return x[0] + x[4]; }' try 0 'int main() { return 4 == 5; }' try 1 'int main() { return 5 == 5; }' try 1 'int main() { return 4 != 5; }' try 0 'int main() { return 5 != 5; }' try 45 'int main() { int x=0; int y=0; do { y=y+x; x=x+1; } while (x < 10); return y; }' try 5 'extern int global_arr[1]; int main() { return global_arr[0]; }' try 8 'int main() { return 3 + ({ return 5; }); }' echo OK