Make function inferPrefix() more concise and extend test cases