Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(80550)

Side by Side Diff: Lib/test/test_bisect.py

Issue 16510: Using appropriate checks in tests
Patch Set: Created 5 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_binop.py ('k') | Lib/test/test_buffer.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 import sys 1 import sys
2 import unittest 2 import unittest
3 from test import support 3 from test import support
4 from collections import UserList 4 from collections import UserList
5 5
6 py_bisect = support.import_fresh_module('bisect', blocked=['_bisect']) 6 py_bisect = support.import_fresh_module('bisect', blocked=['_bisect'])
7 c_bisect = support.import_fresh_module('bisect', fresh=['_bisect']) 7 c_bisect = support.import_fresh_module('bisect', fresh=['_bisect'])
8 8
9 class Range(object): 9 class Range(object):
10 """A trivial range()-like object that has an insert() method.""" 10 """A trivial range()-like object that has an insert() method."""
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 self.assertEqual(data.last_insert, (x + 1, x)) 152 self.assertEqual(data.last_insert, (x + 1, x))
153 153
154 def test_random(self, n=25): 154 def test_random(self, n=25):
155 from random import randrange 155 from random import randrange
156 for i in range(n): 156 for i in range(n):
157 data = [randrange(0, n, 2) for j in range(i)] 157 data = [randrange(0, n, 2) for j in range(i)]
158 data.sort() 158 data.sort()
159 elem = randrange(-1, n+1) 159 elem = randrange(-1, n+1)
160 ip = self.module.bisect_left(data, elem) 160 ip = self.module.bisect_left(data, elem)
161 if ip < len(data): 161 if ip < len(data):
162 self.assertTrue(elem <= data[ip]) 162 self.assertLessEqual(elem, data[ip])
163 if ip > 0: 163 if ip > 0:
164 self.assertTrue(data[ip-1] < elem) 164 self.assertLess(data[ip-1], elem)
165 ip = self.module.bisect_right(data, elem) 165 ip = self.module.bisect_right(data, elem)
166 if ip < len(data): 166 if ip < len(data):
167 self.assertTrue(elem < data[ip]) 167 self.assertLess(elem, data[ip])
168 if ip > 0: 168 if ip > 0:
169 self.assertTrue(data[ip-1] <= elem) 169 self.assertLessEqual(data[ip-1], elem)
170 170
171 def test_optionalSlicing(self): 171 def test_optionalSlicing(self):
172 for func, data, elem, expected in self.precomputedCases: 172 for func, data, elem, expected in self.precomputedCases:
173 for lo in range(4): 173 for lo in range(4):
174 lo = min(len(data), lo) 174 lo = min(len(data), lo)
175 for hi in range(3,8): 175 for hi in range(3,8):
176 hi = min(len(data), hi) 176 hi = min(len(data), hi)
177 ip = func(data, elem, lo, hi) 177 ip = func(data, elem, lo, hi)
178 self.assertTrue(lo <= ip <= hi) 178 self.assertTrue(lo <= ip <= hi)
179 if func is self.module.bisect_left and ip < hi: 179 if func is self.module.bisect_left and ip < hi:
180 self.assertTrue(elem <= data[ip]) 180 self.assertLessEqual(elem, data[ip])
181 if func is self.module.bisect_left and ip > lo: 181 if func is self.module.bisect_left and ip > lo:
182 self.assertTrue(data[ip-1] < elem) 182 self.assertLess(data[ip-1], elem)
183 if func is self.module.bisect_right and ip < hi: 183 if func is self.module.bisect_right and ip < hi:
184 self.assertTrue(elem < data[ip]) 184 self.assertLess(elem, data[ip])
185 if func is self.module.bisect_right and ip > lo: 185 if func is self.module.bisect_right and ip > lo:
186 self.assertTrue(data[ip-1] <= elem) 186 self.assertLessEqual(data[ip-1], elem)
187 self.assertEqual(ip, max(lo, min(hi, expected))) 187 self.assertEqual(ip, max(lo, min(hi, expected)))
188 188
189 def test_backcompatibility(self): 189 def test_backcompatibility(self):
190 self.assertEqual(self.module.bisect, self.module.bisect_right) 190 self.assertEqual(self.module.bisect, self.module.bisect_right)
191 191
192 def test_keyword_args(self): 192 def test_keyword_args(self):
193 data = [10, 20, 30, 40, 50] 193 data = [10, 20, 30, 40, 50]
194 self.assertEqual(self.module.bisect_left(a=data, x=25, lo=1, hi=3), 2) 194 self.assertEqual(self.module.bisect_left(a=data, x=25, lo=1, hi=3), 2)
195 self.assertEqual(self.module.bisect_right(a=data, x=25, lo=1, hi=3), 2) 195 self.assertEqual(self.module.bisect_right(a=data, x=25, lo=1, hi=3), 2)
196 self.assertEqual(self.module.bisect(a=data, x=25, lo=1, hi=3), 2) 196 self.assertEqual(self.module.bisect(a=data, x=25, lo=1, hi=3), 2)
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 class TestDocExamplePython(TestDocExample, unittest.TestCase): 319 class TestDocExamplePython(TestDocExample, unittest.TestCase):
320 module = py_bisect 320 module = py_bisect
321 321
322 class TestDocExampleC(TestDocExample, unittest.TestCase): 322 class TestDocExampleC(TestDocExample, unittest.TestCase):
323 module = c_bisect 323 module = c_bisect
324 324
325 #------------------------------------------------------------------------------ 325 #------------------------------------------------------------------------------
326 326
327 if __name__ == "__main__": 327 if __name__ == "__main__":
328 unittest.main() 328 unittest.main()
OLDNEW
« no previous file with comments | « Lib/test/test_binop.py ('k') | Lib/test/test_buffer.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+