2016116783 김성록
test_driver와 run_test.sh를 생성하는 프로그램 generator.py를 만들었습니다.
파이썬으로 만들어서 이해하기 힘드실까봐 설명글을 올립니다.
import sys
import os
import subprocess
import re
from random import randint
구현에 필요한 모듈들을 가져오는 부분입니다. 모두 내부 모듈을 사용하였습니다.
# read args
if len(sys.argv) != 3 or '.c' not in sys.argv[1] or '.txt' not in sys.argv[2] :
print("[Invaild Input] Please follow the form : python3 generator.py <unitfunc.c> <testcase.txt>")
argument가 form에 맞게 잘 들어왔는지 확인하는 부분입니다.
# read test_cases
with open(sys.argv[2],"r") as f :
data = f.read().split('\\n')
# split with prototype and samples
prototype = data[0]
samples = data[1:]
# split with function name and parameters
i,j = prototype.find('('),prototype.find(')')
params = prototype[i+1:j]
func_name = prototype[:i].split(' ')[-1]
테스트 케이스를 읽어서 첫 줄에 있는 prototype 과 sample을 읽어서 분리하는 부분입니다.
/ function prototype at the first line / 라고 적혀있어서
int triangle(int a, int b, int c);
의 형식으로 나타난다고 가정했습니다.
여러 에러를 가정해서 예외처리를 하긴 했지만 이 형식으로 해야 제가 의도한 결과대로 나옵니다.
# define test case templates
def test_case(func_name,case_num,params,expected) :
return '''\\
if( {}({}) == {}) printf("test case {}: pass\\\\n");
else printf("test case {}: Fail\\\\n");\\
'''.format(func_name,','.join(params),expected,case_num,case_num)
# add cases
cases = []
for s in samples :
sample = [x for x in s.split(' ') if x]
if sample :
cases.append(test_case(func_name,sample[0],sample[1:-1],sample[-1]))
test case를 생성하는 템플릿을 정의하고 test case들을 생성했습니다.