File: test_listsort.py

package info (click to toggle)
pypy 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 107,216 kB
  • sloc: python: 1,201,787; ansic: 62,419; asm: 5,169; cpp: 3,017; sh: 2,534; makefile: 545; xml: 243; lisp: 45; awk: 4
file content (41 lines) | stat: -rw-r--r-- 1,026 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import py
from rpython.rlib.listsort import TimSort
import random, os

def makeset(lst):
    result = {}
    for a in lst:
        result.setdefault(id(a), []).append(True)
    return result

def sorttest(lst1):
    lst2 = lst1[:]
    TimSort(lst2).sort()
    assert len(lst1) == len(lst2)
    assert makeset(lst1) == makeset(lst2)
    position = {}
    i = 0
    for a in lst1:
        position.setdefault(id(a), []).append(i)
        i += 1
    for i in range(len(lst2)-1):
        a, b = lst2[i], lst2[i+1]
        assert a <= b, "resulting list is not sorted"
        if a == b:
            assert position[id(a)][0] < position[id(b)][-1], "not stable"


class C(int):
    pass

def test_v():
    for v in range(137):
        up = 1 + int(v * random.random() * 2.7)
        lst1 = [C(random.randrange(0, up)) for i in range(v)]
        sorttest(lst1)

def test_file():
    for fn in py.path.local(__file__).dirpath().listdir():
        if fn.ext == '.py': 
            lines1 = fn.readlines()
            sorttest(lines1)