Initial commit

This commit is contained in:
Pieter Vander Vennet 2020-04-30 17:23:44 +02:00
parent d6895e025c
commit 2c2a28d30a
105 changed files with 10038 additions and 0 deletions

View file

@ -0,0 +1,13 @@
function all(list)
for _, value in ipairs(list) do
if (value == nil) then
return false
end
if(value ~= "yes" and value ~= "true") then
return false
end
end
return true;
end

View file

@ -0,0 +1,4 @@
function as_number(a)
end

View file

@ -0,0 +1,3 @@
function concat(a, b)
return a .. b
end

View file

View file

View file

@ -0,0 +1,14 @@
function debug_table(table, prefix)
if (prefix == nil) then
prefix = ""
end
for k, v in pairs(table) do
if (type(v) == "table") then
debug_table(v, " ")
else
print(prefix .. tostring(k) .. " = " .. tostring(v))
end
end
print("")
end

View file

@ -0,0 +1,6 @@
function default(defaultValue, realValue)
if(realValue ~= nil) then
return realValue
end
return defaultValue
end

View file

View file

@ -0,0 +1,10 @@
function double_compare(a, b)
if (type(a) ~= "number") then
a = parse(a)
end
if(type(b) ~= "number") then
b = parse(b)
end
return math.abs(a - b) > 0.001
end

View file

View file

@ -0,0 +1,7 @@
function eq(a, b)
if (a == b) then
return "yes"
else
return "no"
end
end

View file

@ -0,0 +1,20 @@
function first_match_of(tags, result, order_of_keys, table)
for _, key in ipairs(order_of_keys) do
local v = tags[key]
if (v ~= nil) then
local mapping = table[key]
if (type(mapping) == "table") then
local resultValue = mapping[v]
if (v ~= nil) then
result.attributes_to_keep[key] = v
return resultValue
end
else
result.attributes_to_keep[key] = v
return mapping
end
end
end
return nil;
end

View file

@ -0,0 +1,3 @@
function id(v)
return v
end

View file

@ -0,0 +1 @@
-- not actually used, should be if_then_else

View file

@ -0,0 +1,7 @@
function if_then_else(condition, thn, els)
if (condition) then
return thn
else
return els -- if no third parameter is given, 'els' will be nil
end
end

View file

@ -0,0 +1,3 @@
function inv(n)
return 1/n
end

View file

@ -0,0 +1,2 @@
-- TODO
-- listDot

View file

@ -0,0 +1,12 @@
function max(list)
local max
for _, value in ipairs(list) do
if (max == nil) then
max = value
elseif (max < value) then
max = value
end
end
return max;
end

View file

@ -0,0 +1,3 @@
function member_of()
???
end

View file

@ -0,0 +1,12 @@
function min(list)
local min
for _, value in ipairs(list) do
if (min == nil) then
min = value
elseif (min > value) then
min = value
end
end
return min;
end

View file

@ -0,0 +1,7 @@
function multiply(list)
local factor = 1
for _, value in ipairs(list) do
factor = factor * value
end
return factor;
end

View file

@ -0,0 +1,25 @@
function must_match(tags, result, needed_keys, table)
local result_list = {}
for _, key in ipairs(needed_keys) do
local v = tags[key]
if (v == nil) then
return false
end
local mapping = table[key]
if (type(mapping) == "table") then
local resultValue = mapping[v]
if (v == nil or v == false) then
return false
end
if (v == "no" or v == "false") then
return false
end
result.attributes_to_keep[key] = v
else
error("The mapping is not a table. This is not supported")
end
end
return true;
end

View file

@ -0,0 +1,2 @@
-- 'not' is actually called 'notEq'
print("Compiler error - not was actually loaded. If you see this in your profile, please report a bug with the full profile")

View file

@ -0,0 +1,7 @@
function notEq(a, b)
if (a ~= b) then
return "yes"
else
return "no"
end
end

View file

@ -0,0 +1,27 @@
function parse(string)
if (string == nil) then
return 0
end
if (type(string) == "number") then
return string
end
if (string == "yes" or string == "true") then
return 1
end
if (string == "no" or string == "false") then
return 0
end
if (type(string) == "boolean") then
if (string) then
return 1
else
return 0
end
end
return tonumber(string)
end

View file

@ -0,0 +1 @@
print("ERROR: stringToTag is needed. This should not happen")

View file

@ -0,0 +1,10 @@
function sum(list)
local sum = 1
for _, value in ipairs(list) do
if(value == 'yes' or value == 'true') then
value = 1
end
sum = sum + value
end
return sum;
end

View file

@ -0,0 +1,20 @@
function table_to_list(tags, result, factor_table)
local list = {}
for key, mapping in pairs(factor_table) do
local v = tags[key]
if (v ~= nil) then
if (type(mapping) == "table") then
local f = mapping[v]
if (f ~= nil) then
table.insert(list, f);
result.attributes_to_keep[key] = v
end
else
table.insert(list, mapping);
result.attributes_to_keep[key] = v
end
end
end
return list;
end

View file

@ -0,0 +1,3 @@
function to_string(o)
return o;
end

View file

@ -0,0 +1,9 @@
failed_tests = false
function unit_test(f, fname, index, expected, parameters, tags)
local result = {attributes_to_keep = {}}
local actual = f(parameters, tags, result)
if (tostring(actual) ~= expected) then
print("[" .. fname .. "] " .. index .. " failed: expected " .. expected .. " but got " .. tostring(actual))
failed_tests = true
end
end

View file

@ -0,0 +1,38 @@
failed_profile_tests = false
--[[
expected should be a table containing 'access', 'speed' and 'weight'
]]
function unit_test_profile(profile_function, profile_name, index, expected, tags)
result = {attributes_to_keep = {}}
profile_function(tags, result)
if (result.access ~= expected.access) then
print("Test " .. tostring(index) .. " failed for " .. profile_name .. ".access: expected " .. expected.access .. " but got " .. result.access)
failed_profile_tests = true
end
if (result.access == 0) then
-- we cannot access this road, the other results are irrelevant
return
end
if (double_compare(result.speed, expected.speed)) then
print("Test " .. tostring(index) .. " failed for " .. profile_name .. ".speed: expected " .. expected.speed .. " but got " .. result.speed)
failed_profile_tests = true
end
if (double_compare(result.oneway, expected.oneway)) then
print("Test " .. tostring(index) .. " failed for " .. profile_name .. ".oneway: expected " .. expected.oneway .. " but got " .. result.oneway)
failed_profile_tests = true
end
if (double_compare(result.oneway, expected.oneway)) then
print("Test " .. tostring(index) .. " failed for " .. profile_name .. ".oneway: expected " .. expected.oneway .. " but got " .. result.oneway)
failed_profile_tests = true
end
if (double_compare(inv(result.factor), 0.033333)) then
print("Test " .. tostring(index) .. " failed for " .. profile_name .. ".factor: expected " .. expected.weight .. " but got " .. inv(result.factor))
failed_profile_tests = true
end
end