Add unit tests for gettail.
Refactor gettail.