|
| 1 | +require 'gnuplot' |
| 2 | +require 'os' |
| 3 | +require 'paths' |
| 4 | +require 'torch' |
| 5 | + |
| 6 | +local tester = torch.Tester() |
| 7 | +local tests = {} |
| 8 | + |
| 9 | +-- Returns a random string of lowercase digits |
| 10 | +local function randomFilenameStr() |
| 11 | + local t = {} |
| 12 | + for i = 1, 10 do |
| 13 | + table.insert(t, string.char(math.random(97, 122))) |
| 14 | + end |
| 15 | + return table.concat(t) |
| 16 | +end |
| 17 | + |
| 18 | +-- Make sure we can write to a new filename, but not to a nonexistent directory. |
| 19 | +function tests.cannotWriteToNonExistentDir() |
| 20 | + -- Save locally, this should work |
| 21 | + local validFilename = randomFilenameStr() .. '.png' |
| 22 | + |
| 23 | + -- If this already exists (bad luck!), don't let the test overwrite it |
| 24 | + assert(not (paths.filep(validFilename) or |
| 25 | + paths.dirp(validFilename)), |
| 26 | + 'random filename aready exists (?)') |
| 27 | + |
| 28 | + -- Should work fine |
| 29 | + gnuplot.pngfigure(validFilename) |
| 30 | + gnuplot.plot({'Sin Curve',torch.sin(torch.linspace(-5,5))}) |
| 31 | + gnuplot.plotflush() |
| 32 | + |
| 33 | + -- Clean up after ourselves |
| 34 | + os.remove(validFilename) |
| 35 | + |
| 36 | + -- Now make an invalid output |
| 37 | + local nonExistentDir = randomFilenameStr() |
| 38 | + assert(not (paths.filep(nonExistentDir) or |
| 39 | + paths.dirp(nonExistentDir)), |
| 40 | + 'random dir aready exists (?)') |
| 41 | + |
| 42 | + -- This makes an absolute path below cwd, seems Lua has no way (?) to query |
| 43 | + -- the file separator charater by itself... |
| 44 | + local invalidFilename = paths.concat(nonExistentDir, validFilename) |
| 45 | + local function shouldCrash() |
| 46 | + gnuplot.pngfigure(invalidFilename) |
| 47 | + end |
| 48 | + tester:assertErrorPattern(shouldCrash, 'directory does not exist') |
| 49 | +end |
| 50 | + |
| 51 | +tester:add(tests) |
| 52 | +return tester:run() |
0 commit comments