add tests and fix bugs in memory.hh