[Python-checkins] python/dist/src/Lib/test test_bisect.py,1.3,1.4

rhettinger@users.sourceforge.net rhettinger@users.sourceforge.net
Thu, 16 Jan 2003 04:31:38 -0800


Update of /cvsroot/python/python/dist/src/Lib/test
In directory sc8-pr-cvs1:/tmp/cvs-serv4539

Modified Files:
	test_bisect.py 
Log Message:
Added doctest for examples in the library reference.
Added random test from bisect to augment the finite precomputed checks.



Index: test_bisect.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_bisect.py,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** test_bisect.py	16 Jan 2003 12:02:35 -0000	1.3
--- test_bisect.py	16 Jan 2003 12:31:36 -0000	1.4
***************
*** 94,97 ****
--- 94,114 ----
              self.assertEqual(func(list, elt), expected)
  
+     def test_random(self, n=20):
+         from random import randrange
+         for i in xrange(n):
+             data = [randrange(0, n, 2) for j in xrange(i)]
+             data.sort()
+             elem = randrange(n)
+             ip = bisect_left(data, elem)
+             if ip < len(data):
+                 self.failUnless(elem <= data[ip])
+             if ip > 0:
+                 self.failUnless(data[ip-1] < elem)
+             ip = bisect_right(data, elem)
+             if ip < len(data):
+                 self.failUnless(elem < data[ip])
+             if ip > 0:
+                 self.failUnless(data[ip-1] <= elem)
+ 
  #==============================================================================
  
***************
*** 117,120 ****
--- 134,177 ----
  #==============================================================================
  
+ libreftest = """
+ Example from the Library Reference:  Doc/lib/libbisect.tex
+ 
+ The bisect() function is generally useful for categorizing numeric data.
+ This example uses bisect() to look up a letter grade for an exam total
+ (say) based on a set of ordered numeric breakpoints: 85 and up is an `A',
+ 75..84 is a `B', etc.
+ 
+     >>> grades = "FEDCBA"
+     >>> breakpoints = [30, 44, 66, 75, 85]
+     >>> from bisect import bisect
+     >>> def grade(total):
+     ...           return grades[bisect(breakpoints, total)]
+     ...
+     >>> grade(66)
+     'C'
+     >>> map(grade, [33, 99, 77, 44, 12, 88])
+     ['E', 'A', 'B', 'D', 'F', 'A']
+ 
+ The bisect module can be used with the Queue module to implement
+ a priority queue (example courtesy of Fredrik Lundh):
+ 
+ >>> import Queue, bisect
+ >>> class PriorityQueue(Queue.Queue):
+ ...     def _put(self, item):
+ ...         bisect.insort(self.queue, item)
+ ...
+ >>> queue = PriorityQueue(0)
+ >>> queue.put((2, "second"))
+ >>> queue.put((1, "first"))
+ >>> queue.put((3, "third"))
+ >>> queue.get()
+ (1, 'first')
+ >>> queue.get()
+ (2, 'second')
+ 
+ """
+ 
+ #==============================================================================
+ 
  def makeAllTests():
      suite = unittest.TestSuite()
***************
*** 127,134 ****
--- 184,194 ----
  #------------------------------------------------------------------------------
  
+ __test__ = {'libreftest' : libreftest}
+ 
  def test_main(verbose=None):
      from test import test_bisect
      suite = makeAllTests()
      test_support.run_suite(suite)
+     test_support.run_doctest(test_bisect, verbose)
  
  if __name__ == "__main__":