Destide@feddit.uk to Programmer Humor@lemmy.mlEnglish · 1 month agoInfallible Codelemmy.mlimagemessage-square163linkfedilinkarrow-up1483
arrow-up1483imageInfallible Codelemmy.mlDestide@feddit.uk to Programmer Humor@lemmy.mlEnglish · 1 month agomessage-square163linkfedilink
minus-squareFrederikNJS@lemmy.ziplinkfedilinkarrow-up2·edit-21 month agoUnittest in Python, enjoy! If you pass it with a function like the one in OPs picture, you have earned it. import unittest import random class TestOddEven(unittest.TestCase): def test_is_odd(self): for _ in range(100): num = random.randint(-2**63, 2**63 - 1) odd_num = num | 1 even_num = num >> 1 << 1 self.assertTrue(is_odd(odd_num)) self.assertFalse(is_odd(even_num)) def test_is_even(self): for _ in range(100): num = random.randint(-2**63, 2**63 - 1) odd_num = num | 1 even_num = num >> 1 << 1 self.assertTrue(is_even(even_num)) self.assertFalse(is_even(odd_num)) if __name__ == '__main__': unittest.main()
minus-squareFishFace@lemmy.worldlinkfedilinkarrow-up2·1 month agoI don’t want unseeded randomness in my tests, ever. Seed the tests, and making these pass would be trivial.
minus-squareFrederikNJS@lemmy.ziplinkfedilinkarrow-up2·1 month agoThe right tool for the right job ¯\(ツ)/¯
minus-squareFishFace@lemmy.worldlinkfedilinkarrow-up2·30 days agoThe right tool here is tests at a level higher than machine code instructions that have been in CPUs since the 70s. Maybe TDD practice is not to test at this level, but every example of TDD sure tends to be something similar!
Unittest in Python, enjoy! If you pass it with a function like the one in OPs picture, you have earned it.
import unittest import random class TestOddEven(unittest.TestCase): def test_is_odd(self): for _ in range(100): num = random.randint(-2**63, 2**63 - 1) odd_num = num | 1 even_num = num >> 1 << 1 self.assertTrue(is_odd(odd_num)) self.assertFalse(is_odd(even_num)) def test_is_even(self): for _ in range(100): num = random.randint(-2**63, 2**63 - 1) odd_num = num | 1 even_num = num >> 1 << 1 self.assertTrue(is_even(even_num)) self.assertFalse(is_even(odd_num)) if __name__ == '__main__': unittest.main()
I don’t want unseeded randomness in my tests, ever.
Seed the tests, and making these pass would be trivial.
The right tool for the right job ¯\(ツ)/¯
The right tool here is tests at a level higher than machine code instructions that have been in CPUs since the 70s. Maybe TDD practice is not to test at this level, but every example of TDD sure tends to be something similar!