1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at http://mozilla.org/MPL/2.0/.
local string = require "string"
local util = require "lsb.util"
local function alpha()
return {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z"}
end
local function behead_array()
local a = alpha()
util.behead_array(10, a)
assert(a[1] == "j", string.format("a[1] should be 'j', is '%s'", tostring(a[1])))
assert(#a == 17, string.format("#a should be 17, is %d", #a))
a = alpha()
util.behead_array(16, a)
assert(a[1] == "p", string.format("a[1] should be 'p', is '%s'", tostring(a[1])))
assert(#a == 11, string.format("#a should be 11, is %d", #a))
a = alpha()
util.behead_array(0, a)
assert(a[1] == "a", string.format("a[1] should be 'a', is '%s'", tostring(a[1])))
assert(#a == 26, string.format("#a should be 26, is %d", #a))
a = alpha()
util.behead_array(45, a)
assert(a[1] == nil, string.format("a[1] should be 'nil', is '%s'", tostring(a[1])))
assert(#a == 0, string.format("#a should be 0, is %d", #a))
end
local function pairs_by_key()
local hash = { a = true , b = true, c = true }
local abc = {"a", "b", "c"}
local cnt = 1
for k, v in util.pairs_by_key(hash) do
assert(abc[cnt] == k)
cnt = cnt + 1
end
cnt = 3
for k, v in util.pairs_by_key(hash, function(a, b) return a > b end) do
assert(abc[cnt] == k)
cnt = cnt - 1
end
end
local function merge_objects()
local a = {
foo = 1,
bar = {1, 1, 3},
quux = 3
}
local b = {
foo = 5,
bar = {0, 0, 5, 1},
baz = {
hello = 100
}
}
local c = util.merge_objects(a, b)
assert(c.foo == 6)
assert(c.bar[1] == 1)
assert(c.bar[2] == 1)
assert(c.bar[3] == 8)
assert(c.bar[4] == 1)
assert(c.baz.hello == 100)
assert(c.quux == 3)
end
behead_array()
pairs_by_key()
merge_objects()
|