Created
August 20, 2013 07:43
-
-
Save fcostin/6278253 to your computer and use it in GitHub Desktop.
answers to things you never wanted to know
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy | |
import py.test | |
MIN_SIZE = 2 # you cant have an unsorted array of size 1 ! | |
MAX_SIZE = 1000 | |
def is_sorted(a): | |
sa = numpy.sort(a) | |
return numpy.all(sa == a) | |
@py.test.fixture(params=range(MIN_SIZE, MAX_SIZE + 1)) | |
def random_non_sorted_array(request): | |
n = request.param | |
a = numpy.random.randint(0, n, n) | |
while is_sorted(a): | |
a = numpy.random.randint(0, n, n) | |
return a | |
@py.test.fixture() | |
def random_sorted_array(random_non_sorted_array): | |
return numpy.sort(random_non_sorted_array) | |
@py.test.fixture() | |
def sorted_half_the_time_array(random_non_sorted_array, random_sorted_array): | |
if numpy.random.randint(2): | |
return random_non_sorted_array | |
else: | |
return random_sorted_array | |
def test_if_ridulous_inequality_chain_works_like_is_sorted(sorted_half_the_time_array): | |
code = '<='.join(map(str, sorted_half_the_time_array)) | |
assert eval(code) == is_sorted(sorted_half_the_time_array) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment