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 `\` (but no absolute path).

For example, when using MSVC 2013, a temporary filename returned might be
`/abc1.2`. The path won't be accessible without elevated rights.

The updated code always finds and uses a temporary file either in the
current working directory or in the set temp directory (based on
accessibility).