Esempio n. 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
Esempio n. 2
0
class PracticingContext(ContextBase):

  COMPILE_LABEL = 'Compile'
  RUN_EXAMPLES_LABEL = 'Run Example Tests'
  RUN_ALL_LABEL = 'Run All Tests'
  RUN_CUSTOM_LABEL = 'Run Custom Test'

  def __init__(self, console, conf, lang, srm, folder_path):
    ContextBase.__init__(self, console)

    self._conf = conf
    self._srm = srm
    self._executor = Executor(lang, srm, folder_path)

  @property
  def menu_items(self):
    return [
      {'label': PracticingContext.COMPILE_LABEL, 'action': self.compile},
      {'label': PracticingContext.RUN_EXAMPLES_LABEL, 'action': self.run_examples},
      {'label': PracticingContext.RUN_ALL_LABEL, 'action': self.run_all},
      {'label': PracticingContext.RUN_CUSTOM_LABEL, 'action': self.run_custom}
    ]

  @TrainerConsole.action(status_text = '> Building...\n', async = True)
  def compile(self, tab):
    def on_done():
      self.console.idle_print_output('> Done\n')
      self.console.idle_finish()

    problem = self._srm.problems[tab.problem_idx]

    self._executor.build( \
      on_output = self.console.idle_print_output, \
      on_error = self.console.idle_print_error, \
      on_done = on_done, \
      defines = ['HARNESS_BUILD_' + problem.type_name])

  @TrainerConsole.action(status_text = '> Running example tests...\n')
  def run_examples(self, tab):
    problem = self._srm.problems[tab.problem_idx]

    self._executor.run_example_tests( \
      problem, \
      onstart = self._on_start, \
      onfinish = self._on_finish, \
      onbeforerun = self._on_before_run)

    self.console.idle_print_output('> Done\n')

  @TrainerConsole.action(status_text = '> Running all tests...\n')
  def run_all(self, tab):
    # TODO: need to let user know if there are not test cases (because there was no winner)
    problem = self._srm.problems[tab.problem_idx]

    num_failed = self._executor.run_all_tests( \
      problem, \
      onstart = self._on_start, \
      onfinish = self._on_finish, \
      onbeforerun = self._on_before_run)
    print num_failed

    if num_failed == 0:
      GObject.idle_add(lambda: self._set_current_problem_solved(problem))

    self.console.idle_print_output('> Done\n')

  def _set_current_problem_solved(self, problem):
    if problem.solved:
      return

    provider = None
    try:
      provider = Provider(self._conf.database_path)
      provider.set_problem_solved(problem)
    finally:
      if provider:
        provider.close()

  @TrainerConsole.action( \
    status_text = '> Running a test with custom data...\n', \
    inputs = [('Enter the data:', lambda self, txt, tab: self.validate_problem_args(txt, tab))])
  def run_custom(self, tab, data):
    problem = self._srm.problems[tab.problem_idx]

    self._executor.run_test( \
      problem, \
      data, \
      onstart = self._on_start, \
      onfinish = self._on_finish, \
      onbeforerun = self._on_before_run)

    self.console.idle_print_output('> Done\n')

  @property
  def srm(self):
    return self._srm

  def _on_start(self, p):
    self.console.idle_print_output('Testing problem \'%s\'...\n' % p.type_name)

  def _on_finish(self, success, result, time, error, inp, expected):
    FAILED_EXCEPTION_MSG = '''***failed***
  Input: %s
  Exception Thrown: %s
'''  

    FAILED_OUTPUT_MSG = '''***failed*** Time: %sms
  Input: %s
  Output: %s
  Expected: %s
'''

    SUCCESS_MSG = '''***success***
  Input: %s
  Output: %s
'''

    inp_str = str(inp).strip()

    if error:
      self.console.idle_print_error(FAILED_EXCEPTION_MSG % (inp_str, str(error)))
    elif not success:
      self.console.idle_print_error(FAILED_OUTPUT_MSG % ( \
        str(time / 1000.0), inp_str, str(result), str(expected)))
    else:
      self.console.idle_print_success(SUCCESS_MSG % (inp_str, str(result)))

  def _on_before_run(self, tc):
    if tc.idx != -1:
      self.console.idle_print_output('Testcase \'%i\'\n' % tc.idx)
    else:
      self.console.idle_print_output('System Testcase\n')