mirror of
				https://github.com/kovidgoyal/calibre.git
				synced 2025-11-03 19:17:02 -05:00 
			
		
		
		
	Refactor matcher test suite
This commit is contained in:
		
							parent
							
								
									bdbc6ccfaa
								
							
						
					
					
						commit
						b88f26adff
					
				@ -219,33 +219,43 @@ class CScorer(object):
 | 
				
			|||||||
            yield score, pos
 | 
					            yield score, pos
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test():
 | 
					def test():
 | 
				
			||||||
    items = ['mx\U0001f431nxox']
 | 
					    import unittest
 | 
				
			||||||
    for q in (PyScorer, CScorer):
 | 
					 | 
				
			||||||
        print (q)
 | 
					 | 
				
			||||||
        m = Matcher(items, scorer=q)
 | 
					 | 
				
			||||||
        for item, positions in m('MNO').iteritems():
 | 
					 | 
				
			||||||
            print ('\tMNO', item, positions)
 | 
					 | 
				
			||||||
            if -1 not in positions:
 | 
					 | 
				
			||||||
                for p in positions:
 | 
					 | 
				
			||||||
                    print (item[p], end=' ')
 | 
					 | 
				
			||||||
                print ()
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_mem():
 | 
					    class Test(unittest.TestCase):
 | 
				
			||||||
    from calibre.utils.mem import gc_histogram, diff_hists
 | 
					
 | 
				
			||||||
    m = Matcher(['a'])
 | 
					        def test_mem_leaks(self):
 | 
				
			||||||
    m('a')
 | 
					 | 
				
			||||||
    del m
 | 
					 | 
				
			||||||
    def doit(c):
 | 
					 | 
				
			||||||
        m = Matcher([c+'im/one.gif', c+'im/two.gif', c+'text/one.html',])
 | 
					 | 
				
			||||||
        m('one')
 | 
					 | 
				
			||||||
            import gc
 | 
					            import gc
 | 
				
			||||||
 | 
					            from calibre.utils.mem import get_memory as memory
 | 
				
			||||||
 | 
					            m = Matcher(['a'], scorer=CScorer)
 | 
				
			||||||
 | 
					            m('a')
 | 
				
			||||||
 | 
					            def doit(c):
 | 
				
			||||||
 | 
					                m = Matcher([c+'im/one.gif', c+'im/two.gif', c+'text/one.html',], scorer=CScorer)
 | 
				
			||||||
 | 
					                m('one')
 | 
				
			||||||
 | 
					            start = memory()
 | 
				
			||||||
 | 
					            for i in xrange(10):
 | 
				
			||||||
 | 
					                doit(str(i))
 | 
				
			||||||
            gc.collect()
 | 
					            gc.collect()
 | 
				
			||||||
    h1 = gc_histogram()
 | 
					            used10 = memory() - start
 | 
				
			||||||
 | 
					            start = memory()
 | 
				
			||||||
            for i in xrange(100):
 | 
					            for i in xrange(100):
 | 
				
			||||||
                doit(str(i))
 | 
					                doit(str(i))
 | 
				
			||||||
            gc.collect()
 | 
					            gc.collect()
 | 
				
			||||||
    h2 = gc_histogram()
 | 
					            used100 = memory() - start
 | 
				
			||||||
    diff_hists(h1, h2)
 | 
					            self.assertLessEqual(used100, 2 * used10)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        def test_non_bmp(self):
 | 
				
			||||||
 | 
					            raw = '_\U0001f431-'
 | 
				
			||||||
 | 
					            m = Matcher([raw], scorer=CScorer)
 | 
				
			||||||
 | 
					            positions = next(m(raw).itervalues())
 | 
				
			||||||
 | 
					            self.assertEqual(positions, (0, 1, (2 if sys.maxunicode >= 0x10ffff else 3)))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    class TestRunner(unittest.main):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        def createTests(self):
 | 
				
			||||||
 | 
					            tl = unittest.TestLoader()
 | 
				
			||||||
 | 
					            self.test = tl.loadTestsFromTestCase(Test)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    TestRunner(verbosity=4)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if sys.maxunicode >= 0x10ffff:
 | 
					if sys.maxunicode >= 0x10ffff:
 | 
				
			||||||
    get_char = lambda string, pos: string[pos]
 | 
					    get_char = lambda string, pos: string[pos]
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user