Skip to content

Commit

Permalink
Add pi.c test
Browse files Browse the repository at this point in the history
  • Loading branch information
ShivamSarodia committed May 2, 2017
1 parent bca0ca0 commit 595f87e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
19 changes: 19 additions & 0 deletions tests/general_tests/pi/pi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*******************************************************************************
pi.c
An obfuscated C program to print the first several digits of pi.
Source: https://cs.uwaterloo.ca/~alopez-o/math-faq/mathtext/node12.html
******************************************************************************/

#include <stdio.h>

int main(){
int a = 10000, b = 0, c=2800, d = 0, e = 0, f[2801], g = 0;
for(; b-c;) f[b++]=a/5;
for(; d=0, g=c*2; c-=14, printf("%.4d",e+d/a), e = d%a)
for(b=c; d+=f[b]*a, f[b]=d%--g, d/=g-- ,--b; d*=b);
printf("\n");
}
40 changes: 29 additions & 11 deletions tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,31 +160,49 @@ class IntegrationTests(TestUtils):
These test the programs found in general_tests/* for proper functionality.
"""

def test_count(self):
"""Test the Count.c program from the first pset of CPSC 223 at Yale."""

rel_dir = "general_tests/count"
def io_test(self, rel_dir, cfile, stdin):
"""Run a general I/O test.
Args:
rel_dir (str): Directory for the test
cfile (str): The .c file to compile and run
stdin (str): The file to pipe into stdin of the executable, or None
"""
dir = str(pathlib.Path(__file__).parent.joinpath(rel_dir))

# Remove leftover files from last test
rm = "rm -f {0}/gcc_out {0}/out {0}/shivyc_output {0}/gcc_output"
subprocess.run(rm.format(dir), shell=True, check=True)

# Compile Count.c with ShivyC
compile_with_shivyc(dir + "/Count.c")
# Compile with ShivyC
compile_with_shivyc(str(pathlib.Path(dir).joinpath(cfile)))
self.assertEqual(error_collector.issues, [])

# Compile Count.c with gcc
gcc_compile = "gcc {0}/Count.c -o gcc_out".format(dir)
gcc_compile = "gcc {0}/{1} -o gcc_out".format(dir, cfile)
subprocess.run(gcc_compile, shell=True, check=True)

# Run ShivyC executable on sample input
shivyc_run = "./out < {0}/input.c > {0}/shivyc_output".format(dir)
subprocess.run(shivyc_run, shell=True, check=True)
if stdin:
shivyc_run = "./out < {0}/input.c > {0}/shivyc_output".format(dir)
gcc_run = "./gcc_out < {0}/input.c > {0}/gcc_output".format(dir)
else:
shivyc_run = "./out > {0}/shivyc_output".format(dir)
gcc_run = "./gcc_out > {0}/gcc_output".format(dir)

# Run gcc executable on sample input
gcc_run = "./gcc_out < {0}/input.c > {0}/gcc_output".format(dir)
subprocess.run(shivyc_run, shell=True, check=True)
subprocess.run(gcc_run, shell=True, check=True)

# Diff the two output files
diff = "diff {0}/gcc_output {0}/shivyc_output".format(dir)
subprocess.run(diff, shell=True, check=True)

def test_count(self):
"""Test the Count.c program from the first pset of CPSC 223 at Yale."""

self.io_test("general_tests/count", "Count.c", "input.c")

def test_pi(self):
"""Test the pi.c program."""

self.io_test("general_tests/pi", "pi.c", None)

0 comments on commit 595f87e

Please sign in to comment.