Skip to content
Snippets Groups Projects
Select Git revision
  • eda69ca6d71e79d96218c211dfd529768c72965d
  • main default protected
2 results

fuzz_find_smallest_positive.py

Blame
  • fuzz_find_smallest_positive.py 1.21 KiB
    from math import inf
    from random import randrange, choice
    
    
    SUITE_TEMPLATE = '''
    from math import inf
    from unittest import TestCase
    from main import SmallestPositiveApp
    
    
    class Test(TestCase):
    {tests}
    '''[1:-1]
    
    TEST_TEMPLATE = '''
        def test_parse_{number}(self):
            inputs = {inputs}
            expected = {expected}
            actual = SmallestPositiveApp._find_smallest_positive(inputs)
            self.assertEqual(actual, expected)
    '''[1:]
    
    VALUE_LIMIT = 100
    PADDING_LIMIT = 5
    
    
    def generate_padding(smallest_positive):
        result = []
        for _ in range(randrange(PADDING_LIMIT)):
            options = [
                randrange(-VALUE_LIMIT, 0),
                0,
            ]
            if smallest_positive < VALUE_LIMIT:
                options.append(randrange(smallest_positive, VALUE_LIMIT + 1))
            result.append(choice(options))
        return result
    
    
    def generate_random_test(number):
        smallest_positive = choice((randrange(1, VALUE_LIMIT), inf))
        inputs = [smallest_positive]  # TODO
        return TEST_TEMPLATE.format(number=number, inputs=inputs, expected=smallest_positive)
    
    
    def main():
        print(SUITE_TEMPLATE.format(tests='\n'.join(generate_random_test(number) for number in range(100))), end='')
    
    
    if __name__ == '__main__':
        main()