Add test filtering and split out tests into .cpp files.

Fix some small memory issues.