test.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/python
  2. #! -*- coding: utf8 -*-
  3. import lorun
  4. import os
  5. RESULT_STR = [
  6. 'Accepted',
  7. 'Presentation Error',
  8. 'Time Limit Exceeded',
  9. 'Memory Limit Exceeded',
  10. 'Wrong Answer',
  11. 'Runtime Error',
  12. 'Output Limit Exceeded',
  13. 'Compile Error',
  14. 'System Error'
  15. ]
  16. def compileSrc(src_path):
  17. if os.system('python -m py_compile %s' % src_path) != 0:
  18. print('compile failure!')
  19. return False
  20. return True
  21. def runone(p_path, in_path, out_path):
  22. fin = open(in_path)
  23. ftemp = open('temp.out', 'w')
  24. runcfg = {
  25. 'args':['python ./add.py'],
  26. 'fd_in':fin.fileno(),
  27. 'fd_out':ftemp.fileno(),
  28. 'timelimit':1000, #in MS
  29. 'memorylimit':20000, #in KB
  30. }
  31. rst = lorun.run(runcfg)
  32. fin.close()
  33. ftemp.close()
  34. if rst['result'] == 0:
  35. ftemp = open('temp.out')
  36. fout = open(out_path)
  37. crst = lorun.check(fout.fileno(), ftemp.fileno())
  38. fout.close()
  39. ftemp.close()
  40. os.remove('temp.out')
  41. if crst != 0:
  42. return {'result':crst}
  43. return rst
  44. def judge(src_path, td_path, td_total):
  45. if not compileSrc(src_path):
  46. return
  47. for i in range(td_total):
  48. in_path = os.path.join(td_path, '%d.in'%i)
  49. out_path = os.path.join(td_path, '%d.out'%i)
  50. if os.path.isfile(in_path) and os.path.isfile(out_path):
  51. rst = runone('./m', in_path, out_path)
  52. rst['result'] = RESULT_STR[rst['result']]
  53. print(rst)
  54. else:
  55. print('testdata:%d incompleted' % i)
  56. os.remove('./m')
  57. exit(-1)
  58. os.remove('./m')
  59. if __name__ == '__main__':
  60. import sys
  61. if len(sys.argv) != 4:
  62. print('Usage:%s srcfile testdata_pth testdata_total')
  63. exit(-1)
  64. judge(sys.argv[1], sys.argv[2], int(sys.argv[3]))