import re


def _RunAndNotrunTests(stdio_tests, user_tests):
  """Return lists of run and not-run instances of given user tests.

  The first list is of test instances present in the |stdio_tests| list.
  Presence indicates that the test instance was run on the build. The second
  list is tests that are absent from the |stdio_tests| list. Absence means
  that no instance of the test was run on the build.

  Note that there can be multiple instances of a user-specified test run on a
  build if a) The test belongs to test group, and b) the test was run with
  multiple test data values. This function uses a regex to search for multiple
  instances of tests that match the user-specifed test name.

  Args:
    stdio_tests: List of test instances run on build.
    user_tests: List of test names specified by user.

  Returns:
    1) run_tests: list of test instances of user tests run on build.
    2) notrun_tests: list of user tests not run on build.
  """
  run_user_tests = []
  notrun_user_tests = []
  for user_test in user_tests:
    pattern = r'(.*/)?%s(/\d*)?$' % user_test  # pattern for test name.
    found_run_test = False
    for stdio_test in stdio_tests:
      if re.search(pattern, stdio_test):
        found_run_test = True
        run_user_tests.append(stdio_test)
    if not found_run_test:
      notrun_user_tests.append(user_test)
  print('  User tests: Instances run: %s' % len(run_user_tests))
  print('  User tests: Not run: %s\n' % len(notrun_user_tests))
  return run_user_tests, notrun_user_tests