Source

sorting_network / tests.py

from sorting_net import *

class Test_mergesort(object):

    def test_half_cleaner(self):
        l = half_cleaner([0, 0, 1, 1, 1, 0, 0, 0])
        assert l == [0, 0, 0, 0, 1, 0, 1, 1]

    def test_bitonic_sorter(self):
        l = bitonic_sorter([0, 0, 1, 1, 1, 0, 0, 0])
        assert l == [0, 0, 0, 0, 0, 1, 1, 1]

    def test_merger(self):
        l = merger([0, 0, 1, 1, 0, 1, 1, 1])
        assert l == [0, 0, 0, 1, 1, 1, 1, 1]

    def test_comparator1(self):
        a = -1; b = 1
        assert comparator(a, b)[0] == -1

    def test_sorter(self):
        seq = [5, 9, 2, 1, 2, 3, 4, 5, 1, 0, 2, 4, 1, 2, 3, 4]
        l = sorter(seq)
        assert l == sorted(seq)

    def test_big_seq(self):
        import random
        r = random.Random()
        seq = []
        add = seq.append
        for i in xrange(2 ** 10):
            add(r.randint(0, 1000))
        l = sorter(seq)
        assert l == sorted(seq)