local tests = require "Module:UnitTests"
local stable_sort = require "Module:stable sort"
local function format_array(arr)
return table.concat(arr, ";")
end
local function format_array_single_digit(arr)
return table.concat(arr, "")
end
local function copy_array(arr)
local copy = { }
for i = 1, #arr do copy = arr end
return copy
end
local function comp(lhs, rhs)
return math.floor(lhs / 10) < math.floor(rhs / 10)
end
function tests:test_sort()
local testcases = {
{ { 2, 7, 1, 9, 9, 5, 7, 0, 0, 7, 5, 7, 2, 9, 2, 9, 1, 0, 4, 6, 9, 7, 4, 7, 7, 8, 0, 8, 6, 7, 2, 3, 1, 8, 2, 4, 8, 9, 0, 8, 7, 7, 3, 1, 4, 2, 5, 9, 5, 0, 1, 5, 6, 9, 5, 6, 3, 3, 8, 6, 6, 1, 0, 6, 7, 6, 3, 8, 3, 7, 9, 9, 6, 8, 7, 3, 0, 9, 4, 6, 0, 2, 1, 9, 0, 9, 1, 3, 7, 4, 8, 5, 2, 3, 8, 4, 5, 3, 3, 5, 4, 7, 5, 3, 3, 4, 3, 5, 8, 3, 4, 1, 9, 0, 0, 5, 6, 1, 8, 0, 2, 1, 2, 8, 5, 7, 8, 2, 3, 0, 0, 7, 5, 3, 5, 5, 4 }, "00000000000000011111111111222222222223333333333333333344444444444555555555555555566666666666777777777777777778888888888888899999999999999" },
}
self:iterate(testcases,
function (self, arr, expected)
local sorted = copy_array(arr)
stable_sort(sorted)
self:equals("<samp>#" .. #arr .. ": " .. format_array_single_digit(arr) .. "</samp>", format_array_single_digit(sorted), expected, options)
end)
end
function tests:test_stable_sort()
local testcases = {
{ { 1 }, "1" },
{ { 10, 20 }, "10;20" },
{ { 20, 10 }, "10;20" },
{ { 10, 20, 30 }, "10;20;30" },
{ { 30, 20, 10 }, "10;20;30" },
{ { 80, 40, 30, 70, 10, 50, 90, 60, 20 }, "10;20;30;40;50;60;70;80;90" },
{ { 59, 83, 34, 74, 40, 90, 73, 48, 94, 33 }, "34;33;40;48;59;74;73;83;90;94" },
{ { 67, 74, 48, 27, 96, 89, 78, 86, 64, 21, 53, 18, 47, 82, 88, 32 }, "18;27;21;32;48;47;53;67;64;74;78;89;86;82;88;96" },
{ { 33, 79, 80, 26, 57, 56, 89, 82, 77, 95, 68, 35, 63, 41, 85, 93, 69, 36, 29, 62 }, "26;29;33;35;36;41;57;56;68;63;69;62;79;77;80;89;82;85;95;93" },
{ { 32, 14, 72, 53, 39, 96, 75, 19, 90, 76, 20, 28, 83, 92, 84, 30, 64, 94, 27, 57, 50, 80, 35, 36, 22, 81, 42, 10, 24, 25, 71 }, "14;19;10;20;28;27;22;24;25;32;39;30;35;36;42;53;57;50;64;72;75;76;71;83;84;80;81;96;90;92;94" },
{ { 74, 87, 76, 54, 70, 27, 69, 21, 45, 57, 64, 84, 79, 43, 37, 95, 88, 46, 85, 97, 36, 81, 58, 16, 96, 66, 26, 10, 23, 78, 11, 55 }, "16;10;11;27;21;26;23;37;36;45;43;46;54;57;58;55;69;64;66;74;76;70;79;78;87;84;88;85;81;95;97;96" },
{ { 77, 59, 85, 27, 11, 73, 47, 86, 67, 32, 14, 84, 36, 13, 98, 35, 72, 49, 25, 64, 10, 89, 19, 80, 21, 46, 92, 78, 53, 83, 69, 41, 16 }, "11;14;13;10;19;16;27;25;21;32;36;35;47;49;46;41;59;53;67;64;69;77;73;72;78;85;86;84;89;80;83;98;92" },
{ { 82, 13, 93, 74, 92, 12, 44, 94, 11, 97, 87, 64, 41, 54, 84, 72, 65, 50, 51, 35, 63, 18, 75, 81, 99, 73, 42, 53, 58, 25, 37, 71, 24, 59, 88, 61, 89, 47, 68, 26, 36, 38, 70, 45, 62, 67, 69, 23, 98, 46, 78, 28, 85, 90, 66, 30, 33, 91, 22, 20, 27, 79, 95 }, "13;12;11;18;25;24;26;23;28;22;20;27;35;37;36;38;30;33;44;41;42;47;45;46;54;50;51;53;58;59;64;65;63;61;68;62;67;69;66;74;72;75;73;71;70;78;79;82;87;84;81;88;89;85;93;92;94;97;99;98;90;91;95" },
{ { 94, 88, 71, 43, 39, 46, 76, 35, 82, 64, 85, 97, 62, 34, 54, 26, 48, 52, 75, 41, 81, 57, 60, 61, 36, 51, 40, 87, 70, 92, 96, 67, 53, 99, 14, 22, 13, 59, 74, 50, 28, 93, 68, 66, 86, 17, 65, 10, 31, 91, 32, 23, 33, 24, 11, 63, 78, 19, 83, 84, 45, 55, 77, 27 }, "14;13;17;10;11;19;26;22;28;23;24;27;39;35;34;36;31;32;33;43;46;48;41;40;45;54;52;57;51;53;59;50;55;64;62;60;61;67;68;66;65;63;71;76;75;70;74;78;77;88;82;85;81;87;86;83;84;94;97;92;96;99;93;91" },
{ { 67, 93, 74, 27, 96, 60, 41, 88, 69, 90, 51, 59, 82, 85, 39, 78, 11, 86, 40, 72, 31, 10, 43, 48, 98, 38, 52, 32, 21, 76, 44, 61, 29, 57, 14, 46, 50, 30, 63, 16, 62, 28, 87, 25, 95, 91, 23, 53, 99, 33, 83, 13, 37, 34, 71, 79, 55, 42, 35, 45, 22, 68, 24, 65, 36 }, "11;10;14;16;13;27;21;29;28;25;23;22;24;39;31;38;32;30;33;37;34;35;36;41;40;43;48;44;46;42;45;51;59;52;57;50;53;55;67;60;69;61;63;62;68;65;74;78;72;76;71;79;88;82;85;86;87;83;93;96;90;98;95;91;99" },
{ { 30, 48, 89, 54, 86, 91, 62, 24, 70, 52, 15, 80, 17, 19, 26, 14, 27, 34, 77, 61, 66, 58, 40, 28, 99, 90, 29, 69, 72, 73, 83, 81, 36, 60, 92, 31, 50, 98, 43, 16, 21, 10, 13, 94, 78, 55, 67, 64, 11, 87, 57, 18, 93, 63, 97, 88, 59, 22, 82, 95, 42, 12, 68, 56, 38, 49, 45, 65, 47, 44, 23, 96, 20, 79, 51, 74, 85, 37, 35, 75, 33, 41, 71, 39, 84, 76, 53, 25, 46, 32 }, "15;17;19;14;16;10;13;11;18;12;24;26;27;28;29;21;22;23;20;25;30;34;36;31;38;37;35;33;39;32;48;40;43;42;49;45;47;44;41;46;54;52;58;50;55;57;59;56;51;53;62;61;66;69;60;67;64;63;68;65;70;77;72;73;78;79;74;75;71;76;89;86;80;83;81;87;88;82;85;84;91;99;90;92;98;94;93;97;95;96" },
}
self:iterate(testcases,
function (self, arr, expected)
local sorted = copy_array(arr)
stable_sort(sorted, comp)
self:equals("<samp>#" .. #arr .. ": " .. format_array(arr) .. "</samp>", format_array(sorted), expected, options)
end)
end
return tests