Module:Validate platter capacity
From Australian Enthusiasts Wiki
This module verifies Template:Infobox hard drive's capacity versus its platter capacity, and tries to find discrepancies. It returns true if no error, false if there is discrepancy.
-- This module validates the platter capacity is correct and makes sense.
-- Returns "true" on successful validation, "false" on failed validation.
-- Validation rules:
-- 1. The total capacity must be equal to (head count / 2 * platter capacity) - short stroked capacity. If the units are different, we must convert them to the same unit before doing validation.
-- 2. There must not be more heads than double the number of platters (otherwise we'd have floating heads)
local p = {}
-- Main entry point for this module.
function p.invokeMain(frame)
return p.validatePlatterCapacity(tonumber(frame.args[1]), frame.args[2], tonumber(frame.args[3]), frame.args[4], tonumber(frame.args[5]), tonumber(frame.args[6]), tonumber(frame.args[7]), frame.args[8], frame.args[9])
end
-- Validates platter capacity. Note that you should not #invoke this, as it won't be passed essential parameters.
function p.validatePlatterCapacity(totalCapacity, totalCapacityUnit, platterCapacity, platterCapacityUnit, platters, heads, shortStrokedCapacity, shortStrokedCapacityUnit)
-- Because Lua has no default params...
shortStrokedCapacity = shortStrokedCapacity or 0
platterCapacity = p.convertCapacity(platterCapacity, platterCapacityUnit, totalCapacityUnit)
if (shortStrokedCapacityUnit ~= nil) then
shortStrokedCapacityUnit = p.convertCapacity(shortStrokedCapacity, shortStrokedCapacityUnit, totalCapacityUnit)
end
if (p.validateNoPlatterlessHeads(platters, heads) == false) then
return false
end
return p.resultIsWithinTolerance(platterCapacity * (heads / 2) - shortStrokedCapacity, totalCapacity)
end
-- Validate there are no "platterless" heads, i.e. there are not less platters than heads. Note that we don't rule out a Seagate-style two platters, yet only two heads.
-- Returns true if there are no platterless heads.
function p.validateNoPlatterlessHeads(platters, heads)
return heads <= platters * 2
end
-- Convert a capacity from one target to another.
function p.convertCapacity(number, from, to)
-- Return immediately if the unit from and to are the same.
if (from == to) then
return number
end
unitArray = {"mb", "mib", "gb", "gib", "tb", "tib", "eb", "eib"}
stepsRemoved = (p.indexOf(unitArray, from) - p.indexOf(unitArray, to)) / 2
return number * (1000 ^ stepsRemoved)
end
-- Return the first index with the given value (or nil if not found).
function p.indexOf(array, value)
for i, v in ipairs(array) do
if v == value then
return i
end
end
return nil
end
-- Since we sometimes cannot equal exactly a platter capacity, build in some tolerance.
function p.resultIsWithinTolerance(number, target)
return (target * 0.99) < number and number < (target * 1.01)
end
return p