464
submitted 2 months ago by sirico@feddit.uk to c/programmerhumor@lemmy.ml
you are viewing a single comment's thread
view the rest of the comments
[-] FrederikNJS@lemmy.zip 2 points 2 months ago* (last edited 2 months ago)

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()
[-] FishFace@lemmy.world 2 points 2 months ago

I don't want unseeded randomness in my tests, ever.

Seed the tests, and making these pass would be trivial.

[-] FrederikNJS@lemmy.zip 2 points 2 months ago

The right tool for the right job ¯\(ツ)

[-] FishFace@lemmy.world 2 points 2 months ago

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!

this post was submitted on 15 Jul 2025
464 points (94.8% liked)

Programmer Humor

38555 readers
140 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 6 years ago
MODERATORS