### Question:
KeySearch { static <KEY,VALUE> int search( PageCursor cursor, TreeNode<KEY,VALUE> bTreeNode, TreeNode.Type type, KEY key, KEY readKey, int keyCount ) { if ( keyCount == 0 ) { return searchResult( 0, false ); } int lower = 0; int higher = keyCount - 1; int pos; boolean hit = false; Comparator<KEY> comparator = bTreeNode.keyComparator(); int comparison; if ( comparator.compare( key, bTreeNode.keyAt( cursor, readKey, higher, type ) ) > 0 ) { pos = keyCount; } else if ( (comparison = comparator.compare( key, bTreeNode.keyAt( cursor, readKey, lower, type ) )) <= 0 ) { if ( comparison == 0 ) { hit = true; } pos = 0; } else { while ( lower < higher ) { pos = (lower + higher) / 2; comparison = comparator.compare( key, bTreeNode.keyAt( cursor, readKey, pos, type ) ); if ( comparison <= 0 ) { higher = pos; } else { lower = pos + 1; } } if ( lower != higher ) { return NO_SUCCESS_FLAG; } pos = lower; hit = comparator.compare( key, bTreeNode.keyAt( cursor, readKey, pos, type ) ) == 0; } return searchResult( pos, hit ); } private  KeySearch();   }### Answer:
@Test void searchEmptyLeaf() { node.initializeLeaf( cursor, STABLE_GENERATION, UNSTABLE_GENERATION ); int keyCount = TreeNode.keyCount( cursor ); int result = search( cursor, node, LEAF, searchKey, readKey, keyCount ); assertSearchResult( false, 0, result ); }
@Test void searchEmptyInternal() { node.initializeInternal( cursor, STABLE_GENERATION, UNSTABLE_GENERATION ); int keyCount = TreeNode.keyCount( cursor ); final int result = search( cursor, node, INTERNAL, searchKey, readKey, keyCount ); assertSearchResult( false, 0, result ); }
@Test void shouldFindExistingKey() { fullLeafWithUniqueKeys(); MutableLong key = layout.newKey(); for ( int i = 0; i < KEY_COUNT; i++ ) { key.setValue( key( i ) ); int result = search( cursor, node, LEAF, key, readKey, KEY_COUNT ); assertSearchResult( true, i, result ); } }
@Test void shouldReturnCorrectIndexesForKeysInBetweenExisting() { fullLeafWithUniqueKeys(); MutableLong key = layout.newKey(); for ( int i = 1; i < KEY_COUNT - 1; i++ ) { key.setValue( key( i ) - 1 ); int result = search( cursor, node, LEAF, key, readKey, KEY_COUNT ); assertSearchResult( false, i, result ); } }