From 64fd49e33bd6b87baafdb482268253302eb258d4 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Mon, 7 Sep 2020 18:44:44 +0200 Subject: [PATCH] Use 'pairs' instead of 'ipairs' so that functions don't break on lists with nil, fix various small bugs --- AspectedRouting/IO/lua/all.lua | 2 +- AspectedRouting/IO/lua/containedIn.lua | 2 +- AspectedRouting/IO/lua/firstMatchOf.lua | 2 +- AspectedRouting/IO/lua/head.lua | 2 +- AspectedRouting/IO/lua/if_then_else_dotted.lua | 17 ++++++++++++++--- AspectedRouting/IO/lua/max.lua | 12 +++++++----- AspectedRouting/IO/lua/min.lua | 12 +++++++----- AspectedRouting/IO/lua/multiply.lua | 6 ++++-- AspectedRouting/IO/lua/parse.lua | 9 +++++++++ AspectedRouting/IO/lua/sum.lua | 12 +++++++----- AspectedRouting/IO/lua/unitTest.lua | 11 ++++++++++- 11 files changed, 62 insertions(+), 25 deletions(-) diff --git a/AspectedRouting/IO/lua/all.lua b/AspectedRouting/IO/lua/all.lua index 211a1ec..ad62167 100644 --- a/AspectedRouting/IO/lua/all.lua +++ b/AspectedRouting/IO/lua/all.lua @@ -1,5 +1,5 @@ function all(list) - for _, value in ipairs(list) do + for _, value in pairs(list) do if (value == nil) then return false end diff --git a/AspectedRouting/IO/lua/containedIn.lua b/AspectedRouting/IO/lua/containedIn.lua index d46a0c4..e4e9fc4 100644 --- a/AspectedRouting/IO/lua/containedIn.lua +++ b/AspectedRouting/IO/lua/containedIn.lua @@ -1,5 +1,5 @@ function containedIn(list, a) - for _, value in ipairs(list) do + for _, value in pairs(list) do if (value == a) then return true end diff --git a/AspectedRouting/IO/lua/firstMatchOf.lua b/AspectedRouting/IO/lua/firstMatchOf.lua index 623c389..68bbc89 100644 --- a/AspectedRouting/IO/lua/firstMatchOf.lua +++ b/AspectedRouting/IO/lua/firstMatchOf.lua @@ -1,5 +1,5 @@ function first_match_of(tags, result, order_of_keys, table) - for _, key in ipairs(order_of_keys) do + for _, key in pairs(order_of_keys) do local v = tags[key] if (v ~= nil) then diff --git a/AspectedRouting/IO/lua/head.lua b/AspectedRouting/IO/lua/head.lua index b049197..3e171ba 100644 --- a/AspectedRouting/IO/lua/head.lua +++ b/AspectedRouting/IO/lua/head.lua @@ -2,7 +2,7 @@ function head(ls) if(ls == nil) then return nil end - for _, v in ipairs(ls) do + for _, v in pairs(ls) do if(v ~= nil) then return v end diff --git a/AspectedRouting/IO/lua/if_then_else_dotted.lua b/AspectedRouting/IO/lua/if_then_else_dotted.lua index 9b0fe42..b8aa205 100644 --- a/AspectedRouting/IO/lua/if_then_else_dotted.lua +++ b/AspectedRouting/IO/lua/if_then_else_dotted.lua @@ -1,11 +1,22 @@ +function applyIfNeeded(f, arg) + if(f == nil) then + return nil + end + if(type(f) == "function") then + return f(arg) + else + return f + end +end + function if_then_else_dotted(conditionf, thnf, elsef, arg) - local condition = conditionf(arg); + local condition = applyIfNeeded(conditionf, arg); if (condition) then - return thnf(arg) + return applyIfNeeded(thnf, arg) else if(elsef == nil) then return nil end - return elsef(arg) -- if no third parameter is given, 'els' will be nil + return applyIfNeeded(elsef, arg) -- if no third parameter is given, 'els' will be nil end end \ No newline at end of file diff --git a/AspectedRouting/IO/lua/max.lua b/AspectedRouting/IO/lua/max.lua index 608a151..3c4e4cb 100644 --- a/AspectedRouting/IO/lua/max.lua +++ b/AspectedRouting/IO/lua/max.lua @@ -1,10 +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 + for _, value in pairs(list) do + if (value == nil) then + if (max == nil) then + max = value + elseif (max < value) then + max = value + end end end diff --git a/AspectedRouting/IO/lua/min.lua b/AspectedRouting/IO/lua/min.lua index a53e545..d26ed67 100644 --- a/AspectedRouting/IO/lua/min.lua +++ b/AspectedRouting/IO/lua/min.lua @@ -1,10 +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 + for _, value in pairs(list) do + if(value ~= nil) then + if (min == nil) then + min = value + elseif (value < min) then + min = value + end end end diff --git a/AspectedRouting/IO/lua/multiply.lua b/AspectedRouting/IO/lua/multiply.lua index 74edab2..7e19c48 100644 --- a/AspectedRouting/IO/lua/multiply.lua +++ b/AspectedRouting/IO/lua/multiply.lua @@ -1,7 +1,9 @@ function multiply(list) local factor = 1 - for _, value in ipairs(list) do - factor = factor * value + for _, value in pairs(list) do + if (value ~= nil) then + factor = factor * value + end end return factor; end \ No newline at end of file diff --git a/AspectedRouting/IO/lua/parse.lua b/AspectedRouting/IO/lua/parse.lua index 479b1fc..41a04ad 100644 --- a/AspectedRouting/IO/lua/parse.lua +++ b/AspectedRouting/IO/lua/parse.lua @@ -22,6 +22,15 @@ function parse(string) end end + if(string:match("%d*:%d*")) then + -- duration in minute + local duration = 0 + for part in string:gmatch "%d*" do + duration = duration * 60 + tonumber(part) + end + return duration + end + return tonumber(string) end \ No newline at end of file diff --git a/AspectedRouting/IO/lua/sum.lua b/AspectedRouting/IO/lua/sum.lua index c9b99ea..80eefce 100644 --- a/AspectedRouting/IO/lua/sum.lua +++ b/AspectedRouting/IO/lua/sum.lua @@ -1,10 +1,12 @@ function sum(list) - local sum = 1 - for _, value in ipairs(list) do - if(value == 'yes' or value == 'true') then - value = 1 + local sum = 0 + for _, value in pairs(list) do + if(value ~= nil) then + if(value == 'yes' or value == 'true') then + value = 1 + end + sum = sum + value end - sum = sum + value end return sum; end \ No newline at end of file diff --git a/AspectedRouting/IO/lua/unitTest.lua b/AspectedRouting/IO/lua/unitTest.lua index c3d0217..bf8cdfd 100644 --- a/AspectedRouting/IO/lua/unitTest.lua +++ b/AspectedRouting/IO/lua/unitTest.lua @@ -1,8 +1,17 @@ failed_tests = false function unit_test(f, fname, index, expected, parameters, tags) + if (f == nil) then + print("Trying to unit test " .. fname .. " but this function is not defined") + failed_tests = true + return + end local result = {attributes_to_keep = {}} local actual = f(parameters, tags, result) - if (tostring(actual) ~= expected) then + if(expected == "null" and actual == nil) then + -- OK! + elseif(tonumber(actual) and tonumber(expected) and tonumber(actual) == tonumber(expected)) then + -- OK! + elseif (tostring(actual) ~= expected) then print("[" .. fname .. "] " .. index .. " failed: expected " .. expected .. " but got " .. tostring(actual)) failed_tests = true end