Last active
December 11, 2015 22:08
-
-
Save auxiliary-character/498f24323cfe137378c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Table = {1} | |
Tests = { | |
{{1, 2}, {1, 1}}, | |
{{1, 1}, {1, 1}}, | |
{{0, 0}, {0, 0}}, | |
{{-5, 10}, {1, 1}}, | |
{{-2, nil}, {1, 1}}, | |
{{-2, 2}, {1, 1}}, | |
{{-2, 1}, {1, 1}}, | |
{{-2, -1}, {1, 1}}, | |
{{-2, -3}, {0, 0}}, | |
{{-5, 2}, {1, 1}}, | |
{{-5, 0}, {0, 0}}, | |
{{-5, -7}, {0, 0}}, | |
{{-5, -3}, {0, 0}} | |
} | |
Table = {1, 2, 3, 4, 5, 6, 7, 8, 9} | |
Tests = { | |
{{1, 2}, {1, 2}}, | |
{{1, 1}, {1, 1}}, | |
{{0, 0}, {0, 0}}, | |
{{-5, 10}, {5, 9}}, | |
{{-2, nil}, {8, 9}}, | |
{{-2, 2}, {8, 9}}, | |
{{-2, 1}, {8, 8}}, | |
{{-2, -1}, {8, 9}}, | |
{{-2, -3}, {0, 0}}, | |
{{-5, 2}, {5, 6}}, | |
{{-5, 0}, {0, 0}}, | |
{{-5, -7}, {0, 0}}, | |
{{-5, -3}, {5, 7}}, | |
{{10, 11}, {0, 0}}, | |
{{15, 15}, {0, 0}}, | |
{{14, 4}, {0, 0}}, | |
{{4, 14}, {4, 9}}, | |
{{-14, -14}, {0, 0}}, | |
{{-14, -4}, {1, 6}}, | |
{{-4, -14}, {0, 0}}, | |
{{-4, -4}, {6, 6}} | |
} | |
NormalizeIndexRange = function(MaximumPosition, Begin, End) | |
if not type(MaximumPosition) == "number" then | |
MaximumPosition = #MaximumPosition | |
end | |
local Valid = not (Begin > MaximumPosition or End and (End < Begin or End < -MaximumPosition or End == 0)) | |
local FirstReturnValue = 0 | |
if Valid then | |
if Begin < 0 then | |
FirstReturnValue = MaximumPosition + Begin + 1 | |
else | |
FirstReturnValue = Begin | |
end | |
FirstReturnValue = math.max(FirstReturnValue,1) | |
end | |
local SecondReturnValue = 0 | |
if Valid then | |
if End then | |
local Bottom | |
if End < 0 then | |
if End == Begin then | |
if Begin < 0 then | |
Bottom = MaximumPosition + Begin + 1 | |
else | |
Bottom = Begin | |
end | |
Bottom = math.max(Bottom, 1) | |
else | |
Bottom = MaximumPosition + End + 1 | |
end | |
else | |
if Begin < 0 then | |
Bottom = MaximumPosition + Begin + End | |
else | |
if End < Begin then | |
Bottom = Begin | |
else | |
Bottom = End | |
end | |
end | |
end | |
Bottom = math.max(Bottom, 1) | |
SecondReturnValue = math.min(Bottom, MaximumPosition) | |
else | |
SecondReturnValue = MaximumPosition | |
end | |
end | |
return FirstReturnValue, SecondReturnValue | |
end | |
for _, Test in next, Tests do | |
local Result = {NormalizeIndexRange(#Table, Test[1][1], Test[1][2])} | |
print((Result[1] == Test[2][1] and Result[2] == Test[2][2] and "Passed! " or "Fail! ") .. "Inputted " .. tostring(Test[1][1]) .. ", " .. tostring(Test[1][2]) .. ", expected " .. tostring(Test[2][1]) .. ", " .. tostring(Test[2][2]) .. ", got " .. tostring(Result[1]) .. ", " .. tostring(Result[2])) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment