optimize function condition & add testcase function