Skip to content

Instantly share code, notes, and snippets.

@mpeterv
Created July 17, 2016 11:36
Show Gist options
  • Save mpeterv/5f5bbd8e00910e679c0ec19c9b4110c0 to your computer and use it in GitHub Desktop.
Save mpeterv/5f5bbd8e00910e679c0ec19c9b4110c0 to your computer and use it in GitHub Desktop.
Script to compare Python's os.path.normpath and Penlight's pl.path.normpath on random inputs
-- Usage: lua test-normpath.lua [count | path]
local path = require "pl.path"
local socket = require "socket"
math.randomseed(math.floor(socket.gettime() * 1000))
local chars = "./A"
local function gen_random_path()
local length = math.random(0, 5) + math.random(0, 5)
local buf = {}
for i = 1, length do
local char_index = math.random(#chars)
buf[i] = chars:sub(char_index, char_index)
end
return table.concat(buf)
end
local function test_path(p)
local pl_normpath = path.normpath(p)
local py_command = ([[python -c 'import os, sys; sys.stdout.write(os.path.normpath("%s"))']]):format(p)
local py_handler = assert(io.popen(py_command))
local py_normpath = py_handler:read("*a")
py_handler:close()
if pl_normpath ~= py_normpath then
print("Mismatch!")
print(" path: " .. p)
print(" pl: " .. pl_normpath)
print(" py: " .. py_normpath)
end
end
if arg[1] and not tonumber(arg[1]) then
test_path(arg[1])
else
for _ = 1, tonumber(arg[1] or "10") do
test_path(gen_random_path())
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment