(self)
| 41 | |
| 42 | class TestBase(unittest.TestCase): |
| 43 | def testStressfully(self): |
| 44 | # Try a variety of sizes at and around powers of 2, and at powers of 10. |
| 45 | sizes = [0] |
| 46 | for power in range(1, 10): |
| 47 | n = 2 ** power |
| 48 | sizes.extend(range(n-1, n+2)) |
| 49 | sizes.extend([10, 100, 1000]) |
| 50 | |
| 51 | class Complains(object): |
| 52 | maybe_complain = True |
| 53 | |
| 54 | def __init__(self, i): |
| 55 | self.i = i |
| 56 | |
| 57 | def __lt__(self, other): |
| 58 | if Complains.maybe_complain and random.random() < 0.001: |
| 59 | if verbose: |
| 60 | print(" complaining at", self, other) |
| 61 | raise RuntimeError |
| 62 | return self.i < other.i |
| 63 | |
| 64 | def __repr__(self): |
| 65 | return "Complains(%d)" % self.i |
| 66 | |
| 67 | class Stable(object): |
| 68 | def __init__(self, key, i): |
| 69 | self.key = key |
| 70 | self.index = i |
| 71 | |
| 72 | def __lt__(self, other): |
| 73 | return self.key < other.key |
| 74 | |
| 75 | def __repr__(self): |
| 76 | return "Stable(%d, %d)" % (self.key, self.index) |
| 77 | |
| 78 | for n in sizes: |
| 79 | x = list(range(n)) |
| 80 | if verbose: |
| 81 | print("Testing size", n) |
| 82 | |
| 83 | s = x[:] |
| 84 | check("identity", x, s) |
| 85 | |
| 86 | s = x[:] |
| 87 | s.reverse() |
| 88 | check("reversed", x, s) |
| 89 | |
| 90 | s = x[:] |
| 91 | random.shuffle(s) |
| 92 | check("random permutation", x, s) |
| 93 | |
| 94 | y = x[:] |
| 95 | y.reverse() |
| 96 | s = x[:] |
| 97 | check("reversed via function", y, s, lambda a, b: (b>a)-(b<a)) |
| 98 | |
| 99 | if verbose: |
| 100 | print(" Checking against an insane comparison function.") |
nothing calls this directly
no test coverage detected