Benjamin Peterson | d7b0eeb | 2009-07-19 20:18:21 +0000 | [diff] [blame^] | 1 | """Various utility functions.""" |
| 2 | |
| 3 | def strclass(cls): |
| 4 | return "%s.%s" % (cls.__module__, cls.__name__) |
| 5 | |
| 6 | def sorted_list_difference(expected, actual): |
| 7 | """Finds elements in only one or the other of two, sorted input lists. |
| 8 | |
| 9 | Returns a two-element tuple of lists. The first list contains those |
| 10 | elements in the "expected" list but not in the "actual" list, and the |
| 11 | second contains those elements in the "actual" list but not in the |
| 12 | "expected" list. Duplicate elements in either input list are ignored. |
| 13 | """ |
| 14 | i = j = 0 |
| 15 | missing = [] |
| 16 | unexpected = [] |
| 17 | while True: |
| 18 | try: |
| 19 | e = expected[i] |
| 20 | a = actual[j] |
| 21 | if e < a: |
| 22 | missing.append(e) |
| 23 | i += 1 |
| 24 | while expected[i] == e: |
| 25 | i += 1 |
| 26 | elif e > a: |
| 27 | unexpected.append(a) |
| 28 | j += 1 |
| 29 | while actual[j] == a: |
| 30 | j += 1 |
| 31 | else: |
| 32 | i += 1 |
| 33 | try: |
| 34 | while expected[i] == e: |
| 35 | i += 1 |
| 36 | finally: |
| 37 | j += 1 |
| 38 | while actual[j] == a: |
| 39 | j += 1 |
| 40 | except IndexError: |
| 41 | missing.extend(expected[i:]) |
| 42 | unexpected.extend(actual[j:]) |
| 43 | break |
| 44 | return missing, unexpected |