-- Kør: luajit tests/test_adCache.lua (fra repo-roden) local failures = 0 local function check(name, actual, expected) if actual == expected then print("OK " .. name) else failures = failures + 1 print(string.format("FAIL %s: forventede %s, fik %s", name, tostring(expected), tostring(actual))) end end g_currentMission = {time = 0} dofile("FS25_ADSmartPickup/adCache.lua") local calls = 0 local function lookup() calls = calls + 1; return "station" end check("K1 slået fra: altid opslag", ADCacheGet("b", "k", 1000, lookup) .. ADCacheGet("b", "k", 1000, lookup), "stationstation") check("K1 to opslag", calls, 2) ADCache.enabled = true calls = 0 ADCacheGet("b", "k", 1000, lookup); ADCacheGet("b", "k", 1000, lookup) check("K2 genbrugt", calls, 1) g_currentMission.time = 900 ADCacheGet("b", "k", 1000, lookup) check("K2 inden for levetid", calls, 1) g_currentMission.time = 1600 ADCacheGet("b", "k", 1000, lookup) check("K3 efter levetid + spredning (maks 1500) -> nyt opslag", calls, 2) local nothing = 0 ADCacheGet("n", "x", 1000, function() nothing = nothing + 1; return nil end) check("K4 intet fundet huskes", ADCacheGet("n", "x", 1000, function() nothing = nothing + 1; return "ny" end), nil) check("K4 kun ét opslag", nothing, 1) -- spredt udløb: 200 poster lavet samtidig udløber ikke samtidig g_currentMission.time = 10000 for i = 1, 200 do ADCacheGet("spread", i, 10000, function() return i end) end local expired = 0 g_currentMission.time = 22000 for i = 1, 200 do ADCacheGet("spread", i, 10000, function() expired = expired + 1; return i end) end check("K5 kun en del udløbet efter 12 s (spredt 10-15 s)", expired > 20 and expired < 180, true) ADCache.clear() check("K6 ryddet", next(ADCache.buckets), nil) print(failures == 0 and "\nALLE TESTS OK" or ("\n" .. failures .. " FEJL")) os.exit(failures == 0 and 0 or 1)