def decode_results(tests):
  """Decode test results and generates failures if any failures exist.

  Each test has an expected result, an actual result, and a flag indicating
  whether the test harness considered the result unexpected. For example, an
  individual test result might look like:

  .. code:: python

    {
      'actual': 'PASS foobar',
      'expected': 'foobar',
      'is_unexpected': True,
    }

  A result is considered a pass if the actual result is just the string 'PASS'.
  A result is considered a flake if the actual result has a value attached and
  the value appears in the expected values. All other kinds of results are
  considered failures. The test result given above is an example of a flake
  result.
  """
  failures = {}
  for (test, result) in tests.iteritems():
    if result.get('is_unexpected'):
      actual_results = result['actual'].split()
      expected_results = result['expected'].split()
      if len(actual_results) > 1:
        if actual_results[1] not in expected_results:
          # We report the first failure type back, even if the second
          # was more severe.
          failures[test] = actual_results[0]
      elif actual_results[0] != 'PASS':
        failures[test] = actual_results[0]  # pragma: no cover

  return failures