[Zope-CVS] CVS: Products/ZCTextIndex/tests - testNBest.py:1.1.2.3

Tim Peters tim.one@comcast.net
Wed, 1 May 2002 22:05:29 -0400


Update of /cvs-repository/Products/ZCTextIndex/tests
In directory cvs.zope.org:/tmp/cvs-serv3399/tests

Modified Files:
      Tag: TextIndexDS9-branch
	testNBest.py 
Log Message:
Give an NBest object a batch-processing "addmany" method.  It's quicker.
Alas, the splitter stuff won't build on Win98 due to ExtensionBuilder.py
creating command lines longer than this OS can handle, so I didn't change
our *use* of NBest to match (can't test it here).  Will do tomorrow from
a Win2K box.


=== Products/ZCTextIndex/tests/testNBest.py 1.1.2.2 => 1.1.2.3 ===
         self.assertEqual(nb.getbest(), [('b', 1)])
 
+        nb.addmany([('d', 3), ('e', -6), ('f', 5), ('g', 4)])
+        self.assertEqual(len(nb), 1)
+        self.assertEqual(nb.capacity(), 1)
+        self.assertEqual(nb.getbest(), [('f', 5)])
+
     def testMany(self):
         import random
-        n = 10
         inputs = [(-i, i) for i in range(50)]
-        expected = inputs[-n:]
-        expected.reverse()
 
-        # Feed them in in increasing order.
-        nb = NBest(n)
-        for item, score in inputs:
-            nb.add(item, score)
-        self.assertEqual(len(nb), n)
-        self.assertEqual(nb.capacity(), n)
-        self.assertEqual(nb.getbest(), expected)
-
-        # Feed them in in decreasing order.
-        nb = NBest(n)
-        inputs.reverse()
-        for item, score in inputs:
-            nb.add(item, score)
-        self.assertEqual(len(nb), n)
-        self.assertEqual(nb.capacity(), n)
-        self.assertEqual(nb.getbest(), expected)
-
-        # Feed them in in random order.
-
-        nb = NBest(n)
-        random.shuffle(inputs)
-        for item, score in inputs:
-            nb.add(item, score)
-        self.assertEqual(len(nb), n)
-        self.assertEqual(nb.capacity(), n)
-        self.assertEqual(nb.getbest(), expected)
+        reversed_inputs = inputs[:]
+        reversed_inputs.reverse()
+
+        # Test the N-best for a variety of n (1, 6, 11, ... 50).
+        for n in range(1, len(inputs)+1, 5):
+            expected = inputs[-n:]
+            expected.reverse()
+
+            random_inputs = inputs[:]
+            random.shuffle(random_inputs)
+
+            for source in inputs, reversed_inputs, random_inputs:
+                # Try feeding them one at a time.
+                nb = NBest(n)
+                for item, score in source:
+                    nb.add(item, score)
+                self.assertEqual(len(nb), n)
+                self.assertEqual(nb.capacity(), n)
+                self.assertEqual(nb.getbest(), expected)
+
+                # And again in one gulp.
+                nb = NBest(n)
+                nb.addmany(source)
+                self.assertEqual(len(nb), n)
+                self.assertEqual(nb.capacity(), n)
+                self.assertEqual(nb.getbest(), expected)
 
 def test_suite():
     return makeSuite(NBestTest)