<s> import unittest <EOL> from basename_ranker import BasenameRanker <EOL> class BasenameRankerTest ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . ranker = BasenameRanker ( ) <EOL> def test_is_wordstart ( self ) : <EOL> def check ( s , expectations ) : <EOL> assert len ( s ) == len ( expectations ) <EOL> for i in range ( len ( s ) ) : <EOL> self . assertEquals ( expectations [ i ] , self . ranker . _is_wordstart ( s , i ) , "<STR_LIT>" % i ) <EOL> check ( "<STR_LIT:foo>" , [ True , False , False ] ) <EOL> check ( "<STR_LIT>" , [ True , False , False , True , False , False ] ) <EOL> check ( "<STR_LIT:o>" , [ True ] ) <EOL> check ( "<STR_LIT:_>" , [ True ] ) <EOL> check ( "<STR_LIT:F>" , [ True ] ) <EOL> check ( "<STR_LIT>" , [ True , False , False , True , False , False ] ) <EOL> check ( "<STR_LIT>" , [ True , False , False , False , True , False , False ] ) <EOL> check ( "<STR_LIT>" , [ True , True , False , False ] ) <EOL> check ( "<STR_LIT>" , [ True , True , False , False ] ) <EOL> check ( "<STR_LIT>" , [ True , False , False , False , True , False , False ] ) <EOL> check ( "<STR_LIT>" , [ True , False ] ) <EOL> check ( "<STR_LIT>" , [ True , False , False ] ) <EOL> check ( "<STR_LIT>" , [ True , False , True ] ) <EOL> check ( "<STR_LIT>" , [ True , False , False , False , False , True , False , False ] ) <EOL> check ( "<STR_LIT>" , [ True , False , False , True , True ] ) <EOL> check ( "<STR_LIT>" , [ True , False , False , True , False ] ) <EOL> check ( "<STR_LIT>" , [ True , True , False ] ) <EOL> check ( "<STR_LIT>" , [ True , True , False ] ) <EOL> check ( "<STR_LIT>" , [ True , True , True , False , False ] ) <EOL> def test_get_word_starts ( self ) : <EOL> data = { <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT:abc>' : [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : [ <NUM_LIT:0> , <NUM_LIT:4> ] , <EOL> '<STR_LIT>' : [ <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:6> ] , <EOL> '<STR_LIT>' : [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : [ <NUM_LIT:0> , <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:3> ] , <EOL> '<STR_LIT>' : [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : [ <NUM_LIT:0> , <NUM_LIT:3> ] , <EOL> '<STR_LIT>' : [ <NUM_LIT:0> , <NUM_LIT:4> ] , <EOL> } <EOL> for word , expected_starts in data . items ( ) : <EOL> starts = self . ranker . get_starts ( word ) <EOL> self . assertEquals ( expected_starts , starts , "<STR_LIT>" % ( word , expected_starts ) ) <EOL> def assertBasicRankAndWordHitCountIs ( self , expected_rank , expected_word_count , query , candidate ) : <EOL> res = self . ranker . _get_basic_rank ( query , candidate ) <EOL> self . assertEquals ( expected_rank , res [ <NUM_LIT:0> ] ) <EOL> self . assertEquals ( expected_word_count , res [ <NUM_LIT:1> ] ) <EOL> def test_query_hits_on_word_starts ( self ) : <EOL> self . assertBasicRankAndWordHitCountIs ( <NUM_LIT:8> , <NUM_LIT:4> , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertBasicRankAndWordHitCountIs ( <NUM_LIT:6> , <NUM_LIT:3> , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertBasicRankAndWordHitCountIs ( <NUM_LIT> , <NUM_LIT:2> , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertBasicRankAndWordHitCountIs ( <NUM_LIT:2> , <NUM_LIT:1> , '<STR_LIT:w>' , '<STR_LIT>' ) <EOL> self . assertBasicRankAndWordHitCountIs ( <NUM_LIT:2> , <NUM_LIT:1> , '<STR_LIT:v>' , '<STR_LIT>' ) <EOL> self . assertBasicRankAndWordHitCountIs ( <NUM_LIT:4> , <NUM_LIT:2> , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertBasicRankAndWordHitCountIs ( <NUM_LIT:5> , <NUM_LIT:2> , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertBasicRankAndWordHitCountIs ( <NUM_LIT:4> , <NUM_LIT:2> , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertBasicRankAndWordHitCountIs ( <NUM_LIT:6> , <NUM_LIT:0> , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_basic_rank_pays_attention_to_case ( self ) : <EOL> self . assertBasicRankAndWordHitCountIs ( <NUM_LIT> , <NUM_LIT:1> , "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . assertBasicRankAndWordHitCountIs ( <NUM_LIT:4> , <NUM_LIT:2> , "<STR_LIT>" , "<STR_LIT>" ) <EOL> def test_basic_rank_works_at_all ( self ) : <EOL> self . assertBasicRankAndWordHitCountIs ( <NUM_LIT:8> , <NUM_LIT:4> , "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . assertBasicRankAndWordHitCountIs ( <NUM_LIT:10> , <NUM_LIT:5> , "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . assertBasicRankAndWordHitCountIs ( <NUM_LIT:10> , <NUM_LIT:5> , "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . assertBasicRankAndWordHitCountIs ( <NUM_LIT> , <NUM_LIT:4> , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_basic_rank_query_case_doesnt_influence_rank_query ( self ) : <EOL> a = self . ranker . _get_basic_rank ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> b = self . ranker . _get_basic_rank ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . assertEquals ( a , b ) <EOL> def test_basic_rank_isnt_only_greedy ( self ) : <EOL> self . assertBasicRankAndWordHitCountIs ( <NUM_LIT:10> , <NUM_LIT:5> , "<STR_LIT>" , "<STR_LIT>" ) <EOL> def test_basic_rank_on_corner_cases ( self ) : <EOL> self . assertBasicRankAndWordHitCountIs ( <NUM_LIT:0> , <NUM_LIT:0> , "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . assertBasicRankAndWordHitCountIs ( <NUM_LIT:0> , <NUM_LIT:0> , "<STR_LIT>" , "<STR_LIT:x>" ) <EOL> self . assertBasicRankAndWordHitCountIs ( <NUM_LIT:0> , <NUM_LIT:0> , "<STR_LIT:x>" , "<STR_LIT>" ) <EOL> self . assertBasicRankAndWordHitCountIs ( <NUM_LIT:2> , <NUM_LIT:1> , "<STR_LIT:x>" , "<STR_LIT:x>" ) <EOL> self . assertBasicRankAndWordHitCountIs ( <NUM_LIT:1> , <NUM_LIT:0> , "<STR_LIT:x>" , "<STR_LIT>" ) <EOL> self . assertBasicRankAndWordHitCountIs ( <NUM_LIT:0> , <NUM_LIT:0> , "<STR_LIT:x>" , "<STR_LIT>" ) <EOL> def test_basic_rank_on_mixed_wordstarts_and_full_words ( self ) : <EOL> self . assertBasicRankAndWordHitCountIs ( <NUM_LIT> , <NUM_LIT:3> , "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . assertBasicRankAndWordHitCountIs ( <NUM_LIT:15> , <NUM_LIT:2> , "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . assertBasicRankAndWordHitCountIs ( <NUM_LIT:8> , <NUM_LIT:4> , "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . assertBasicRankAndWordHitCountIs ( <NUM_LIT> , <NUM_LIT:5> , "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . assertBasicRankAndWordHitCountIs ( <NUM_LIT:10> , <NUM_LIT:5> , "<STR_LIT>" , "<STR_LIT>" ) <EOL> def test_basic_rank_overconditioned_query ( self ) : <EOL> self . assertBasicRankAndWordHitCountIs ( <NUM_LIT:2> , <NUM_LIT:1> , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_rank_corner_cases ( self ) : <EOL> self . assertEquals ( <NUM_LIT:0> , self . ranker . rank_query ( '<STR_LIT:foo>' , '<STR_LIT>' ) ) <EOL> self . assertEquals ( <NUM_LIT:0> , self . ranker . rank_query ( '<STR_LIT>' , '<STR_LIT:foo>' ) ) <EOL> self . assertEquals ( <NUM_LIT:0> , self . ranker . rank_query ( '<STR_LIT:foo>' , '<STR_LIT:m>' ) ) <EOL> self . assertEquals ( <NUM_LIT:0> , self . ranker . rank_query ( '<STR_LIT:f>' , '<STR_LIT>' ) ) <EOL> self . assertEquals ( <NUM_LIT:6> , self . ranker . rank_query ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def test_rank_subclasses_lower_ranked_than_base ( self ) : <EOL> base_rank = self . ranker . rank_query ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> subclass_rank = self . ranker . rank_query ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . assertTrue ( base_rank > subclass_rank ) <EOL> def test_rank_order_for_hierarchy_puts_bases_first ( self ) : <EOL> names = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> ] <EOL> self . _assertRankDecreasesOrStaysTheSame ( "<STR_LIT>" , names ) <EOL> def _assertRankDecreasesOrStaysTheSame ( self , query , names ) : <EOL> """<STR_LIT>""" <EOL> ranks = [ self . ranker . rank_query ( query , n ) for n in names ] <EOL> nw = [ self . ranker . get_num_words ( n ) for n in names ] <EOL> basic_ranks = [ self . ranker . _get_basic_rank ( query , n ) for n in names ] <EOL> for i in range ( <NUM_LIT:1> , len ( ranks ) ) : <EOL> changeInRank = ranks [ i ] - ranks [ i - <NUM_LIT:1> ] <EOL> self . assertTrue ( changeInRank <= <NUM_LIT:0> ) <EOL> def test_rank_order_prefers_capitals ( self ) : <EOL> self . assertBasicRankAndWordHitCountIs ( <NUM_LIT:6> , <NUM_LIT:3> , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def test_rank_order_puts_tests_second ( self ) : <EOL> q = "<STR_LIT>" <EOL> a1 = self . ranker . rank_query ( q , '<STR_LIT>' ) <EOL> a2 = self . ranker . rank_query ( q , '<STR_LIT>' ) <EOL> b = self . ranker . rank_query ( q , '<STR_LIT>' ) <EOL> self . assertTrue ( a1 > b ) ; <EOL> self . assertTrue ( a2 > b ) ; <EOL> q = "<STR_LIT>" <EOL> a1 = self . ranker . rank_query ( q , '<STR_LIT>' ) <EOL> a2 = self . ranker . rank_query ( q , '<STR_LIT>' ) <EOL> b = self . ranker . rank_query ( q , '<STR_LIT>' ) <EOL> self . assertTrue ( a1 > b ) ; <EOL> self . assertTrue ( a2 > b ) ; <EOL> def test_rank_order_for_hierarchy_puts_prefixed_second ( self ) : <EOL> q = "<STR_LIT>" <EOL> a = self . ranker . rank_query ( q , '<STR_LIT>' ) <EOL> b1 = self . ranker . rank_query ( q , '<STR_LIT>' ) <EOL> b2 = self . ranker . rank_query ( q , '<STR_LIT>' ) <EOL> self . assertTrue ( a > b1 ) ; <EOL> def test_rank_order_puts_tests_second_2 ( self ) : <EOL> q = "<STR_LIT>" <EOL> a1 = self . ranker . rank_query ( q , '<STR_LIT>' ) <EOL> a2 = self . ranker . rank_query ( q , '<STR_LIT>' ) <EOL> b = self . ranker . rank_query ( q , '<STR_LIT>' ) <EOL> self . assertTrue ( a1 > b ) ; <EOL> self . assertTrue ( a2 > b ) ; <EOL> q = "<STR_LIT>" <EOL> a = self . ranker . rank_query ( q , '<STR_LIT>' ) <EOL> b = self . ranker . rank_query ( q , '<STR_LIT>' ) <EOL> self . assertTrue ( a > b ) <EOL> def test_refinement_improves_rank_query ( self ) : <EOL> a = self . ranker . rank_query ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> b = self . ranker . rank_query ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertTrue ( b > a ) <EOL> def test_document_rank ( self ) : <EOL> a = self . ranker . rank_query ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> b = self . ranker . rank_query ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertTrue ( a > b ) <EOL> def test_enclosing_run_length_memoization_real_user_data ( self ) : <EOL> q = '<STR_LIT>' <EOL> items = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> for x in items : <EOL> stateless_ranker = BasenameRanker ( ) <EOL> rStateless = stateless_ranker . rank_query ( q , x ) <EOL> rStateful = self . ranker . rank_query ( q , x ) <EOL> self . assertEquals ( rStateless , rStateful , "<STR_LIT>" % ( x , rStateless , rStateful ) ) <EOL> def test_enclosing_run_length_memoization ( self ) : <EOL> q1 = BasenameRanker ( ) . rank_query ( "<STR_LIT:bar>" , "<STR_LIT>" ) <EOL> q2 = BasenameRanker ( ) . rank_query ( "<STR_LIT:bar>" , "<STR_LIT>" ) <EOL> r = BasenameRanker ( ) <EOL> r1 = r . rank_query ( "<STR_LIT:bar>" , "<STR_LIT>" ) <EOL> r2 = r . rank_query ( "<STR_LIT:bar>" , "<STR_LIT>" ) <EOL> self . assertEquals ( q1 , r1 ) <EOL> self . assertEquals ( q2 , r2 ) </s>
