[Zope-CVS] CVS: Products/ZCTextIndex/tests - testNBest.py:1.1.2.1
Tim Peters
tim.one@comcast.net
Wed, 1 May 2002 17:03:20 -0400
Update of /cvs-repository/Products/ZCTextIndex/tests
In directory cvs.zope.org:/tmp/cvs-serv6649/tests
Added Files:
Tag: TextIndexDS9-branch
testNBest.py
Log Message:
Adding an NBest object to remember the N best-scoring objects passed to
it. This is intended to be used for final result lists. The dirt-simple
sorted-list + bisect.bisect implementation has the right theoretical
worst-case number of comparisons, but for large N a min-heap would
eventually win on data-movement cost. But don't optimize this
prematurely! I suspect it will work fine as-is.
=== Added File Products/ZCTextIndex/tests/testNBest.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
from unittest import TestCase, TestSuite, main, makeSuite
from Products.ZCTextIndex.NBest import NBest
class NBestTest(TestCase):
def testConstructor(self):
self.assertRaises(ValueError, NBest, 0)
self.assertRaises(ValueError, NBest, -1)
for n in range(1, 11):
nb = NBest(n)
self.assertEqual(len(nb), 0)
self.assertEqual(nb.capacity(), n)
def testOne(self):
nb = NBest(1)
nb.add('a', 0)
self.assertEqual(nb.getbest(), [('a', 0)])
nb.add('b', 1)
self.assertEqual(len(nb), 1)
self.assertEqual(nb.capacity(), 1)
self.assertEqual(nb.getbest(), [('b', 1)])
nb.add('c', -1)
self.assertEqual(len(nb), 1)
self.assertEqual(nb.capacity(), 1)
self.assertEqual(nb.getbest(), [('b', 1)])
def testMany(self):
import random
n = 10
inputs = []
for i in range(50):
inputs.append((i, i))
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 decreading 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)
def test_suite():
return makeSuite(NBestTest)
if __name__=='__main__':
main(defaultTest='test_suite')