Пређи на садржај

Модул:HeavyTest

Iz Vojne Enciklopedije

Документацију овог модула можете да направите на страници Модул:HeavyTest/док

-- Module:HeavyTest
-- Težak test za Scribunto/LuaSandbox (tabele, string, mw.*, JSON, hash, title)

local p = {}

local function makeDataset(n)
    local t = {}
    for i = 1, n do
        -- napravimo pseudo-nasumičan tekst, malo unicode, malo ascii
        local s = ("Ред-%04d | abcDEF | čćšđž | %s"):format(i, tostring(i * 7919 % 104729))
        t[i] = {
            id = i,
            text = s,
            len = mw.ustring.len(s),
            hash = mw.hash.hashValue("sha1", s)
        }
    end
    return t
end

local function stats(dataset)
    local minLen, maxLen, sumLen = 10^9, 0, 0
    for i = 1, #dataset do
        local l = dataset[i].len
        if l < minLen then minLen = l end
        if l > maxLen then maxLen = l end
        sumLen = sumLen + l
    end
    return minLen, maxLen, sumLen, (sumLen / #dataset)
end

function p.run(frame)
    local n = tonumber(frame.args.n) or 1200  -- povećaj na 2000 ako želiš jači test
    local titleText = frame.args.title or "Главна страна"

    local dataset = makeDataset(n)

    -- sortiranje po hash-u (teže od sortiranja po id)
    table.sort(dataset, function(a, b) return a.hash < b.hash end)

    -- statistika dužina stringova
    local minLen, maxLen, sumLen, avgLen = stats(dataset)

    -- JSON round-trip test
    local json = mw.text.jsonEncode({count = n, sample = dataset[1]})
    local decoded = mw.text.jsonDecode(json)

    -- title test (proverava da mw.title radi)
    local t = mw.title.new(titleText)

    -- generiši mali wiki izlaz (ne previše, da ne ubije stranicu)
    local out = {}
    out[#out+1] = "LuaHeavyTest ✅"
    out[#out+1] = ""
    out[#out+1] = ("* Broj elemenata: **%d**"):format(n)
    out[#out+1] = ("* Min dužina: **%d**"):format(minLen)
    out[#out+1] = ("* Max dužina: **%d**"):format(maxLen)
    out[#out+1] = ("* Prosek dužine: **%.2f**"):format(avgLen)
    out[#out+1] = ("* JSON decode count: **%s**"):format(tostring(decoded.count))
    out[#out+1] = ("* Title postoji: **%s**"):format(t and t.exists and tostring(t:exists()) or "n/a")
    out[#out+1] = ""
    out[#out+1] = "== Prvih 10 elemenata (posle sortiranja) =="
    out[#out+1] = '{| class="wikitable"'
    out[#out+1] = "! # !! id !! len !! sha1 (skraćeno) !! text"
    for i = 1, math.min(10, #dataset) do
        local row = dataset[i]
        out[#out+1] = "|-"
        out[#out+1] = ("| %d || %d || %d || %s || %s"):format(
            i, row.id, row.len, mw.ustring.sub(row.hash, 1, 12), mw.text.nowiki(row.text)
        )
    end
    out[#out+1] = "|}"
    return table.concat(out, "\n")
end

return p