Fixed unit tests writing to root

Depending on the implementation, `tmpnam()` might return a unique filename
for the current working directory only, which would still be prefixed by
`/` or `\`. This causes the unit tests to try to write to the file system
root (e.g. when using MSVC 2013).

The updated code always finds and uses a temporary file in the current
working directory. This isn't perfect either, but should be more reliable.