gazprea-fuzzer-python/test/test_correctness.py
ayrton d211131c4e IT'S ALIIIIIIIIVE
Took 1 hour 15 minutes
2023-11-20 20:28:55 -07:00

161 lines
4.8 KiB
Python

import concurrent.futures
import os
import sys
import unittest
from contextlib import redirect_stdout
from io import StringIO
import yaml
import xml.etree.ElementTree as ET
import signal
from ast_generator.ast_generator import AstGenerator
from ast_parser.python_unparser import PythonUnparser
class TestCorrectness(unittest.TestCase):
@classmethod
def setUpClass(cls):
with open("config.yaml", 'r') as stream:
cls.props = yaml.safe_load(stream)
def setUp(cls):
cls.ast_gen = AstGenerator(cls.props)
cls.python_unparser = PythonUnparser(cls.ast_gen.ast, True)
def test_assignment(self):
self.ast_gen.ast = ET.Element("block")
self.ast_gen.current_ast_element = self.ast_gen.ast
self.ast_gen.generate_declaration(mut='var')
self.ast_gen.generate_assignment()
self.python_unparser.xml = self.ast_gen.ast
self.python_unparser.unparse()
try:
exec(self.python_unparser.source)
except Exception as e:
self.fail(e)
def test_binary_operator(self):
self.ast_gen.ast = ET.Element("block")
self.ast_gen.current_ast_element = self.ast_gen.ast
self.ast_gen.generate_declaration()
self.ast_gen.generate_binary('addition', 'int')
self.python_unparser.xml = self.ast_gen.ast
self.python_unparser.unparse()
try:
exec(self.python_unparser.source)
except Exception as e:
self.fail(e)
def test_unary_operator(self):
self.ast_gen.ast = ET.Element("block")
self.ast_gen.current_ast_element = self.ast_gen.ast
self.ast_gen.generate_declaration()
self.ast_gen.generate_unary('negation', 'int')
self.python_unparser.xml = self.ast_gen.ast
self.python_unparser.unparse()
try:
exec(self.python_unparser.source)
except Exception as e:
print(self.python_unparser.source)
self.fail(e)
def test_output(self):
self.ast_gen.ast = ET.Element("block")
self.ast_gen.current_ast_element = self.ast_gen.ast
self.ast_gen.generate_out_stream()
output = StringIO()
self.python_unparser.xml = self.ast_gen.ast
self.python_unparser.unparse()
with redirect_stdout(output):
exec(self.python_unparser.source)
try:
exec(output.getvalue())
except Exception as e:
self.fail(e)
outstring = output.getvalue()
self.assertNotEqual("", outstring, self.python_unparser.source)
def test_input(self): #FIXME this doesn't actually accept user input, I think I need to create a global dict
# to store the input and then access it from the python in some way...
self.ast_gen.ast = ET.Element("block")
self.ast_gen.current_ast_element = self.ast_gen.ast
self.ast_gen.generate_in_stream()
self.python_unparser.xml = self.ast_gen.ast
self.python_unparser.unparse()
try:
exec(self.python_unparser.source)
except Exception as e:
self.fail(e)
def test_conditional(self):
self.ast_gen.ast = ET.Element("block")
self.ast_gen.current_ast_element = self.ast_gen.ast
self.ast_gen.generate_conditional()
self.python_unparser.xml = self.ast_gen.ast
self.python_unparser.unparse()
try:
exec(self.python_unparser.source)
except Exception as e:
print(self.python_unparser.source)
self.fail(e)
def test_loop(self):
for i in range(1000):
self.ast_gen.ast = ET.Element("block")
self.ast_gen.current_ast_element = self.ast_gen.ast
self.ast_gen.generate_loop()
self.python_unparser.xml = self.ast_gen.ast
self.python_unparser.unparse()
try:
compile(self.python_unparser.source, "beeeans", "exec")
except Exception as e:
print(self.python_unparser.source)
self.fail(e)
def test_routine(self):
self.ast_gen.ast = ET.Element("block")
self.ast_gen.current_ast_element = self.ast_gen.ast
self.ast_gen.generate_routine()
self.python_unparser.xml = self.ast_gen.ast
self.python_unparser.unparse()
try:
exec(self.python_unparser.source)
except Exception as e:
print(self.python_unparser.source)
self.fail(e)
def test_program(self):
self.ast_gen.generate_ast()
self.python_unparser.xml = self.ast_gen.ast
self.python_unparser.unparse()
try:
exec(self.python_unparser.source)
except Exception as e:
print(self.python_unparser.source)
self.fail(e)
if __name__ == '__main__':
unittest.main()