add test for pattern recognition and fix the bugs