diff options
| author | ruki <[email protected]> | 2023-10-01 21:14:20 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-10-01 21:14:20 +0800 |
| commit | 4ea0573047db301f1569c9d6b0d117f92433db49 (patch) | |
| tree | f495cd7b7286c3d81f5a209515b662da4f0c5002 /tests/modules/table | |
| parent | 15e5f277191e8a088998d0f797dd1f44b5491e17 (diff) | |
| parent | 85a605758c47eaa92b0a22b028cc4896eceeaf3f (diff) | |
Merge pull request #4253 from A2va/orderkeys-sort-function
Sort keys by callback function
Diffstat (limited to 'tests/modules/table')
| -rw-r--r-- | tests/modules/table/test.lua | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/tests/modules/table/test.lua b/tests/modules/table/test.lua index 007782cf4..2b586e7a4 100644 --- a/tests/modules/table/test.lua +++ b/tests/modules/table/test.lua @@ -29,3 +29,18 @@ function test_unwrap(t) local a = table.wrap_lock({1}) t:are_equal(table.unwrap(a), a) end + +function test_orderkeys(t) + -- sort by modulo 2 then from the smallest to largest + local f = function(a, b) + if a % 2 == 0 and b % 2 ~= 0 then + return true + elseif b % 2 == 0 and a % 2 ~= 0 then + return false + end + return a < b + end + + t:are_equal(table.orderkeys({[2] = 2, [1] = 1, [4] = 4, [3] = 3}, f), {2, 4, 1, 3}) + t:are_equal(table.orderkeys({[1] = 1, [2] = 2, [3] = 3, [4] = 4}), {1, 2 , 3, 4}) +end |
