#229: add unit tests and functional test and improve codes