blob: 12929156bac1b72360bee8cf2a62c0b02b6aa731 [file] [log] [blame]
Tim Peters36cdad12000-12-29 02:06:45 +00001from test_support import TestFailed
2
3import bisect
4import sys
5
6nerrors = 0
7
8def check_bisect(func, list, elt, expected):
9 global nerrors
10 got = func(list, elt)
11 if got != expected:
12 print >> sys.stderr, \
13 "expected %s(%s, %s) -> %s, but got %s" % (func.__name__,
14 list,
15 elt,
16 expected,
17 got)
18 nerrors += 1
19
20# XXX optional slice arguments need tests.
21
22check_bisect(bisect.bisect_right, [], 1, 0)
23check_bisect(bisect.bisect_right, [1], 0, 0)
24check_bisect(bisect.bisect_right, [1], 1, 1)
25check_bisect(bisect.bisect_right, [1], 2, 1)
26check_bisect(bisect.bisect_right, [1, 1], 0, 0)
27check_bisect(bisect.bisect_right, [1, 1], 1, 2)
28check_bisect(bisect.bisect_right, [1, 1], 2, 2)
29check_bisect(bisect.bisect_right, [1, 1, 1], 0, 0)
30check_bisect(bisect.bisect_right, [1, 1, 1], 1, 3)
31check_bisect(bisect.bisect_right, [1, 1, 1], 2, 3)
32check_bisect(bisect.bisect_right, [1, 1, 1, 1], 0, 0)
33check_bisect(bisect.bisect_right, [1, 1, 1, 1], 1, 4)
34check_bisect(bisect.bisect_right, [1, 1, 1, 1], 2, 4)
35check_bisect(bisect.bisect_right, [1, 2], 0, 0)
36check_bisect(bisect.bisect_right, [1, 2], 1, 1)
37check_bisect(bisect.bisect_right, [1, 2], 1.5, 1)
38check_bisect(bisect.bisect_right, [1, 2], 2, 2)
39check_bisect(bisect.bisect_right, [1, 2], 3, 2)
40check_bisect(bisect.bisect_right, [1, 1, 2, 2], 0, 0)
41check_bisect(bisect.bisect_right, [1, 1, 2, 2], 1, 2)
42check_bisect(bisect.bisect_right, [1, 1, 2, 2], 1.5, 2)
43check_bisect(bisect.bisect_right, [1, 1, 2, 2], 2, 4)
44check_bisect(bisect.bisect_right, [1, 1, 2, 2], 3, 4)
45check_bisect(bisect.bisect_right, [1, 2, 3], 0, 0)
46check_bisect(bisect.bisect_right, [1, 2, 3], 1, 1)
47check_bisect(bisect.bisect_right, [1, 2, 3], 1.5, 1)
48check_bisect(bisect.bisect_right, [1, 2, 3], 2, 2)
49check_bisect(bisect.bisect_right, [1, 2, 3], 2.5, 2)
50check_bisect(bisect.bisect_right, [1, 2, 3], 3, 3)
51check_bisect(bisect.bisect_right, [1, 2, 3], 4, 3)
52check_bisect(bisect.bisect_right, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 0, 0)
53check_bisect(bisect.bisect_right, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 1, 1)
54check_bisect(bisect.bisect_right, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 1.5, 1)
55check_bisect(bisect.bisect_right, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 2, 3)
56check_bisect(bisect.bisect_right, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 2.5, 3)
57check_bisect(bisect.bisect_right, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 3, 6)
58check_bisect(bisect.bisect_right, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 3.5, 6)
59check_bisect(bisect.bisect_right, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 4, 10)
60check_bisect(bisect.bisect_right, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 5, 10)
61
62check_bisect(bisect.bisect_left, [], 1, 0)
63check_bisect(bisect.bisect_left, [1], 0, 0)
64check_bisect(bisect.bisect_left, [1], 1, 0)
65check_bisect(bisect.bisect_left, [1], 2, 1)
66check_bisect(bisect.bisect_left, [1, 1], 0, 0)
67check_bisect(bisect.bisect_left, [1, 1], 1, 0)
68check_bisect(bisect.bisect_left, [1, 1], 2, 2)
69check_bisect(bisect.bisect_left, [1, 1, 1], 0, 0)
70check_bisect(bisect.bisect_left, [1, 1, 1], 1, 0)
71check_bisect(bisect.bisect_left, [1, 1, 1], 2, 3)
72check_bisect(bisect.bisect_left, [1, 1, 1, 1], 0, 0)
73check_bisect(bisect.bisect_left, [1, 1, 1, 1], 1, 0)
74check_bisect(bisect.bisect_left, [1, 1, 1, 1], 2, 4)
75check_bisect(bisect.bisect_left, [1, 2], 0, 0)
76check_bisect(bisect.bisect_left, [1, 2], 1, 0)
77check_bisect(bisect.bisect_left, [1, 2], 1.5, 1)
78check_bisect(bisect.bisect_left, [1, 2], 2, 1)
79check_bisect(bisect.bisect_left, [1, 2], 3, 2)
80check_bisect(bisect.bisect_left, [1, 1, 2, 2], 0, 0)
81check_bisect(bisect.bisect_left, [1, 1, 2, 2], 1, 0)
82check_bisect(bisect.bisect_left, [1, 1, 2, 2], 1.5, 2)
83check_bisect(bisect.bisect_left, [1, 1, 2, 2], 2, 2)
84check_bisect(bisect.bisect_left, [1, 1, 2, 2], 3, 4)
85check_bisect(bisect.bisect_left, [1, 2, 3], 0, 0)
86check_bisect(bisect.bisect_left, [1, 2, 3], 1, 0)
87check_bisect(bisect.bisect_left, [1, 2, 3], 1.5, 1)
88check_bisect(bisect.bisect_left, [1, 2, 3], 2, 1)
89check_bisect(bisect.bisect_left, [1, 2, 3], 2.5, 2)
90check_bisect(bisect.bisect_left, [1, 2, 3], 3, 2)
91check_bisect(bisect.bisect_left, [1, 2, 3], 4, 3)
92check_bisect(bisect.bisect_left, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 0, 0)
93check_bisect(bisect.bisect_left, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 1, 0)
94check_bisect(bisect.bisect_left, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 1.5, 1)
95check_bisect(bisect.bisect_left, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 2, 1)
96check_bisect(bisect.bisect_left, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 2.5, 3)
97check_bisect(bisect.bisect_left, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 3, 3)
98check_bisect(bisect.bisect_left, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 3.5, 6)
99check_bisect(bisect.bisect_left, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 4, 6)
100check_bisect(bisect.bisect_left, [1, 2, 2, 3, 3, 3, 4, 4, 4, 4], 5, 10)
101
102def check_insort(n):
103 global nerrors
104 from random import choice
105 import sys
106 digits = "0123456789"
107 raw = []
108 insorted = []
109 for i in range(n):
110 digit = choice(digits)
111 raw.append(digit)
112 if digit in "02468":
113 f = bisect.insort_left
114 else:
115 f = bisect.insort_right
116 f(insorted, digit)
117 sorted = raw[:]
118 sorted.sort()
119 if sorted == insorted:
120 return
121 print >> sys.stderr, "insort test failed: raw %s got %s" % (raw, insorted)
122 nerrors += 1
123
124check_insort(500)
125
126if nerrors:
127 raise TestFailed("%d errors in test_bisect" % nerrors)