I am trying to learn code coverage analysis; I am bit confused because the following code has loops.
Example if the code like this
read a;
read b;
i=0
if a>b
while(to i<a)
print(i)
i++;
end while
else
while(to i<b)
print(i)
i++;
end while
end if;
what is the percentage of statement coverage if a = 5 and b =7 ?
Related
I am trying to find a solution to the problem "Two Sum" if you recognize it , and I've run into a problem and I cannot figure it out (Lua)
Code:
num = {2,7,11,15}
target = 9
current = 0
repeat
createNum1 = tonumber(num[math.random(1,#num)])
createNum2 = tonumber(num[math.random(1,#num)])
current = createNum1 + createNum2
until current == target
print(table.find(num,createNum1), table.find(num,createNum2))
Error:
lua5.3: HelloWorld.lua:9: attempt to call a nil value (field 'find')
stack traceback:
HelloWorld.lua:9: in main chunk
[C]: in ?
Thank you!
Lua has no table.find function in its very small standard library; just take a look at the reference manual.
You could implement your own table.find function, but that would just be monkey-patching an overall broken algorithm. There is no need to use a probabilistic algorithm that probably runs in at least quadratic time if there only is one pair of numbers that adds up to the desired number. Instead, you should leverage Lua's tables - associative arrays - here. First build an index of [number] = last index:
local num = {2,7,11,15}
local target = 9
local idx = {}
for i, n in ipairs(num) do idx[n] = i end
then loop over the numbers; given a number m you just need to look for target - m in your idx lookup:
for i, n in ipairs(num) do local j = idx[target - n]; if j then print(i, j) break end end
if you want to exit early - sometimes without building the full idx table - you can fuse the two loops:
local idx = {}
for i, n in ipairs(num) do
local j = idx[target - n]
if j then
print(j, i)
break
end
idx[n] = i
end
other solutions exist (e.g. using sorting, which requires no auxiliary space), but this one is elegant in that it runs in O(n) time & O(n) space to produce a solution and leverages Lua's builtin data structures.
What does this code do?
It's all Lua for World of Warcraft 3.5.5
function __(r) local d = string.sub(r,0x0001,string.len(r)-0x0040) local k = string.sub(r,string.len(r)-0x003F, string.len(r)) d = string.gsub(d, '[^'..k..'=]', '') return (d:gsub('.', function(x) if (x == '=') then return '' end local r,f='',(k:find(x)-1) for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end return r; end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x) if (#x ~= 8) then return '' end local c=0 for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end return string.char(c) end)) end _={_=_G} _._["\108\111\097\100\115\116\114\105\110\103"](_._["\095\095"]("SuperLongStringThatWasRemovedForPrivacyReasons"))()
Now what I am guessing is that this is some sort of encrypted code or something? I am not really sure. What do you guys think it is? / Do you know what this code would do when ran?
NOTE: The really long string... really is long. It's about 150,000 characters long.
The code is equivalent to
loadstring(
decode_from_base64(
("SuperLongStringThatWasRemovedForPrivacyReasons"):sub(1, -65)
)
)()
So, SuperLongStringThatWasRemovedForPrivacyReasons is actually a base-64 encoded Lua program (program may be Lua source or Lua bytecode).
You can easily decode it yourself, there are a lot of online base-64 decoders available.
I'm learning Lua and trying to create a simple coroutine. In Lua 5.1, the code below gives the error: "attempt to yield across metamethod/C-call boundary." I've read about that limitation and I can't see how it applies to my code. I tried it in Lua 5.2 and got "attempt to yield from outside a coroutine," which is equally confusing to me. I'm sure the answer will be embarrassingly obvious!
output = {}
done = false
function mainLoop()
while not done do
if co == nil then
co = coroutine.create(subLoop())
elseif coroutine.status(co) == "suspended" then
print(output[k])
coroutine.resume(co)
elseif coroutine.status(co) == "dead" then
done = true
end
end
end
function subLoop()
for k=1, 20 do
table.insert(output, "This is line " .. k .. " of the test output")
coroutine.yield()
end
end
mainLoop()
You are calling subLoop
if co == nil then
co = coroutine.create(subLoop())
instead of passing it to coroutine.create
if co == nil then
co = coroutine.create(subLoop)
This results in you attempting to yield from the main state / (not-really-)coroutine, which gives errors with varying descriptions across versions.
I have signal:
signal sig: std_logic_vector(N - 1 downto 0);
Where N defined in generic, and can be from 16 to 1024.
In code i need to compare this to zero:
if unsigned(sig) = 0 then
do somth
end if;
But how can I know which delay would be of such comparator?
My design work on 100 MHz, so I need to make some divider, which will skip some tackts to obtain result, something like that:
constant CHECK_TACKTS : natural := 100;
signal check : boolean;
signal wait_check_cntr: natural range 0 to CHECK_TACKTS;
-- states
when SOME_STATE=>
check <= unsigned(sig) = 0;
wait_check_cntr <= 0;
state <= CHECK_ZERO
when CHECK_ZERO =>
if wait_check_cntr = CHECK_TACKTS then
if check then
--do somth
end if;
else
wait_check_cntr <= wait_check_cntr + 1;
end if;
But how can I calculate CHECK_TACKTS, if I know, thet counter period is 10 ns? If xilinx synthes tool build full compare tree on luts, seems like compare time can be proportional to log2(N), but what about lut time? Of course I can do research and measure timings from report on several points and than perform regression, but may be there are simpiler way?
The easiest way to pipeline your comparaison operation on Xilinx is to let the tool do it for you. You need to activate the "register balancing" option and use syntax such as:
if rising_edge(clk) then
check_0 <= unsigned(sig) = 0;
check_1 <= check_0;
check <= check_1;
end if;
XST (or Vivado) will distribute the compare operation on three cycles (for that case).
If you prefer not to rely on the synthesis tool, you can manually divide the operation yourself:
if rising_edge(clk) then
check_msb <= unsigned(sig'left downto sig'length/2) = 0;
check_lsb <= unsigned(sig'length/2-1 downto 0) = 0;
check <= check_msb and check_lsb;
end if;
This may not be the optimal way to balance the comparaison, but the VHDL code is simple, easy to modify and to understand.
function findWord(s,i)
-- find first word in given text
local j = i+1
while not _isWhite(s:byte(j)) and j < #s do -- getting error here
j = j + 1
end
return s:sub(i,j), j
end
function splitText(s,maxLen)
-- split text into chunks of maxLen length
rs ={}
local function _g(s,i,c,rs)
-- recursively split text
local function _f(s,i,c)
-- recursively find words and add each word to a chunk
local w,i = findWord(s,i)
if i == #s then return c..w end
if #(c..w) <= maxLen then
c = c..w
s = s:sub(i+1,#s,true)
return _f(s,1,c)
else
return c
end
end
rs[#rs+1] = _f(s,1,'')
i = i+#rs[#rs]
if i < #s then
local s = s:sub(i,#s,true)
return _g(s,1,'',rs)
else
return rs
end
end
return _g(s,1,'',rs)
end
I have above function to split a string, It has been working earlier but this time it started giving error "call stack has exceeded maximum of depth of 100, verify a function is not calling itself by accident."
Any idea why I might be getting this error, this behaviour seems random since I am quite sure about rest of the script and same split function has been working fine as well.
EDIT:
Yes, isWhiteSpace was provided to me and has the following code, I am not supposed to change it since it worked earlier . Here is isWhite function:
function _isWhite(byte)
return byte == 32 or byte == 9
end
So both _g and _f call themselves, and _g calls _f. So clearly the recursion-stop conditions you have are too weak. In _g I see
if i < #s then
local s = ...
return _g(s,1,'',rs)
else
return rs
end
which will stop when i >= #s. If this never happens, you will get infinite recursion. It is hard to say by looking at the code how i varies, but based on this line:
i = i+#rs[#rs]
it appears to by some value, but can't say if there is guarantee that stop condition will ever be reached. With _f it is worse: the stop recursion conditions are
if i == #s then return c..w end
and
#(c..w) > maxLen
Again very hard to say if this is strong enough: what if i is greater than #s, does the rest of the function work? Although findWord() returns i<#s for non empty s, not sure what will happen if s empty.
Best way to find out is to put some print statements that give you a trace of _g and _f and the parameters received, this will tell you clearly what stop conditions are being missed.