-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_selfreproducingprogram.py
executable file
·50 lines (42 loc) · 1.47 KB
/
test_selfreproducingprogram.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python
import sys, subprocess
usage = "%s <interpreter> <filename>" % sys.argv[0]
try:
selfrepInterpreter = sys.argv[1]
selfrepProgram = sys.argv[2]
selfrepSourceFromFile = file(selfrepProgram, 'r').read()
except IndexError:
print usage
sys.exit(1)
def outputOfSelfReproducingProgram(selfrepSource):
p = subprocess.Popen([selfrepInterpreter],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = p.communicate(selfrepSource)
if err:
raise RuntimeError("Error when interpreting source")
return out
def sourceMatchesOutput(selfrepSource, selfrepOutput):
return outputOfSelfReproducingProgram(selfrepSource) == selfrepOutput
print 'Checking that program is not empty... ',
if not (selfrepSourceFromFile.strip() == ''):
print 'passed.'
else:
print 'failed.'
print 'Checking that program does not reference its own filename... ',
if not (selfrepProgram in selfrepSourceFromFile):
print 'passed.'
else:
print 'failed.'
print 'Checking that source matches output... ',
selfrepOutput = outputOfSelfReproducingProgram(selfrepSourceFromFile)
if sourceMatchesOutput(selfrepSourceFromFile, selfrepOutput):
print 'passed.'
else:
print 'failed.'
print 'Checking the output of the output... ',
if sourceMatchesOutput(selfrepOutput, outputOfSelfReproducingProgram(selfrepOutput)):
print 'passed.'
else:
print 'failed.'