2017-06-05 18:38:44 +00:00
|
|
|
|
2017-07-17 11:36:56 +00:00
|
|
|
local SUITE = require 'TestSuite' 'sorting'
|
2017-06-05 18:38:44 +00:00
|
|
|
SUITE:setEnviroment{
|
|
|
|
pretty = require('pretty')
|
|
|
|
}
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2017-06-25 10:18:24 +00:00
|
|
|
-- Compat
|
|
|
|
if not loadstring then loadstring = load end -- Lua 5.3 compat
|
|
|
|
local HAS_ADV_GETLOCAL = not not debug.getinfo(1, 'u').nparams -- Lua 5.1 compat
|
|
|
|
local HAS_UNICODE_IDEN = not not loadstring 'local ϕ = 1; return ϕ' -- Lua 5.1 compat
|
|
|
|
local HAS_JIT_LIBRARY = type(rawget(_G, 'jit')) == 'table' -- Non-LuaJIT compat
|
|
|
|
--
|
|
|
|
|
2017-06-05 18:38:44 +00:00
|
|
|
local function format_test (t)
|
|
|
|
SUITE:addTest(t.name or t.expect:gsub('[ \n\t]+', ' '), function ()
|
|
|
|
assert_equal(t.expect, pretty(t.input, t.options))
|
|
|
|
end, { line = debug.getinfo(2).currentline })
|
|
|
|
end
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Table Sorting
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { 'c', 'b', 'a' },
|
|
|
|
expect = '{ \'c\', \'b\', \'a\' }',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { a = 1, a1 = 0 },
|
|
|
|
expect = '{ a = 1, a1 = 0 }',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { a10 = 1, a1 = 0 },
|
|
|
|
expect = '{ a1 = 0, a10 = 1 }',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { a00 = 0, a1 = 1 },
|
|
|
|
expect = '{ a00 = 0, a1 = 1 }',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { a = {}, b = true },
|
|
|
|
expect = '{ b = true, a = {} }',
|
|
|
|
}
|
|
|
|
|
2017-06-25 11:16:34 +00:00
|
|
|
format_test {
|
|
|
|
name = 'Keys should be sorted such that uppercase and lowercase lies near each other',
|
|
|
|
input = { Dave = 1, dave = 2, mary = 3, Mary = 4, Adam = 5, adam = 6 },
|
2017-07-20 10:01:26 +00:00
|
|
|
expect = '{\n Adam = 5, adam = 6, Dave = 1,\n dave = 2, Mary = 4, mary = 3\n}',
|
2017-06-25 11:16:34 +00:00
|
|
|
}
|
|
|
|
|
2017-06-05 18:38:44 +00:00
|
|
|
format_test {
|
|
|
|
input = { a = {}, b = true, b1 = false },
|
|
|
|
expect = '{ b = true, b1 = false, a = {} }',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { {}, true, false, 5 },
|
|
|
|
expect = '{ {}, true, false, 5 }',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { [1] = {}, [2] = {}, [3] = {}, ['down_info'] = {}, ['up_info'] = {} },
|
2017-06-09 15:24:05 +00:00
|
|
|
expect = '{\n [1] = {},\n [2] = {},\n [3] = {},\n [\'down_info\'] = {},\n [\'up_info\'] = {}\n}',
|
2017-06-05 18:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
input = { 'hello', 123, {}, true },
|
|
|
|
expect = '{ \'hello\', 123, {}, true }',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
name = 'Small sequence holes should be filled with nil',
|
|
|
|
input = { 1, nil, 3 },
|
|
|
|
expect = '{ 1, nil, 3 }',
|
|
|
|
}
|
|
|
|
|
2017-08-07 08:39:45 +00:00
|
|
|
format_test {
|
|
|
|
name = 'Proper sorting of number keys',
|
|
|
|
input = { [-1/0] = 'a', [-100] = 'b', [-1] = 'c', [0] = 'd', [1] = 'e', [100] = 'f', [1/0] = 'g' },
|
|
|
|
expect = '{\n [-1/0] = \'a\', [-100] = \'b\',\n [-1] = \'c\', [0] = \'d\',\n [1] = \'e\', [100] = \'f\',\n [1/0] = \'g\'\n}',
|
|
|
|
}
|
|
|
|
|
|
|
|
format_test {
|
|
|
|
name = 'Proper sorting of number strings keys',
|
2017-10-21 11:24:33 +00:00
|
|
|
input = { ['-100'] = 'a', ['-1'] = 'b', ['0'] = 'c', ['1'] = 'd', ['100'] = 'e' },
|
2017-08-07 08:39:45 +00:00
|
|
|
expect = '{\n [\'-100\'] = \'b\', [\'-1\'] = \'c\',\n [\'0\'] = \'d\', [\'1\'] = \'e\',\n [\'100\'] = \'f\'\n}',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-20 08:38:24 +00:00
|
|
|
--[[ Sorting is hard in unicode, and I can't be bothered.
|
|
|
|
format_test {
|
|
|
|
name = 'Unicode: ø comes before å in danish',
|
|
|
|
input = loadstring 'return { øl = 1, ål = 2 }' (),
|
|
|
|
expect = '{ øl = 1, ål = 2 }',
|
|
|
|
}
|
|
|
|
--]]
|
2017-06-15 17:03:42 +00:00
|
|
|
|
2017-06-05 18:38:44 +00:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Alphanum Algorithm Example 1
|
|
|
|
|
|
|
|
|
|
|
|
local EXAMPLE_1_INPUT = {
|
|
|
|
['z1.doc'] = 1,
|
|
|
|
['z10.doc'] = 1,
|
|
|
|
['z100.doc'] = 1,
|
|
|
|
['z101.doc'] = 1,
|
|
|
|
['z102.doc'] = 1,
|
|
|
|
['z11.doc'] = 1,
|
|
|
|
['z12.doc'] = 1,
|
|
|
|
['z13.doc'] = 1,
|
|
|
|
['z14.doc'] = 1,
|
|
|
|
['z15.doc'] = 1,
|
|
|
|
['z16.doc'] = 1,
|
|
|
|
['z17.doc'] = 1,
|
|
|
|
['z18.doc'] = 1,
|
|
|
|
['z19.doc'] = 1,
|
|
|
|
['z2.doc'] = 1,
|
|
|
|
['z20.doc'] = 1,
|
|
|
|
['z3.doc'] = 1,
|
|
|
|
['z4.doc'] = 1,
|
|
|
|
['z5.doc'] = 1,
|
|
|
|
['z6.doc'] = 1,
|
|
|
|
['z7.doc'] = 1,
|
|
|
|
['z8.doc'] = 1,
|
|
|
|
['z9.doc'] = 1,
|
|
|
|
}
|
|
|
|
|
2017-06-09 15:24:05 +00:00
|
|
|
local EXAMPLE_1_OUTPUT = ([[{
|
2017-07-20 10:01:26 +00:00
|
|
|
['z1.doc'] = 1, ['z2.doc'] = 1,
|
|
|
|
['z3.doc'] = 1, ['z4.doc'] = 1,
|
|
|
|
['z5.doc'] = 1, ['z6.doc'] = 1,
|
|
|
|
['z7.doc'] = 1, ['z8.doc'] = 1,
|
|
|
|
['z9.doc'] = 1, ['z10.doc'] = 1,
|
|
|
|
['z11.doc'] = 1, ['z12.doc'] = 1,
|
|
|
|
['z13.doc'] = 1, ['z14.doc'] = 1,
|
|
|
|
['z15.doc'] = 1, ['z16.doc'] = 1,
|
|
|
|
['z17.doc'] = 1, ['z18.doc'] = 1,
|
|
|
|
['z19.doc'] = 1, ['z20.doc'] = 1,
|
|
|
|
['z100.doc'] = 1, ['z101.doc'] = 1,
|
2017-06-05 18:38:44 +00:00
|
|
|
['z102.doc'] = 1
|
2017-06-09 15:24:05 +00:00
|
|
|
}]]):gsub('\t', ' ')
|
2017-06-05 18:38:44 +00:00
|
|
|
|
|
|
|
SUITE:addTest('alphanum algorithm example 1', function ()
|
|
|
|
-- This is a test-case taken from http://www.davekoelle.com/alphanum.html
|
|
|
|
assert_equal(EXAMPLE_1_OUTPUT, pretty(EXAMPLE_1_INPUT))
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Alphanum Algorithm Example 2
|
|
|
|
|
|
|
|
local EXAMPLE_2_INPUT = {
|
|
|
|
['1000X Radonius Maximus'] = 1,
|
|
|
|
['10X Radonius'] = 1,
|
|
|
|
['200X Radonius'] = 1,
|
|
|
|
['20X Radonius'] = 1,
|
|
|
|
['20X Radonius Prime'] = 1,
|
|
|
|
['30X Radonius'] = 1,
|
|
|
|
['40X Radonius'] = 1,
|
|
|
|
['Allegia 50 Clasteron'] = 1,
|
|
|
|
['Allegia 500 Clasteron'] = 1,
|
|
|
|
['Allegia 50B Clasteron'] = 1,
|
|
|
|
['Allegia 51 Clasteron'] = 1,
|
|
|
|
['Allegia 6R Clasteron'] = 1,
|
|
|
|
['Alpha 100'] = 1,
|
|
|
|
['Alpha 2'] = 1,
|
|
|
|
['Alpha 200'] = 1,
|
|
|
|
['Alpha 2A'] = 1,
|
|
|
|
['Alpha 2A-8000'] = 1,
|
|
|
|
['Alpha 2A-900'] = 1,
|
|
|
|
['Callisto Morphamax'] = 1,
|
|
|
|
['Callisto Morphamax 500'] = 1,
|
|
|
|
['Callisto Morphamax 5000'] = 1,
|
|
|
|
['Callisto Morphamax 600'] = 1,
|
|
|
|
['Callisto Morphamax 6000 SE'] = 1,
|
|
|
|
['Callisto Morphamax 6000 SE2'] = 1,
|
|
|
|
['Callisto Morphamax 700'] = 1,
|
|
|
|
['Callisto Morphamax 7000'] = 1,
|
|
|
|
['Xiph Xlater 10000'] = 1,
|
|
|
|
['Xiph Xlater 2000'] = 1,
|
|
|
|
['Xiph Xlater 300'] = 1,
|
|
|
|
['Xiph Xlater 40'] = 1,
|
|
|
|
['Xiph Xlater 5'] = 1,
|
|
|
|
['Xiph Xlater 50'] = 1,
|
|
|
|
['Xiph Xlater 500'] = 1,
|
|
|
|
['Xiph Xlater 5000'] = 1,
|
|
|
|
['Xiph Xlater 58'] = 1,
|
|
|
|
}
|
|
|
|
|
2017-06-09 15:24:05 +00:00
|
|
|
local EXAMPLE_2_OUTPUT = ([[{
|
2017-06-05 18:38:44 +00:00
|
|
|
['10X Radonius'] = 1,
|
|
|
|
['20X Radonius'] = 1,
|
|
|
|
['20X Radonius Prime'] = 1,
|
|
|
|
['30X Radonius'] = 1,
|
|
|
|
['40X Radonius'] = 1,
|
|
|
|
['200X Radonius'] = 1,
|
|
|
|
['1000X Radonius Maximus'] = 1,
|
|
|
|
['Allegia 6R Clasteron'] = 1,
|
|
|
|
['Allegia 50 Clasteron'] = 1,
|
|
|
|
['Allegia 50B Clasteron'] = 1,
|
|
|
|
['Allegia 51 Clasteron'] = 1,
|
|
|
|
['Allegia 500 Clasteron'] = 1,
|
|
|
|
['Alpha 2'] = 1,
|
|
|
|
['Alpha 2A'] = 1,
|
|
|
|
['Alpha 2A-900'] = 1,
|
|
|
|
['Alpha 2A-8000'] = 1,
|
|
|
|
['Alpha 100'] = 1,
|
|
|
|
['Alpha 200'] = 1,
|
|
|
|
['Callisto Morphamax'] = 1,
|
|
|
|
['Callisto Morphamax 500'] = 1,
|
|
|
|
['Callisto Morphamax 600'] = 1,
|
|
|
|
['Callisto Morphamax 700'] = 1,
|
|
|
|
['Callisto Morphamax 5000'] = 1,
|
|
|
|
['Callisto Morphamax 6000 SE'] = 1,
|
|
|
|
['Callisto Morphamax 6000 SE2'] = 1,
|
|
|
|
['Callisto Morphamax 7000'] = 1,
|
|
|
|
['Xiph Xlater 5'] = 1,
|
|
|
|
['Xiph Xlater 40'] = 1,
|
|
|
|
['Xiph Xlater 50'] = 1,
|
|
|
|
['Xiph Xlater 58'] = 1,
|
|
|
|
['Xiph Xlater 300'] = 1,
|
|
|
|
['Xiph Xlater 500'] = 1,
|
|
|
|
['Xiph Xlater 2000'] = 1,
|
|
|
|
['Xiph Xlater 5000'] = 1,
|
|
|
|
['Xiph Xlater 10000'] = 1
|
2017-06-09 15:24:05 +00:00
|
|
|
}]]):gsub('\t', ' ')
|
2017-06-05 18:38:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
SUITE:addTest('alphanum algorithm example 2', function ()
|
|
|
|
-- This is a test-case taken from http://www.davekoelle.com/alphanum.html
|
|
|
|
assert_equal(EXAMPLE_2_OUTPUT, pretty(EXAMPLE_2_INPUT))
|
|
|
|
end)
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
SUITE:addTest('alphanum algorithm extension 1', function ()
|
|
|
|
-- This is a test-case taken from http://www.davekoelle.com/alphanum.html
|
2017-07-20 10:01:26 +00:00
|
|
|
local OUTPUT = "{\n ['z2'] = 1, ['z2.'] = 1,\n ['z2.z'] = 1, ['z2.0'] = 1\n}"
|
2017-06-05 18:38:44 +00:00
|
|
|
assert_equal(OUTPUT, pretty { ['z2.z'] = 1, ['z2.0'] = 1, ['z2.'] = 1, ['z2'] = 1 })
|
|
|
|
end)
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
return SUITE
|