TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Paste a block of code here and we will generate a series of unit tests

3 点作者 oliviergg超过 2 年前

2 条评论

rjvs超过 2 年前
What privacy policies and governance applies to code pasted into your form?
eesmith超过 2 年前
I tried it on Bob Martin&#x27;s &quot;Prime Factors&quot; Kata solution, translated into Python:<p><pre><code> def prime_factors(n: int): primes = [] candidate = 2 while n &gt; 1: while n % candidate == 0: primes.append(candidate) n &#x2F;&#x2F;= candidate candidate += 1 return primes </code></pre> It gave a decent initial unit test:<p><pre><code> import unittest class TestPrimeFactors(unittest.TestCase): def test_prime_factors(self): self.assertEqual(prime_factors(4), [2, 2]) self.assertEqual(prime_factors(9), [3, 3]) self.assertEqual(prime_factors(12), [2, 2, 3]) self.assertEqual(prime_factors(17), [17]) self.assertEqual(prime_factors(24), [2, 2, 2, 3]) if __name__ == &#x27;__main__&#x27;: unittest.main() </code></pre> Well done! It generate cases with duplicates factors, which is the biggest likely mistake in this algorithm. (It&#x27;s also possible to use &quot;&#x2F;&quot; instead of &quot;&#x2F;&#x2F;&quot;, but that doesn&#x27;t cause a failure until working with values above 2^53 or so, like 3^53.)<p>It&#x27;s not done in Martin&#x27;s one-test-per-test-case style, but that&#x27;s fine - I prefer this more compact form.<p>I chose this Kata because it&#x27;s very simple, and lets us examine what the generated test suite doesn&#x27;t cover.<p>Like, there&#x27;s no test for 1, 0, or negative number, which will return an empty list.<p>We can use hypothesis to help identify that case:<p><pre><code> import math from hypothesis import example, given, strategies as st @given(st.integers()) def test_product(n): factors = prime_factors(n) assert math.prod(factors) == n if __name__ == &#x27;__main__&#x27;: test_product() </code></pre> reporting<p><pre><code> assert math.prod(factors) == n AssertionError Falsifying example: test_product( n=0, ) </code></pre> With st.integers(min_value=2) we then find a performance issue. It tries to factor 5123983848966349269, but Martin&#x27;s algorithm is extremely slow at computing [3, 7, 1049, 90631, 2566470031] because it must count to 2566470031, in Python, by ones.<p>The upper-bound was never specified in Martin&#x27;s formulation of the Kata. It was written in Java using integers, so the Mersenne prime 2^31-1 is allowed, but even that will take a long time to compute.<p>Using a max_value of 1,000,000 has Hypothesis finish with no other reported problems.<p>I think all that&#x27;s needed is an extra bounds check:<p><pre><code> if not (2 &lt;= n &lt;= 1_000_000): raise ValueError(&quot;out of range&quot;) </code></pre> and corresponding test:<p><pre><code> def test_bounds(self): for n in (-1, 0, 1, 1_000_001): with self.assertRaisesRegex(ValueError, &quot;out of range&quot;): prime_factors(n) </code></pre> Mutation testing (with mutmut) shows no further issues.<p>Again, well done on coming up with a good set of initial cases, automatically.