Пример #1
0
def test_srm_solution(search_term, problem=None, run_all=False, data=None, build=True):
  if problem:
    print ('Testing problem "%s" of SRM "%s"...' % (problem, search_term))
  else:
    print ('Testing SRM "%s"...' % search_term)

  if data == None and problem == None:
    print ('ERROR: Need to specify problem when using custom test data.')
    return 1

  parsed_data = None
  if data:
    try:
      parsed_data = Executor.to_python_tuple(data)
    except:
      print ('ERROR: The custom data supplied could not be parsed.')
      raise
      return 1

  folder_name = make_folder_name(search_term)

  provider = Provider(conf.database_path)
  srm = provider.srm_for_folder(folder_name)

  if srm == None:
    print ('ERROR: Couldn\'t figure out what SRM \'%s\' belongs to.' % folder_name)
    provider.close()
    return 1

  if srm.problems == None or len(srm.problems) == 0:
    print ('Retreiving SRM data...')
    
    conn = create_conn_and_login()
    conn.fill_whole_srm(srm)
    conn.close()

    provider.insert_srm_data(srm)

  provider.close()

  problem_to_test = None
  if problem:
    for p in srm.problems:
      if p.type_name == problem:
        problem_to_test = p
        break

    if not problem_to_test:
      print ('ERROR: \'%s\' is not a valid problem name.' % problem)

  executor = Executor('cpp', srm, folder_name)
  if build or not executor.solution:
    print ('Building solution "%s":' % folder_name)

    if not executor.build():
      print ('ERROR: Failed to build, aborting.')
      return 1

  if data == None:
    num_failed = None
    if run_all:
      num_failed = executor.run_all_tests( \
        problem=problem_to_test, onstart=display_testing_start, \
        onfinish=display_test_results, onbeforerun=display_testcase_id)
    else:
      num_failed = executor.run_example_tests( \
        problem=problem_to_test, onstart=display_testing_start, \
        onfinish=display_test_results, onbeforerun=display_testcase_id)

    if num_failed == 0:
      print ('')
      print ('All tests pass')
      print ('')
    else:
      print ('')
      print ('%i tests failed' % num_failed)
      print ('')
  else:
    executor.run_test(problem_to_test, data, onstart=display_testing_start, onfinish=display_test_results)

  # finito
  return 0