Skip to content

Commit 595f87e

Browse files
committed
Add pi.c test
1 parent bca0ca0 commit 595f87e

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

tests/general_tests/pi/pi.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*******************************************************************************
2+
3+
pi.c
4+
5+
An obfuscated C program to print the first several digits of pi.
6+
7+
Source: https://cs.uwaterloo.ca/~alopez-o/math-faq/mathtext/node12.html
8+
9+
******************************************************************************/
10+
11+
#include <stdio.h>
12+
13+
int main(){
14+
int a = 10000, b = 0, c=2800, d = 0, e = 0, f[2801], g = 0;
15+
for(; b-c;) f[b++]=a/5;
16+
for(; d=0, g=c*2; c-=14, printf("%.4d",e+d/a), e = d%a)
17+
for(b=c; d+=f[b]*a, f[b]=d%--g, d/=g-- ,--b; d*=b);
18+
printf("\n");
19+
}

tests/test_all.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -160,31 +160,49 @@ class IntegrationTests(TestUtils):
160160
These test the programs found in general_tests/* for proper functionality.
161161
"""
162162

163-
def test_count(self):
164-
"""Test the Count.c program from the first pset of CPSC 223 at Yale."""
165-
166-
rel_dir = "general_tests/count"
163+
def io_test(self, rel_dir, cfile, stdin):
164+
"""Run a general I/O test.
165+
166+
Args:
167+
rel_dir (str): Directory for the test
168+
cfile (str): The .c file to compile and run
169+
stdin (str): The file to pipe into stdin of the executable, or None
170+
"""
167171
dir = str(pathlib.Path(__file__).parent.joinpath(rel_dir))
168172

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

173-
# Compile Count.c with ShivyC
174-
compile_with_shivyc(dir + "/Count.c")
177+
# Compile with ShivyC
178+
compile_with_shivyc(str(pathlib.Path(dir).joinpath(cfile)))
175179
self.assertEqual(error_collector.issues, [])
176180

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

181185
# Run ShivyC executable on sample input
182-
shivyc_run = "./out < {0}/input.c > {0}/shivyc_output".format(dir)
183-
subprocess.run(shivyc_run, shell=True, check=True)
186+
if stdin:
187+
shivyc_run = "./out < {0}/input.c > {0}/shivyc_output".format(dir)
188+
gcc_run = "./gcc_out < {0}/input.c > {0}/gcc_output".format(dir)
189+
else:
190+
shivyc_run = "./out > {0}/shivyc_output".format(dir)
191+
gcc_run = "./gcc_out > {0}/gcc_output".format(dir)
184192

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

196+
# Diff the two output files
189197
diff = "diff {0}/gcc_output {0}/shivyc_output".format(dir)
190198
subprocess.run(diff, shell=True, check=True)
199+
200+
def test_count(self):
201+
"""Test the Count.c program from the first pset of CPSC 223 at Yale."""
202+
203+
self.io_test("general_tests/count", "Count.c", "input.c")
204+
205+
def test_pi(self):
206+
"""Test the pi.c program."""
207+
208+
self.io_test("general_tests/pi", "pi.c", None)

0 commit comments

Comments
 (0)