generate an array of random numbers with hypothesis in python

996 views Asked by At

I have a sorting algorithm that seems to work fine but I would like to test it more with random samples. So to test it, I'm trying to generate random arrays with hypothesis in Python, I would like the arrays to be of various lengths and contain any random numbers. However hypothesis seem to be stuck on filling the arrays with the digit "1" only

Here is my code:

def mysorting_function function(ArrToSort):
    #...

@given (
    i= lists( integers(min_value=1) )
    )
def test_partition(i):
    #sort the random array i
    mysorting_function(i)
    #run the test to check if it is sorted
    for i in range( len(i)-1):
        assert a[i]<= a[i+1]

However when I print the output of the test, the samples that hypothesis generates are:

[1]
[1]
[1, 1]
[1, 1, 1]
[1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1]
[1]

If I remove the min_value=1 I get array of 0, if I put min_value=2, I get arrays with nothing but 2! which is weird, I want these values to be random.

Can someone please help me and explain what I'm doing wrong? is there a random seed that I need to initialize? A missing parameter? something wrong with my imports?

Thanks in advance

p.s: I know there are other ways to generate the random list, but I want to get to work with Hypothesis please :)

1

There are 1 answers

3
Kris On BEST ANSWER
from hypothesis.strategies import lists, integers, given


def func(i):
    return sorted(i)


@given(i=lists(integers(min_value=1, max_value=1e12)))
def test_partition(i):
    a = func(i)
    for k in range(len(i) - 1):
        print(f"Given: {i}")
        assert a[k] <= a[k + 1]

Run using pytest tmp.py -s

platform darwin -- Python 3.6.12, pytest-6.0.2, py-1.9.0, pluggy-0.13.1
rootdir: /Users/krishna.singh
plugins: hypothesis-5.36.1
collected 1 item

tmp.py Given: [], Sorted: []
Given: [1], Sorted: [1]
Given: [1], Sorted: [1]
Given: [1], Sorted: [1]
Given: [1], Sorted: [1]
Given: [122661386], Sorted: [122661386]
Given: [1], Sorted: [1]
Given: [1], Sorted: [1]
Given: [1], Sorted: [1]
Given: [], Sorted: []
Given: [1], Sorted: [1]
Given: [274042271187, 26280, 3469576980, 62595], Sorted: [26280, 62595, 3469576980, 274042271187]
Given: [26280, 26280, 3469576980, 62595], Sorted: [26280, 26280, 62595, 3469576980]
Given: [131, 58771, 43, 459711760009, 22071, 2072838034, 796926885113, 970422184418, 1757], Sorted: [43, 131, 1757, 22071, 58771, 2072838034, 459711760009, 796926885113, 970422184418]
Given: [33286, 58771, 43, 459711760009, 22071, 2072838034, 796926885113, 970422184418, 1757], Sorted: [43, 1757, 22071, 33286, 58771, 2072838034, 459711760009, 796926885113, 970422184418]
Given: [225, 2063187514, 59787, 30, 19115], Sorted: [30, 225, 19115, 59787, 2063187514]
Given: [32241, 222], Sorted: [222, 32241]
Given: [32241, 222], Sorted: [222, 32241]
Given: [467990765333], Sorted: [467990765333]
Given: [467990765333, 467990765333, 4223774210, 223411433807, 56529], Sorted: [56529, 4223774210, 223411433807, 467990765333, 467990765333]
Given: [467990765333, 467990765333, 33276335, 223411433807, 56529], Sorted: [56529, 33276335, 223411433807, 467990765333, 467990765333]
Given: [467990765333, 467990765333, 4223774210, 223411433807, 56529], Sorted: [56529, 4223774210, 223411433807, 467990765333, 467990765333]
Given: [467990765333, 467990765333, 33276335, 309, 33610961], Sorted: [309, 33276335, 33610961, 467990765333, 467990765333]
Given: [467990765333, 467990765333, 33610961, 309, 33610961], Sorted: [309, 33610961, 33610961, 467990765333, 467990765333]
Given: [467990765333, 467990765333], Sorted: [467990765333, 467990765333]
Given: [59, 29978, 362493699905, 134, 58229], Sorted: [59, 134, 29978, 58229, 362493699905]
Given: [14851, 29978, 362493699905, 134, 58229], Sorted: [134, 14851, 29978, 58229, 362493699905]
Given: [14851, 29978, 362493699905, 134, 58229], Sorted: [134, 14851, 29978, 58229, 362493699905]
Given: [14851, 29978, 362493699905, 134, 58229], Sorted: [134, 14851, 29978, 58229, 362493699905]
Given: [142, 489066059452], Sorted: [142, 489066059452]
Given: [605624009221, 146, 34956, 601792711832, 57058, 36345, 11448, 28, 65312, 61727, 48367, 31458, 31641, 560287896129, 26498], Sorted: [28, 146, 11448, 26498, 31458, 31641, 34956, 36345, 48367, 57058, 61727, 65312, 560287896129, 601792711832, 605624009221]
Given: [36099, 4395827713], Sorted: [36099, 4395827713]
Given: [36099, 4395827713], Sorted: [36099, 4395827713]
Given: [36099, 4395827713, 3329724595], Sorted: [36099, 3329724595, 4395827713]
Given: [36099, 263, 4362142151], Sorted: [263, 36099, 4362142151]

I am probably doing something wrong with the printing process, but as you can see that different outputs are generated by the hypothesis library