Add unit test and fix bugs of the test