|
54 | 54 | " \"\"\"\n",
|
55 | 55 | " This is our prototype shrink function. It is very bad. It makes the\n",
|
56 | 56 | " mistake of only making very small changes to an example each time.\n",
|
57 |
| - " \n", |
| 57 | + "\n", |
58 | 58 | " Most people write something like this the first time they come to\n",
|
59 | 59 | " implement example shrinking. In particular early Hypothesis very much\n",
|
60 | 60 | " made this mistake.\n",
|
61 |
| - " \n", |
| 61 | + "\n", |
62 | 62 | " What this does:\n",
|
63 |
| - " \n", |
| 63 | + "\n", |
64 | 64 | " For each index, if the value of the index is non-zero we try\n",
|
65 | 65 | " decrementing it by 1.\n",
|
66 |
| - " \n", |
| 66 | + "\n", |
67 | 67 | " We then (regardless of if it's zero) try the list with the value at\n",
|
68 | 68 | " that index deleted.\n",
|
69 | 69 | " \"\"\"\n",
|
|
88 | 88 | " \"\"\"\n",
|
89 | 89 | " This is a debug function. You shouldn't concern yourself with\n",
|
90 | 90 | " its implementation too much.\n",
|
91 |
| - " \n", |
| 91 | + "\n", |
92 | 92 | " What it does is print out every intermediate step in applying a\n",
|
93 | 93 | " simplifier (a function of the form (list, constraint) -> list)\n",
|
94 | 94 | " along with whether it is a successful shrink or not.\n",
|
|
193 | 193 | " s = list(ls)\n",
|
194 | 194 | " del s[i]\n",
|
195 | 195 | " yield list(s)\n",
|
196 |
| - " \n", |
| 196 | + "\n", |
197 | 197 | " for i in range(len(ls)):\n",
|
198 | 198 | " for x in range(ls[i]):\n",
|
199 | 199 | " s = list(ls)\n",
|
|
1285 | 1285 | "def shrink_integer(n):\n",
|
1286 | 1286 | " \"\"\"\n",
|
1287 | 1287 | " Shrinker for individual integers.\n",
|
1288 |
| - " \n", |
| 1288 | + "\n", |
1289 | 1289 | " What happens is that we start from the left, first probing upwards in powers of two.\n",
|
1290 |
| - " \n", |
| 1290 | + "\n", |
1291 | 1291 | " When this would take us past our target value we then binary chop towards it.\n",
|
1292 | 1292 | " \"\"\"\n",
|
1293 | 1293 | " if not n:\n",
|
|
1540 | 1540 | " s = list(ls)\n",
|
1541 | 1541 | " s[i] = x\n",
|
1542 | 1542 | " yield s\n",
|
1543 |
| - " \n", |
| 1543 | + "\n", |
1544 | 1544 | "def shrink4(ls):\n",
|
1545 | 1545 | " yield from shrink_to_prefix(ls)\n",
|
1546 | 1546 | " yield from delete_individual_elements(ls)\n",
|
1547 |
| - " yield from shrink_individual_elements(ls) " |
| 1547 | + " yield from shrink_individual_elements(ls)" |
1548 | 1548 | ]
|
1549 | 1549 | },
|
1550 | 1550 | {
|
|
1864 | 1864 | " \"\"\"\n",
|
1865 | 1865 | " Look for all sets of shared indices and try to perform a simultaneous shrink on\n",
|
1866 | 1866 | " their value, replacing all of them at once.\n",
|
1867 |
| - " \n", |
| 1867 | + "\n", |
1868 | 1868 | " In actual Hypothesis we also try replacing only subsets of the values when there\n",
|
1869 | 1869 | " are more than two shared values, but we won't worry about that here.\n",
|
1870 | 1870 | " \"\"\"\n",
|
|
2573 | 2573 | " counts = []\n",
|
2574 | 2574 | "\n",
|
2575 | 2575 | " for ex in dataset:\n",
|
2576 |
| - " counter = [0]\n", |
2577 |
| - " \n", |
| 2576 | + " counter = 0\n", |
| 2577 | + "\n", |
2578 | 2578 | " def run_and_count(ls):\n",
|
2579 |
| - " counter[0] += 1\n", |
2580 |
| - " if counter[0] > MAX_COUNT:\n", |
2581 |
| - " raise MaximumCountExceeded()\n", |
| 2579 | + " nonlocal counter\n", |
| 2580 | + " counter += 1\n", |
| 2581 | + " if counter > MAX_COUNT:\n", |
| 2582 | + " raise MaximumCountExceeded\n", |
2582 | 2583 | " return constraint(ls)\n",
|
2583 |
| - " \n", |
| 2584 | + "\n", |
2584 | 2585 | " try:\n",
|
2585 | 2586 | " simplifier(ex, run_and_count)\n",
|
2586 |
| - " counts.extend(counter)\n", |
| 2587 | + " counts.append(counter)\n", |
2587 | 2588 | " except MaximumCountExceeded:\n",
|
2588 | 2589 | " counts.append(MAX_COUNT + 1)\n",
|
2589 | 2590 | " break\n",
|
2590 | 2591 | " return counts\n",
|
2591 |
| - " \n", |
| 2592 | + "\n", |
2592 | 2593 | "def worst_case(condition, simplifier):\n",
|
2593 | 2594 | " return max(call_counts(condition, simplifier))\n",
|
2594 | 2595 | "\n",
|
|
2619 | 2620 | " for h in header:\n",
|
2620 | 2621 | " html_fragments.append(\"<th>%s</th>\" % (h,))\n",
|
2621 | 2622 | " html_fragments.append(\"</tr>\\n</thead>\\n<tbody>\")\n",
|
2622 |
| - " \n", |
| 2623 | + "\n", |
2623 | 2624 | " for name in conditions:\n",
|
2624 |
| - " bits = [name.replace(\">\", \">\")] \n", |
| 2625 | + " bits = [name.replace(\">\", \">\")]\n", |
2625 | 2626 | " for _, simplifier in named_simplifiers:\n",
|
2626 | 2627 | " value = worst_case(name, simplifier)\n",
|
2627 | 2628 | " if value <= MAX_COUNT:\n",
|
|
4534 | 4535 | " (\"Single pass\", partial(greedy_shrink_with_dedupe,\n",
|
4535 | 4536 | " shrink=shrink6)),\n",
|
4536 | 4537 | " (\"Multi pass\", multicourse_shrink3)\n",
|
4537 |
| - " \n", |
| 4538 | + "\n", |
4538 | 4539 | "])"
|
4539 | 4540 | ]
|
4540 | 4541 | },
|
|
4622 | 4623 | " (\"Single pass\", partial(greedy_shrink_with_dedupe,\n",
|
4623 | 4624 | " shrink=shrink6)),\n",
|
4624 | 4625 | " (\"Multi pass\", multicourse_shrink3)\n",
|
4625 |
| - " \n", |
| 4626 | + "\n", |
4626 | 4627 | "])"
|
4627 | 4628 | ]
|
4628 | 4629 | },
|
|
4731 | 4732 | " (\"Single pass\", partial(greedy_shrink_with_dedupe,\n",
|
4732 | 4733 | " shrink=shrink6)),\n",
|
4733 | 4734 | " (\"Multi pass\", multicourse_shrink3),\n",
|
4734 |
| - " (\"Multi pass with restart\", multicourse_shrink4) \n", |
4735 |
| - " \n", |
| 4735 | + " (\"Multi pass with restart\", multicourse_shrink4)\n", |
| 4736 | + "\n", |
4736 | 4737 | "])"
|
4737 | 4738 | ]
|
4738 | 4739 | },
|
|
4871 | 4872 | "compare_simplifiers([\n",
|
4872 | 4873 | " (\"Single pass\", partial(greedy_shrink_with_dedupe,\n",
|
4873 | 4874 | " shrink=shrink6)),\n",
|
4874 |
| - " (\"Multi pass\", multicourse_shrink3), \n", |
| 4875 | + " (\"Multi pass\", multicourse_shrink3),\n", |
4875 | 4876 | " (\"Multi pass with restart\", multicourse_shrink4),\n",
|
4876 |
| - " (\"Multi pass with variable restart\", multicourse_shrink5) \n", |
| 4877 | + " (\"Multi pass with variable restart\", multicourse_shrink5)\n", |
4877 | 4878 | "])"
|
4878 | 4879 | ]
|
4879 | 4880 | },
|
|
0 commit comments